public function up() { $news_menu = Menu::findOne(['menu_name' => "资讯管理", 'menu_depth' => 0]); // 首页热门活动管理 $this->insert('{{%admin_menu}}', ['menu_name' => '首页热门活动管理', 'menu_link' => "news/activity-block", 'show_order' => 1, 'creator' => "niancode", 'create_time' => time(), 'parent_id' => $news_menu->id, 'menu_depth' => 1]); $insert_id = $this->db->getLastInsertID(); $this->insert('{{%admin_menu}}', ['menu_name' => '添加热门活动', 'menu_link' => "news/activity-block-save", 'show_order' => 0, 'creator' => "niancode", 'create_time' => time(), 'parent_id' => $insert_id, 'menu_depth' => 2]); $this->insert('{{%admin_menu}}', ['menu_name' => '修改热门活动', 'menu_link' => "news/activity-block-delete", 'show_order' => 1, 'creator' => "niancode", 'create_time' => time(), 'parent_id' => $insert_id, 'menu_depth' => 2]); }
public static function getMenus($pid = 0) { $arr = array(); $arr[0] = '作为一级菜单'; $menus = Menu::find()->where(['pid' => $pid])->all(); if ($menus) { foreach ($menus as $key => $value) { $arr[$key] = $value->name; } } return $arr; }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Menu::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to return any records when validation fails // $query->where('0=1'); return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'order' => $this->order]); $query->andFilterWhere(['like', 'name', $this->name])->andFilterWhere(['like', 'route', $this->route])->andFilterWhere(['like', 'data', $this->data]); return $dataProvider; }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Menu::find(); $dataProvider = new ActiveDataProvider(['query' => $query, 'sort' => ['defaultOrder' => ['id' => SORT_ASC, 'sort_val' => SORT_ASC]]]); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to return any records when validation fails // $query->where('0=1'); return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'pid' => $this->pid, 'mg_id' => $this->mg_id, 'sort_val' => $this->sort_val, 'created_at' => $this->created_at]); $query->andFilterWhere(['like', 'name', $this->name])->andFilterWhere(['like', 'url', $this->url])->andFilterWhere(['like', 'param', $this->param])->andFilterWhere(['like', 'theme', $this->theme]); return $dataProvider; }
public function up() { // 修改菜单名称 $activity_menu = Menu::findOne(['menu_name' => "首页热门活动管理", 'menu_depth' => 1]); if ($activity_menu) { $activity_menu->menu_name = '资讯首页热门活动管理'; $activity_menu->save(false); } // 资讯首页热门活动 $this->insert('{{%page_custom_block}}', ['id' => 6, 'name' => "首页资讯管理", 'custom_id' => "main_page_news", 'title' => "首页资讯管理", 'sub_title' => "首页资讯管理", 'data' => '', 'create_time' => time(), 'update_time' => time()]); // 首页推荐资讯菜单 $news_menu = Menu::findOne(['menu_name' => "首页管理", 'menu_depth' => 0]); $this->insert('{{%admin_menu}}', ['menu_name' => '首页资讯管理', 'menu_link' => "news/recommend", 'show_order' => 8, 'creator' => "niancode", 'create_time' => time(), 'parent_id' => $news_menu->id, 'menu_depth' => 1]); }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Menu::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to any records when validation fails // $query->where('0=1'); return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'timestamp' => $this->timestamp]); $query->andFilterWhere(['like', 'title', $this->title]); return $dataProvider; }
<?php use yii\helpers\Html; use yii\widgets\ActiveForm; use backend\models\Menu; /* @var $this yii\web\View */ /* @var $model backend\models\Menu */ /* @var $form yii\widgets\ActiveForm */ ?> <?php $form = ActiveForm::begin(); ?> <div class="box-body menu-form"> <?php echo $form->field($model, 'pid')->textInput()->dropDownList(Menu::getParents($model->id)); ?> <?php echo $form->field($model, 'mg_id')->textInput(['maxlength' => true, 'placeholder' => '输入' . $model->getAttributeLabel('mg_id')]); ?> <?php echo $form->field($model, 'name')->textInput(['maxlength' => true, 'placeholder' => '输入' . $model->getAttributeLabel('name')]); ?> <?php echo $form->field($model, 'url')->textInput(['maxlength' => true, 'placeholder' => '输入' . $model->getAttributeLabel('url')]); ?> <?php echo $form->field($model, 'param')->textInput(['maxlength' => true, 'placeholder' => '输入' . $model->getAttributeLabel('param')]); ?> <?php echo $form->field($model, 'theme')->textInput(['maxlength' => true, 'placeholder' => '输入' . $model->getAttributeLabel('theme')]);
height: 13px; width: 13px; } .notice { color: #c33; } </style> <div class="col-md-12"> <div class="row"> <div class="col-md-4"> <label>Select Menu</label> <select name="MENU_select" class="form-control MENU_select"> <?php $menus = Menu::find()->all(); foreach ($menus as $key) { echo '<option value="' . $key->id . '">' . $key->title . '</option>'; } ?> </select> </div> </div> </div> <div class="col-md-4"> <div class="box dark full-screen-box"> <header>
<?php use yii\helpers\Html; use backend\models\Menu; use yii\helpers\Url; use backend\assets\AppAsset; AppAsset::register($this); $mMenu = new Menu(); $session = Yii::$app->session; $session->isActive ? '' : $session->open(); $admin = $session->get('admin'); $session->close(); $ids = json_decode($admin['auth']); $menu = $mMenu->menu($ids); $parent_id = ''; $son_id = ''; foreach ($menu as $key => $value) { if ($value['menu_link'] == Yii::$app->request->pathInfo) { $parent_id = $value['parent_id']; $son_id = $value['id']; break; } } $icon = ['首页管理' => 'ti-home', '产品' => 'fa fa-cubes', '设置' => 'ti-settings', '主页' => 'ti-user']; // print_r(array_column($menu, 'menu_link', 'id', 'menu_name')); // $menu = $mMenu->menu(); $this->beginPage(); ?> <!DOCTYPE html> <!-- Template Name: Clip-Two - Responsive Admin Template build with Twitter Bootstrap 3.x | Author: ClipTheme --> <!--[if IE 8]><html class="ie8" lang="en"><![endif]-->
/** * 获取快捷菜单列表 * @return [type] [description] * @version 1.0 2016-01-25T17:32:51+0800 * @author zhangxianglong@yolo24.com */ public static function getMyMenuList() { $cache_key = 'backend_my_menu_' . Yii::$app->user->id; $menu_list = Yii::$app->cache->get($cache_key); if ($menu_list === false) { $menu_list = Menu::find()->where(['mg_id' => Yii::$app->user->id])->orderBy('sort_val ASC')->asArray()->all(); Yii::$app->cache->set($cache_key, $menu_list, 86400); } return $menu_list; }
<?php use yii\helpers\Html; use backend\models\Menu; use yii\helpers\Url; use backend\assets\AppAsset; AppAsset::register($this); $mMenu = new Menu(); $uri = Yii::$app->request->pathInfo; $uri = substr_count($uri, '/') == 2 ? substr($uri, 0, strrpos($uri, '/')) : $uri; $session = Yii::$app->session; $session->isActive ? '' : $session->open(); $admin = $session->get('admin'); $session->close(); $ids = json_decode($admin['auth']); $menu = $mMenu->menu(); $sub_parent_id = ''; $parent_id = ''; $son_id = ''; foreach ($menu as $key => $value) { if ($value['menu_link'] == $uri) { if ($value['menu_depth'] >= 2) { $sub_parent_id = $value['parent_id']; $parent_id = $mMenu->get_menu_by_parent_id($sub_parent_id, 2)->id; $son_id = $mMenu->get_menu_by_parent_id($sub_parent_id, 1)->id; } else { $parent_id = $value['parent_id']; $son_id = $value['id']; } break; } }
echo $child_row['name']; ?> </a> </li> <?php } ?> </ul> </li> <?php } ?> <!-- <li><a href="documentation/index.html"><i class="fa fa-book"></i> <span>Documentation</span></a></li> --> <li class="header">LABELS-快捷菜单</li> <?php $my_menu_list = Menu::getMyMenuList(); foreach ($my_menu_list as $key => $menu_row) { ?> <li class="quick-menu"><a href="<?php echo $menu_row['url']; ?> " <?php echo strpos($menu_row['url'], 'http://') !== false ? 'target="_blank"' : ''; ?> ><i class="fa <?php echo $menu_row['theme']; ?> "></i> <span><?php echo $menu_row['name']; ?> </span></a></li>
/** * 快捷菜单新建 * @return {[type]} [description] * @version 1.0 2016-01-25T12:24:11+0800 * @author cnzhangxl@foxmail.com */ public function actionMyCreate() { $model = new Menu(['scenario' => 'my_create']); $model->mg_id = Yii::$app->user->id; if ($model->load(Yii::$app->request->post()) && $model->save()) { Menu::removeMyMenuCache(); return $this->redirect(['my']); } else { return $this->render('my_create', ['model' => $model]); } }
public function actionDataTables() { $responseType = Yii::$app->request->get('type'); $returnData = []; switch ($responseType) { case "fetch": $returnData = Menu::find()->select(['id', 'name', 'parent', 'route', 'order', 'data'])->where(['parent' => Yii::$app->request->get('id')])->orderBy(['order' => SORT_ASC])->all(); return Json::encode($returnData); case "crud": $requestAction = Yii::$app->request->post('action'); switch ($requestAction) { case "create": $requestID = key(Yii::$app->request->post('data')); $requestData = Yii::$app->request->post('data')[$requestID]; $model = new Menu(); //块赋值 $model->attributes = $requestData; //返回错误信息给datatable if (!$model->validate()) { $fieldErrors = []; foreach ($model->errors as $name => $status) { $fieldErrors[] = ['name' => $name, 'status' => Json::encode($status)]; } return Json::encode(['fieldErrors' => $fieldErrors, 'data' => []]); } if ($model->save()) { $requestData['id'] = $model->id; $data = []; $data[] = $requestData; return Json::encode(['data' => $data]); } else { var_dump($model->errors); $error = '恢复操作发生意外!'; throw new Exception($error); } case "edit": $editID = array_keys(Yii::$app->request->post('data')); //执行事务,保存必须都成功了才行 $transaction = Menu::getDb()->beginTransaction(); //返回值 $data = []; try { foreach ($editID as $requestID) { $requestData = Yii::$app->request->post('data')[$requestID]; $model = Menu::findOne($requestID); //块赋值 $model->attributes = $requestData; //返回错误信息给datatable if (!$model->validate()) { $fieldErrors = []; foreach ($model->errors as $name => $status) { $fieldErrors[] = ['name' => $name, 'status' => Json::encode($status)]; } return Json::encode(['fieldErrors' => $fieldErrors, 'data' => []]); } $model->save(); //单个字段更新的时候 if (count($requestData) == 1 || !is_array($requestData)) { $requestData = Menu::find()->select(['id', 'name', 'parent', 'route', 'order', 'data'])->where(['id' => $requestID])->one(); } else { $requestData['id'] = $requestID; } $data[] = $requestData; } //提交事务 $transaction->commit(); return Json::encode(['data' => $data]); } catch (\Exception $e) { $transaction->rollBack(); throw $e; } case "remove": //多选则删除全部 用deleteAll()不会触发 event: EVENT_BEFORE_DELETE 和 EVENT_AFTER_DELETE foreach (Yii::$app->request->post('data') as $removeID => $removeData) { Menu::findOne($removeID)->delete(); } return Json::encode($returnData); default: return Json::encode($returnData); } default: throw new \Exception('参数错误!'); } }
/** * 修改管理员 * * @param array $_POST[] 管理员修改数据 */ public function actionUpdate() { //菜单权限检测 Yii::$app->util->adminAuth() ? '' : $this->redirect('/admin/login'); $mAdmin = new Admin(); if ($params = Yii::$app->request->post()) { //验证 if (empty($params['user_name'])) { Yii::$app->util->msg('参数错误'); } if (empty($params['password']) && empty($params['repassword'])) { $options = ['id' => $params['id'], 'user_name' => $params['user_name'], 'auth' => isset($params['auth']) ? json_encode($params['auth']) : '']; } else { if ($params['password'] != $params['repassword']) { Yii::$app->util->msg('两次密码不一致'); } else { $str = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; $salt = substr(str_shuffle($str), 0, 8); $options = ['id' => $params['id'], 'user_name' => $params['user_name'], 'salt' => $salt, 'password' => Yii::$app->util->passwordEncode($params['password'], $salt), 'auth' => isset($params['auth']) ? json_encode($params['auth']) : '']; } } if ($mAdmin->store($options)) { return $this->redirect('admin-list'); } else { Yii::$app->util->msg('入库错误'); } } else { $id = Yii::$app->request->get('id', 0); $one = $mAdmin->getAdminById($id); if (!$one) { return $this->redirect('admin-list'); } else { $mMenu = new Menu(); $data = $mMenu->menu(); $auth = json_decode($one['auth']); if (!empty($auth)) { foreach ($data as $k => $v) { if (in_array($v['id'], $auth)) { $data[$k]['checked'] = 'checked'; } else { $data[$k]['checked'] = ''; } } } return $this->render('update', ['data' => $data, 'one' => $one]); } } }
function adminAuth() { $session = Yii::$app->session; $session->isActive ? '' : $session->open(); $admin = $session->get('admin'); $session->close(); if ($admin == NUll) { return FALSE; } else { // print_r($_SERVER);exit; $request = $_SERVER['REQUEST_URI']; $request = explode('?', $request); $request = $request[0]; $request = trim($request, '/'); $request = explode('/', $request); $request = $request[0] . '/' . $request[1]; // print_r($request);exit; $mMenu = new Menu(); $row = $mMenu->getRowByOptions(['menu_link' => $request]); // 权限验证 $field = json_decode($admin['auth']); if (empty($field)) { return true; } else { if (is_null($row)) { return FALSE; } else { return in_array($row['id'], $field); } } } }
/** * 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.'); } }
<img src="<?php echo Yii::$app->user->identity->avatar48; ?> " class="img-rounded" alt="User Image"/> </div> <div class="pull-left info"> <p><?php echo Yii::$app->user->identity->username; ?> </p> <a href="#"><i class="fa fa-circle text-success"></i> Online</a> </div> </div> <?php $item = []; $menu = Menu::find()->orderBy('order')->all(); foreach ($menu as $k => $m) { $item[$k]['label'] = $m->data . '<span>' . $m->name . '</span>'; $item[$k]['url'] = [$m->route]; } ?> <?php echo Nav::widget(['encodeLabels' => false, 'activateItems' => true, 'options' => ['class' => 'sidebar-menu'], 'items' => $item]); ?> </section> </aside>