コード例 #1
0
ファイル: Menu.php プロジェクト: fbarrento/yii2-content
 /**
  * List menu item by menu location;
  *
  * @param string $menu_location
  * @param int    $menu_parent
  *
  * @return array|null
  */
 protected static function getListMenuItem($menu_location, $menu_parent = 0)
 {
     $menuItem = [];
     $menuItemModel = MenuItem::find()->innerJoinWith(['menu'])->andWhere(['menu_location' => $menu_location])->andWhere(['menu_parent' => $menu_parent])->orderBy('menu_order')->all();
     if (empty($menuItemModel)) {
         return $menuItem = null;
     }
     foreach ($menuItemModel as $model) {
         $menuItem[] = ['id' => $model->id, 'label' => $model->menu_label, 'url' => $model->menu_url, 'parent' => $model->menu_parent, 'items' => self::getListMenuItem($menu_location, $model->id)];
     }
     return $menuItem;
 }
コード例 #2
0
 /**
  * Delete menu item via ajax post.
  *
  * @throws NotFoundHttpException
  * @throws \Exception
  */
 public function actionDeleteMenuItem()
 {
     /* @var $child \common\models\MenuItem */
     if ($id = Yii::$app->request->post('id')) {
         $model = $this->findMenuItem($id);
         if ($model && $model->delete()) {
             $children = MenuItem::find()->where(['menu_parent' => $model->id])->all();
             foreach ($children as $child) {
                 $child->menu_parent = $model->menu_parent;
                 $child->save();
             }
         } else {
             throw new NotFoundHttpException('content', 'The requested page does not exist.');
         }
     }
 }