/** * Updates an existing Menu model. * If update is successful, the browser will be redirected to the 'view' page. * @param integer $id * @return mixed */ public function actionUpdate($id) { $model = $this->findModel($id); $item = new MenuItem(); if ($model->load(Yii::$app->request->post()) && $model->save()) { return $this->redirect(['update', 'id' => $model->id]); } else { if ($item->load(Yii::$app->request->post())) { $model->link('menuItems', $item); return $this->redirect(['update', 'id' => $model->id]); } else { return $this->render('update', ['model' => $model, 'item' => $item]); } } }
public function save() { if (!$this->validate()) { return false; } $model = new MenuItem(); if (!empty($this->id)) { $model = MenuItem::findOne($this->id); } $model->setAttributes(['name' => $this->name, 'menuID' => $this->menuID, 'linkType' => $this->linkType, 'order' => $this->order, 'link' => $this->link]); if ($model->save(false)) { $this->id = $model->id; } return !empty($this->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); }
public function actionEdit() { \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; if (!empty($_POST['Menu']['id'])) { $menu = Menu::findOne($_POST['Menu']['id']); $menu->name = $_POST['menu-name']; $menu->save(); if (!empty($_POST['order'])) { foreach ($_POST['order'] as $key => $value) { $item = MenuItem::findOne($value); $item->parent_id = $_POST['parent'][$value]; $item->type_name = $_POST['name'][$value]; $item->order = $key + 1; $item->save(); } } return ['message' => 'ok']; } }
/** * @param AcceptanceTester $I */ public function testCreateMenuItem(AcceptanceTester $I) { $I->wantTo('ensure that create menu item works'); $indexPage = IndexPage::openBy($I); $I->see('Menus', 'h1'); $I->see('Menu Primary', 'h2'); if (method_exists($I, 'wait')) { $I->amGoingTo('submit menu form with correct data'); $indexPage->submitMenuItem(['label' => 'New Menu Item', 'url' => 'http://writesdown.com']); $I->expectTo('see new menu item'); $I->see('New Menu Item', '.dd-handle'); } MenuItem::deleteAll(['label' => 'Test Menu Item']); }
/** * @inheritdoc */ public function rules() { return [[['menu_id', 'parent_id', 'status', 'created_by', 'updated_by', 'created_at', 'updated_at'], 'integer'], [['title', 'url'], 'required'], [['title', 'url'], 'string', 'max' => 255], [['menu_id'], 'exist', 'skipOnError' => true, 'targetClass' => Menu::className(), 'targetAttribute' => ['menu_id' => 'id']], [['parent_id'], 'exist', 'skipOnError' => true, 'targetClass' => MenuItem::className(), 'targetAttribute' => ['parent_id' => 'id']]]; }
/** * @return \yii\db\ActiveQuery */ public function getMenuItems() { return $this->hasMany(MenuItem::className(), ['menu_id' => 'id']); }
/** * Finds the Post model based on its primary key value. * If the model is not found, it will return false. * * @param integer $id * * @return MenuItem|bool|null|static */ protected function findMenuItem($id) { if (($model = MenuItem::findOne($id)) !== null) { return $model; } else { return false; } }
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; }
/** * Finds the MenuItem model based on its primary key value. * If the model is not found, it will return false. * * @param integer $id * @return MenuItem the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findMenuItem($id) { if (($model = MenuItem::findOne($id)) !== null) { return $model; } throw new NotFoundHttpException(Yii::t('writesdown', 'The requested page does not exist.')); }
/** * @return \yii\db\ActiveQuery */ public function getItems() { return $this->hasMany(MenuItem::className(), ['menuID' => 'id'])->orderBy('order'); }