Skip to content

Commit

Permalink
Write editor to header if no author is available
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthias Richter committed Nov 11, 2024
1 parent 7b4184c commit 5ccca0c
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 0 deletions.
93 changes: 93 additions & 0 deletions Classes/Controller/BibliographyController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Liszt Catalog Raisonne project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
*/

namespace Slub\LisztBibliography\Controller;

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;

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

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

protected string $bibIndex;
protected string $localeIndex;

public function __construct(ResponseFactoryInterface $responseFactory, StreamFactoryInterface $streamFactory)
{
$this->responseFactory = $responseFactory;
$this->streamFactory = $streamFactory;
}

public function initializeAction(): void
{
$extConf = GeneralUtility::makeInstance(ExtensionConfiguration::class)->get('liszt_bibliography');
$this->bibIndex = $extConf['elasticIndexName'];
$this->localeIndex = $extConf['elasticLocaleIndexName'];
}

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
{
$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>';
}
}
23 changes: 23 additions & 0 deletions Classes/Processing/BibEntryConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,29 @@ class BibEntryConfig
]
]
];
const ALT_HEADER_FIELDS = [
[
'compound' => [
'fields' => [
[
'field' => 'firstName',
'conditionField' => 'creatorType',
'conditionValue' => 'editor',
'conditionRelation' => 'eq'
],
[
'field' => 'lastName',
'conditionField' => 'creatorType',
'conditionValue' => 'editor',
'conditionRelation' => 'eq'
]
],
'field' => 'creators',
'separator' => ', ',
'reverseFirst' => true
]
]
];
const BODY_FIELDS = [
[
'field' => 'title',
Expand Down
3 changes: 3 additions & 0 deletions Classes/Processing/BibEntryProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ public static function process(
}
$bibliographyItem['tei'] = $teiDataSets->get($key);
$bibliographyItem['tx_lisztcommon_header'] = self::buildListingField($bibliographyItem, BibEntryConfig::HEADER_FIELDS);
if ($bibliographyItem['tx_lisztcommon_header'] == '') {
$bibliographyItem['tx_lisztcommon_header'] = self::buildListingField($bibliographyItem, BibEntryConfig::ALT_HEADER_FIELDS);
}
$bibliographyItem['tx_lisztcommon_body'] = self::buildListingField($bibliographyItem, BibEntryConfig::BODY_FIELDS);
$bibliographyItem['tx_lisztcommon_footer'] = self::buildListingField($bibliographyItem, BibEntryConfig::FOOTER_FIELDS);
$bibliographyItem['tx_lisztcommon_searchable'] = self::buildListingField($bibliographyItem, BibEntryConfig::SEARCHABLE_FIELDS);
Expand Down
25 changes: 25 additions & 0 deletions Tests/Unit/Processing/BibEntryProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ final class BibEntryProcessorTest extends UnitTestCase
{
private ?BibEntryProcessor $subject = null;
private array $exampleBookArray = [];
private array $exampleBookWithoutAuthorArray = [];
private array $exampleBookSectionArray = [];
private array $exampleArticleArray = [];

Expand All @@ -35,6 +36,7 @@ final class BibEntryProcessorTest extends UnitTestCase
private string $issue = 'ex_issue';

private string $exampleBook = '';
private string $exampleBookWithoutAuthor = '';
private string $exampleArticle = '';
private string $exampleBookSection = '';

Expand All @@ -60,6 +62,24 @@ protected function setUp(): void
}
JSON;

$this->exampleBookWithoutAuthor =
<<<JSON
{
"key": "key",
"itemType": "book",
"title": "$this->title",
"creators": [
{
"creatorType": "editor",
"firstName": "$this->editorFirstName",
"lastName": "$this->editorLastName"
}
],
"place": "$this->place",
"date": "$this->date"
}
JSON;

$this->exampleArticle =
<<<JSON
{
Expand Down Expand Up @@ -108,6 +128,7 @@ protected function setUp(): void

$this->subject = GeneralUtility::makeInstance(BibEntryProcessor::class);
$this->exampleBookArray = json_decode($this->exampleBook, true);
$this->exampleBookWithoutAuthorArray = json_decode($this->exampleBookWithoutAuthor, true);
$this->exampleArticleArray = json_decode($this->exampleArticle, true);
$this->exampleBookSectionArray = json_decode($this->exampleBookSection, true);
}
Expand All @@ -125,11 +146,15 @@ public function headerIsProcessedCorrectly(): void
$book = $this->subject->process($this->exampleBookArray, new Collection(), new Collection());
$bookSection = $this->subject->process($this->exampleBookSectionArray, new Collection(), new Collection());
$article = $this->subject->process($this->exampleArticleArray, new Collection(), new Collection());
$bookWithoutAuthor = $this->subject->process($this->exampleBookWithoutAuthorArray, new Collection(), new Collection());

$expected = Str::of($this->authorLastName . ', ' . $this->authorFirstName);
$expectedWithoutAuthor = Str::of($this->editorLastName . ', ' . $this->editorFirstName);

self::assertEquals($book['tx_lisztcommon_header'], $expected);
self::assertEquals($bookSection['tx_lisztcommon_header'], $expected);
self::assertEquals($article['tx_lisztcommon_header'], $expected);
self::assertEquals($bookWithoutAuthor['tx_lisztcommon_header'], $expectedWithoutAuthor);
}

/**
Expand Down

0 comments on commit 5ccca0c

Please sign in to comment.