/**
  * Finds the MenuType model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param integer $id
  * @return MenuType the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     if (($model = MenuType::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }
示例#2
0
 /**
  * @param null $parent_id
  * @param string $menuTypeName
  * @return array
  */
 public function getTree($parent_id = null, $menuTypeName = 'main')
 {
     $items = [];
     $titleFiled = 'title_' . Yii::$app->language;
     $model = MenuItem::find()->where(['status' => '1', 'parent_id' => $parent_id]);
     // Filter by parent
     if (isset($menuType)) {
         $menuType = MenuType::findOne(['like', 'name', '%' . $menuTypeName . '%']);
         $model = $model->andWhere(['menu_type_id' => $menuType->id]);
     }
     $menuItems = $model->orderBy(['sort' => 'DESC'])->all();
     foreach ($menuItems as $menuItem) {
         $url = $this->checkParams($menuItem->url);
         if ($menuItem->visible == 'all') {
             $visible = true;
         } elseif ($menuItem->visible == 'notuser') {
             $visible = Yii::$app->user->isGuest;
         } else {
             $visible = Yii::$app->user->can($menuItem->visible);
         }
         $item = ['label' => $menuItem->{$titleFiled}, 'url' => is_array($url) ? Yii::$app->urlManager->createUrl($url) : $url, 'visible' => $visible, 'linkOptions' => $menuItem->data_method == 'post' ? ['data-method' => 'post'] : []];
         // Get the item's children
         $children = $this->getTree($menuItem->id, $menuTypeName);
         if ($children) {
             $item['items'] = $children;
         }
         $items[] = $item;
     }
     return $items;
 }