diff --git a/behaviors/ImageBehavior.php b/behaviors/ImageBehavior.php index e79e601..17819bf 100755 --- a/behaviors/ImageBehavior.php +++ b/behaviors/ImageBehavior.php @@ -5,24 +5,30 @@ use yii\base\Behavior; use yii\base\Event; use yii\db\ActiveRecord; - + /** * Class ImageBehavior + * * @package artweb\artbox\behaviors */ class ImageBehavior extends Behavior { - + /** * @var string column where file name is stored */ public $link; - + /** * @var string directory name */ public $directory; - + + /** + * @var string Image path for dummy + */ + public $dummy_path = '/images/image-not-found'; + /** * @inheritdoc */ @@ -32,18 +38,18 @@ ActiveRecord::EVENT_BEFORE_DELETE => 'beforeDelete', ]; } - + /** * @param Event $event */ public function beforeDelete($event) { $file = $this->getImageFile(); - if(file_exists($file)) { + if (file_exists($file)) { unlink($file); } } - + /** * Get image file path * @@ -52,17 +58,21 @@ public function getImageFile() { $link = $this->link; - return empty( $this->owner->$link ) ? NULL : \Yii::getAlias('@storage/' . $this->directory . '/' . $this->owner->$link); + return empty( $this->owner->$link ) ? null : \Yii::getAlias( + '@storage/' . $this->directory . '/' . $this->owner->$link + ); } /** * Get image file url * + * @param bool $dummy + * * @return null|string */ - public function getImageUrl() + public function getImageUrl(bool $dummy = true) { $link = $this->link; - return empty( $this->owner->$link ) ? NULL : '/storage/' . $this->directory . '/' . $this->owner->$link; + return empty( $this->owner->$link ) ? ( $dummy ? $this->dummy_path : null ) : '/storage/' . $this->directory . '/' . $this->owner->$link; } } \ No newline at end of file diff --git a/behaviors/SaveImgBehavior.php b/behaviors/SaveImgBehavior.php index e4a80a2..a0822fc 100755 --- a/behaviors/SaveImgBehavior.php +++ b/behaviors/SaveImgBehavior.php @@ -18,6 +18,11 @@ public $fields; public $isLanguage = false; + + /** + * @var string Image path for dummy + */ + public $dummy_path = '/images/image-not-found'; public function events() { @@ -79,17 +84,18 @@ $directory = $fieldset[ 'directory' ]; return empty( $this->owner->$name ) ? NULL : '/storage/' . $directory . '/' . $this->owner->$name; } - + /** - * @param int $field + * @param int $field + * @param bool $dummy * * @return null|string */ - public function getImageUrl($field = 0) + public function getImageUrl($field = 0, bool $dummy = true) { $fieldset = $this->fields[ $field ]; $name = $fieldset[ 'name' ]; $directory = $fieldset[ 'directory' ]; - return empty( $this->owner->$name ) ? NULL : '/storage/' . $directory . '/' . $this->owner->$name; + return empty( $this->owner->$name ) ? ( $dummy ? $this->dummy_path : null ) : '/storage/' . $directory . '/' . $this->owner->$name; } } \ No newline at end of file -- libgit2 0.21.4