1- <?php namespace Winter \Pages \Controllers ;
1+ <?php
2+
3+ namespace Winter \Pages \Controllers ;
24
35use ApplicationException ;
46use Backend \Classes \Controller ;
7+ use Backend \Widgets \Form ;
58use BackendMenu ;
9+ use Cache ;
610use Cms \Classes \CmsObject ;
11+ use Cms \Classes \CmsObjectCollection ;
712use Cms \Classes \Theme ;
813use Cms \Widgets \TemplateList ;
914use Config ;
10- use Cache ;
1115use Event ;
1216use Exception ;
1317use Flash ;
1721use Url ;
1822use Winter \Pages \Classes \Content ;
1923use Winter \Pages \Classes \MenuItem ;
24+ use Winter \Pages \Classes \ObjectHelper ;
2025use Winter \Pages \Classes \Page as StaticPage ;
2126use Winter \Pages \Classes \SnippetManager ;
27+ use Winter \Pages \FormWidgets \MenuItemSearch ;
2228use Winter \Pages \Plugin as PagesPlugin ;
2329use Winter \Pages \Widgets \MenuList ;
2430use Winter \Pages \Widgets \PageList ;
2531use Winter \Pages \Widgets \SnippetList ;
26-
27- use Winter \Pages \Classes \ObjectHelper ;
32+ use Winter \Storm \Halcyon \Datasource \DatasourceInterface ;
2833
2934/**
3035 * Pages and Menus index
@@ -120,14 +125,28 @@ protected function getObjectType(): string
120125 * Gets the object from the current request
121126 * @throws ApplicationException if the current user does not have permissions to manage the identified type
122127 */
123- public function getObjectFromRequest ()
128+ public function getObjectFromRequest (): CmsObject
124129 {
125130 $ type = $ this ->getObjectType ();
131+ $ objectPath = trim (Request::input ('objectPath ' ));
132+
133+ $ object = $ objectPath
134+ ? ObjectHelper::loadObject ($ this ->theme , $ type , $ objectPath )
135+ : ObjectHelper::createObject ($ this ->theme , $ type );
136+
137+ // Set page layout super early because it cascades to other elements
138+ if ($ type === 'page ' && ($ layout = post ('viewBag[layout] ' ))) {
139+ $ object ->getViewBag ()->setProperty ('layout ' , $ layout );
140+ }
141+
142+ $ formWidget = $ this ->makeObjectFormWidget ($ type , $ object , Request::input ('formWidgetAlias ' ));
143+
126144 return ObjectHelper::fillObject (
127145 $ this ->theme ,
128146 $ type ,
129- Request::input ('objectPath ' ),
130- post ()
147+ $ objectPath ,
148+ array_merge (post (), $ formWidget ->getSaveData ()),
149+ $ object
131150 );
132151 }
133152
@@ -152,12 +171,7 @@ public function pageAction()
152171 $ widget ->bindEvent ('form.refreshFields ' , function ($ allFields ) use ($ widget ) {
153172 $ this ->validateRequestTheme ();
154173
155- $ object = ObjectHelper::fillObject (
156- $ this ->theme ,
157- $ this ->getObjectType (),
158- Request::input ('objectPath ' ),
159- post ()
160- );
174+ $ object = $ this ->getObjectFromRequest ();
161175
162176 Cache::put (
163177 ObjectHelper::getTypePreviewSessionCacheKey ($ this ->getObjectType (), $ widget ->alias ),
@@ -189,7 +203,7 @@ public function index()
189203 }
190204 }
191205
192- public function index_onOpen ()
206+ public function index_onOpen (): array
193207 {
194208 $ this ->validateRequestTheme ();
195209
@@ -199,17 +213,11 @@ public function index_onOpen()
199213 return $ this ->pushObjectForm ($ type , $ object );
200214 }
201215
202- public function onSave ()
216+ public function onSave (): array
203217 {
204218 $ this ->validateRequestTheme ();
205219 $ type = $ this ->getObjectType ();
206-
207- $ object = ObjectHelper::fillObject (
208- $ this ->theme ,
209- $ type ,
210- Request::input ('objectPath ' ),
211- post ()
212- );
220+ $ object = $ this ->getObjectFromRequest ();
213221 $ object ->save ();
214222
215223 /*
@@ -235,7 +243,7 @@ public function onSave()
235243 return $ result ;
236244 }
237245
238- public function onCreateObject ()
246+ public function onCreateObject (): array
239247 {
240248 $ this ->validateRequestTheme ();
241249
@@ -273,7 +281,7 @@ public function onCreateObject()
273281 return $ result ;
274282 }
275283
276- public function onDelete ()
284+ public function onDelete (): array
277285 {
278286 $ this ->validateRequestTheme ();
279287
@@ -291,7 +299,7 @@ public function onDelete()
291299 return $ result ;
292300 }
293301
294- public function onDeleteObjects ()
302+ public function onDeleteObjects (): array
295303 {
296304 $ this ->validateRequestTheme ();
297305
@@ -340,7 +348,7 @@ public function onOpenConcurrencyResolveForm()
340348 return $ this ->makePartial ('concurrency_resolve_form ' );
341349 }
342350
343- public function onGetMenuItemTypeInfo ()
351+ public function onGetMenuItemTypeInfo (): array
344352 {
345353 $ type = Request::input ('type ' );
346354
@@ -349,22 +357,17 @@ public function onGetMenuItemTypeInfo()
349357 ];
350358 }
351359
352- public function onUpdatePageLayout ()
360+ public function onUpdatePageLayout (): array
353361 {
354362 $ this ->validateRequestTheme ();
355363
356364 $ type = $ this ->getObjectType ();
357- $ object = ObjectHelper::fillObject (
358- $ this ->theme ,
359- $ type ,
360- Request::input ('objectPath ' ),
361- post ()
362- );
365+ $ object = $ this ->getObjectFromRequest ();
363366
364367 return $ this ->pushObjectForm ($ type , $ object , Request::input ('formWidgetAlias ' ));
365368 }
366369
367- public function onGetInspectorConfiguration ()
370+ public function onGetInspectorConfiguration (): array
368371 {
369372 $ configuration = [];
370373
@@ -389,7 +392,7 @@ public function onGetInspectorConfiguration()
389392 ];
390393 }
391394
392- public function onGetSnippetNames ()
395+ public function onGetSnippetNames (): array
393396 {
394397 $ codes = array_unique (Request::input ('codes ' ));
395398 $ result = [];
@@ -418,12 +421,12 @@ public function onGetSnippetNames()
418421 ];
419422 }
420423
421- public function onMenuItemReferenceSearch ()
424+ public function onMenuItemReferenceSearch (): array
422425 {
423426 $ alias = Request::input ('alias ' );
424427
425428 $ widget = $ this ->makeFormWidget (
426- ' Winter\Pages\FormWidgets\ MenuItemSearch' ,
429+ MenuItemSearch::class ,
427430 [],
428431 ['alias ' => $ alias ]
429432 );
@@ -433,10 +436,8 @@ public function onMenuItemReferenceSearch()
433436
434437 /**
435438 * Commits the DB changes of a object to the filesystem
436- *
437- * @return array $response
438439 */
439- public function onCommit ()
440+ public function onCommit (): array
440441 {
441442 $ this ->validateRequestTheme ();
442443 $ type = $ this ->getObjectType ();
@@ -456,10 +457,8 @@ public function onCommit()
456457
457458 /**
458459 * Resets a object to the version on the filesystem
459- *
460- * @return array $response
461460 */
462- public function onReset ()
461+ public function onReset (): array
463462 {
464463 $ this ->validateRequestTheme ();
465464 $ type = $ this ->getObjectType ();
@@ -482,12 +481,8 @@ public function onReset()
482481
483482 /**
484483 * Get the response to return in an AJAX request that updates an object
485- *
486- * @param CmsObject $object The object that has been affected
487- * @param string $type The type of object being affected
488- * @return array $result;
489484 */
490- protected function getUpdateResponse (CmsObject $ object , string $ type )
485+ protected function getUpdateResponse (CmsObject $ object , string $ type ): array
491486 {
492487 $ result = [
493488 'objectPath ' => $ type != 'content ' ? $ object ->getBaseFileName () : $ object ->fileName ,
@@ -508,22 +503,17 @@ protected function getUpdateResponse(CmsObject $object, string $type)
508503
509504 /**
510505 * Get the active theme's datasource
511- *
512- * @return \Winter\Storm\Halcyon\Datasource\DatasourceInterface
513506 */
514- protected function getThemeDatasource ()
507+ protected function getThemeDatasource (): DatasourceInterface
515508 {
516509 return $ this ->theme ->getDatasource ();
517510 }
518511
519512 /**
520513 * Check to see if the provided object can be committed
521514 * Only available in debug mode, the DB layer must be enabled, and the object must exist in the database
522- *
523- * @param CmsObject $object
524- * @return boolean
525515 */
526- protected function canCommitObject (CmsObject $ object )
516+ protected function canCommitObject (CmsObject $ object ): bool
527517 {
528518 $ result = false ;
529519
@@ -564,7 +554,7 @@ protected function validateRequestTheme(): void
564554 }
565555 }
566556
567- protected function makeObjectFormWidget ($ type , $ object , $ alias = null )
557+ protected function makeObjectFormWidget ($ type , $ object , $ alias = null ): Form
568558 {
569559 $ formConfigs = [
570560 'page ' => '~/plugins/winter/pages/classes/page/fields.yaml ' ,
@@ -581,7 +571,7 @@ protected function makeObjectFormWidget($type, $object, $alias = null)
581571 $ widgetConfig ->alias = $ alias ?: 'form ' . studly_case ($ type ) . md5 ($ object ->exists ? $ object ->getFileName () : uniqid ());
582572 $ widgetConfig ->context = !$ object ->exists ? 'create ' : 'update ' ;
583573
584- $ widget = $ this ->makeWidget (' Backend\Widgets\ Form' , $ widgetConfig );
574+ $ widget = $ this ->makeWidget (Form::class , $ widgetConfig );
585575
586576 if ($ type == 'page ' ) {
587577 $ widget ->bindEvent ('form.extendFieldsBefore ' , function () use ($ widget , $ object ) {
@@ -594,7 +584,7 @@ protected function makeObjectFormWidget($type, $object, $alias = null)
594584 return $ widget ;
595585 }
596586
597- protected function checkContentField ($ formWidget , $ page )
587+ protected function checkContentField ($ formWidget , $ page ): void
598588 {
599589 if (!($ layout = $ page ->getLayoutObject ())) {
600590 return ;
@@ -614,7 +604,7 @@ protected function checkContentField($formWidget, $page)
614604 /**
615605 * addPageSyntaxFields adds syntax defined fields to the form
616606 */
617- protected function addPageSyntaxFields ($ formWidget , $ page )
607+ protected function addPageSyntaxFields ($ formWidget , $ page ): void
618608 {
619609 $ fields = $ page ->listLayoutSyntaxFields ();
620610
@@ -656,7 +646,7 @@ protected function addPageSyntaxFields($formWidget, $page)
656646 }
657647 }
658648
659- protected function addPagePlaceholders ($ formWidget , $ page )
649+ protected function addPagePlaceholders ($ formWidget , $ page ): void
660650 {
661651 $ placeholders = $ page ->listLayoutPlaceholders ();
662652
@@ -736,7 +726,7 @@ protected function getTabTitle(string $type, CmsObject $object): string
736726 return $ result ;
737727 }
738728
739- protected function pushObjectForm ($ type , $ object , $ alias = null )
729+ protected function pushObjectForm ($ type , $ object , $ alias = null ): array
740730 {
741731 $ widget = $ this ->makeObjectFormWidget ($ type , $ object , $ alias );
742732 $ widget ->bindToController ();
@@ -762,7 +752,7 @@ protected function pushObjectForm($type, $object, $alias = null)
762752 ];
763753 }
764754
765- protected function bindFormWidgetToController ()
755+ protected function bindFormWidgetToController (): void
766756 {
767757 $ alias = Request::input ('formWidgetAlias ' );
768758 $ type = $ this ->getObjectType ();
@@ -785,12 +775,9 @@ protected function bindFormWidgetToController()
785775 }
786776
787777 /**
788- * Replaces Windows style (/r/n) line endings with unix style (/n)
789- * line endings.
790- * @param string $markup The markup to convert to unix style endings
791- * @return string
778+ * Replaces Windows style (/r/n) line endings with unix style (/n) line endings.
792779 */
793- protected function convertLineEndings ($ markup )
780+ protected function convertLineEndings (string $ markup ): string
794781 {
795782 $ markup = str_replace ("\r\n" , "\n" , $ markup );
796783 $ markup = str_replace ("\r" , "\n" , $ markup );
@@ -800,9 +787,8 @@ protected function convertLineEndings($markup)
800787
801788 /**
802789 * Returns a list of content files
803- * @return \Winter\Storm\Database\Collection
804790 */
805- protected function getContentTemplateList ()
791+ protected function getContentTemplateList (): CmsObjectCollection
806792 {
807793 $ templates = Content::listInTheme ($ this ->theme , true );
808794
0 commit comments