public function actionDeleteitem($id) { $menuitem = MenuItem::find()->where(['parent_id' => $id])->all(); if (!empty($menuitem)) { foreach ($menuitem as $value) { $item = MenuItem::findOne($value->id); $item->parent_id = 0; $item->save(); } } MenuItem::findOne($id)->delete(); return $this->redirect(['view', 'id' => $_GET['menu_id']]); }
public function save() { if (!$this->validate()) { return false; } $model = new Menu(); if (!empty($this->id)) { $model = Menu::findOne($this->id); } $model->setAttributes(['name' => $this->name, 'alias' => $this->alias]); $isNew = $model->isNewRecord; if ($model->save(false)) { $this->id = $model->id; if (!$isNew) { $oldItems = MenuItem::find()->where(['menuID' => $this->id])->all(); $existedMenus = ArrayHelper::getColumn($oldItems, 'id'); $newItems = ArrayHelper::getColumn($this->items, 'id'); if ($needToRemoveItems = array_diff($existedMenus, $newItems)) { \Yii::trace($needToRemoveItems); foreach ($oldItems as $item) { if (in_array($item->id, $needToRemoveItems)) { $item->delete(); } } } } foreach ($this->items as $order => $menuItem) { if (!$menuItem->menuID) { $menuItem->menuID = $this->id; } if (!$menuItem->order) { $menuItem->order = $order; } $menuItem->save(); } } return !empty($this->id); }
/** * 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('writesdown', 'The requested page does not exist.'); } } }
function menu($id, &$data = [], $parent = 0) { $category = MenuItem::find()->where(['parent_id' => $parent, 'menu_id' => $id])->orderBy(['order' => SORT_ASC])->all(); if (!empty($category)) { echo '<ul id=' . $parent . '>'; foreach ($category as $key => $value) { ?> <li id="<?php echo $value->id; ?> "> <div class="menu-item" style="border: 1px solid #ccc; padding: 8px 10px; background: #F7F7F7"> <label class="label_<?php echo $value->id; ?> "><?php echo $value->type_name; ?> </label> <a href="javascript:void(0)" class="pull-right edit" data-bind="<?php echo $value->id; ?> ">Sửa</a> <div id="menu-edit-<?php echo $value->id; ?> " style="display:none; margin-top: 15px;"> <div class="form-group"> <input type="text" class="form-control" name="name[<?php echo $value->id; ?> ]" value="<?php echo $value->type_name; ?> "> </div> <a href="<?php echo Yii::$app->urlManager->createUrl(["menu/deleteitem", 'id' => $value->id, 'menu_id' => $value->menu_id]); ?> " class="text-danger">Xóa</a> </div> </div> <input type="hidden" name="order[]" value="<?php echo $value->id; ?> "> <input type="hidden" id="parent-<?php echo $value->id; ?> " name="parent[<?php echo $value->id; ?> ]" value="<?php echo $value->parent_id; ?> "> <?php echo menu($id, $data, $value->id); ?> </li> <?php } echo '</ul>'; } }
/** * List menu item by menu location; * * @param string $menuLocation * @param int $menuParent * * @return array|null */ protected static function getListMenuItem($menuLocation, $menuParent = 0) { /* @var $menuItemModel \common\models\MenuItem[] */ $menuItem = []; $menuItemModel = MenuItem::find()->innerJoinWith(['menu'])->andWhere(['menu_location' => $menuLocation])->andWhere(['menu_parent' => $menuParent])->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' => static::getListMenuItem($menuLocation, $model->id)]; } return $menuItem; }