Commit 12a1e136ee9c863045a816946a5d1c29629e66f8
1 parent
4a427d3b
Dummy image
Showing
2 changed files
with
25 additions
and
12 deletions
Show diff stats
common/behaviors/ImageBehavior.php
| ... | ... | @@ -5,24 +5,25 @@ |
| 5 | 5 | use yii\base\Behavior; |
| 6 | 6 | use yii\base\Event; |
| 7 | 7 | use yii\db\ActiveRecord; |
| 8 | - | |
| 8 | + | |
| 9 | 9 | /** |
| 10 | 10 | * Class ImageBehavior |
| 11 | + * | |
| 11 | 12 | * @package common\behaviors |
| 12 | 13 | */ |
| 13 | 14 | class ImageBehavior extends Behavior |
| 14 | 15 | { |
| 15 | - | |
| 16 | + | |
| 16 | 17 | /** |
| 17 | 18 | * @var string column where file name is stored |
| 18 | 19 | */ |
| 19 | 20 | public $link; |
| 20 | - | |
| 21 | + | |
| 21 | 22 | /** |
| 22 | 23 | * @var string directory name |
| 23 | 24 | */ |
| 24 | 25 | public $directory; |
| 25 | - | |
| 26 | + | |
| 26 | 27 | /** |
| 27 | 28 | * @inheritdoc |
| 28 | 29 | */ |
| ... | ... | @@ -32,18 +33,18 @@ |
| 32 | 33 | ActiveRecord::EVENT_BEFORE_DELETE => 'beforeDelete', |
| 33 | 34 | ]; |
| 34 | 35 | } |
| 35 | - | |
| 36 | + | |
| 36 | 37 | /** |
| 37 | 38 | * @param Event $event |
| 38 | 39 | */ |
| 39 | 40 | public function beforeDelete($event) |
| 40 | 41 | { |
| 41 | 42 | $file = $this->getImageFile(); |
| 42 | - if(file_exists($file)) { | |
| 43 | + if (file_exists($file)) { | |
| 43 | 44 | unlink($file); |
| 44 | 45 | } |
| 45 | 46 | } |
| 46 | - | |
| 47 | + | |
| 47 | 48 | /** |
| 48 | 49 | * Get image file path |
| 49 | 50 | * |
| ... | ... | @@ -52,17 +53,27 @@ |
| 52 | 53 | public function getImageFile() |
| 53 | 54 | { |
| 54 | 55 | $link = $this->link; |
| 55 | - return empty( $this->owner->$link ) ? NULL : \Yii::getAlias('@storage/' . $this->directory . '/' . $this->owner->$link); | |
| 56 | + return empty( $this->owner->$link ) ? null : \Yii::getAlias( | |
| 57 | + '@storage/' . $this->directory . '/' . $this->owner->$link | |
| 58 | + ); | |
| 56 | 59 | } |
| 57 | - | |
| 60 | + | |
| 58 | 61 | /** |
| 59 | 62 | * Get image file url |
| 60 | 63 | * |
| 64 | + * @param bool $dummy | |
| 65 | + * | |
| 61 | 66 | * @return null|string |
| 62 | 67 | */ |
| 63 | - public function getImageUrl() | |
| 68 | + public function getImageUrl($dummy = true) | |
| 64 | 69 | { |
| 65 | 70 | $link = $this->link; |
| 66 | - return empty( $this->owner->$link ) ? NULL : '/storage/' . $this->directory . '/' . $this->owner->$link; | |
| 71 | + if (!empty( $this->owner->$link )) { | |
| 72 | + return '/storage/' . $this->directory . '/' . $this->owner->$link; | |
| 73 | + } elseif ($dummy) { | |
| 74 | + return '/storage/no-image.png'; | |
| 75 | + } else { | |
| 76 | + return null; | |
| 77 | + } | |
| 67 | 78 | } |
| 68 | 79 | } |
| 69 | 80 | \ No newline at end of file | ... | ... |
common/behaviors/MultipleImgBehavior.php
| ... | ... | @@ -131,11 +131,13 @@ |
| 131 | 131 | return $op; |
| 132 | 132 | } |
| 133 | 133 | |
| 134 | - public function getImageUrl() | |
| 134 | + public function getImageUrl($dummy = true) | |
| 135 | 135 | { |
| 136 | 136 | $image = $this->getImage()->one(); |
| 137 | 137 | if(!empty($image)) { |
| 138 | 138 | return $image->getImageUrl(); |
| 139 | + } elseif($dummy) { | |
| 140 | + return '/storage/no-image.png'; | |
| 139 | 141 | } else { |
| 140 | 142 | return NULL; |
| 141 | 143 | } | ... | ... |