From a12a6cb03e81e3a8849275838f98508506cb0a4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20=C5=A0pa=C4=8Dek?= Date: Mon, 14 Aug 2023 04:22:46 +0200 Subject: [PATCH] Display a direct link to the talk in the other locale, if available And a generic "list all talks" link if not. --- site/app/Talks/TalkLocaleUrls.php | 31 ++++++++ site/app/Www/Presenters/TalksPresenter.php | 24 ++++++ site/config/services.neon | 1 + site/tests/Talks/TalkLocaleUrlsTest.phpt | 85 ++++++++++++++++++++++ 4 files changed, 141 insertions(+) create mode 100644 site/app/Talks/TalkLocaleUrls.php create mode 100644 site/tests/Talks/TalkLocaleUrlsTest.phpt diff --git a/site/app/Talks/TalkLocaleUrls.php b/site/app/Talks/TalkLocaleUrls.php new file mode 100644 index 000000000..57889d54f --- /dev/null +++ b/site/app/Talks/TalkLocaleUrls.php @@ -0,0 +1,31 @@ + locale => action + */ + public function get(Talk $talk): array + { + if (!$talk->getTranslationGroupId()) { + return []; + } + return $this->database->fetchPairs( + 'SELECT l.locale, t.action FROM talks t JOIN locales l ON t.key_locale = l.id_locale WHERE t.key_translation_group = ?', + $talk->getTranslationGroupId(), + ); + } + +} diff --git a/site/app/Www/Presenters/TalksPresenter.php b/site/app/Www/Presenters/TalksPresenter.php index 8a579d86c..84578df35 100644 --- a/site/app/Www/Presenters/TalksPresenter.php +++ b/site/app/Www/Presenters/TalksPresenter.php @@ -8,6 +8,7 @@ use MichalSpacekCz\Media\SlidesPlatform; use MichalSpacekCz\Talks\Exceptions\TalkDoesNotExistException; use MichalSpacekCz\Talks\Exceptions\UnknownSlideException; +use MichalSpacekCz\Talks\TalkLocaleUrls; use MichalSpacekCz\Talks\Talks; use MichalSpacekCz\Talks\TalkSlides; use MichalSpacekCz\Talks\TalksList; @@ -20,9 +21,14 @@ class TalksPresenter extends BasePresenter { + /** @var array */ + private array $localeLinkParams = []; + + public function __construct( private readonly Talks $talks, private readonly TalkSlides $talkSlides, + private readonly TalkLocaleUrls $talkLocaleUrls, private readonly UpcomingTrainingDates $upcomingTrainingDates, private readonly TalksListFactory $talksListFactory, private readonly LocaleLinkGeneratorInterface $localeLinkGenerator, @@ -76,6 +82,9 @@ public function actionTalk(string $name, ?string $slide = null): void } catch (UnknownSlideException | TalkDoesNotExistException $e) { throw new BadRequestException($e->getMessage(), previous: $e); } + foreach ($this->talkLocaleUrls->get($talk) as $locale => $action) { + $this->localeLinkParams[$locale] = ['name' => $action]; + } $this->template->pageTitle = $this->talks->pageTitle('messages.title.talk', $talk); $this->template->pageHeader = $talk->getTitle(); @@ -89,6 +98,21 @@ public function actionTalk(string $name, ?string $slide = null): void } + protected function getLocaleLinkAction(): string + { + return (count($this->localeLinkParams) > 1 ? parent::getLocaleLinkAction() : 'Www:Talks:'); + } + + + /** + * @return array + */ + protected function getLocaleLinkParams(): array + { + return $this->localeLinkParams; + } + + protected function createComponentTalksList(string $name): TalksList { return $this->talksListFactory->create(); diff --git a/site/config/services.neon b/site/config/services.neon index 378eee75e..2c6dc8b9a 100644 --- a/site/config/services.neon +++ b/site/config/services.neon @@ -84,6 +84,7 @@ services: - MichalSpacekCz\Tags\Tags - MichalSpacekCz\Talks\TalkFactory(videoFactory: @talkVideoFactory) - MichalSpacekCz\Talks\TalkInputsFactory(videoThumbnails: @talkVideoThumbnails) + - MichalSpacekCz\Talks\TalkLocaleUrls - MichalSpacekCz\Talks\Talks - MichalSpacekCz\Talks\TalkSlides - MichalSpacekCz\Talks\TalksListFactory diff --git a/site/tests/Talks/TalkLocaleUrlsTest.phpt b/site/tests/Talks/TalkLocaleUrlsTest.phpt new file mode 100644 index 000000000..389e370c5 --- /dev/null +++ b/site/tests/Talks/TalkLocaleUrlsTest.phpt @@ -0,0 +1,85 @@ + 'foobar']; + $this->database->setFetchPairsResult($expected); + Assert::same([], $this->talkLocaleUrls->get($this->buildTalk(null))); + Assert::same($expected, $this->talkLocaleUrls->get($this->buildTalk(1337))); + } + + + private function buildTalk(?int $translationGroup): Talk + { + $video = new Video( + null, + null, + null, + null, + null, + null, + 320, + 200, + null, + ); + return new Talk( + 10, + 1, + 'cs_CZ', + $translationGroup, + null, + null, + Html::fromText('title'), + 'title', + null, + null, + new DateTime(), + null, + null, + false, + null, + null, + $video, + null, + Html::fromText('event'), + 'event', + null, + null, + null, + null, + null, + null, + null, + null, + null, + null, + false, + ); + } + +} + +$runner->run(TalkLocaleUrlsTest::class);