| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace app\modules\image\widgets; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use app\modules\image\models\Image; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use app\traits\GetImages; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use devgroup\TagDependencyHelper\ActiveRecordHelper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Yii; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use yii\base\Widget; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use yii\caching\TagDependency; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * Class ObjectImageWidget | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * @package app\widgets | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 15 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  | class ObjectImageWidget extends Widget | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |      * @var \app\properties\HasProperties|\yii\db\ActiveRecord|GetImages|null | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |     public $model = null; | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |      * @var string | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |     public $viewFile = 'img'; | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |      * @var null|int | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |     public $limit = null; | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |      * @var int | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |     public $offset = 0; | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |      * @var bool | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |     public $thumbnailOnDemand = false; | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |      * @var int | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |     public $thumbnailWidth = 400; | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |      * @var int | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |     public $thumbnailHeight = 200; | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |      * @var bool | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |     public $useWatermark = false; | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |      * @var bool if true and images array empty show "No image" | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |     public $noImageOnEmptyImages = false; | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |     /** @var array $additional Additional data passed to view */ | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |     public $additional = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     public function run() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         if (is_null($this->model) || empty($this->model->object)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             return ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         $cacheKey = static::className() . ':' . implode( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |                 "_", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |                 [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |                     $this->model->object->id, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |                     $this->model->id, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |                     $this->viewFile, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |                     $this->limit, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |                     $this->offset, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |                     $this->thumbnailOnDemand ? '1' : '0', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |                     $this->thumbnailWidth, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |                     $this->thumbnailHeight, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |                     $this->useWatermark, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |                 ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         $result = Yii::$app->cache->get($cacheKey); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         if ($result === false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             if ($this->offset > 0 || !is_null($this->limit)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |                 $images = $this->model->getImages()->limit($this->limit)->offset($this->offset)->all(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |                 $images = $this->model->images; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |             if ($this->noImageOnEmptyImages === true && count($images) === 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |                 return $this->render( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |                     'noimage', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |                     [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |                         'model' => $this->model, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |                         'thumbnailOnDemand' => $this->thumbnailOnDemand, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |                         'thumbnailWidth' => $this->thumbnailWidth, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |                         'thumbnailHeight' => $this->thumbnailHeight, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |                         'useWatermark' => $this->useWatermark, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |                         'additional' => $this->additional, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |                     ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |                 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |             $result = $this->render( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |                 $this->viewFile, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |                 [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |                     'model' => $this->model, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |                     'images' => $images, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |                     'thumbnailOnDemand' => $this->thumbnailOnDemand, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |                     'thumbnailWidth' => $this->thumbnailWidth, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |                     'thumbnailHeight' => $this->thumbnailHeight, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |                     'useWatermark' => $this->useWatermark, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |                     'additional' => $this->additional, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |                 ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |             Yii::$app->cache->set( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |                 $cacheKey, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |                 $result, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |                 86400, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |                 new TagDependency( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |                     [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |                         'tags' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |                             ActiveRecordHelper::getCommonTag(Image::className()), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |                             ActiveRecordHelper::getCommonTag($this->model->className()), | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |                         ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |                     ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |                 ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         return $result; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 127 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 128 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 129 |  |  |  | 
            
                        
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@returnannotation as described here.