Completed
Push — master ( 951207...f9572e )
by Leandro
03:36
created

QueryBuilder   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 148
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 10

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 6
c 4
b 0
f 0
lcom 2
cbo 10
dl 0
loc 148
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 9 2
A run() 0 11 1
A initFilters() 0 10 3
1
<?php
2
3
namespace leandrogehlen\querybuilder;
4
5
use Yii;
6
use yii\base\InvalidConfigException;
7
use yii\base\Widget;
8
use yii\helpers\Html;
9
use yii\helpers\Json;
10
11
12
/**
13
 * QueryBuilder renders a jQuery QueryBuilder component.
14
 *
15
 * @see http://mistic100.github.io/jQuery-QueryBuilder/
16
 * @author Leandro Gehlen <[email protected]>
17
 */
18
class QueryBuilder extends  Widget {
0 ignored issues
show
Coding Style introduced by
Expected 1 space before "Widget"; 2 found
Loading history...
19
20
    use OptionTrait;
21
22
    /**
23
     * @var bool Allow creation of rules groups
24
     */
25
    public $allowGroups;
26
27
    /**
28
     * @var bool No error will be thrown is the builder is entirely empty.
29
     */
30
    public $allowEmpty = true;
31
32
    /**
33
     * @var string[] available conditions
34
     */
35
    public $conditions;
36
37
    /**
38
     * @var string Default active condition
39
     */
40
    public $defaultCondition;
41
42
    /**
43
     * @var string Piece of HTML used to separate multiple inputs (for between operator)
44
     */
45
    public $inputsSeparator;
46
47
    /**
48
     * @var string Label of the "no filter" option.
49
     */
50
    public $selectPlaceholder;
51
52
    /**
53
     * @var string One of the language files code available. Default is English or the last loaded language.
54
     */
55
    public $langCode;
56
57
    /**
58
     * @var array (key-value pairs)
59
     */
60
    public $lang;
61
62
    /**
63
     * @var bool When an error occurs on a rule, display an icon  with a tooltip explaining the error.
64
     */
65
    public $displayErrors;
66
67
    /**
68
     * @var array Array of operators
69
     */
70
    public $operators;
71
72
    /**
73
     * @var array (key-value pairs)
74
     */
75
    public $plugins;
76
77
    /**
78
     * @var bool Enable sortable rules and groups. Might not work on old browsers not supporting HTML5 Drag & Drop.
79
     */
80
    public $sortable;
81
82
    /**
83
     * @var array icon configuration. For example:
84
     * ```php
85
     * [
86
     *     'addGroup' => 'glyphicon glyphicon-plus-sign',
87
     *     'addRule' => 'glyphicon glyphicon-plus',
88
     *     'removeGroup' => 'glyphicon glyphicon-remove',
89
     *     'removeRule' => 'glyphicon glyphicon-remove',
90
     *     'sort' => 'glyphicon glyphicon-sort'
91
     * ]
92
     * ```
93
     */
94
    public $icons;
95
96
97
    /**
98
     * @var array filter configuration. Each array element represents the configuration
99
     * for one particular filter. For example,
100
     *
101
     * ```php
102
     * [
103
     *     [
104
     *         'id' => 'name',
105
     *         'label' => 'Name',
106
     *         'type' => 'string',
107
     *     ], [
108
     *         'id' => 'category',
109
     *         'label' => 'Category',
110
     *         'type' => 'integer',
111
     *         'input' => 'select',
112
     *         'values' => [
113
     *             1 => 'Books',
114
     *             2 => 'Movies',
115
     *         ]
116
     *     ]
117
     * ]
118
     * ```
119
     */
120
    public $filters = [];
121
122
    /**
123
     * Initializes the query builder.
124
     * This method will instantiate [[filters]] objects and .
125
     */
126
    public function init()
127
    {
128
        if ($this->icons !== null) {
129
            $this->icons['class'] = Icon::className();
130
            $this->icons = Yii::createObject($this->icons);
0 ignored issues
show
Documentation Bug introduced by
It seems like \Yii::createObject($this->icons) of type object is incompatible with the declared type array of property $icons.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
131
        }
132
133
        $this->initFilters();
134
    }
135
136
    /**
137
     * @inheritdoc
138
     */
139
    public function run()
140
    {
141
        $id = $this->getId();
142
        $options = Json::encode($this->toOptions());
143
144
        echo Html::tag('div', '', ['id' => $id]);
145
146
        $view = $this->getView();
147
        QueryBuilderAsset::register($view);
148
        $view->registerJs("jQuery('#$id').queryBuilder($options);");
149
    }
150
151
    /**
152
     * Creates filter objects and initializes them.
153
     */
154
    public function initFilters()
155
    {
156
        if (empty($this->filters)) {
157
            throw new InvalidConfigException('The property "filters" does not is empty');
158
        }
159
        foreach ($this->filters as $i => $filter) {
160
            $filter['class'] = Filter::className();
161
            $this->filters[$i] = Yii::createObject($filter);
162
        }
163
    }
164
165
}