/**
  * Creates a new Category model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate($module)
 {
     $model = new Category();
     $model['module'] = $module;
     $parents = [];
     $root = Category::findOne(['parent_id' => null, 'title' => $module, 'module' => $module]);
     if (empty($root)) {
         throw new NotFoundHttpException('Module không tồn tại');
     }
     $parents = $root->children()->all();
     $parent_id = $this->buildTree($parents);
     if ($model->load(Yii::$app->request->post()) && $model->validate()) {
         $parent_id = $model['parent_id'];
         $model['slug'] = $this->slugAlias($model);
         if (empty($parent_id)) {
             $model->appendTo($root);
         } else {
             $parent = Category::findOne(['id' => $parent_id, 'module' => $module]);
             $model->appendTo($parent);
         }
         if ($model->save()) {
             return $this->redirect(['index', 'module' => $module]);
         }
     } else {
         return $this->render('create', ['model' => $model, 'module' => $module, 'parent_id' => $parent_id]);
     }
 }
Example #2
0
 public function actionCreatecategory()
 {
     $category = new Category();
     $data = \Yii::$app->request->post("Category");
     if (!empty($data)) {
         $category->load(\Yii::$app->request->post());
     }
     if (!empty($data) && $category->validate() && $category->save()) {
         return $this->render('success_create_category');
     }
     return $this->render('category_edit', ['category' => $category, 'parents' => Category::getList()]);
 }
 /**
  * Экшен '/admin/catalog/insert-cat' - добавление категории
  * Экшен нельзя задать с помощью CRUD т.к. в 'nested sets' переопределяется метод 'insert'
  * @return string
  * @throws InvalidParamException
  */
 public function actionInsertCat()
 {
     $category = new Category();
     $category->scenario = 'insert';
     if ($category->load(Yii::$app->request->post())) {
         // Валидируем модель
         if ($category->validate()) {
             $parent = Category::findOne($category->parent);
             $this->afterSaveCategory($category->appendTo($parent));
         }
     }
     return $this->render('update-cat', ['model' => $category]);
 }
 /**
  * Creates a new Category model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Category();
     if ($model->load(Yii::$app->request->post()) && $model->validate()) {
         if (!isset($model->parentId)) {
             $result = $model->saveNode();
         } else {
             $parent = Category::findOne($model->parentId);
             $result = $model->appendTo($parent);
         }
         if ($result) {
             return $this->redirect(['view', 'id' => $model->id]);
         }
     }
     return $this->render('create', ['model' => $model]);
 }