Skip to content

Commit

Permalink
Merge pull request #47 from dmstr/feature/actionRefPage
Browse files Browse the repository at this point in the history
Feature/action ref page
  • Loading branch information
schmunk42 committed Nov 12, 2021
2 parents 4f358c5 + 1806a01 commit 8cf02e0
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 4 deletions.
4 changes: 2 additions & 2 deletions Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public function init()
parent::init();

// add routes from settings module
if ($this->checkSettingsInstalled()) {
if (self::checkSettingsInstalled()) {
$routes = explode("\n", \Yii::$app->settings->get('pages.availableRoutes'));
foreach ($routes as $route) {
$routeEntry = trim($route);
Expand Down Expand Up @@ -130,7 +130,7 @@ public function getLocalizedRootNode()
* Check for "pheme/yii2-settings" component and module
* @return bool
*/
private function checkSettingsInstalled()
public static function checkSettingsInstalled()
{
return \Yii::$app->hasModule('settings') && \Yii::$app->has('settings');
}
Expand Down
91 changes: 90 additions & 1 deletion controllers/DefaultController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,18 @@
use dmstr\modules\pages\assets\PagesBackendAsset;
use dmstr\modules\pages\helpers\PageHelper;
use dmstr\modules\pages\models\Tree;
use dmstr\modules\pages\Module;
use dmstr\modules\pages\traits\RequestParamActionTrait;
use pheme\settings\components\Settings;
use Yii;
use yii\base\Event;
use yii\helpers\ArrayHelper;
use yii\helpers\Json;
use yii\helpers\Url;
use yii\web\Controller;
use yii\web\HttpException;
use yii\web\MethodNotAllowedHttpException;
use yii\web\NotFoundHttpException;
use yii\web\View;

/**
Expand All @@ -31,6 +37,69 @@
class DefaultController extends Controller implements ContextMenuItemsInterface
{

use RequestParamActionTrait;

/**
* ignore pageId param as req-param for actionPage as the id is provided from model->id itself
* required as we use RequestParamActionTrait in this controller
*
* @return false
*/
protected function pageActionParamPageId()
{
return false;
}

/**
* pageId param provider for actionRefPage()
* pages are fetched from defined rootIds
*
* @return array
*/
protected function refPageActionParamPageId()
{

$rootIds = [Tree::ROOT_NODE_PREFIX];
/** @var Settings \Yii::$app->settings */
if (Module::checkSettingsInstalled() && Yii::$app->settings->get('refPageRootIds', 'pages', null)) {
$tmp = explode("\n", \Yii::$app->settings->get('pages.refPageRootIds'));
$tmp = array_filter(array_map('trim', $tmp));
$rootIds = $tmp ?? $rootIds;
}

$pages = [];
foreach ($rootIds as $rootId) {
$rootNode = Tree::getRootByDomainId($rootId);
if ($rootNode) {
$leaves = Tree::getLeavesFromRoot($rootNode)->andWhere(['route' => Tree::DEFAULT_PAGE_ROUTE])->all();
if (!empty($leaves)) {
$leaves = array_filter($leaves, function($leave) {
if (!empty($leave->request_params)) {
$params = Json::decode($leave->request_params);
if (!empty($params) && isset($params->pageId) && $params->pageId == $leave->id) {
return false;
}
}
return true;
});
/** @var Tree $leave */
foreach ($leaves as $leave) {
Yii::debug(ArrayHelper::map($leave->parents()->all(), 'id', 'name'));
if (!$leave->isPage()) {
continue;
}
// build human-readable label for each leave
$pages[$leave->id] = implode(' :: ', ArrayHelper::merge(ArrayHelper::map($leave->parents()->all(), 'id', 'name'), [$leave->name . ' (' . $leave->id . ')']));
}
}
}
}

$params = ArrayHelper::merge(['' => Yii::t('pages', 'Select target page')], $pages);
return $params;

}

/**
* @inheritdoc
*/
Expand Down Expand Up @@ -112,6 +181,26 @@ public function actionResolveRouteToSchema()
throw new MethodNotAllowedHttpException(Yii::t('pages', 'You are not allowed to access this page like this'));
}


/**
* Redirect to URL for given pageId
* This is useful if one will create multiple menu items to one existing content page
*
* @param $pageId
*
* @throws NotFoundHttpException
*/
public function actionRefPage($pageId)
{

$page = Tree::findOne(['id' => $pageId]);
if ($page && $page instanceof Tree) {
$this->redirect($page->createUrl());
} else {
throw new NotFoundHttpException();
}
}

/**
* renders a page view from the database.
*
Expand All @@ -124,7 +213,7 @@ public function actionResolveRouteToSchema()
public function actionPage($pageId)
{
Url::remember();
\Yii::$app->session['__crudReturnUrl'] = null;
\Yii::$app->session->set('__crudReturnUrl', null);

// Set layout
$this->layout = $this->module->defaultPageLayout;
Expand Down
3 changes: 2 additions & 1 deletion traits/RequestParamActionTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,14 +207,15 @@ private function generateJson($parameters, $actionId)
} else {

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

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

// generate default if nothing else is defined
$extraProperties = implode(',', $extraProperties);
$properties[] = $this->defaultFieldJson($parameterName, $extraProperties);
}
}
Expand Down

0 comments on commit 8cf02e0

Please sign in to comment.