Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store talk locale #194

Merged
merged 2 commits into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions site/app/Application/Locales.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
declare(strict_types = 1);

namespace MichalSpacekCz\Application;

use Nette\Database\Explorer;

class Locales
{

/** @var array<int, string>|null */
private ?array $locales = null;


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


/**
* @return array<int, string> of id => locale
*/
public function getAllLocales(): array
{
if ($this->locales === null) {
$this->locales = $this->database->fetchPairs('SELECT id_locale, locale FROM locales ORDER BY id_locale');
}
return $this->locales;
}


public function getLocaleById(int $id): ?string
{
return $this->getAllLocales()[$id] ?? null;
}

}
22 changes: 0 additions & 22 deletions site/app/Articles/Blog/BlogPosts.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@
class BlogPosts
{

/** @var array<int, string>|null */
private ?array $locales = null;


/**
* @param array<string, array<string, array<int, string>>> $allowedTags
*/
Expand Down Expand Up @@ -314,24 +310,6 @@ public function update(BlogPost $post): void
}


/**
* @return array<int, string> of id => locale
*/
public function getAllLocales(): array
{
if ($this->locales === null) {
$this->locales = $this->database->fetchPairs('SELECT id_locale, locale FROM locales ORDER BY id_locale');
}
return $this->locales;
}


public function getLocaleById(int $id): ?string
{
return $this->getAllLocales()[$id] ?? null;
}


