Skip to content

Commit

Permalink
first refactorings,dependency injection, search controller
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas-sc committed May 13, 2024
1 parent 65ccbe2 commit 6af58c5
Show file tree
Hide file tree
Showing 8 changed files with 170 additions and 64 deletions.
16 changes: 16 additions & 0 deletions Classes/Command/IndexCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,13 +249,29 @@ protected function fetchTeiData(): void
$this->io->progressFinish();
}


protected function commitBibliography(): void
{
$index = $this->extConf['elasticIndexName'];
$this->io->text('Committing the ' . $index . ' index');

$this->io->progressStart(count($this->bibliographyItems));

// index params -> mapping fields for facetting in index
// Todo: optimize with synthetic _source and copy fields?
/* $elasticIndexMappings = [
'index' => ['index' => $index],
'body' => [
'mappings' => [
'properties' => [
'itemType' => [
'type' => 'keyword',
]
]
]
]
];*/

/* For more recent versions of Elasticsearch (8.x),
a call to $client->indices()->exists($indexParams) no longer returns a boolean,
it instead returns an instance of Elastic\Elasticsearch\Response\Elasticsearch.
Expand Down
76 changes: 18 additions & 58 deletions Classes/Controller/BibliographyController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,78 +16,38 @@
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\StreamFactoryInterface;
use Slub\LisztCommon\Controller\ClientEnabledController;
use TYPO3\CMS\Core\Configuration\ExtensionConfiguration;
use TYPO3\CMS\Core\Utility\GeneralUtility;

class BibliographyController extends ClientEnabledController
{
/** id of list target **/
const MAIN_TARGET = 'bib-list';
const SIDE_TARGET = 'bib-list-side';
const SIZE = 20;
use Slub\LisztBibliography\Interfaces\ElasticSearchServiceInterface;

/** @var jsCall */
protected string $jsCall;

/** @var div */
protected string $div;
final class BibliographyController extends ClientEnabledController
{

protected string $bibIndex;
protected string $localeIndex;
// set resultLimit as intern variable from $this->settings['resultLimit'];
protected int $resultLimit;

public function __construct(ResponseFactoryInterface $responseFactory, StreamFactoryInterface $streamFactory)
{
$this->responseFactory = $responseFactory;
$this->streamFactory = $streamFactory;
}
// Dependency Injection of Repository
// https://docs.typo3.org/m/typo3/reference-coreapi/main/en-us/ApiOverview/DependencyInjection/Index.html#Dependency-Injection

public function initializeAction(): void
public function __construct(private readonly ElasticSearchServiceInterface $elasticSearchService)
{
$extConf = GeneralUtility::makeInstance(ExtensionConfiguration::class)->get('liszt_bibliography');
$this->bibIndex = $extConf['elasticIndexName'];
$this->localeIndex = $extConf['elasticLocaleIndexName'];
}
$this->resultLimit = $this->settings['resultLimit'] ?? 25;

public function indexAction(): ResponseInterface
{
$this->createJsCall();
$this->wrapTargetDiv();
$contentStream = $this->
streamFactory->
createStream(
$this->div .
$this->jsCall
);

return $this->
responseFactory->
createResponse()->
withBody($contentStream);
}

private function wrapTargetDiv(): void
{
$sideCol = '<div id="' .
self::SIDE_TARGET .
'" class="col-md-4 col-xl-3 order-md-2"><ul class="list-group"></ul></div>';
$mainCol = '<div id="' .
self::MAIN_TARGET .
'" class="col-md order-md-1"></div>';
$this->div = '<div class="container"><div class="row">' .
$sideCol . $mainCol . '</div>';
}

private function createJsCall(): void

public function listAction(): ResponseInterface
{
$this->jsCall =
'<script>;document.addEventListener("DOMContentLoaded", _ => new BibliographyController({' .
'target:"' . self::MAIN_TARGET . '",' .
'sideTarget:"' . self::SIDE_TARGET . '",' .
'size:' . self::SIZE . ',' .
'bibIndex:"' . $this->bibIndex . '",' .
'localeIndex:"' . $this->localeIndex . '"' .
'}));</script>';
$this->view->assign('bibliographyList', $this->elasticSearchService->search());
return $this->htmlResponse();
}





}
13 changes: 13 additions & 0 deletions Classes/Interfaces/ElasticSearchServiceInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
namespace Slub\LisztBibliography\Interfaces;
use Illuminate\Support\Collection;


interface ElasticSearchServiceInterface
{
public function init(): bool;

public function getElasticInfo(): array;

public function search(): Collection;
}
72 changes: 72 additions & 0 deletions Classes/Services/ElasticSearchService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php


namespace Slub\LisztBibliography\Services;

use Elastic\Elasticsearch\Client;
use Elastic\Elasticsearch\Response\Elasticsearch;
use Http\Promise\Promise;
use Illuminate\Support\Collection;
use Slub\LisztCommon\Common\ElasticClientBuilder;
use Slub\LisztBibliography\Interfaces\ElasticSearchServiceInterface;

use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Configuration\ExtensionConfiguration;



class ElasticSearchService implements ElasticSearchServiceInterface
{

protected ?Client $client = null;
protected string $bibIndex;
protected string $localeIndex;

// Todo: Enable Elasticsearch Security: built-in security features are not enabled.
public function init(): bool
{
$this->client = ElasticClientBuilder::getClient();
$extConf = GeneralUtility::makeInstance(ExtensionConfiguration::class)->get('liszt_bibliography');
$this->bibIndex = $extConf['elasticIndexName'];
$this->localeIndex = $extConf['elasticLocaleIndexName'];
return true;
}

public function getElasticInfo(): array
{
$this->init();
return ($this->client->info()->asArray());
}

public function search(): Collection
{
$this->init();
$params = [
'index' => $this->bibIndex,
'body' => [
'query' => [
'match_all' => new \stdClass()
],
'size' => 10,
'_source' => ['itemType', 'title', 'creators', 'pages','date','language'],
'aggs' => [
'itemType' => [
'terms' => [
'field' => 'itemType.keyword',
]
],
'place' => [
'terms' => [
'field' => 'place.keyword',
]
]
]
]
];
// ToDo: handle exceptions!
$response = $this->client->search($params);
return new Collection($response->asArray());
}


}
4 changes: 1 addition & 3 deletions Configuration/Services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ services:
Slub\LisztBibliography\:
resource: '../Classes/*'
exclude: '../Classes/Domain/Model/*'
Slub\LisztBibliography\Classes\Controller\BibliographyController:
tags:
- backend.controller
# if more than one! class implement the same interface (i.e.Search Interface) read: https://usetypo3.com/dependency-injection/
Slub\LisztBibliography\Command\IndexCommand:
tags:
-
Expand Down
6 changes: 5 additions & 1 deletion Configuration/TypoScript/setup.typoscript
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@ lib.contentElement {
}
}


