Class luya\cms\admin\helpers\MenuHelper

Inheritanceluya\cms\admin\helpers\MenuHelper
Available since version1.0.0
Source Code https://github.com/luyadev/luya-module-cms/blob/master/src/admin/helpers/MenuHelper.php

Menu Helper to collect Data used in Administration areas.

Public Methods

Hide inherited methods

Method Description Defined By
checkUserWebsitePermissions() Check that the current user is allowed to access to the given website data. luya\cms\admin\helpers\MenuHelper
getContainers() Get all cms containers luya\cms\admin\helpers\MenuHelper
getDrafts() Get all drafts nav items luya\cms\admin\helpers\MenuHelper
getItems() Get all nav data entries with corresponding item content luya\cms\admin\helpers\MenuHelper
getWebsites() Get all cms websites luya\cms\admin\helpers\MenuHelper

Method Details

Hide inherited methods

checkUserWebsitePermissions() public static method

Check that the current user is allowed to access to the given website data.

public static boolean checkUserWebsitePermissions ( array $website )
$website array

Array with website data. Have to provide user_ids and group_ids in json string.

                public static function checkUserWebsitePermissions(array $website)
{
    /** @var User $user */
    $user = Yii::$app->adminuser->identity;
    $userGroupIds = ArrayHelper::getColumn($user->groups, 'id');
    $users = Json::decode($website['user_ids']) ?? [];
    foreach ($users as $item) {
        if ($item['value'] === 0 || $item['value'] == $user->id) {
            return true;
        }
    }
    $groups = Json::decode($website['group_ids']) ?? [];
    foreach ($groups as $item) {
        if ($item['value'] === 0 || in_array($item['value'], $userGroupIds)) {
            return true;
        }
    }
    return false;
}

            
getContainers() public static method

Get all cms containers

public static array getContainers ( )

                public static function getContainers()
{
    if (self::$containers === null) {
        self::$containers = (new Query())->select(['cms_nav_container.id', 'name' => 'cms_nav_container.name', 'website_name' => 'cms_website.name', 'alias', 'website_id'])
            ->from('cms_nav_container')
            ->innerJoin('cms_website', 'website_id = cms_website.id')
            ->where(['cms_nav_container.is_deleted' => false])
            ->orderBy(['cms_website.name' => 'ASC', 'cms_nav_container.name' => 'ASC'])
            ->all();
    }
    return self::$containers;
}

            
getDrafts() public static method

Get all drafts nav items

public static array getDrafts ( )

                public static function getDrafts()
{
    if (self::$drafts === null) {
        self::$drafts = (new Query())
        ->select(['cms_nav.id', 'nav_container_id', 'parent_nav_id', 'is_hidden', 'is_offline', 'is_draft', 'is_home', 'cms_nav_item.title'])
        ->from('cms_nav')
        ->leftJoin('cms_nav_item', 'cms_nav.id=cms_nav_item.nav_id')
        ->orderBy('cms_nav.sort_index ASC')
        ->where(['cms_nav_item.lang_id' => Yii::$app->adminLanguage->defaultLanguage['id'], 'cms_nav.is_deleted' => false, 'cms_nav.is_draft' => true])
        ->all();
    }
    return self::$drafts;
}

            
getItems() public static method

Get all nav data entries with corresponding item content

public static array getItems ( )

                public static function getItems()
{
    if (self::$items === null) {
        $items = Nav::find()
            ->select(['cms_nav.id', 'nav_item_id' => 'cms_nav_item.id', 'nav_container_id', 'parent_nav_id', 'is_hidden', 'layout_file', 'is_offline', 'is_draft', 'is_home', 'cms_nav_item.title'])
            ->leftJoin('cms_nav_item', 'cms_nav.id=cms_nav_item.nav_id')
            ->with(['parents'])
            ->orderBy(['sort_index' => SORT_ASC])
            ->where([
                'cms_nav_item.lang_id' => Yii::$app->adminLanguage->defaultLanguage['id'],
                'cms_nav.is_deleted' => false,
                'cms_nav.is_draft' => false,
            ])
            ->asArray()
            ->all();
        self::loadInheritanceData(0);
        $data = [];
        foreach ($items as $key => $item) {
            $item['is_draft'] = (int) $item['is_draft'];
            $item['is_hidden'] = (int) $item['is_hidden'];
            $item['is_home'] = (int) $item['is_home'];
            $item['is_offline'] = (int) $item['is_offline'];
            $item['is_editable'] = (int) Yii::$app->adminuser->canRoute('cmsadmin/page/update');
            $item['toggle_open'] = (int) Yii::$app->adminuser->identity->setting->get('tree.'.$item['id']);
            $item['has_children'] = empty($item['parents']) ? 0 : count($item['parents']); //(new Query())->from('cms_nav')->select(['id'])->where(['parent_nav_id' => $item['id']])->count();
            // the user have "page edit" permission, now we can check if the this group has more fined tuned permisionss from the
            // cms_nav_permissions table or not
            if ($item['is_editable']) {
                $permitted = false;
                // check permissions for all groups, if alreay permited skip
                foreach (Yii::$app->adminuser->identity->groups as $group) {
                    if ($permitted) {
                        continue;
                    }
                    $permitted = self::navGroupPermission($item['id'], $group->id);
                }
                // if not yet permitted, check in inheritance table.
                if (!$permitted) {
                    $value = self::$_inheritData[$item['id']] ?? false;
                    if ($value === true) {
                        $permitted = true;
                    }
                }
                $item['is_editable'] = (int) $permitted;
            }
            $data[$key] = $item;
        }
        self::$items = $data;
    }
    return self::$items;
}

            
getWebsites() public static method (available since version 4.0.0)

Get all cms websites

public static array getWebsites ( )

                public static function getWebsites()
{
    if (self::$websites === null) {
        $websites = (new Query())
            ->select([
                'cms_website.id',
                'cms_website.name',
                'cms_website.host',
                'cms_website.is_default',
                'default_container_id' => 'MIN(cms_nav_container.id)',
                'group_ids',
                'user_ids'
            ])
            ->from('cms_website')
            ->leftJoin('cms_nav_container', 'website_id = cms_website.id')
            ->where(['cms_website.is_active' => true, 'cms_website.is_deleted' => false])
            ->groupBy('cms_website.id')
            ->all();
        foreach ($websites as $websiteIndex => $website) {
            if (!self::checkUserWebsitePermissions($website)) {
                unset($websites[$websiteIndex]);
            }
        }
        self::$websites = array_values($websites);
    }
    return self::$websites;
}