/**
* @return list<ArticleEdit>
* @throws InvalidTimezoneException
Expand Down
6 changes: 4 additions & 2 deletions site/app/Form/PostFormFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use Contributte\Translation\Translator;
use DateTime;
use MichalSpacekCz\Application\Locales;
use MichalSpacekCz\Articles\Blog\BlogPost;
use MichalSpacekCz\Articles\Blog\BlogPostPreview;
use MichalSpacekCz\Articles\Blog\BlogPostRecommendedLinks;
Expand Down Expand Up @@ -39,6 +40,7 @@ public function __construct(
private readonly FormValues $formValues,
private readonly TwitterCards $twitterCards,
private readonly BlogPostRecommendedLinks $recommendedLinks,
private readonly Locales $locales,
) {
}

Expand All @@ -48,7 +50,7 @@ public function create(callable $onSuccessAdd, callable $onSuccessEdit, DefaultT
$form = $this->factory->create();
$form->addInteger('translationGroup', 'Skupina překladů:')
->setRequired(false);
$form->addSelect('locale', 'Jazyk:', $this->blogPosts->getAllLocales())
$form->addSelect('locale', 'Jazyk:', $this->locales->getAllLocales())
->setRequired('Zadejte prosím jazyk')
->setPrompt('- vyberte -');
$form->addText('title', 'Titulek:')
Expand Down Expand Up @@ -165,7 +167,7 @@ private function buildPost(stdClass $values, ?int $postId): BlogPost
$post->postId = $postId;
$post->translationGroupId = (empty($values->translationGroup) ? null : $values->translationGroup);
$post->localeId = $values->locale;
$post->locale = $this->blogPosts->getLocaleById($values->locale);
$post->locale = $this->locales->getLocaleById($values->locale);
$post->slug = $values->slug;
$post->titleTexy = $values->title;
$post->leadTexy = (empty($values->lead) ? null : $values->lead);
Expand Down
8 changes: 8 additions & 0 deletions site/app/Form/TalkFormFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace MichalSpacekCz\Form;

use MichalSpacekCz\Application\Locales;
use MichalSpacekCz\Form\Controls\TrainingControlsFactory;
use MichalSpacekCz\Media\VideoThumbnails;
use MichalSpacekCz\Talks\Talk;
Expand All @@ -22,6 +23,7 @@ public function __construct(
private readonly Talks $talks,
private readonly LinkGenerator $linkGenerator,
private readonly VideoThumbnails $videoThumbnails,
private readonly Locales $locales,
) {
}

Expand All @@ -36,6 +38,9 @@ public function create(callable $onSuccess, ?Talk $talk = null): Form
$form = $this->factory->create();
$allTalks = $this->getAllTalksExcept($talk ? (string)$talk->getAction() : null);

$form->addSelect('locale', 'Jazyk:', $this->locales->getAllLocales())
->setRequired('Zadejte prosím jazyk')
->setPrompt('- vyberte -');
$form->addText('action', 'Akce:')
->setRequired(false)
->addRule($form::MAX_LENGTH, 'Maximální délka akce je %d znaků', 200);
Expand Down Expand Up @@ -106,6 +111,7 @@ public function create(callable $onSuccess, ?Talk $talk = null): Form
$removeVideoThumbnailAlternative = $values->removeVideoThumbnailAlternative ?? false;
$this->talks->update(
$talk->getId(),
$values->locale,
$values->action,
$values->title,
$values->description,
Expand Down Expand Up @@ -138,6 +144,7 @@ public function create(callable $onSuccess, ?Talk $talk = null): Form
$message = Html::el()->setText('Přednáška upravena ');
} else {
$talkId = $this->talks->add(
$values->locale,
$values->action,
$values->title,
$values->description,
Expand Down Expand Up @@ -177,6 +184,7 @@ public function setTalk(Form $form, Talk $talk, SubmitButton $submit): void
{
$values = [
'action' => $talk->getAction(),
'locale' => $talk->getLocaleId(),
'title' => $talk->getTitleTexy(),
'description' => $talk->getDescriptionTexy(),
'date' => $talk->getDate()->format('Y-m-d H:i'),
Expand Down
7 changes: 7 additions & 0 deletions site/app/Talks/Talk.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class Talk

public function __construct(
private readonly int $id,
private readonly int $localeId,
private readonly ?string $action,
private readonly Html $title,
private readonly string $titleTexy,
Expand Down Expand Up @@ -48,6 +49,12 @@ public function getId(): int
}


public function getLocaleId(): int
{
return $this->localeId;
}


public function getAction(): ?string
{
return $this->action;
Expand Down
1 change: 1 addition & 0 deletions site/app/Talks/TalkFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public function createFromDatabaseRow(Row $row): Talk
{
return new Talk(
$row->id,
$row->localeId,
$row->action,
$this->texyFormatter->format($row->title),
$row->title,
Expand Down
10 changes: 10 additions & 0 deletions site/app/Talks/Talks.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public function getAll(?int $limit = null): array
{
$query = 'SELECT
t.id_talk AS id,
t.key_locale AS localeId,
t.action,
t.title,
t.description,
Expand Down Expand Up @@ -89,6 +90,7 @@ public function getUpcoming(): array
{
$query = 'SELECT
t.id_talk AS id,
t.key_locale AS localeId,
t.action,
t.title,
t.description,
Expand Down Expand Up @@ -135,6 +137,7 @@ public function get(string $name): Talk
$result = $this->database->fetch(
'SELECT
t.id_talk AS id,
t.key_locale AS localeId,
t.action,
t.title,
t.description,
Expand Down Expand Up @@ -181,6 +184,7 @@ public function getById(int $id): Talk
$result = $this->database->fetch(
'SELECT
t.id_talk AS id,
t.key_locale AS localeId,
t.action,
t.title,
t.description,
Expand Down Expand Up @@ -249,6 +253,7 @@ public function getFavorites(): array
*/
public function update(
int $id,
int $localeId,
?string $action,
string $title,
?string $description,
Expand All @@ -272,6 +277,7 @@ public function update(
bool $publishSlides,
): void {
$params = $this->getAddUpdateParams(
$localeId,
$action,
$title,
$description,
Expand Down Expand Up @@ -304,6 +310,7 @@ public function update(
* @throws TalkDateTimeException
*/
public function add(
int $localeId,
?string $action,
string $title,
?string $description,
Expand All @@ -327,6 +334,7 @@ public function add(
bool $publishSlides,
): int {
$params = $this->getAddUpdateParams(
$localeId,
$action,
$title,
$description,
Expand Down Expand Up @@ -372,6 +380,7 @@ public function pageTitle(string $translationKey, Talk $talk): Html
* @throws TalkDateTimeException
*/
private function getAddUpdateParams(
int $localeId,
?string $action,
string $title,
?string $description,
Expand Down Expand Up @@ -400,6 +409,7 @@ private function getAddUpdateParams(
throw new TalkDateTimeException($date, $e);
}
return [
'key_locale' => $localeId,
'action' => (empty($action) ? null : $action),
'title' => $title,
'description' => (empty($description) ? null : $description),
Expand Down
3 changes: 3 additions & 0 deletions site/app/Talks/talkInputs.latte
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
{varType int $videoThumbnailHeight}
{form talk class => "aligned wide"}
<table>
<tr>
<th>{label locale /}</th><td class="short">{input locale}</td>
</tr>
<tr>
<th class="align-top">{label action /}</th><td>{input action}<br><small>nevyplňujte, pokud nechcete samostatnou stránku pro přednášku</small></td>
</tr>
Expand Down
1 change: 1 addition & 0 deletions site/config/services.neon
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ services:
- MichalSpacekCz\Application\AppRequest
- MichalSpacekCz\Application\Error
localeLinkGenerator: MichalSpacekCz\Application\LocaleLinkGenerator
- MichalSpacekCz\Application\Locales
- MichalSpacekCz\Application\RouterFactory(supportedLocales: %locales.supported%, rootDomainMapping: %locales.rootDomainMapping%, translatedRoutes: %translatedRoutes.presenters%)
- @MichalSpacekCz\Application\RouterFactory::createRouter
- MichalSpacekCz\Application\Theme
Expand Down