1515use dmstr \modules \pages \assets \PagesBackendAsset ;
1616use dmstr \modules \pages \helpers \PageHelper ;
1717use dmstr \modules \pages \models \Tree ;
18+ use dmstr \modules \pages \Module ;
19+ use dmstr \modules \pages \traits \RequestParamActionTrait ;
20+ use pheme \settings \components \Settings ;
1821use Yii ;
1922use yii \base \Event ;
23+ use yii \helpers \ArrayHelper ;
24+ use yii \helpers \Json ;
2025use yii \helpers \Url ;
2126use yii \web \Controller ;
2227use yii \web \HttpException ;
2328use yii \web \MethodNotAllowedHttpException ;
29+ use yii \web \NotFoundHttpException ;
2430use yii \web \View ;
2531
2632/**
3137class DefaultController extends Controller implements ContextMenuItemsInterface
3238{
3339
40+ use RequestParamActionTrait;
41+
42+ /**
43+ * ignore pageId param as req-param for actionPage as the id is provided from model->id itself
44+ * required as we use RequestParamActionTrait in this controller
45+ *
46+ * @return false
47+ */
48+ protected function pageActionParamPageId ()
49+ {
50+ return false ;
51+ }
52+
53+ /**
54+ * pageId param provider for actionRefPage()
55+ * pages are fetched from defined rootIds
56+ *
57+ * @return array
58+ */
59+ protected function refPageActionParamPageId ()
60+ {
61+
62+ $ rootIds = [Tree::ROOT_NODE_PREFIX ];
63+ /** @var Settings \Yii::$app->settings */
64+ if (Module::checkSettingsInstalled () && Yii::$ app ->settings ->get ('refPageRootIds ' , 'pages ' , null )) {
65+ $ tmp = explode ("\n" , \Yii::$ app ->settings ->get ('pages.refPageRootIds ' ));
66+ $ tmp = array_filter (array_map ('trim ' , $ tmp ));
67+ $ rootIds = $ tmp ?? $ rootIds ;
68+ }
69+
70+ $ pages = [];
71+ foreach ($ rootIds as $ rootId ) {
72+ $ rootNode = Tree::getRootByDomainId ($ rootId );
73+ if ($ rootNode ) {
74+ $ leaves = Tree::getLeavesFromRoot ($ rootNode )->andWhere (['route ' => Tree::DEFAULT_PAGE_ROUTE ])->all ();
75+ if (!empty ($ leaves )) {
76+ $ leaves = array_filter ($ leaves , function ($ leave ) {
77+ if (!empty ($ leave ->request_params )) {
78+ $ params = Json::decode ($ leave ->request_params );
79+ if (!empty ($ params ) && isset ($ params ->pageId ) && $ params ->pageId == $ leave ->id ) {
80+ return false ;
81+ }
82+ }
83+ return true ;
84+ });
85+ /** @var Tree $leave */
86+ foreach ($ leaves as $ leave ) {
87+ Yii::debug (ArrayHelper::map ($ leave ->parents ()->all (), 'id ' , 'name ' ));
88+ if (!$ leave ->isPage ()) {
89+ continue ;
90+ }
91+ // build human-readable label for each leave
92+ $ pages [$ leave ->id ] = implode (' :: ' , ArrayHelper::merge (ArrayHelper::map ($ leave ->parents ()->all (), 'id ' , 'name ' ), [$ leave ->name . ' ( ' . $ leave ->id . ') ' ]));
93+ }
94+ }
95+ }
96+ }
97+
98+ $ params = ArrayHelper::merge (['' => Yii::t ('pages ' , 'Select target page ' )], $ pages );
99+ return $ params ;
100+
101+ }
102+
34103 /**
35104 * @inheritdoc
36105 */
@@ -112,6 +181,26 @@ public function actionResolveRouteToSchema()
112181 throw new MethodNotAllowedHttpException (Yii::t ('pages ' , 'You are not allowed to access this page like this ' ));
113182 }
114183
184+
185+ /**
186+ * Redirect to URL for given pageId
187+ * This is useful if one will create multiple menu items to one existing content page
188+ *
189+ * @param $pageId
190+ *
191+ * @throws NotFoundHttpException
192+ */
193+ public function actionRefPage ($ pageId )
194+ {
195+
196+ $ page = Tree::findOne (['id ' => $ pageId ]);
197+ if ($ page && $ page instanceof Tree) {
198+ $ this ->redirect ($ page ->createUrl ());
199+ } else {
200+ throw new NotFoundHttpException ();
201+ }
202+ }
203+
115204 /**
116205 * renders a page view from the database.
117206 *
@@ -124,7 +213,7 @@ public function actionResolveRouteToSchema()
124213 public function actionPage ($ pageId )
125214 {
126215 Url::remember ();
127- \Yii::$ app ->session [ '__crudReturnUrl ' ] = null ;
216+ \Yii::$ app ->session -> set ( '__crudReturnUrl ' , null ) ;
128217
129218 // Set layout
130219 $ this ->layout = $ this ->module ->defaultPageLayout ;
0 commit comments