| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace leandrogehlen\querybuilder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use yii\base\InvalidConfigException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use yii\base\Widget; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Yii; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use yii\helpers\Html; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use yii\helpers\Inflector; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use yii\helpers\Json; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * QueryBuilderForm renders a form for to submit rule information. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * This form renders hidden input with name defined into [[rulesParam]]. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * The hidden input will be used to send JSON rules into string format. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * The typical usage of QueryBuilderForm is as follows, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * ```php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  * <?php QueryBuilderForm::begin([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  *    'rules' => $rules, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  *    'builder' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  *        'id' => 'query-builder', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  *        'pluginOptions' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  *            'filters' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  *                ['id' => 'id', 'label' => 'Id', 'type' => 'integer'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  *                ['id' => 'name', 'label' => 'Name', 'type' => 'string'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  *                ['id' => 'lastName', 'label' => 'Last Name', 'type' => 'string'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  *            ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  *        ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  *    ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  * ])?> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  *    <?= Html::submitButton('Apply'); ?> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  * <?php QueryBuilderForm::end() ?> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  * ``` | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  * @author Leandro Gehlen <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | class QueryBuilderForm extends Widget | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * @param array|string $action the form action URL. This parameter will be processed by [[\yii\helpers\Url::to()]]. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * @see method for specifying the HTTP method for this form. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     public $action = ['']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * @var string the form submission method. This should be either 'post' or 'get'. Defaults to 'get'. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * When you set this to 'get' you may see the url parameters repeated on each request. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * This is because the default value of [[action]] is set to be the current request url and each submit | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * will add new parameters instead of replacing existing ones. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     public $method = 'get'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * @var array the HTML attributes (name-value pairs) for the form tag. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     public $options = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      * @var string the hidden input name that will be used to send JSON rules into string format | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     public $rulesParam = 'rules'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      * @var array|QueryBuilder QueryBuilder column configuration. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      * For example, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * ```php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      * <?= QueryBuilderForm::widget([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      *    'builder' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      *        'id' => 'query-builder', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      *        'filters' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      *            ['id' => 'id', 'label' => 'Id', 'type' => 'integer'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      *            ['id' => 'name', 'label' => 'Name', 'type' => 'string'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      *            ['id' => 'lastName', 'label' => 'Last Name', 'type' => 'string'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      *        ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      *    ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      *]) ?> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      * ``` | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     public $builder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      * @var string JSON rules representation into array format | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     public $rules; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |      * @inheritdoc | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 98 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |     public function init() | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |         if (is_array($this->builder)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |             $this->builder = Yii::createObject(array_merge([ | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |                     'class' => QueryBuilder::className() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |                 ], $this->builder) | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |             ); | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  |         if (!$this->builder instanceof QueryBuilder) { | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  |             throw new InvalidConfigException('The "builder" property must be instance of "QueryBuilder'); | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 112 |  |  |         if (!isset($this->options['id'])) { | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  |             $this->options['id'] = $this->getId(); | 
            
                                                                        
                            
            
                                    
            
            
                | 114 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 116 |  |  |         echo $this->builder->run(); | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  |         echo Html::beginForm($this->action, $this->method, $this->options); | 
            
                                                                        
                            
            
                                    
            
            
                | 118 |  |  |         echo Html::hiddenInput($this->rulesParam); | 
            
                                                                        
                            
            
                                    
            
            
                | 119 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |      * @inheritdoc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |     public function run() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         echo Html::endForm(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |         $id = $this->options['id']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         $builderId = $this->builder->getId(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |         $view = $this->getView(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |         if ($this->rules) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |             $rules = Json::encode($this->rules); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |             $view->registerJs("$('#{$builderId}').queryBuilder('setRules', {$rules});"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         $frm = Inflector::variablize("frm-$id-querybuilder"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |         $btn = Inflector::variablize("btn-$id-querybuilder-reset"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |         $view->registerJs("var $frm = $('#{$id}');"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |         $view->registerJs(<<<JS | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |     var $btn = {$frm}.find('button:reset:first'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |     if ($btn.length){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |         $btn.on('click', function(){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |             $('#{$builderId}').queryBuilder('reset'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  | JS | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |         $view->registerJs(<<<JS | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  | {$frm}.on('submit', function(){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |     var rules = $('#{$builderId}').queryBuilder('getRules'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |     if ($.isEmptyObject(rules)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |         return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |     } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |         var input = $(this).find("input[name='{$this->rulesParam}']:first"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |         input.val(JSON.stringify(rules)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  | }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  | JS | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 164 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 165 |  |  |  | 
            
                        
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.