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; }
/** * returns main model image * @return array|null|ActiveRecord */ public function getImage() { if ($this->getModule()->className === null) { $imageQuery = Image::find(); } else { $class = $this->getModule()->className; $imageQuery = $class::find(); } $finder = $this->getImagesFinder(['isMain' => 1]); $imageQuery->where($finder); $imageQuery->orderBy(['isMain' => SORT_DESC, 'id' => SORT_ASC]); $img = $imageQuery->one(); if (!$img) { return $this->getModule()->getPlaceHolder(); } return $img; }
/** * @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'))); }
/** * returns main model image * @return array|null|ActiveRecord */ public function getImage() { $finder = $this->getImagesFinder(['isMain' => 1]); $imageQuery = Image::find()->where($finder); $imageQuery->orderBy(['isMain' => SORT_DESC, 'id' => SORT_ASC]); $img = $imageQuery->one(); if (!$img) { return $this->getModule()->getPlaceHolder(); } return $img; }
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); } }