Skip to content

Commit 8cf02e0

Browse files
authored
Merge pull request #47 from dmstr/feature/actionRefPage
Feature/action ref page
2 parents 4f358c5 + 1806a01 commit 8cf02e0

File tree

3 files changed

+94
-4
lines changed

3 files changed

+94
-4
lines changed

Module.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public function init()
7676
parent::init();
7777

7878
// add routes from settings module
79-
if ($this->checkSettingsInstalled()) {
79+
if (self::checkSettingsInstalled()) {
8080
$routes = explode("\n", \Yii::$app->settings->get('pages.availableRoutes'));
8181
foreach ($routes as $route) {
8282
$routeEntry = trim($route);
@@ -130,7 +130,7 @@ public function getLocalizedRootNode()
130130
* Check for "pheme/yii2-settings" component and module
131131
* @return bool
132132
*/
133-
private function checkSettingsInstalled()
133+
public static function checkSettingsInstalled()
134134
{
135135
return \Yii::$app->hasModule('settings') && \Yii::$app->has('settings');
136136
}

controllers/DefaultController.php

Lines changed: 90 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,18 @@
1515
use dmstr\modules\pages\assets\PagesBackendAsset;
1616
use dmstr\modules\pages\helpers\PageHelper;
1717
use dmstr\modules\pages\models\Tree;
18+
use dmstr\modules\pages\Module;
19+
use dmstr\modules\pages\traits\RequestParamActionTrait;
20+
use pheme\settings\components\Settings;
1821
use Yii;
1922
use yii\base\Event;
23+
use yii\helpers\ArrayHelper;
24+
use yii\helpers\Json;
2025
use yii\helpers\Url;
2126
use yii\web\Controller;
2227
use yii\web\HttpException;
2328
use yii\web\MethodNotAllowedHttpException;
29+
use yii\web\NotFoundHttpException;
2430
use yii\web\View;
2531

2632
/**
@@ -31,6 +37,69 @@
3137
class 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;

traits/RequestParamActionTrait.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,14 +207,15 @@ private function generateJson($parameters, $actionId)
207207
} else {
208208

209209
// add defaults here again to guarantee same behavior as if property would have a corresponding method
210-
$extraProperties = '"type": "string","title": "' . Inflector::camel2words($parameterName) . '"';
210+
$extraProperties = ['"type": "string"','"title": "' . Inflector::camel2words($parameterName) . '"'];
211211

212212
if (!$parameter->isOptional()) {
213213
$requiredFields[] = $parameterName;
214214
$extraProperties[] = '"minLength": 1';
215215
}
216216

217217
// generate default if nothing else is defined
218+
$extraProperties = implode(',', $extraProperties);
218219
$properties[] = $this->defaultFieldJson($parameterName, $extraProperties);
219220
}
220221
}

0 commit comments

Comments
 (0)