Commit b9a5d332a20dd113452e022d3c115d72988bd131
1 parent
7d64df32
Home page continue
Showing
2 changed files
with
30 additions
and
14 deletions
Show diff stats
behaviors/ImageBehavior.php
| ... | ... | @@ -5,24 +5,30 @@ |
| 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 artweb\artbox\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 | + | |
| 27 | + /** | |
| 28 | + * @var string Image path for dummy | |
| 29 | + */ | |
| 30 | + public $dummy_path = '/images/image-not-found'; | |
| 31 | + | |
| 26 | 32 | /** |
| 27 | 33 | * @inheritdoc |
| 28 | 34 | */ |
| ... | ... | @@ -32,18 +38,18 @@ |
| 32 | 38 | ActiveRecord::EVENT_BEFORE_DELETE => 'beforeDelete', |
| 33 | 39 | ]; |
| 34 | 40 | } |
| 35 | - | |
| 41 | + | |
| 36 | 42 | /** |
| 37 | 43 | * @param Event $event |
| 38 | 44 | */ |
| 39 | 45 | public function beforeDelete($event) |
| 40 | 46 | { |
| 41 | 47 | $file = $this->getImageFile(); |
| 42 | - if(file_exists($file)) { | |
| 48 | + if (file_exists($file)) { | |
| 43 | 49 | unlink($file); |
| 44 | 50 | } |
| 45 | 51 | } |
| 46 | - | |
| 52 | + | |
| 47 | 53 | /** |
| 48 | 54 | * Get image file path |
| 49 | 55 | * |
| ... | ... | @@ -52,17 +58,21 @@ |
| 52 | 58 | public function getImageFile() |
| 53 | 59 | { |
| 54 | 60 | $link = $this->link; |
| 55 | - return empty( $this->owner->$link ) ? NULL : \Yii::getAlias('@storage/' . $this->directory . '/' . $this->owner->$link); | |
| 61 | + return empty( $this->owner->$link ) ? null : \Yii::getAlias( | |
| 62 | + '@storage/' . $this->directory . '/' . $this->owner->$link | |
| 63 | + ); | |
| 56 | 64 | } |
| 57 | 65 | |
| 58 | 66 | /** |
| 59 | 67 | * Get image file url |
| 60 | 68 | * |
| 69 | + * @param bool $dummy | |
| 70 | + * | |
| 61 | 71 | * @return null|string |
| 62 | 72 | */ |
| 63 | - public function getImageUrl() | |
| 73 | + public function getImageUrl(bool $dummy = true) | |
| 64 | 74 | { |
| 65 | 75 | $link = $this->link; |
| 66 | - return empty( $this->owner->$link ) ? NULL : '/storage/' . $this->directory . '/' . $this->owner->$link; | |
| 76 | + return empty( $this->owner->$link ) ? ( $dummy ? $this->dummy_path : null ) : '/storage/' . $this->directory . '/' . $this->owner->$link; | |
| 67 | 77 | } |
| 68 | 78 | } |
| 69 | 79 | \ No newline at end of file | ... | ... |
behaviors/SaveImgBehavior.php
| ... | ... | @@ -18,6 +18,11 @@ |
| 18 | 18 | public $fields; |
| 19 | 19 | |
| 20 | 20 | public $isLanguage = false; |
| 21 | + | |
| 22 | + /** | |
| 23 | + * @var string Image path for dummy | |
| 24 | + */ | |
| 25 | + public $dummy_path = '/images/image-not-found'; | |
| 21 | 26 | |
| 22 | 27 | public function events() |
| 23 | 28 | { |
| ... | ... | @@ -79,17 +84,18 @@ |
| 79 | 84 | $directory = $fieldset[ 'directory' ]; |
| 80 | 85 | return empty( $this->owner->$name ) ? NULL : '/storage/' . $directory . '/' . $this->owner->$name; |
| 81 | 86 | } |
| 82 | - | |
| 87 | + | |
| 83 | 88 | /** |
| 84 | - * @param int $field | |
| 89 | + * @param int $field | |
| 90 | + * @param bool $dummy | |
| 85 | 91 | * |
| 86 | 92 | * @return null|string |
| 87 | 93 | */ |
| 88 | - public function getImageUrl($field = 0) | |
| 94 | + public function getImageUrl($field = 0, bool $dummy = true) | |
| 89 | 95 | { |
| 90 | 96 | $fieldset = $this->fields[ $field ]; |
| 91 | 97 | $name = $fieldset[ 'name' ]; |
| 92 | 98 | $directory = $fieldset[ 'directory' ]; |
| 93 | - return empty( $this->owner->$name ) ? NULL : '/storage/' . $directory . '/' . $this->owner->$name; | |
| 99 | + return empty( $this->owner->$name ) ? ( $dummy ? $this->dummy_path : null ) : '/storage/' . $directory . '/' . $this->owner->$name; | |
| 94 | 100 | } |
| 95 | 101 | } |
| 96 | 102 | \ No newline at end of file | ... | ... |