Пример #1
0
 public function getImage($item, $dirtyAlias)
 {
     //Get params
     $params = $data = $this->parseImageAlias($dirtyAlias);
     $alias = $params['alias'];
     $size = $params['size'];
     $itemId = preg_replace('/[^0-9]+/', '', $item);
     $modelName = preg_replace('/[0-9]+/', '', $item);
     //Lets get image
     $image = Image::find()->where(['modelName' => $modelName, 'itemId' => $itemId, 'urlAlias' => $alias])->one();
     if (!$image) {
         return $this->getPlaceHolder();
     }
     return $image;
 }
 /**
  * @param $id
  * @return array
  * @throws NotFoundHttpException
  * @throws InvalidParamException
  */
 public function actionDelete($id)
 {
     $image = Image::findOne($id);
     $filePath = $image->getPathToOrigin();
     if (!$image) {
         throw new NotFoundHttpException('Image not found');
     }
     $product = Product::findOne($image->itemId);
     if (!$product) {
         throw new NotFoundHttpException('Product not found');
     }
     $product->removeImage($image);
     if (file_exists($filePath)) {
         return ['error' => 'Ошибка удаления файла'];
     } else {
         Yii::$app->getResponse()->setStatusCode(204);
     }
 }
Пример #3
0
 /**
  * Updates an existing Gallery model.
  * If update is successful, the browser will be redirected to the 'view' page.
  * @param string $id
  * @return mixed
  */
 public function actionUpdate($id)
 {
     $model = $this->findModel($id);
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         if (isset($_POST['deleteImg'])) {
             foreach ($_POST['deleteImg'] as $del_img) {
                 $img = Image::findOne(['id' => $del_img]);
                 $model->removeImage($img);
             }
         }
         $model->imageFiles = UploadedFile::getInstances($model, 'imageFiles');
         $model->upload();
         return $this->redirect(['view', 'id' => $model->id]);
     } else {
         $images = $model->getImages();
         return $this->render('update', ['model' => $model, 'imgs' => $images]);
     }
 }
Пример #4
0
 /**
  *
  * removes concrete model's image
  * @param Image $img
  * @throws \Exception
  */
 public function removeImage(\rico\yii2images\models\Image $img)
 {
     if (!$img->isNewRecord) {
         $imgInfoweb = Image::findOne(['id' => $img->id]);
         $imgInfoweb->clearCache();
         $storePath = $this->getModule()->getStorePath();
         $fileToRemove = $storePath . DIRECTORY_SEPARATOR . $img->filePath;
         if (preg_match('@\\.@', $fileToRemove) and is_file($fileToRemove)) {
             unlink($fileToRemove);
         }
         $img->delete();
     }
 }
Пример #5
0
 /**
  *
  * removes concrete model's image
  * @param Image $img
  * @throws \Exception
  */
 public function removeImage(Image $img)
 {
     $img->clearCache();
     $storePath = $this->getModule()->getStorePath();
     $fileToRemove = $storePath . DIRECTORY_SEPARATOR . $img->filePath;
     if (preg_match('@\\.@', $fileToRemove) and is_file($fileToRemove)) {
         unlink($fileToRemove);
     }
     $img->delete();
 }
Пример #6
0
 public function behaviors()
 {
     return ArrayHelper::merge(parent::behaviors(), ['timestamp' => ['class' => TimestampBehavior::className(), 'attributes' => [ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'], ActiveRecord::EVENT_BEFORE_UPDATE => 'updated_at'], 'value' => function () {
         return time();
     }], 'translateable' => ['class' => TranslateableBehavior::className(), 'translationAttributes' => ['alt', 'title', 'subtitle', 'description', 'link']]]);
 }
Пример #7
0
 /**
  * @depends testAttachImage
  * @depends testGetImage
  */
 public function testRemoveImage()
 {
     $this->model->attachImage(__DIR__ . '/data/testPicture.jpg');
     $img = $this->model->getImage();
     //Make cache copy
     $file = scandir(vfsStream::url('root/Store/ActiveRecordImages/ActiveRecordImage1'))[2];
     mkdir(vfsStream::url('root/Cache/ActiveRecordImages/'));
     mkdir(vfsStream::url('root/Cache/ActiveRecordImages/ActiveRecordImage1'));
     copy(vfsStream::url('root/Store/ActiveRecordImages/ActiveRecordImage1') . '/' . $file, vfsStream::url('root/Cache/ActiveRecordImages/ActiveRecordImage1') . '/' . $file);
     $this->assertTrue(file_exists(vfsStream::url('root/Cache/ActiveRecordImages/ActiveRecordImage1') . '/' . $file));
     $this->model->removeImage($img);
     //Check db record removed
     $imageRecord = Image::find()->where(['itemId' => $this->model->id, 'modelName' => 'ActiveRecordImage'])->one();
     $this->assertTrue($imageRecord == NULL);
     //check files not exists
     $files = scandir(vfsStream::url('root/Store/ActiveRecordImages/ActiveRecordImage1'));
     $this->assertTrue(count($files) == 2);
     //Check cache file and folder
     $this->assertFalse(file_exists(vfsStream::url('root/Cache/ActiveRecordImages/ActiveRecordImage1') . '/' . $file));
     $this->assertFalse(file_exists(vfsStream::url('root/Cache/ActiveRecordImages/ActiveRecordImage1')));
 }
Пример #8
0
 public function actionRemoveImage($id)
 {
     $model = Image::find()->where(['id' => $id])->one();
     if ($model->delete()) {
         return Json::encode(true);
     } else {
         var_dump($model->errors);
         return Json::encode(false);
     }
 }
Пример #9
0
 public function actionRemoveImage($id)
 {
     $model = Image::findOne($id);
     $itemId = $model->itemId;
     if ($model->delete()) {
         return $this->redirect(['update', 'id' => $itemId]);
     } else {
         return $model->errors;
     }
 }