/**
  * 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();
     try {
         if ($model->load($_POST) && $model->save()) {
             return $this->redirect(Url::previous());
         } elseif (!\Yii::$app->request->isPost) {
             $model->load($_GET);
         }
     } catch (\Exception $e) {
         $msg = isset($e->errorInfo[2]) ? $e->errorInfo[2] : $e->getMessage();
         $model->addError('_exception', $msg);
     }
     return $this->render('create', ['model' => $model]);
 }
 /**
  * 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]);
     }
 }
 /**
  * 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->save()) {
         return $this->redirect(['view', 'id' => $model->category_id]);
     } else {
         return $this->render('create', ['model' => $model]);
     }
 }
Example #4
0
 public function insert(CategoryDto $categoryDto)
 {
     $category = new Category();
     if ($category->load(['Category' => ArrayHelper::toArray($categoryDto)])) {
         $category->insert();
         return ArrayHelper::toArray($category, ['common\\models\\Category' => $categoryDto->activeAttributes()]);
     } else {
         return [];
     }
 }
Example #5
0
 /**
  * Creates a new Category model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @param int $id id of the parent category
  * @return mixed
  */
 public function actionCreate($id = null)
 {
     $categories = Category::find()->all();
     $model = new Category();
     $model->parent_id = $id;
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         return $this->redirect(['view', 'id' => $model->id]);
     } else {
         return $this->render('create', ['model' => $model, 'categories' => $categories]);
     }
 }
 /**
  * Creates a new Category model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     //if(!Yii::$app->user->can('createYourAuth')) throw new ForbiddenHttpException(Yii::t('app', 'No Auth'));
     $model = new Category();
     $model->loadDefaultValues();
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         return $this->redirect(['view', 'id' => $model->id]);
     } else {
         return $this->render('create', ['model' => $model]);
     }
 }
 /**
  * Creates a new Category model.
  * If creation is successful, the browser will be redirected to the main 'index' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Category();
     $model->scenario = 'create';
     $dataProvider = new ActiveDataProvider(['query' => Category::find()]);
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         Yii::$app->session->setFlash('message', 'The category has been created successfully.');
         return $this->redirect('index', ['dataProvider' => $dataProvider]);
     } else {
         return $this->render('create', ['model' => $model]);
     }
 }
Example #8
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]);
 }
 public function actionAdd()
 {
     $model = new Category();
     if ($model->load(Yii::$app->request->post())) {
         if ($model->save()) {
             Yii::$app->session->setFlash('success', '成功添加分类“' . $model->name . '”。');
             return $this->redirect(['index']);
         } else {
             Yii::$app->session->setFlash('danger', '分类添加失败。');
         }
     }
     return $this->render('form', ['model' => $model]);
 }
Example #11
0
 /**
  * Creates a new Meta model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate($parent = 0)
 {
     $model = new Category();
     $model->parent = intval($parent);
     if (Yii::$app->request->isPost) {
         if ($model->load(Yii::$app->request->post())) {
             if ($model->save()) {
                 return $this->redirect(['index']);
             }
         }
     }
     return $this->render('create', ['model' => $model]);
 }
Example #12
0
 public function actionCreate()
 {
     $model = new Category();
     if (isset($_POST['sbm_create_category'])) {
         $model->load($_POST);
         //отправляем массив на парсинг
         var_dump($model);
         die;
         //$model->save();
     } else {
         $model = Category::findAll("where parent_id is NULL");
         //выбор коневых категорий
         $this->render('create', ['model' => $model]);
     }
 }
 /**
  * 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]);
 }
Example #14
0
 /**
  * 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())) {
         $parent = Category::findOne($model->parent_id);
         if (!empty($parent)) {
             $model->indent = $parent->indent + 1;
         } else {
             $model->indent = 0;
         }
         $model->save();
         return $this->redirect(['index']);
     } else {
         return $this->render('create', ['model' => $model]);
     }
 }
 public function actionIndex()
 {
     if (Yii::$app->request->get('id')) {
         $model = Category::findOne(Yii::$app->request->get('id'));
     } else {
         $model = new Category();
     }
     if ($model->load(Yii::$app->request->post())) {
         if ($model->save()) {
             Yii::$app->session->setFlash("success", "Update Category Successful");
             return $this->refresh();
         }
     }
     $items = Category::find()->where(['parent_id' => null])->all();
     return $this->render("index", ['model' => $model, 'items' => $items]);
 }
 public function actionAddCategory()
 {
     $user = Yii::$app->user->identity;
     if ($user->role == User::ROLE_ADMIN) {
         $model = new Category();
         $image = new Image();
         $model->load(Yii::$app->request->post(), '');
         $image->img = UploadedFile::getInstancesByName("image");
         if ($image->validate() && $model->save()) {
             if (!empty($image->img)) {
                 $image->uploads($model, Image::CATEGORY_STATUS);
             }
             return $model;
         }
         return array_merge($model->getErrors(), $image->getErrors());
     }
     return "Access denied";
 }
 /**
  * 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();
     $groupProperties[] = ['group' => new GroupProperty(), 'properties' => [new Property()]];
     $modelsGroup = [new GroupProperty()];
     $modelsProperties[] = [new Property()];
     $request = Yii::$app->request;
     $post = $request->post();
     $groupPost = $request->post('GroupProperty', []);
     $propertyPost = $request->post('Property', []);
     $model->scenario = $model::SCENARIO_CREATE;
     if ($model->load($post)) {
         if ($groupPost && $propertyPost) {
             $groupProperties = Model::loadProperties($groupPost, $propertyPost);
         }
         $model->image = UploadedFile::getInstance($model, 'image');
         $model->icon = UploadedFile::getInstance($model, 'icon');
         $model->icon_hover = UploadedFile::getInstance($model, 'icon_hover');
         if ($model->validate()) {
             if ($groupPost && $propertyPost) {
                 Model::validateProperties($groupProperties);
             }
             $transaction = $model::getDb()->beginTransaction();
             try {
                 if ($model->save()) {
                     if ($groupPost) {
                         Model::linkProperties($model, $groupProperties);
                     }
                     $model->upload();
                     $transaction->commit();
                     Yii::$app->session->setFlash('success', 'Category have been created.');
                 }
             } catch (\Exception $e) {
                 Yii::$app->session->setFlash('error', 'Failed to create category.');
                 $transaction->rollBack();
             }
         }
     } else {
         return $this->render('create', ['model' => $model, 'groupProperties' => $groupProperties, 'modelsGroup' => $modelsGroup, 'modelsProperties' => $modelsProperties]);
     }
     return $this->redirect('index');
 }
Example #18
0
 /**
  * Creates a new Category model.
  * If creation is successful, the browser will be redirected to the 'index' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Category();
     $searchModel = new CategorySearch();
     $dataProvider = $searchModel->search(\Yii::$app->request->queryParams);
     if ($model->load(Yii::$app->request->post())) {
         if (\Yii::$app->request->isAjax) {
             \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
             return \yii\widgets\ActiveForm::validate($model);
         }
         $model->attributes = $_POST['Category'];
         $model->create_at = new \yii\db\Expression('NOW()');
         $model->update_at = new \yii\db\Expression('NOW()');
         if ($model->save()) {
             return $this->redirect(['index']);
         }
     } else {
         return $this->render('index', ['model' => $model, 'searchModel' => $searchModel, 'dataProvider' => $dataProvider]);
     }
 }
Example #19
0
 /**
  * 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())) {
         $file = UploadedFile::getInstances($model, 'file');
         $model->file = $file[0];
         if ($model->validate()) {
             if (!empty($model->file)) {
                 $image_name = '/category/' . $model->file->baseName . '_' . time() . '.' . $model->file->extension;
                 $image_path = Yii::getAlias('@frontend') . '/web/uploads' . $image_name;
                 $model->file->saveAs($image_path);
                 $model->cat_pic = $image_name;
                 $model->save();
             }
         }
         return $this->redirect(['view', 'id' => $model->cat_id]);
     } else {
         return $this->render('create', ['model' => $model]);
     }
 }
Example #20
0
 /**
  * Creates a new Category model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $post = Yii::$app->request->post();
     $model = new Category();
     // Если пост получен и загружен.
     if ($model->load($post)) {
         // Если передан ID родительской категории.
         if (isset($post['parent_category_id']) && !empty($post['parent_category_id'])) {
             $parentCategoryId = (int) $post['parent_category_id'];
             $parentCategory = Category::findOne($parentCategoryId);
             if ($model->prependTo($parentCategory)) {
                 return $this->redirect(['index']);
             }
             return $this->render('create', ['model' => $model]);
         }
         // Создание новой корневой категории.
         if ($model->makeRoot()) {
             return $this->redirect(['index']);
         }
         return $this->render('create', ['model' => $model]);
     } else {
         return $this->render('create', ['model' => $model]);
     }
 }
Example #21
0
 /**
  * Creates a new Category model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     if (!$this->is_access('category/create')) {
         Yii::$app->session->setFlash('error', $this->errorInfo);
         return $this->redirect($this->redirectUrl);
     }
     $model = new Category();
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         // 加入分类推荐
         $CatRecommend = new CatRecommend();
         $CatRecommend->insertRecommend($model->cat_id, $_POST['Category']['recommend_type']);
         // 是否需要显示在导航栏
         if ($model->show_in_nav == 1) {
             $Nav = new Nav();
             $Nav->addData('c', $model->cat_id);
         }
         Yii::$app->session->setFlash('success', '添加成功');
         return $this->redirect(['index']);
     } else {
         Yii::$app->view->params['meta_title'] = '添加分类';
         $catList = $model->parent(0);
         return $this->render('create', ['model' => $model, 'catList' => $catList]);
     }
 }
 /**
  * Creates a new Category model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionAdd($parent_id = null)
 {
     $model = new Category();
     if ($model->load(Yii::$app->request->post())) {
         return $this->redirect('index');
     } else {
         if ($parent_id) {
             $model->parent_id = $parent_id;
         }
         return $this->render('create', ['model' => $model]);
     }
 }