/** * Searching menu * @param array $params * @return \yii\data\ActiveDataProvider */ public function search($params) { $query = MenuModel::find()->from(MenuModel::tableName() . ' t')->joinWith(['menuParent' => function ($q) { $q->from(MenuModel::tableName() . ' parent'); }]); $dataProvider = new ActiveDataProvider(['query' => $query]); $sort = $dataProvider->getSort(); $sort->attributes['menuParent.name'] = ['asc' => ['parent.name' => SORT_ASC], 'desc' => ['parent.name' => SORT_DESC], 'label' => 'parent']; $sort->attributes['order'] = ['asc' => ['parent.order' => SORT_ASC, 't.order' => SORT_ASC], 'desc' => ['parent.order' => SORT_DESC, 't.order' => SORT_DESC], 'label' => 'order']; $sort->defaultOrder = ['menuParent.name' => SORT_ASC]; if (!($this->load($params) && $this->validate())) { return $dataProvider; } $query->andFilterWhere(['t.id' => $this->id, 't.parent' => $this->parent]); $query->andFilterWhere(['like', 'lower(t.name)', strtolower($this->name)])->andFilterWhere(['like', 't.route', $this->route])->andFilterWhere(['like', 'lower(parent.name)', strtolower($this->parent_name)]); return $dataProvider; }
/** * Use to get assigned menu of user. * @param mixed $userId * @param integer $root * @param \Closure $callback use to reformat output. * callback should have format like * * ``` * function ($menu) { * return [ * 'label' => $menu['name'], * 'url' => [$menu['route']], * 'options' => $data, * 'items' => $menu['children'] * ] * ] * } * ``` * @param boolean $refresh * @return array */ public static function getAssignedMenu($userId, $root = null, $callback = null, $refresh = false) { $config = Configs::instance(); /* @var $manager \yii\rbac\BaseManager */ $manager = Yii::$app->getAuthManager(); $menus = Menu::find()->asArray()->indexBy('id')->all(); $key = [__METHOD__, $userId, $manager->defaultRoles]; $cache = $config->cache; if ($refresh || $cache === null || ($assigned = $cache->get($key)) === false) { $routes = $filter1 = $filter2 = []; if ($userId !== null) { foreach ($manager->getPermissionsByUser($userId) as $name => $value) { if ($name[0] === '/') { if (substr($name, -2) === '/*') { $name = substr($name, 0, -1); } $routes[] = $name; } } } foreach ($manager->defaultRoles as $role) { foreach ($manager->getPermissionsByRole($role) as $name => $value) { if ($name[0] === '/') { if (substr($name, -2) === '/*') { $name = substr($name, 0, -1); } $routes[] = $name; } } } $routes = array_unique($routes); sort($routes); $prefix = '\\'; foreach ($routes as $route) { if (strpos($route, $prefix) !== 0) { if (substr($route, -1) === '/') { $prefix = $route; $filter1[] = $route . '%'; } else { $filter2[] = $route; } } } $assigned = []; $query = Menu::find()->select(['id'])->asArray(); if (count($filter2)) { $assigned = $query->where(['route' => $filter2])->column(); } if (count($filter1)) { $query->where('route like :filter'); foreach ($filter1 as $filter) { $assigned = array_merge($assigned, $query->params([':filter' => $filter])->column()); } } $assigned = static::requiredParent($assigned, $menus); if ($cache !== null) { $cache->set($key, $assigned, $config->cacheDuration, new TagDependency(['tags' => Configs::CACHE_TAG])); } } $key = [__METHOD__, $assigned, $root]; if ($refresh || $callback !== null || $cache === null || ($result = $cache->get($key)) === false) { $result = static::normalizeMenu($assigned, $menus, $callback, $root); if ($cache !== null && $callback === null) { $cache->set($key, $result, $config->cacheDuration, new TagDependency(['tags' => Configs::CACHE_TAG])); } } return $result; }