/** * 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() . ' men_parent'); }]); $dataProvider = new ActiveDataProvider(['query' => $query]); $sort = $dataProvider->getSort(); $sort->attributes['menuParent.men_nombre'] = ['asc' => ['men_parent.men_nombre' => SORT_ASC], 'desc' => ['men_parent.men_nombre' => SORT_DESC], 'label' => 'men_parent']; $sort->attributes['men_orden'] = ['asc' => ['men_parent.men_orden' => SORT_ASC, 't.men_orden' => SORT_ASC], 'desc' => ['men_parent.men_orden' => SORT_DESC, 't.men_orden' => SORT_DESC], 'label' => 'men_orden']; $sort->defaultOrder = ['menuParent.men_nombre' => SORT_ASC]; if (!($this->load($params) && $this->validate())) { return $dataProvider; } $query->andFilterWhere(['t.men_id' => $this->id, 't.men_parent' => $this->men_parent]); $query->andFilterWhere(['like', 'lower(t.men_nnombre)', strtolower($this->men_nombre)])->andFilterWhere(['like', 't.men_url', $this->men_url])->andFilterWhere(['like', 'lower(men_parent.men_nombre)', 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('men_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 = []; $cWhereModule = '(men_modulo = \'' . Yii::$app->params['moduleActive']['module'] . '\' OR men_modulo = \'\' OR men_modulo IS NULL)'; $query = Menu::find()->select(['men_id'])->asArray(); if (count($filter2)) { $assigned = $query->where(['men_modulo' => [Yii::$app->params['moduleActive']['module'], ''], 'men_url' => $filter2])->column(); } if (count($filter1)) { $query->where('men_url like :filter and ' . $cWhereModule); 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' => self::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' => self::CACHE_TAG])); } } return $result; }
/** * Finds the Menu model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Menu the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Menu::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
/** * Get menu children * @return \yii\db\ActiveQuery */ public function getMenus() { return $this->hasMany(Menu::className(), ['men_parent' => 'men_id']); }
?> <?php echo $form->field($model, 'men_nombre')->textInput(['maxlength' => 128]); ?> <?php echo $form->field($model, 'parent_name')->widget('yii\\jui\\AutoComplete', ['options' => ['class' => 'form-control'], 'clientOptions' => ['source' => Menu::find()->select(['men_nombre'])->column()]]); ?> <?php echo $form->field($model, 'men_modulo')->widget('yii\\jui\\AutoComplete', ['options' => ['class' => 'form-control'], 'clientOptions' => ['source' => Configs::getModules()]]); ?> <?php echo $form->field($model, 'men_url')->widget('yii\\jui\\AutoComplete', ['options' => ['class' => 'form-control'], 'clientOptions' => ['source' => Menu::getSavedRoutes()]]); ?> <?php echo $form->field($model, 'men_orden')->input('number'); ?> <?php echo $form->field($model, 'men_data')->textarea(['rows' => 4]); ?> <div class="form-group"> <?php echo Html::submitButton($model->isNewRecord ? Yii::t('rbac-admin', 'Create') : Yii::t('rbac-admin', 'Update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']); ?> </div>