| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * @link      http://www.writesdown.com/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * @author    Agiel K. Saputra <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * @copyright Copyright (c) 2015 WritesDown | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * @license   http://www.writesdown.com/license | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | namespace backend\widgets; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use backend\assets\MediaModalAsset; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use common\components\Json; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Yii; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use yii\base\Widget; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use yii\helpers\ArrayHelper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use yii\helpers\Html; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use yii\helpers\Url; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | class MediaModal extends Widget | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     const TYPE_IMAGE = 'image'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     const TYPE_AUDIO = 'audio'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     const TYPE_VIDEO = 'video'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     const TYPE_FILE = 'application'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     public $title = 'Media Files Browser'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     public $post = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     public $type = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     public $editor = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     public $multiple = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     public $callback = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         'name' => '', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         'value' => '', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     public $buttonTag = 'button'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     public $buttonContent = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     public $buttonOptions = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * @inheritdoc | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 41 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |     public function init() | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |         if (empty($this->buttonContent)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |             $this->buttonContent = '<i class="fa fa-folder-open"></i> ' . Yii::t('writesdown', 'Open Media'); | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |         if ($this->buttonTag == 'button') { | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |             $this->buttonOptions['type'] = 'button'; | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |         } elseif ($this->buttonTag == 'a' && empty($this->buttonOptions['href'])) { | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |             $this->buttonOptions['href'] = '#'; | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |         $this->buttonOptions['data-toggle'] = 'media-browser'; | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |         $this->buttonOptions['id'] = $this->id; | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * @inheritdoc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     public function run() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         $this->registerClientScript(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         return Html::tag($this->buttonTag, $this->buttonContent, $this->buttonOptions); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      * Register needed scrips | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     public function registerClientScript() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         $view = $this->getView(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         MediaModalAsset::register($view); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         $callbackName = ArrayHelper::remove($this->callback, 'name'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         $callbackValue = ArrayHelper::remove($this->callback, 'value'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         if ($callbackName) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             $view->registerJs('var ' . $callbackName . ' = ' . $callbackValue, $view::POS_END); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         $settings['title'] = $this->title; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         $settings['url'] = Url::to([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             'media-browser/index', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |             'post' => $this->post, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |             'type' => $this->type, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |             'editor' => $this->editor, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |             'multiple' => $this->multiple, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |             'callback' => $callbackName, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         $settings = Json::htmlEncode($settings); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         $view->registerJs('$("#' . $this->id . '").mediamodal(' . $settings . ')'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 95 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 96 |  |  |  | 
            
                        
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.