Skip to content

Commit

Permalink
move ElasticSearchService from liszt_bibliography to liszt_common
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas-sc committed Oct 9, 2024
1 parent 050b46e commit 6df21ae
Show file tree
Hide file tree
Showing 23 changed files with 520 additions and 133 deletions.
8 changes: 8 additions & 0 deletions Classes/Common/QueryParameterProcessing.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Slub\LisztCommon\Common;

class QueryParameterProcessing
{

}
46 changes: 42 additions & 4 deletions Classes/Controller/SearchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,49 @@
declare(strict_types=1);

namespace Slub\LisztCommon\Controller;
use Psr\Http\Message\ResponseInterface;
use Slub\LisztCommon\Interfaces\ElasticSearchServiceInterface;

class SearchController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionController
// ToDo:
// Organize the transfer of the necessary parameters (index name, fields, etc.) from the other extensions (ExtensionConfiguration?) -> see in ElasticSearchServic
// Elastic Search Index return standardized fields? Standardized search fields or own params from the respective extension?
// process search parameters from the URL query parameters to search

final class SearchController extends ClientEnabledController
{
public function indexAction(): void

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


// Dependency Injection of Repository
// https://docs.typo3.org/m/typo3/reference-coreapi/main/en-us/ApiOverview/DependencyInjection/Index.html#Dependency-Injection

public function __construct(private readonly ElasticSearchServiceInterface $elasticSearchService)
{
$this->resultLimit = $this->settings['resultLimit'] ?? 25;
}



public function indexAction(): ResponseInterface
{
$this->view->assign('foo', 'bar');
$language = $this->request->getAttribute('language');
$locale = $language->getLocale();

$elasticResponse = $this->elasticSearchService->search();

$this->view->assign('locale', $locale);

$this->view->assign('totalItems', $elasticResponse['hits']['total']['value']);

$searchParams = $this->request->getQueryParams();
$this->view->assign('searchParams', $searchParams);


$this->view->assign('searchResults', $elasticResponse);
return $this->htmlResponse();
}
}


}
15 changes: 15 additions & 0 deletions Classes/Interfaces/ElasticSearchServiceInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
namespace Slub\LisztCommon\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\LisztCommon\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\LisztCommon\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', 'localizedCitations'],
'aggs' => [
'itemType' => [
'terms' => [
'field' => 'itemType.keyword',
]
],
'place' => [
'terms' => [
'field' => 'place.keyword',
]
]
]
]
];
// ToDo: handle exceptions!
$response = $this->client->search($params);
return new Collection($response->asArray());
}


}
28 changes: 28 additions & 0 deletions Classes/ViewHelpers/GetQueryParamsViewHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Slub\LisztCommon\ViewHelpers;

use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Type\File\ImageInfo;



final class GetQueryParamsViewHelper extends AbstractViewHelper
{


public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext): array
{

/** @var RenderingContext $renderingContext */
$request = $renderingContext->getRequest();


return $request->getQueryParams();

}
}
2 changes: 2 additions & 0 deletions Configuration/Services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ services:
Slub\LisztCommon\:
resource: '../Classes/*'
exclude: '../Classes/Domain/Model/*'
# if more than one! class implement the same interface (i.e.Search Interface) read: https://usetypo3.com/dependency-injection/
# and https://docs.typo3.org/m/typo3/reference-coreapi/main/en-us/ApiOverview/DependencyInjection/Index.html
4 changes: 2 additions & 2 deletions Configuration/TCA/Overrides/sys_template.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
/*defined('TYPO3') or die('Access denied.');
defined('TYPO3') or die('Access denied.');
call_user_func(function()
{

Expand All @@ -8,4 +8,4 @@
'Configuration/TypoScript',
'Liszt-Common'
);
});*/
});
43 changes: 39 additions & 4 deletions Configuration/TCA/Overrides/tt_content.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,49 @@


use TYPO3\CMS\Extbase\Utility\ExtensionUtility;
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;

defined('TYPO3') or die();

defined('TYPO3') or die();

// register search Plugin
ExtensionUtility::registerPlugin(
'liszt_common',
'SearchBar',
'Liszt Search Bar'
'SearchListing',
'Liszt Search Results'
);


// Adds the content element to the "Type" dropdown
ExtensionManagementUtility::addTcaSelectItem(
'tt_content',
'CType',
[
'label' => 'LLL:EXT:liszt_common/Resources/Private/Language/locallang.xlf:listing_title',
'value' => 'lisztcommon_searchlisting',
'icon' => 'content-text',
'group' => 'plugins',
'description' => 'LLL:EXT:liszt_common/Resources/Private/Language/locallang.xlf:listing_description'
]
);


// configure the backend form fields for SearchList Plugin (no extra fields needed)
$GLOBALS['TCA']['tt_content']['types']['lisztcommon_searchlisting'] = [
'showitem' => '
--div--;LLL:EXT:core/Resources/Private/Language/Form/locallang_tabs.xlf:general,
--palette--;;general,
header; Title,
bodytext;LLL:EXT:core/Resources/Private/Language/Form/locallang_ttc.xlf:bodytext_formlabel,
--div--;LLL:EXT:core/Resources/Private/Language/Form/locallang_tabs.xlf:access,
--palette--;;hidden,
--palette--;;acces,
',
'columnsOverrides' => [
'bodytext' => [
'config' => [
'enableRichtext' => true,
'richtextConfiguration' => 'default'
]
]
]
];
48 changes: 46 additions & 2 deletions Configuration/TsConfig/page.tsconfig
Original file line number Diff line number Diff line change
@@ -1,14 +1,58 @@
mod.web_layout.BackendLayouts {
searchResults {
title = Liszt Suchergebnisse
## icon = EXT:liszt_web/Resources/Public/Images/BackendLayouts/default.gif
config {
backend_layout {
doktype = 130
colCount = 1
rowCount = 1
rows {
1 {
columns {
1 {
name = Hauptinhalt
colPos = 0
}
}
}
}
}
}
}
}

// add content element to wizard
// https://docs.typo3.org/m/typo3/reference-coreapi/12.4/en-us/ApiOverview/ContentElements/AddingYourOwnContentElements.html
mod.wizards.newContentElement.wizardItems {
plugins {
elements {
lisztcommon_searchlisting {
iconIdentifier = content-text
title = LLL:EXT:liszt_common/Resources/Private/Language/locallang.xlf:listing_title
description = LLL:EXT:liszt_common/Resources/Private/Language/locallang.xlf:listing_description
tt_content_defValues {
CType = list
list_type = lisztcommon_searchlisting
}
}
}
show := addToList(lisztcommon_searchlisting)
}
}


# Add layout for editors to hide SearchBar in backend page setup if needed

TCEFORM {
/*TCEFORM {
pages {
layout {
addItems {
4 = Layout ohne Searchbar
}
}
}
}
}*/

/*mod.wizards.newContentElement.wizardItems {
// add the content elementS to the tab "plugins" TODO: can be removed
Expand Down
Loading

0 comments on commit 6df21ae

Please sign in to comment.