2020use Nette \Forms \Controls \SubmitButton ;
2121use Nette \Forms \Controls \TextInput ;
2222use Nette \Utils \Html ;
23+ use Nette \Utils \Json ;
2324use Spaze \ContentSecurityPolicy \CspConfig ;
2425use 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