コード例 #1
0
ファイル: UploadAction.php プロジェクト: liangdabiao/funshop
 public function afterUpload($data)
 {
     ImageDropzone::saveThumbnail($this->uploadDir . '/', $data['filename'], $this->thumbnail_width, $this->thumbnail_height);
     $image = new ProductImage(['product_id' => $data['params']['productId'], 'filename' => $data['filename'], 'image' => $data['src'] . $data['filename'], 'thumb' => $data['src'] . 'small-' . $data['filename'], 'description' => '', 'sort_order' => 50]);
     if ($image->save()) {
         return $image->toArray();
     } else {
         return $image->getErrors();
     }
 }
コード例 #2
0
 /**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = ProductImage::find();
     $query->orderBy(['created_at' => SORT_DESC]);
     $dataProvider = new ActiveDataProvider(['query' => $query]);
     if ($this->load($params) && !$this->validate()) {
         return $dataProvider;
     }
     $query->andFilterWhere(['id' => $this->id, 'product_id' => $this->product_id, 'sort_order' => $this->sort_order]);
     $query->andFilterWhere(['like', 'filename', $this->filename])->andFilterWhere(['like', 'description', $this->description])->andFilterWhere(['like', 'image', $this->image])->andFilterWhere(['like', 'thumb', $this->thumb])->andFilterWhere(['like', 'origin', $this->origin]);
     return $dataProvider;
 }
コード例 #3
0
ファイル: RemoveAction.php プロジェクト: CTaiDeng/funshop
 public function run($id)
 {
     $image = ProductImage::findOne(['id' => $id]);
     if ($image) {
         $filename = $image->filename;
         if (ProductImage::deleteAll(['id' => $id])) {
             if (unlink(\Yii::getAlias($this->uploadDir . '/' . $filename))) {
                 if (unlink(\Yii::getAlias($this->uploadDir . '/small-' . $filename))) {
                     Yii::$app->response->redirect(Yii::$app->request->referrer);
                 }
             }
         }
     }
     return false;
 }
コード例 #4
0
 /**
  * batch export product
  * @param integer $id
  * @return mixed
  */
 public function actionExport()
 {
     //if(!Yii::$app->user->can('viewYourAuth')) throw new ForbiddenHttpException(Yii::t('app', 'No Auth'));
     $format = Product::getImportExportFormat();
     $products = Product::find()->orderBy(['id' => SORT_ASC])->all();
     $str = '';
     // 生成csv标题行
     $product = new Product();
     $start = true;
     foreach ($format as $column) {
         if ($start) {
             $str .= '"' . iconv('utf-8', 'gb2312', $product->attributeLabels()[$column]) . '"';
             $start = false;
         } else {
             $str .= ',"' . iconv('utf-8', 'gb2312', $product->attributeLabels()[$column]) . '"';
         }
     }
     $str .= ',' . iconv('utf-8', 'gb2312', Yii::t('app', 'Thumbs')) . ',' . iconv('utf-8', 'gb2312', Yii::t('app', 'Images'));
     $str .= "\n";
     foreach ($products as $row) {
         // 导出 product表中的数据
         $start = true;
         foreach ($format as $column) {
             $value = '';
             if ($column == 'category_id') {
                 if ($row[$column] > 0) {
                     $category = Category::findOne($row[$column]);
                     $value = iconv('utf-8', 'gb2312', $category->name);
                 }
             } elseif ($column == 'brand_id') {
                 if ($row[$column] > 0) {
                     $brand = Brand::findOne($row[$column]);
                     $value = iconv('utf-8', 'gb2312', $brand->name);
                 }
             } else {
                 $value = iconv('utf-8', 'gb2312', $row[$column]);
             }
             if ($start) {
                 $str .= '"' . $value . '"';
                 $start = false;
             } else {
                 $str .= ',"' . str_replace("\"", "\"\"", $value) . '"';
             }
         }
         // 导出product_image表中的数据
         $start = true;
         $strThumb = $strImage = '';
         $productImages = ProductImage::find()->where(['product_id' => $row->id])->all();
         foreach ($productImages as $item) {
             if ($start) {
                 $strThumb .= $item->thumb;
                 $strImage .= $item->image;
                 $start = false;
             } else {
                 $strThumb .= '|' . $item->thumb;
                 $strImage .= '|' . $item->image;
             }
         }
         $str .= ',"' . $strThumb . '","' . $strImage . '"';
         $str .= "\n";
     }
     $filename = date('Ymd') . '.csv';
     header("Content-type:text/csv");
     header("Content-Disposition:attachment;filename=" . $filename);
     header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
     header('Expires:0');
     header('Pragma:public');
     echo $str;
 }
コード例 #5
0
ファイル: Product.php プロジェクト: akiu/first-yii
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getProductImages()
 {
     return $this->hasMany(ProductImage::className(), ['product_id' => 'product_id']);
 }
コード例 #6
0
ファイル: Product.php プロジェクト: liangdabiao/funshop
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getProductImagesSort()
 {
     return $this->hasMany(ProductImage::className(), ['product_id' => 'id'])->orderBy(['sort_order' => SORT_ASC]);
 }
コード例 #7
0
ファイル: ProductImage.php プロジェクト: quyettvq/luspel
 /**
  * function ->delete ()
  */
 public function delete()
 {
     $now = strtotime('now');
     $username = Yii::$app->user->identity->username;
     $model = $this;
     if ($log = new UserLog()) {
         $log->username = $username;
         $log->action = 'Delete';
         $log->object_class = 'ProductImage';
         $log->object_pk = $model->id;
         $log->created_at = $now;
         $log->is_success = 0;
         $log->save();
     }
     if (parent::delete()) {
         if ($log) {
             $log->is_success = 1;
             $log->save();
         }
         FileUtils::removeFolder(Yii::$app->params['images_folder'] . $model->image_path);
         return true;
     }
     return false;
 }