Example #1
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]);
     }
 }
Example #2
0
 /**
  * @param integer $id
  * @return mixed
  */
 public function actionUpdate($id)
 {
     if (!$this->is_access('goods/update')) {
         Yii::$app->session->setFlash('error', $this->errorInfo);
         return $this->redirect($this->redirectUrl);
     }
     $model = $this->findModel($id);
     $GoodsGallery = new GoodsGallery();
     $post = Yii::$app->request->post();
     if ($model->load($post)) {
         $GoodsGallery->load($post);
         if ($model->validate() && $GoodsGallery->validate()) {
             // 商品封面图片
             if ($_FILES['Goods']['error']['goods_img'] === 0) {
                 $model->goods_img = File::uploadImage($model, 'goods_img', 'shop');
                 $model->goods_thumb = Images::thumb('./' . $model->goods_img, 250, 250);
             }
             if ($model->save(false)) {
                 // 商品相册
                 if ($_FILES['GoodsGallery']['error']['img_url'][0] === 0) {
                     $img_url = File::uploadImages($GoodsGallery, 'img_url', 'goods');
                     $GoodsGallery->addData($model->goods_id, $img_url);
                 }
                 // 商品属性
                 GoodsAttr::addData($model->goods_id);
             }
             Yii::$app->session->setFlash('success', '编辑成功');
         }
         return $this->redirect(['index']);
     } else {
         // 商品分类
         $Category = new Category();
         $categoryList = $Category->parent(0, false);
         // 品牌
         $Brand = new Brand();
         $brandList = $Brand->getLists();
         // 商品类型
         $GoodsType = new GoodsType();
         $goodsTypeList = $GoodsType->dropList();
         // 相册
         $goodsGalleryList = $GoodsGallery->getGoodsGallery($id);
         Yii::$app->view->params['meta_title'] = '编辑商品';
         return $this->render('update', ['model' => $model, 'GoodsGallery' => $GoodsGallery, 'categoryList' => $categoryList, 'brandList' => $brandList, 'goodsTypeList' => $goodsTypeList, 'goodsGalleryList' => $goodsGalleryList]);
     }
 }