/** * 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]); } } }
/** * Create menu item by post * * @param int $id */ public function actionCreateMenuItem($id) { if (Yii::$app->request->post('type') === 'link') { $model = new MenuItem(); $model->menu_id = $id; if ($model->load(Yii::$app->request->post()) && ($model->menu_id = $id) && $model->save()) { echo $this->renderPartial('_render-item', ['item' => $model, 'wrapper' => 'true']); } } else { if (Yii::$app->request->post('type') === 'post' && ($postIds = Yii::$app->request->post('postIds'))) { foreach ($postIds as $postId) { if ($postModel = $this->findPost($postId)) { $model = new MenuItem(); $model->menu_id = $id; $model->menu_label = $postModel->post_title; $model->menu_url = $postModel->url; if ($model->save()) { echo $this->renderPartial('_render-item', ['item' => $model, 'wrapper' => 'true']); } } } } else { if (Yii::$app->request->post('type') === 'taxonomy' && ($termIds = Yii::$app->request->post('termIds'))) { foreach ($termIds as $termId) { if ($termModel = $this->findTerm($termId)) { $model = new MenuItem(); $model->menu_id = $id; $model->menu_label = $termModel->term_name; $model->menu_url = $termModel->url; if ($model->save()) { echo $this->renderPartial('_render-item', ['item' => $model, 'wrapper' => 'true']); } } } } } } }
/** * Create MenuItem models. * * @param int $id * @return string */ public function actionCreateMenuItem($id) { $items = ''; if (Yii::$app->request->post('type') === 'link') { $model = new MenuItem(); $model->menu_id = $id; if ($model->load(Yii::$app->request->post()) && $model->save()) { $items .= $this->renderPartial('_render-item', ['item' => $model, 'wrap' => 'true']); } } if (Yii::$app->request->post('type') === 'post' && ($postIds = Yii::$app->request->post('postIds'))) { foreach ($postIds as $postId) { if ($post = $this->findPost($postId)) { $model = new MenuItem(['menu_id' => $id, 'label' => $post->title, 'url' => $post->getUrl()]); if ($model->save()) { $items .= $this->renderPartial('_render-item', ['item' => $model, 'wrap' => 'true']); } } } } if (Yii::$app->request->post('type') === 'taxonomy' && ($termIds = Yii::$app->request->post('termIds'))) { foreach ($termIds as $termId) { if ($term = $this->findTerm($termId)) { $model = new MenuItem(['menu_id' => $id, 'label' => $term->name, 'url' => $term->getUrl()]); if ($model->save()) { $items .= $this->renderPartial('_render-item', ['item' => $model, 'wrap' => 'true']); } } } } return $items; }