// ToDo: needed?
tt_content {
lisztbibliography_listing =< lib.contentElement
lisztbibliography_listing {
templateName = BibliographyListing
}
}

page.includeJSFooter.BibliographyController = EXT:liszt_bibliography/Resources/Public/JavaScript/Src/BibliographyController.js
// page.includeJSFooter.BibliographyController = EXT:liszt_bibliography/Resources/Public/JavaScript/Src/BibliographyController.js


plugin.tx_liszt_common {
Expand All @@ -35,5 +37,7 @@ plugin.tx_liszt_common {
href = #
}
}
resultLimit = 20

}
}
43 changes: 43 additions & 0 deletions Resources/Private/Templates/Bibliography/List.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<html xmlns:f="http://typo3.org/ns/TYPO3/CMS/Fluid/ViewHelpers" data-namespace-typo3-fluid="true">

<f:debug>{_all}</f:debug>

<div class="row" style="margin-top: 3rem;">
<div class="col-12 col-md-3 col-lg-2 ">
<nav aria-label="Suchfilter" class="filter" >

<h3 class="visually-hidden">Suchfilter</h3>

<f:for each="{bibliographyList.aggregations}" key="key" as="filterGroup">
<f:if condition="{filterGroup.buckets}">
<div class="filter-block">
<h4 class="">{key}</h4>
<ul>
<f:for each="{filterGroup.buckets}" as="filter">
<li><span class="form-check"><input class="form-check-input" type="checkbox" id="{filter.key}" value="{filter.key}"><label class="form-check-label" for="{filter.key}">{filter.key}</label></span><span class="count">{filter.doc_count}</span></li>
</f:for>
</ul>
</div>
</f:if>
</f:for>

</nav>

</div>
<div class="col-12 col-md-9 col-lg-10 filter">

<f:for each="{bibliographyList.hits.hits}" as="hit">
<div class="search-result-item">
<h3>{hit._source.title}</h3>
<p>
<f:for each="{hit._source.creators}" as="creator">
{creator.firstName} {creator.lastName}
</f:for>
,{hit._source.date}, {hit._source.pages}, {hit._source.language}</p>
</div>
</f:for>



</div>
</div>
4 changes: 2 additions & 2 deletions ext_localconf.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
ExtensionUtility::configurePlugin(
'LisztBibliography',
'BibliographyListing',
[ BibliographyController::class => 'index' ],
[ BibliographyController::class => 'index' ]
[ BibliographyController::class => 'list' ],
[ BibliographyController::class => '' ]
);

0 comments on commit 6af58c5

Please sign in to comment.