Skip to content

Commit

Permalink
Merge pull request #46 from dmstr/feature/split-fat-methods
Browse files Browse the repository at this point in the history
move getter for rootNode and leaves from Tree::getMenuItems()
  • Loading branch information
schmunk42 authored Nov 11, 2021
2 parents a8823f3 + 889a0f1 commit 4f358c5
Showing 1 changed file with 35 additions and 12 deletions.
47 changes: 35 additions & 12 deletions models/Tree.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
*
Expand All @@ -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 [];
Expand All @@ -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 [];
}
Expand Down

0 comments on commit 4f358c5

Please sign in to comment.