Пример #1
0
 public function actionPhoto()
 {
     Yii::beginProfile('action');
     $data = Yii::$app->request->post();
     if (empty($data['ParseImage']) || empty($data['id'])) {
         return false;
     }
     $parse = Parse::findOne($data['id']);
     if (empty($parse)) {
         return false;
     }
     //sleep(1);
     foreach ($data['ParseImage'] as $key => $url) {
         $url = @$url['url'];
         if (empty($key)) {
             $image = new ParseImage();
             $image->parse_id = $parse->id;
             $image->url = $url;
             Yii::beginProfile('get');
             if ($file = file_get_contents($url)) {
                 Yii::endProfile('get');
                 $path = Yii::$app->params['uploadSalePath'] . DIRECTORY_SEPARATOR . $parse->sale_id;
                 BaseFileHelper::createDirectory($path);
                 $photo = new SalePhoto();
                 $photo->sale_id = $parse->sale_id;
                 if ($photo->save()) {
                     $photo->sort = $photo->id;
                     $name = $photo->id . '.jpg';
                     if ($photo->save()) {
                         if (!file_put_contents($path . DIRECTORY_SEPARATOR . $name, $file)) {
                             $photo->delete();
                         } else {
                             $photo->hash = md5_file($path . DIRECTORY_SEPARATOR . $name);
                             $photo->save();
                             $image->photo_id = $photo->id;
                             $image->save();
                         }
                     }
                 }
             }
         } elseif (empty($url)) {
             $image = ParseImage::findOne($key);
             if (!empty($image)) {
                 if ($image->photo_id) {
                     $photo = SalePhoto::findOne($image->photo_id);
                     $photo->delete();
                 }
                 $image->delete();
             }
         }
     }
     Yii::endProfile('action');
     return true;
 }