Beispiel #1
0
 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();
     }
 }
 /**
  * batch import product
  * @param integer $id
  * @return mixed
  */
 public function actionImport()
 {
     //if(!Yii::$app->user->can('viewYourAuth')) throw new ForbiddenHttpException(Yii::t('app', 'No Auth'));
     $format = Product::getImportExportFormat();
     if (Yii::$app->request->post()) {
         $countCreate = $countUpdate = 0;
         $file = UploadedFile::getInstanceByName('importFile');
         $handle = fopen($file->tempName, 'r');
         $result = $this->inputCsv($handle);
         $arrData = [];
         if (count($result) <= 1) {
             Yii::$app->getSession()->setFlash('danger', Yii::t('app', 'No Record, please check file.'));
         } else {
             // 将数据的key从数字变成固定的格式
             for ($i = 1; $i < count($result); $i++) {
                 $j = 0;
                 foreach ($format as $item) {
                     $data[$item] = $result[$i][$j];
                     $j++;
                 }
                 $data['thumbs'] = $result[$i][$j];
                 $data['images'] = $result[$i][$j + 1];
                 array_push($arrData, $data);
             }
             // 处理数据,如果ID大于0,则更新,否则新增
             $line = 2;
             $errorLines = [];
             foreach ($arrData as $item) {
                 if ($item['id'] > 0) {
                     // 已存在的值,则更新数据,以及判断缩略图和图片
                     $model = Product::findOne($item['id']);
                     if ($model === null) {
                         array_push($errorLines, $line);
                         $line++;
                         continue;
                     }
                     foreach ($item as $k => $v) {
                         if ($k == 'id' || $k == 'thumbs' || $k == 'images') {
                             continue;
                         }
                         $model[$k] = iconv('gb2312', 'utf-8', trim($v));
                     }
                     $result = $model->save();
                     if (!$result) {
                         //如果保存失败
                         array_push($errorLines, $line);
                         $line++;
                         continue;
                     }
                     $countUpdate++;
                     if ($item['thumbs'] && $item['images']) {
                         $arrThumb = explode('|', $item['thumbs']);
                         $arrImage = explode('|', $item['images']);
                         $i = 0;
                         $ids = [];
                         foreach ($arrThumb as $thumb) {
                             $thumb = trim($thumb);
                             $image = trim($arrImage[$i]);
                             $productImage = ProductImage::find()->where(['product_id' => $item['id'], 'thumb' => $thumb, 'image' => $image])->one();
                             if ($productImage) {
                                 //如果图片在数据库中已经存在,则假如到ids数组,防止后续被删除
                                 array_push($ids, $productImage->id);
                             } else {
                                 //不存在的话,新增记录并将id加入到ids
                                 $productImage = new ProductImage(['product_id' => $item['id'], 'thumb' => $thumb, 'image' => $image]);
                                 $productImage->save();
                                 array_push($ids, $productImage->id);
                             }
                             $i++;
                         }
                         //删除在ids数组中记录
                         ProductImage::deleteAll(['and', 'product_id=' . $item['id'], ['not in', 'id', $ids]]);
                     }
                 } else {
                     // 新的数据,插入,并将缩略图和图片插入
                     $model = new Product();
                     foreach ($item as $k => $v) {
                         if ($k == 'id' || $k == 'thumbs' || $k == 'images') {
                             continue;
                         }
                         $model[$k] = iconv('gb2312', 'utf-8', trim($v));
                     }
                     // 将分类和品牌转换成对应的ID
                     $category = Category::find()->where(['name' => trim($model->category_id)])->one();
                     $model->category_id = $category ? $category->id : 1;
                     $brand = Brand::find()->where(['name' => trim($model->brand_id)])->one();
                     $model->brand_id = $brand ? $brand->id : 0;
                     $result = $model->save();
                     if (!$result) {
                         //如果保存失败
                         array_push($errorLines, $line);
                         $line++;
                         continue;
                     }
                     $countCreate++;
                     if ($item['thumbs'] && $item['images']) {
                         $arrThumb = explode('|', $item['thumbs']);
                         $arrImage = explode('|', $item['images']);
                         $i = 0;
                         foreach ($arrThumb as $thumb) {
                             $thumb = trim($thumb);
                             $image = trim($arrImage[$i]);
                             if ($thumb && $image) {
                                 // 缩略图和图片都有才加入
                                 $productImage = new ProductImage(['product_id' => $model->id, 'thumb' => $thumb, 'image' => $image]);
                                 $productImage->save();
                             }
                             $i++;
                         }
                     }
                 }
                 $line++;
             }
             if (count($errorLines)) {
                 $strLine = implode(', ', $errorLines);
                 Yii::$app->getSession()->setFlash('danger', Yii::t('app', "Line {strLine} error.", ['strLine' => $strLine]));
             }
             Yii::$app->getSession()->setFlash('success', Yii::t('app', "Import Data Success. Create: {countCreate}  Update: {countUpdate}", ['countCreate' => $countCreate, 'countUpdate' => $countUpdate]));
         }
     }
     return $this->render('import', []);
 }