Skip to content

Commit dbfbb38

Browse files
committed
I've realized I can remove the shared included blog post form by using just one template
1 parent b0fc786 commit dbfbb38

File tree

5 files changed

+86
-96
lines changed

5 files changed

+86
-96
lines changed

site/app/Admin/Presenters/BlogPresenter.php

Lines changed: 10 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,19 @@
99
use MichalSpacekCz\Articles\Blog\Exceptions\BlogPostDoesNotExistException;
1010
use MichalSpacekCz\Form\PostFormFactory;
1111
use MichalSpacekCz\Formatter\TexyFormatter;
12-
use MichalSpacekCz\Tags\Tags;
1312
use Nette\Application\BadRequestException;
1413
use Nette\Application\UI\Form;
1514
use Nette\Utils\Html;
16-
use Nette\Utils\Json;
1715

1816
class BlogPresenter extends BasePresenter
1917
{
2018

21-
private BlogPost $post;
19+
private ?BlogPost $post = null;
2220

2321

2422
public function __construct(
2523
private readonly BlogPosts $blogPosts,
2624
private readonly TexyFormatter $texyFormatter,
27-
private readonly Tags $tags,
2825
private readonly PostFormFactory $postFormFactory,
2926
) {
3027
parent::__construct();
@@ -47,19 +44,27 @@ public function actionDefault(): void
4744
public function actionAdd(): void
4845
{
4946
$this->template->pageTitle = 'Přidat příspěvek';
47+
$this->setView('edit');
5048
}
5149

5250

53-
protected function createComponentAddPost(): Form
51+
protected function createComponentPost(): Form
5452
{
5553
return $this->postFormFactory->create(
5654
function (BlogPost $post): never {
5755
$this->blogPosts->add($post);
5856
$this->flashMessage($this->texyFormatter->translate('messages.blog.admin.postadded', [$post->titleTexy, $this->link('edit', [$post->postId]), $post->href]));
5957
$this->redirect('Blog:');
6058
},
59+
function (BlogPost $post): never {
60+
$post->previousSlugTags = $this->post->slugTags ?? [];
61+
$this->blogPosts->update($post);
62+
$this->flashMessage($this->texyFormatter->translate('messages.blog.admin.postupdated', [$post->titleTexy, $this->link('edit', [$post->postId]), $post->href]));
63+
$this->redirect('Blog:');
64+
},
6165
$this->template,
6266
$this->sendTemplate(...),
67+
$this->post,
6368
);
6469
}
6570

@@ -74,44 +79,4 @@ public function actionEdit(int $param): void
7479
$this->blogPosts->setTemplateTitleAndHeader($this->post, $this->template, Html::el()->setText('Příspěvek '));
7580
}
7681

77-
78-
protected function createComponentEditPost(): Form
79-
{
80-
$form = $this->postFormFactory->create(
81-
function (BlogPost $post): never {
82-
$post->previousSlugTags = $this->post->slugTags;
83-
$this->blogPosts->update($post);
84-
$this->flashMessage($this->texyFormatter->translate('messages.blog.admin.postupdated', [$post->titleTexy, $this->link('edit', [$post->postId]), $post->href]));
85-
$this->redirect('Blog:');
86-
},
87-
$this->template,
88-
$this->sendTemplate(...),
89-
$this->post->postId,
90-
);
91-
92-
$values = [
93-
'translationGroup' => $this->post->translationGroupId,
94-
'locale' => $this->post->localeId,
95-
'title' => $this->post->titleTexy,
96-
'slug' => $this->post->slug,
97-
'published' => $this->post->published?->format('Y-m-d H:i'),
98-
'previewKey' => $this->post->previewKey,
99-
'lead' => $this->post->leadTexy,
100-
'text' => $this->post->textTexy,
101-
'originally' => $this->post->originallyTexy,
102-
'ogImage' => $this->post->ogImage,
103-
'twitterCard' => $this->post->twitterCard?->getCard(),
104-
'tags' => ($this->post->tags ? $this->tags->toString($this->post->tags) : null),
105-
'recommended' => (empty($this->post->recommended) ? null : Json::encode($this->post->recommended)),
106-
'cspSnippets' => $this->post->cspSnippets,
107-
'allowedTags' => $this->post->allowedTags,
108-
'omitExports' => $this->post->omitExports,
109-
];
110-
$form->setDefaults($values);
111-
$form->getComponent('editSummary')
112-
->setDisabled($this->post->needsPreviewKey());
113-
$form->getComponent('submit')->caption = 'Upravit';
114-
return $form;
115-
}
116-
11782
}

