From 889a0f104d932f5af43eb81f7c7349dafc4b8ab4 Mon Sep 17 00:00:00 2001 From: Jens Giessmann Date: Thu, 11 Nov 2021 14:18:54 +0100 Subject: [PATCH] move getter for rootNode and leaves from Tree::getMenuItems() to separate methods so we can reuse these in other places if needed (e.g. as refPage action params) --- models/Tree.php | 47 +++++++++++++++++++++++++++++++++++------------ 1 file changed, 35 insertions(+), 12 deletions(-) diff --git a/models/Tree.php b/models/Tree.php index 005dea6..37225bf 100755 --- a/models/Tree.php +++ b/models/Tree.php @@ -13,6 +13,7 @@ use dmstr\modules\pages\helpers\PageHelper; use dmstr\modules\pages\Module as PagesModule; use dosamigos\translateable\TranslateableBehavior; +use kartik\tree\models\TreeQuery; use rmrevin\yii\fontawesome\FA; use Yii; use yii\caching\TagDependency; @@ -265,6 +266,38 @@ public function createUrl($additionalParams = []) return Url::to($this->createRoute($additionalParams)); } + /** + * get Root node by given domainId + * + * @param $domainId + * + * @return Tree|null + */ + public static function getRootByDomainId($domainId) + { + $rootCondition[self::ATTR_DOMAIN_ID] = $domainId; + $rootCondition[self::ATTR_ACCESS_DOMAIN] = [self::GLOBAL_ACCESS_DOMAIN, mb_strtolower(\Yii::$app->language)]; + return self::findOne($rootCondition); + } + + /** + * return activeQuery that select children for given root Node + * + * @param Tree $rootNode + * + * @return TreeQuery + */ + public static function getLeavesFromRoot(Tree $rootNode) + { + $leavesQuery = $rootNode->children()->andWhere( + [ + self::ATTR_ACTIVE => self::ACTIVE, + self::ATTR_ACCESS_DOMAIN => [self::GLOBAL_ACCESS_DOMAIN, mb_strtolower(\Yii::$app->language)], + ] + ); + return $leavesQuery->with('translationsMeta'); + } + /** * Build array with active and visible menu nodes for the current application language * @@ -286,9 +319,7 @@ public static function getMenuItems($domainId, $checkUserPermissions = false, ar Yii::trace(['Building menu items', $cacheKey], __METHOD__); // Get root node by domain id - $rootCondition[self::ATTR_DOMAIN_ID] = $domainId; - $rootCondition[self::ATTR_ACCESS_DOMAIN] = [self::GLOBAL_ACCESS_DOMAIN, mb_strtolower(\Yii::$app->language)]; - $rootNode = self::findOne($rootCondition); + $rootNode = self::getRootByDomainId($domainId); if ($rootNode === null) { return []; @@ -303,15 +334,7 @@ public static function getMenuItems($domainId, $checkUserPermissions = false, ar */ // Get all leaves from this root node - $leavesQuery = $rootNode->children()->andWhere( - [ - self::ATTR_ACTIVE => self::ACTIVE, - self::ATTR_ACCESS_DOMAIN => [self::GLOBAL_ACCESS_DOMAIN, mb_strtolower(\Yii::$app->language)], - ] - ); - $leavesQuery->with('translationsMeta'); - $leaves = $leavesQuery->all(); - + $leaves = self::getLeavesFromRoot($rootNode)->all(); if ($leaves === null) { return []; }