Skip to content

Commit

Permalink
Display a direct link to the talk in the other locale, if available
Browse files Browse the repository at this point in the history
And a generic "list all talks" link if not.
  • Loading branch information
spaze committed Aug 14, 2023
1 parent 5e8fea4 commit a12a6cb
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 0 deletions.
31 changes: 31 additions & 0 deletions site/app/Talks/TalkLocaleUrls.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
declare(strict_types = 1);

namespace MichalSpacekCz\Talks;

use Nette\Database\Explorer;

class TalkLocaleUrls
{

public function __construct(
private readonly Explorer $database,
) {
}


/**
* @return array<string, string> 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(),
);
}

}
24 changes: 24 additions & 0 deletions site/app/Www/Presenters/TalksPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -20,9 +21,14 @@
class TalksPresenter extends BasePresenter
{

/** @var array<string, array{name: string}> */
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,
Expand Down Expand Up @@ -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();
Expand All @@ -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<string, array{name: string}>
*/
protected function getLocaleLinkParams(): array
{
return $this->localeLinkParams;
}


protected function createComponentTalksList(string $name): TalksList
{
return $this->talksListFactory->create();
Expand Down
1 change: 1 addition & 0 deletions site/config/services.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
85 changes: 85 additions & 0 deletions site/tests/Talks/TalkLocaleUrlsTest.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php
declare(strict_types = 1);

namespace MichalSpacekCz\Talks;

use DateTime;
use MichalSpacekCz\Media\Video;
use MichalSpacekCz\Test\Database\Database;
use Nette\Utils\Html;
use Tester\Assert;
use Tester\TestCase;

$runner = require __DIR__ . '/../bootstrap.php';

/** @testCase */
class TalkLocaleUrlsTest extends TestCase
{

public function __construct(
private readonly TalkLocaleUrls $talkLocaleUrls,
private readonly Database $database,
) {
}


public function testGet(): void
{
$expected = ['cs_CZ' => '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);

0 comments on commit a12a6cb

Please sign in to comment.