site/app/Admin/Presenters/templates/Blog/add.latte

Lines changed: 0 additions & 9 deletions
This file was deleted.

site/app/Admin/Presenters/templates/Blog/common/postForm.latte

Lines changed: 0 additions & 29 deletions
This file was deleted.

site/app/Admin/Presenters/templates/Blog/edit.latte

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,33 @@
55
{/define}
66

77
{define #content}
8-
{include "common/postForm.latte", form: editPost}
8+
{form post class => "aligned wide blocking"}
9+
<div n:foreach="$form->errors as $error" class="flash error"><strong>{$error}</strong></div>
10+
<table>
11+
<tbody><tr><th>{label translationGroup /}</th><td class="short">{input translationGroup}</td></tr></tbody>
12+
<tbody><tr><th>{label locale /}</th><td class="short">{input locale}</td></tr></tbody>
13+
<tbody><tr><th>{label title /}</th><td>{input title}</td></tr></tbody>
14+
<tbody><tr><th>{label slug /}</th><td>{input slug}</td></tr></tbody>
15+
<tbody><tr><th>{label published /}</th><td>{input published}</td></tr></tbody>
16+
<tbody><tr><th>{label previewKey /}</th><td>{input previewKey}</td></tr></tbody>
17+
<tbody><tr><th class="align-top">{label lead /}</th><td>{input lead}</td></tr></tbody>
18+
<tbody><tr><th class="align-top">{label text /}</th><td>{input text}</td></tr></tbody>
19+
<tbody><tr><th class="align-top">{label originally /}</th><td>{input originally}</td></tr></tbody>
20+
<tbody><tr><th>{label twitterCard /}</th><td>{input twitterCard}</td></tr></tbody>
21+
<tbody><tr><th>{label ogImage /}</th><td>{input ogImage}</td></tr></tbody>
22+
<tbody><tr><th>{label tags /}</th><td>{input tags}</td></tr></tbody>
23+
<tbody><tr><th>{label recommended /}</th><td>{input recommended}</td></tr></tbody>
24+
<tbody><tr><th>{label cspSnippets /}</th><td>{input cspSnippets}</td></tr></tbody>
25+
<tbody><tr><th>{label allowedTags /}</th><td>{input allowedTags}</td></tr></tbody>
26+
<tbody><tr><th>{label omitExports: /}</th><td>{input omitExports:}</td></tr></tbody>
27+
<tbody><tr><th>{label editSummary /}</th><td>{input editSummary}<br><small>Shrnutí editace bude u článku zobrazeno jako aktualizace, ponechte prázdné pro malé editace</small></td></tr></tbody>
28+
{capture $link}{link preview}{/capture}
29+
<tr><td></td><td class="short"><p>{input submit} {input preview id => preview-button, class => lighter, formtarget => preview}</p></td></tr>
30+
</table>
31+
{/form}
32+
<div id="preview" class="hidden">
33+
<h2>{_messages.label.preview}</h2>
34+
<iframe id="preview-frame" name="preview" class="column-content"></iframe>
35+
</div>
36+
<div class="clear"></div>
937
{/define}

site/app/Form/PostFormFactory.php

Lines changed: 47 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Nette\Forms\Controls\SubmitButton;
2121
use Nette\Forms\Controls\TextInput;
2222
use Nette\Utils\Html;
23+
use Nette\Utils\Json;
2324
use Spaze\ContentSecurityPolicy\CspConfig;
2425
use stdClass;
2526

@@ -42,7 +43,7 @@ public function __construct(
4243
}
4344

4445

45-
public function create(callable $onSuccess, DefaultTemplate $template, callable $sendTemplate, ?int $postId = null): Form
46+
public function create(callable $onSuccessAdd, callable $onSuccessEdit, DefaultTemplate $template, callable $sendTemplate, ?BlogPost $post): Form
4647
{
4748
$form = $this->factory->create();
4849
$form->addInteger('translationGroup', 'Skupina překladů:')
@@ -118,27 +119,31 @@ public function create(callable $onSuccess, DefaultTemplate $template, callable
118119
$form->addSubmit('submit', 'Přidat');
119120
$form->addSubmit('preview', $this->translator->translate('messages.label.preview'))
120121
->setHtmlAttribute('data-loading-value', 'Moment…')
121-
->onClick[] = function (SubmitButton $button) use ($postId, $template, $sendTemplate): void {
122-
$post = $this->buildPost($this->formValues->getValues($button), $postId);
123-
$this->blogPostPreview->sendPreview($post, $template, $sendTemplate);
122+
->onClick[] = function (SubmitButton $button) use ($post, $template, $sendTemplate): void {
123+
$newPost = $this->buildPost($this->formValues->getValues($button), $post?->postId);
124+
$this->blogPostPreview->sendPreview($newPost, $template, $sendTemplate);
124125
};
125126

126-
$form->onValidate[] = function (Form $form) use ($postId): void {
127-
$post = $this->buildPost($form->getValues(), $postId);
128-
if ($post->needsPreviewKey() && $post->previewKey === null) {
127+
$form->onValidate[] = function (Form $form) use ($post): void {
128+
$newPost = $this->buildPost($form->getValues(), $post?->postId);
129+
if ($newPost->needsPreviewKey() && $newPost->previewKey === null) {
129130
$input = $form->getComponent('previewKey');
130131
if (!$input instanceof TextInput) {
131132
throw new ShouldNotHappenException(sprintf("The 'previewKey' component should be '%s' but it's a %s", TextInput::class, get_debug_type($input)));
132133
}
133-
$input->addError(sprintf('Tento %s příspěvek vyžaduje klíč pro náhled', $post->published === null ? 'nepublikovaný' : 'budoucí'));
134+
$input->addError(sprintf('Tento %s příspěvek vyžaduje klíč pro náhled', $newPost->published === null ? 'nepublikovaný' : 'budoucí'));
134135
}
135136
};
136-
$form->onSuccess[] = function (Form $form) use ($onSuccess, $postId): void {
137+
$form->onSuccess[] = function (Form $form) use ($onSuccessAdd, $onSuccessEdit, $post): void {
137138
$values = $form->getValues();
138-
$post = $this->buildPost($values, $postId);
139-
$this->blogPosts->enrich($post);
139+
$newPost = $this->buildPost($values, $post?->postId);
140+
$this->blogPosts->enrich($newPost);
140141
try {
141-
$onSuccess($post);
142+
if ($post) {
143+
$onSuccessEdit($newPost);
144+
} else {
145+
$onSuccessAdd($newPost);
146+
}
142147
} catch (UniqueConstraintViolationException) {
143148
$slug = $form->getComponent('slug');
144149
if (!$slug instanceof TextInput) {
@@ -147,6 +152,9 @@ public function create(callable $onSuccess, DefaultTemplate $template, callable
147152
$slug->addError($this->texyFormatter->translate('messages.blog.admin.duplicateslug'));
148153
}
149154
};
155+
if ($post) {
156+
$this->setDefaults($post, $form);
157+
}
150158
return $form;
151159
}
152160

@@ -188,4 +196,31 @@ private function addPublishedDate(TextInput $field, bool $required = false): Tex
188196
);
189197
}
190198

199+
200+
private function setDefaults(BlogPost $post, Form $form): void
201+
{
202+
$values = [
203+
'translationGroup' => $post->translationGroupId,
204+
'locale' => $post->localeId,
205+
'title' => $post->titleTexy,
206+
'slug' => $post->slug,
207+
'published' => $post->published?->format('Y-m-d H:i'),
208+
'previewKey' => $post->previewKey,
209+
'lead' => $post->leadTexy,
210+
'text' => $post->textTexy,
211+
'originally' => $post->originallyTexy,
212+
'ogImage' => $post->ogImage,
213+
'twitterCard' => $post->twitterCard?->getCard(),
214+
'tags' => ($post->tags ? $this->tags->toString($post->tags) : null),
215+
'recommended' => (empty($post->recommended) ? null : Json::encode($post->recommended)),
216+
'cspSnippets' => $post->cspSnippets,
217+
'allowedTags' => $post->allowedTags,
218+
'omitExports' => $post->omitExports,
219+
];
220+
$form->setDefaults($values);
221+
$form->getComponent('editSummary')
222+
->setDisabled($post->needsPreviewKey());
223+
$form->getComponent('submit')->caption = 'Upravit';
224+
}
225+
191226
}

0 commit comments

Comments
 (0)