@@ -37,7 +37,7 @@ |
||
37 | 37 | * |
38 | 38 | * @param array|callable|string $options |
39 | 39 | * |
40 | - * @return $this|mixed |
|
40 | + * @return Select |
|
41 | 41 | */ |
42 | 42 | public function options($options = []) { |
43 | 43 | // remote options |
@@ -8,103 +8,103 @@ discard block |
||
8 | 8 | use Illuminate\Support\Str; |
9 | 9 | |
10 | 10 | class Select extends Field { |
11 | - /** |
|
12 | - * @var array |
|
13 | - */ |
|
14 | - protected static $css = [ |
|
15 | - '/vendor/laravel-admin/AdminLTE/plugins/select2/select2.min.css', |
|
16 | - ]; |
|
17 | - |
|
18 | - /** |
|
19 | - * @var array |
|
20 | - */ |
|
21 | - protected static $js = [ |
|
22 | - '/vendor/laravel-admin/AdminLTE/plugins/select2/select2.full.min.js', |
|
23 | - ]; |
|
24 | - |
|
25 | - /** |
|
26 | - * @var array |
|
27 | - */ |
|
28 | - protected $groups = []; |
|
29 | - |
|
30 | - /** |
|
31 | - * @var array |
|
32 | - */ |
|
33 | - protected $config = []; |
|
34 | - |
|
35 | - /** |
|
36 | - * Set options. |
|
37 | - * |
|
38 | - * @param array|callable|string $options |
|
39 | - * |
|
40 | - * @return $this|mixed |
|
41 | - */ |
|
42 | - public function options($options = []) { |
|
43 | - // remote options |
|
44 | - if (is_string($options)) { |
|
45 | - return $this->loadRemoteOptions(...func_get_args()); |
|
46 | - } |
|
47 | - |
|
48 | - if ($options instanceof Arrayable) { |
|
49 | - $options = $options->toArray(); |
|
50 | - } |
|
51 | - |
|
52 | - if (is_callable($options)) { |
|
53 | - $this->options = $options; |
|
54 | - } else { |
|
55 | - $this->options = (array) $options; |
|
56 | - } |
|
57 | - |
|
58 | - return $this; |
|
59 | - } |
|
60 | - |
|
61 | - /** |
|
62 | - * @param array $groups |
|
63 | - */ |
|
64 | - |
|
65 | - /** |
|
66 | - * Set option groups. |
|
67 | - * |
|
68 | - * eg: $group = [ |
|
69 | - * [ |
|
70 | - * 'label' => 'xxxx', |
|
71 | - * 'options' => [ |
|
72 | - * 1 => 'foo', |
|
73 | - * 2 => 'bar', |
|
74 | - * ... |
|
75 | - * ], |
|
76 | - * ... |
|
77 | - * ] |
|
78 | - * |
|
79 | - * @param array $groups |
|
80 | - * |
|
81 | - * @return $this |
|
82 | - */ |
|
83 | - public function groups(array $groups) { |
|
84 | - $this->groups = $groups; |
|
85 | - |
|
86 | - return $this; |
|
87 | - } |
|
88 | - |
|
89 | - /** |
|
90 | - * Load options for other select on change. |
|
91 | - * |
|
92 | - * @param string $field |
|
93 | - * @param string $sourceUrl |
|
94 | - * @param string $idField |
|
95 | - * @param string $textField |
|
96 | - * |
|
97 | - * @return $this |
|
98 | - */ |
|
99 | - public function load($field, $sourceUrl, $idField = 'id', $textField = 'text') { |
|
100 | - if (Str::contains($field, '.')) { |
|
101 | - $field = $this->formatName($field); |
|
102 | - $class = str_replace(['[', ']'], '_', $field); |
|
103 | - } else { |
|
104 | - $class = $field; |
|
105 | - } |
|
106 | - |
|
107 | - $script = <<<EOT |
|
11 | + /** |
|
12 | + * @var array |
|
13 | + */ |
|
14 | + protected static $css = [ |
|
15 | + '/vendor/laravel-admin/AdminLTE/plugins/select2/select2.min.css', |
|
16 | + ]; |
|
17 | + |
|
18 | + /** |
|
19 | + * @var array |
|
20 | + */ |
|
21 | + protected static $js = [ |
|
22 | + '/vendor/laravel-admin/AdminLTE/plugins/select2/select2.full.min.js', |
|
23 | + ]; |
|
24 | + |
|
25 | + /** |
|
26 | + * @var array |
|
27 | + */ |
|
28 | + protected $groups = []; |
|
29 | + |
|
30 | + /** |
|
31 | + * @var array |
|
32 | + */ |
|
33 | + protected $config = []; |
|
34 | + |
|
35 | + /** |
|
36 | + * Set options. |
|
37 | + * |
|
38 | + * @param array|callable|string $options |
|
39 | + * |
|
40 | + * @return $this|mixed |
|
41 | + */ |
|
42 | + public function options($options = []) { |
|
43 | + // remote options |
|
44 | + if (is_string($options)) { |
|
45 | + return $this->loadRemoteOptions(...func_get_args()); |
|
46 | + } |
|
47 | + |
|
48 | + if ($options instanceof Arrayable) { |
|
49 | + $options = $options->toArray(); |
|
50 | + } |
|
51 | + |
|
52 | + if (is_callable($options)) { |
|
53 | + $this->options = $options; |
|
54 | + } else { |
|
55 | + $this->options = (array) $options; |
|
56 | + } |
|
57 | + |
|
58 | + return $this; |
|
59 | + } |
|
60 | + |
|
61 | + /** |
|
62 | + * @param array $groups |
|
63 | + */ |
|
64 | + |
|
65 | + /** |
|
66 | + * Set option groups. |
|
67 | + * |
|
68 | + * eg: $group = [ |
|
69 | + * [ |
|
70 | + * 'label' => 'xxxx', |
|
71 | + * 'options' => [ |
|
72 | + * 1 => 'foo', |
|
73 | + * 2 => 'bar', |
|
74 | + * ... |
|
75 | + * ], |
|
76 | + * ... |
|
77 | + * ] |
|
78 | + * |
|
79 | + * @param array $groups |
|
80 | + * |
|
81 | + * @return $this |
|
82 | + */ |
|
83 | + public function groups(array $groups) { |
|
84 | + $this->groups = $groups; |
|
85 | + |
|
86 | + return $this; |
|
87 | + } |
|
88 | + |
|
89 | + /** |
|
90 | + * Load options for other select on change. |
|
91 | + * |
|
92 | + * @param string $field |
|
93 | + * @param string $sourceUrl |
|
94 | + * @param string $idField |
|
95 | + * @param string $textField |
|
96 | + * |
|
97 | + * @return $this |
|
98 | + */ |
|
99 | + public function load($field, $sourceUrl, $idField = 'id', $textField = 'text') { |
|
100 | + if (Str::contains($field, '.')) { |
|
101 | + $field = $this->formatName($field); |
|
102 | + $class = str_replace(['[', ']'], '_', $field); |
|
103 | + } else { |
|
104 | + $class = $field; |
|
105 | + } |
|
106 | + |
|
107 | + $script = <<<EOT |
|
108 | 108 | $(document).off('change', "{$this->getElementClassSelector()}"); |
109 | 109 | $(document).on('change', "{$this->getElementClassSelector()}", function () { |
110 | 110 | var target = $(this).closest('.fields-group').find(".$class"); |
@@ -121,25 +121,25 @@ discard block |
||
121 | 121 | }); |
122 | 122 | EOT; |
123 | 123 | |
124 | - Admin::script($script); |
|
125 | - |
|
126 | - return $this; |
|
127 | - } |
|
128 | - |
|
129 | - /** |
|
130 | - * Load options for other selects on change. |
|
131 | - * |
|
132 | - * @param string $fields |
|
133 | - * @param string $sourceUrls |
|
134 | - * @param string $idField |
|
135 | - * @param string $textField |
|
136 | - * |
|
137 | - * @return $this |
|
138 | - */ |
|
139 | - public function loads($fields = [], $sourceUrls = [], $idField = 'id', $textField = 'text') { |
|
140 | - $fieldsStr = implode('.', $fields); |
|
141 | - $urlsStr = implode('^', $sourceUrls); |
|
142 | - $script = <<<EOT |
|
124 | + Admin::script($script); |
|
125 | + |
|
126 | + return $this; |
|
127 | + } |
|
128 | + |
|
129 | + /** |
|
130 | + * Load options for other selects on change. |
|
131 | + * |
|
132 | + * @param string $fields |
|
133 | + * @param string $sourceUrls |
|
134 | + * @param string $idField |
|
135 | + * @param string $textField |
|
136 | + * |
|
137 | + * @return $this |
|
138 | + */ |
|
139 | + public function loads($fields = [], $sourceUrls = [], $idField = 'id', $textField = 'text') { |
|
140 | + $fieldsStr = implode('.', $fields); |
|
141 | + $urlsStr = implode('^', $sourceUrls); |
|
142 | + $script = <<<EOT |
|
143 | 143 | var fields = '$fieldsStr'.split('.'); |
144 | 144 | var urls = '$urlsStr'.split('^'); |
145 | 145 | |
@@ -172,28 +172,28 @@ discard block |
||
172 | 172 | }); |
173 | 173 | EOT; |
174 | 174 | |
175 | - Admin::script($script); |
|
175 | + Admin::script($script); |
|
176 | 176 | |
177 | - return $this; |
|
178 | - } |
|
177 | + return $this; |
|
178 | + } |
|
179 | 179 | |
180 | - /** |
|
181 | - * Load options from remote. |
|
182 | - * |
|
183 | - * @param string $url |
|
184 | - * @param array $parameters |
|
185 | - * @param array $options |
|
186 | - * |
|
187 | - * @return $this |
|
188 | - */ |
|
189 | - protected function loadRemoteOptions($url, $parameters = [], $options = []) { |
|
190 | - $ajaxOptions = [ |
|
191 | - 'url' => $url . '?' . http_build_query($parameters), |
|
192 | - ]; |
|
180 | + /** |
|
181 | + * Load options from remote. |
|
182 | + * |
|
183 | + * @param string $url |
|
184 | + * @param array $parameters |
|
185 | + * @param array $options |
|
186 | + * |
|
187 | + * @return $this |
|
188 | + */ |
|
189 | + protected function loadRemoteOptions($url, $parameters = [], $options = []) { |
|
190 | + $ajaxOptions = [ |
|
191 | + 'url' => $url . '?' . http_build_query($parameters), |
|
192 | + ]; |
|
193 | 193 | |
194 | - $ajaxOptions = json_encode(array_merge($ajaxOptions, $options)); |
|
194 | + $ajaxOptions = json_encode(array_merge($ajaxOptions, $options)); |
|
195 | 195 | |
196 | - $this->script = <<<EOT |
|
196 | + $this->script = <<<EOT |
|
197 | 197 | |
198 | 198 | $.ajax($ajaxOptions).done(function(data) { |
199 | 199 | $("{$this->getElementClassSelector()}").select2({data: data}); |
@@ -201,20 +201,20 @@ discard block |
||
201 | 201 | |
202 | 202 | EOT; |
203 | 203 | |
204 | - return $this; |
|
205 | - } |
|
206 | - |
|
207 | - /** |
|
208 | - * Load options from ajax results. |
|
209 | - * |
|
210 | - * @param string $url |
|
211 | - * @param $idField |
|
212 | - * @param $textField |
|
213 | - * |
|
214 | - * @return $this |
|
215 | - */ |
|
216 | - public function ajax($url, $idField = 'id', $textField = 'text') { |
|
217 | - $this->script = <<<EOT |
|
204 | + return $this; |
|
205 | + } |
|
206 | + |
|
207 | + /** |
|
208 | + * Load options from ajax results. |
|
209 | + * |
|
210 | + * @param string $url |
|
211 | + * @param $idField |
|
212 | + * @param $textField |
|
213 | + * |
|
214 | + * @return $this |
|
215 | + */ |
|
216 | + public function ajax($url, $idField = 'id', $textField = 'text') { |
|
217 | + $this->script = <<<EOT |
|
218 | 218 | |
219 | 219 | $("{$this->getElementClassSelector()}").select2({ |
220 | 220 | ajax: { |
@@ -251,75 +251,75 @@ discard block |
||
251 | 251 | |
252 | 252 | EOT; |
253 | 253 | |
254 | - return $this; |
|
255 | - } |
|
256 | - |
|
257 | - /** |
|
258 | - * Set config for select2. |
|
259 | - * |
|
260 | - * all configurations see https://select2.org/configuration/options-api |
|
261 | - * |
|
262 | - * @param string $key |
|
263 | - * @param mixed $val |
|
264 | - * |
|
265 | - * @return $this |
|
266 | - */ |
|
267 | - public function config($key, $val) { |
|
268 | - $this->config[$key] = $val; |
|
269 | - |
|
270 | - return $this; |
|
271 | - } |
|
272 | - |
|
273 | - /** |
|
274 | - * {@inheritdoc} |
|
275 | - */ |
|
276 | - public function render() { |
|
277 | - $configs = array_merge([ |
|
278 | - 'allowClear' => true, |
|
279 | - 'placeholder' => $this->label, |
|
280 | - ], $this->config); |
|
281 | - |
|
282 | - $configs = json_encode($configs); |
|
283 | - |
|
284 | - if (empty($this->script)) { |
|
285 | - $this->script = "$(\"{$this->getElementClassSelector()}\").select2($configs);"; |
|
286 | - } |
|
287 | - |
|
288 | - if ($this->options instanceof \Closure) { |
|
289 | - if ($this->form) { |
|
290 | - $this->options = $this->options->bindTo($this->form->model()); |
|
291 | - } |
|
292 | - |
|
293 | - $this->options(call_user_func($this->options, $this->value)); |
|
294 | - } |
|
295 | - |
|
296 | - $this->options = array_filter($this->options); |
|
297 | - |
|
298 | - return parent::render()->with([ |
|
299 | - 'options' => $this->options, |
|
300 | - 'groups' => $this->groups, |
|
301 | - ]); |
|
302 | - } |
|
303 | - |
|
304 | - /** |
|
305 | - * Select Level 2 linkage, support ajax paging. |
|
306 | - * |
|
307 | - * @param string $field parent field name |
|
308 | - * @param string $sourceUrl resource route |
|
309 | - * @param string $idField |
|
310 | - * @param string $textField |
|
311 | - * |
|
312 | - * @return $this |
|
313 | - */ |
|
314 | - public function ajaxLoad($field, $sourceUrl, $idField = 'id', $textField = 'text') { |
|
315 | - if (Str::contains($field, '.')) { |
|
316 | - $field = $this->formatName($field); |
|
317 | - $class = str_replace(['[', ']'], '_', $field); |
|
318 | - } else { |
|
319 | - $class = $field; |
|
320 | - } |
|
321 | - |
|
322 | - $this->script = <<<EOT |
|
254 | + return $this; |
|
255 | + } |
|
256 | + |
|
257 | + /** |
|
258 | + * Set config for select2. |
|
259 | + * |
|
260 | + * all configurations see https://select2.org/configuration/options-api |
|
261 | + * |
|
262 | + * @param string $key |
|
263 | + * @param mixed $val |
|
264 | + * |
|
265 | + * @return $this |
|
266 | + */ |
|
267 | + public function config($key, $val) { |
|
268 | + $this->config[$key] = $val; |
|
269 | + |
|
270 | + return $this; |
|
271 | + } |
|
272 | + |
|
273 | + /** |
|
274 | + * {@inheritdoc} |
|
275 | + */ |
|
276 | + public function render() { |
|
277 | + $configs = array_merge([ |
|
278 | + 'allowClear' => true, |
|
279 | + 'placeholder' => $this->label, |
|
280 | + ], $this->config); |
|
281 | + |
|
282 | + $configs = json_encode($configs); |
|
283 | + |
|
284 | + if (empty($this->script)) { |
|
285 | + $this->script = "$(\"{$this->getElementClassSelector()}\").select2($configs);"; |
|
286 | + } |
|
287 | + |
|
288 | + if ($this->options instanceof \Closure) { |
|
289 | + if ($this->form) { |
|
290 | + $this->options = $this->options->bindTo($this->form->model()); |
|
291 | + } |
|
292 | + |
|
293 | + $this->options(call_user_func($this->options, $this->value)); |
|
294 | + } |
|
295 | + |
|
296 | + $this->options = array_filter($this->options); |
|
297 | + |
|
298 | + return parent::render()->with([ |
|
299 | + 'options' => $this->options, |
|
300 | + 'groups' => $this->groups, |
|
301 | + ]); |
|
302 | + } |
|
303 | + |
|
304 | + /** |
|
305 | + * Select Level 2 linkage, support ajax paging. |
|
306 | + * |
|
307 | + * @param string $field parent field name |
|
308 | + * @param string $sourceUrl resource route |
|
309 | + * @param string $idField |
|
310 | + * @param string $textField |
|
311 | + * |
|
312 | + * @return $this |
|
313 | + */ |
|
314 | + public function ajaxLoad($field, $sourceUrl, $idField = 'id', $textField = 'text') { |
|
315 | + if (Str::contains($field, '.')) { |
|
316 | + $field = $this->formatName($field); |
|
317 | + $class = str_replace(['[', ']'], '_', $field); |
|
318 | + } else { |
|
319 | + $class = $field; |
|
320 | + } |
|
321 | + |
|
322 | + $this->script = <<<EOT |
|
323 | 323 | var current=$("{$this->getElementClassSelector()}"); |
324 | 324 | var target = current.closest('.fields-group').find(".$class"); |
325 | 325 | |
@@ -365,7 +365,7 @@ discard block |
||
365 | 365 | }); |
366 | 366 | EOT; |
367 | 367 | |
368 | - return $this; |
|
369 | - } |
|
368 | + return $this; |
|
369 | + } |
|
370 | 370 | |
371 | 371 | } |
@@ -188,7 +188,7 @@ |
||
188 | 188 | */ |
189 | 189 | protected function loadRemoteOptions($url, $parameters = [], $options = []) { |
190 | 190 | $ajaxOptions = [ |
191 | - 'url' => $url . '?' . http_build_query($parameters), |
|
191 | + 'url' => $url.'?'.http_build_query($parameters), |
|
192 | 192 | ]; |
193 | 193 | |
194 | 194 | $ajaxOptions = json_encode(array_merge($ajaxOptions, $options)); |
@@ -27,230 +27,230 @@ discard block |
||
27 | 27 | * @method AbstractFilter hidden($name, $value) |
28 | 28 | */ |
29 | 29 | class Filter { |
30 | - /** |
|
31 | - * @var Model |
|
32 | - */ |
|
33 | - protected $model; |
|
34 | - |
|
35 | - /** |
|
36 | - * @var array |
|
37 | - */ |
|
38 | - protected $filters = []; |
|
39 | - |
|
40 | - /** |
|
41 | - * @var array |
|
42 | - */ |
|
43 | - protected $supports = [ |
|
44 | - 'equal', 'notEqual', 'ilike', 'like', 'gt', 'lt', 'between', |
|
45 | - 'where', 'in', 'notIn', 'date', 'day', 'month', 'year', 'hidden', |
|
46 | - ]; |
|
47 | - |
|
48 | - /** |
|
49 | - * If use id filter. |
|
50 | - * |
|
51 | - * @var bool |
|
52 | - */ |
|
53 | - protected $useIdFilter = true; |
|
54 | - |
|
55 | - /** |
|
56 | - * Id filter was removed. |
|
57 | - * |
|
58 | - * @var bool |
|
59 | - */ |
|
60 | - protected $idFilterRemoved = false; |
|
61 | - |
|
62 | - /** |
|
63 | - * Action of search form. |
|
64 | - * |
|
65 | - * @var string |
|
66 | - */ |
|
67 | - protected $action; |
|
68 | - |
|
69 | - /** |
|
70 | - * @var string |
|
71 | - */ |
|
72 | - protected $view = 'admin::filter.modal'; |
|
73 | - |
|
74 | - /** |
|
75 | - * @var string |
|
76 | - */ |
|
77 | - protected $filterModalId = 'filter-modal'; |
|
78 | - |
|
79 | - /** |
|
80 | - * @var bool |
|
81 | - */ |
|
82 | - protected $filterCondition = false; |
|
83 | - |
|
84 | - /** |
|
85 | - * Create a new filter instance. |
|
86 | - * |
|
87 | - * @param Model $model |
|
88 | - */ |
|
89 | - public function __construct(Model $model) { |
|
90 | - $this->model = $model; |
|
91 | - |
|
92 | - $pk = $this->model->eloquent()->getKeyName(); |
|
93 | - |
|
94 | - $this->equal($pk, strtoupper($pk)); |
|
95 | - } |
|
96 | - |
|
97 | - /** |
|
98 | - * Filter unwanted query conditions. |
|
99 | - * |
|
100 | - * @param array $attributes |
|
101 | - * @return $this |
|
102 | - */ |
|
103 | - public function setNotWhere($attributes = []) { |
|
104 | - $this->filterCondition = $attributes; |
|
105 | - return $this; |
|
106 | - } |
|
107 | - |
|
108 | - /** |
|
109 | - * Set action of search form. |
|
110 | - * |
|
111 | - * @param string $action |
|
112 | - * |
|
113 | - * @return $this |
|
114 | - */ |
|
115 | - public function setAction($action) { |
|
116 | - $this->action = $action; |
|
117 | - |
|
118 | - return $this; |
|
119 | - } |
|
120 | - |
|
121 | - /** |
|
122 | - * Set modalId of search form. |
|
123 | - * |
|
124 | - * @param string $filterModalId |
|
125 | - * |
|
126 | - * @return $this |
|
127 | - */ |
|
128 | - public function setModalId($filterModalId) { |
|
129 | - $this->filterModalId = $filterModalId; |
|
130 | - |
|
131 | - return $this; |
|
132 | - } |
|
133 | - |
|
134 | - /** |
|
135 | - * Disable Id filter. |
|
136 | - */ |
|
137 | - public function disableIdFilter() { |
|
138 | - $this->useIdFilter = false; |
|
139 | - } |
|
140 | - |
|
141 | - /** |
|
142 | - * Remove ID filter if needed. |
|
143 | - */ |
|
144 | - public function removeIDFilterIfNeeded() { |
|
145 | - if (!$this->useIdFilter && !$this->idFilterRemoved) { |
|
146 | - array_shift($this->filters); |
|
147 | - $this->idFilterRemoved = true; |
|
148 | - } |
|
149 | - } |
|
150 | - |
|
151 | - /** |
|
152 | - * Get all conditions of the filters. |
|
153 | - * |
|
154 | - * @return array |
|
155 | - */ |
|
156 | - public function conditions() { |
|
157 | - $input = Input::all(); |
|
158 | - if ($this->filterCondition) { |
|
159 | - foreach ($this->filterCondition as $attr) { |
|
160 | - unset($input[$attr]); |
|
161 | - } |
|
162 | - } |
|
163 | - $inputs = array_dot($input); |
|
164 | - $inputs = array_filter($inputs, function ($input) { |
|
165 | - return $input !== '' && !is_null($input); |
|
166 | - }); |
|
167 | - |
|
168 | - if (empty($inputs)) { |
|
169 | - return []; |
|
170 | - } |
|
171 | - |
|
172 | - $params = []; |
|
173 | - |
|
174 | - foreach ($inputs as $key => $value) { |
|
175 | - array_set($params, $key, $value); |
|
176 | - } |
|
177 | - |
|
178 | - $conditions = []; |
|
179 | - |
|
180 | - $this->removeIDFilterIfNeeded(); |
|
181 | - |
|
182 | - foreach ($this->filters() as $filter) { |
|
183 | - $conditions[] = $filter->condition($params); |
|
184 | - } |
|
185 | - |
|
186 | - return array_filter($conditions); |
|
187 | - } |
|
188 | - |
|
189 | - /** |
|
190 | - * Add a filter to grid. |
|
191 | - * |
|
192 | - * @param AbstractFilter $filter |
|
193 | - * |
|
194 | - * @return AbstractFilter |
|
195 | - */ |
|
196 | - protected function addFilter(AbstractFilter $filter) { |
|
197 | - $filter->setParent($this); |
|
198 | - |
|
199 | - return $this->filters[] = $filter; |
|
200 | - } |
|
201 | - |
|
202 | - /** |
|
203 | - * Use a custom filter. |
|
204 | - * |
|
205 | - * @param AbstractFilter $filter |
|
206 | - * |
|
207 | - * @return AbstractFilter |
|
208 | - */ |
|
209 | - public function use (AbstractFilter $filter) { |
|
210 | - return $this->addFilter($filter); |
|
211 | - } |
|
212 | - |
|
213 | - /** |
|
214 | - * Get all filters. |
|
215 | - * |
|
216 | - * @return AbstractFilter[] |
|
217 | - */ |
|
218 | - public function filters() { |
|
219 | - return $this->filters; |
|
220 | - } |
|
221 | - |
|
222 | - /** |
|
223 | - * Execute the filter with conditions. |
|
224 | - * |
|
225 | - * @return array |
|
226 | - */ |
|
227 | - public function execute() { |
|
228 | - return $this->model->addConditions($this->conditions())->buildData(); |
|
229 | - } |
|
230 | - |
|
231 | - /** |
|
232 | - * @param callable $callback |
|
233 | - * @param int $count |
|
234 | - * |
|
235 | - * @return bool |
|
236 | - */ |
|
237 | - public function chunk(callable $callback, $count = 100) { |
|
238 | - return $this->model->addConditions($this->conditions())->chunk($callback, $count); |
|
239 | - } |
|
240 | - |
|
241 | - /** |
|
242 | - * Get the string contents of the filter view. |
|
243 | - * |
|
244 | - * @return \Illuminate\View\View|string |
|
245 | - */ |
|
246 | - public function render() { |
|
247 | - $this->removeIDFilterIfNeeded(); |
|
248 | - |
|
249 | - if (empty($this->filters)) { |
|
250 | - return ''; |
|
251 | - } |
|
252 | - |
|
253 | - $script = <<<'EOT' |
|
30 | + /** |
|
31 | + * @var Model |
|
32 | + */ |
|
33 | + protected $model; |
|
34 | + |
|
35 | + /** |
|
36 | + * @var array |
|
37 | + */ |
|
38 | + protected $filters = []; |
|
39 | + |
|
40 | + /** |
|
41 | + * @var array |
|
42 | + */ |
|
43 | + protected $supports = [ |
|
44 | + 'equal', 'notEqual', 'ilike', 'like', 'gt', 'lt', 'between', |
|
45 | + 'where', 'in', 'notIn', 'date', 'day', 'month', 'year', 'hidden', |
|
46 | + ]; |
|
47 | + |
|
48 | + /** |
|
49 | + * If use id filter. |
|
50 | + * |
|
51 | + * @var bool |
|
52 | + */ |
|
53 | + protected $useIdFilter = true; |
|
54 | + |
|
55 | + /** |
|
56 | + * Id filter was removed. |
|
57 | + * |
|
58 | + * @var bool |
|
59 | + */ |
|
60 | + protected $idFilterRemoved = false; |
|
61 | + |
|
62 | + /** |
|
63 | + * Action of search form. |
|
64 | + * |
|
65 | + * @var string |
|
66 | + */ |
|
67 | + protected $action; |
|
68 | + |
|
69 | + /** |
|
70 | + * @var string |
|
71 | + */ |
|
72 | + protected $view = 'admin::filter.modal'; |
|
73 | + |
|
74 | + /** |
|
75 | + * @var string |
|
76 | + */ |
|
77 | + protected $filterModalId = 'filter-modal'; |
|
78 | + |
|
79 | + /** |
|
80 | + * @var bool |
|
81 | + */ |
|
82 | + protected $filterCondition = false; |
|
83 | + |
|
84 | + /** |
|
85 | + * Create a new filter instance. |
|
86 | + * |
|
87 | + * @param Model $model |
|
88 | + */ |
|
89 | + public function __construct(Model $model) { |
|
90 | + $this->model = $model; |
|
91 | + |
|
92 | + $pk = $this->model->eloquent()->getKeyName(); |
|
93 | + |
|
94 | + $this->equal($pk, strtoupper($pk)); |
|
95 | + } |
|
96 | + |
|
97 | + /** |
|
98 | + * Filter unwanted query conditions. |
|
99 | + * |
|
100 | + * @param array $attributes |
|
101 | + * @return $this |
|
102 | + */ |
|
103 | + public function setNotWhere($attributes = []) { |
|
104 | + $this->filterCondition = $attributes; |
|
105 | + return $this; |
|
106 | + } |
|
107 | + |
|
108 | + /** |
|
109 | + * Set action of search form. |
|
110 | + * |
|
111 | + * @param string $action |
|
112 | + * |
|
113 | + * @return $this |
|
114 | + */ |
|
115 | + public function setAction($action) { |
|
116 | + $this->action = $action; |
|
117 | + |
|
118 | + return $this; |
|
119 | + } |
|
120 | + |
|
121 | + /** |
|
122 | + * Set modalId of search form. |
|
123 | + * |
|
124 | + * @param string $filterModalId |
|
125 | + * |
|
126 | + * @return $this |
|
127 | + */ |
|
128 | + public function setModalId($filterModalId) { |
|
129 | + $this->filterModalId = $filterModalId; |
|
130 | + |
|
131 | + return $this; |
|
132 | + } |
|
133 | + |
|
134 | + /** |
|
135 | + * Disable Id filter. |
|
136 | + */ |
|
137 | + public function disableIdFilter() { |
|
138 | + $this->useIdFilter = false; |
|
139 | + } |
|
140 | + |
|
141 | + /** |
|
142 | + * Remove ID filter if needed. |
|
143 | + */ |
|
144 | + public function removeIDFilterIfNeeded() { |
|
145 | + if (!$this->useIdFilter && !$this->idFilterRemoved) { |
|
146 | + array_shift($this->filters); |
|
147 | + $this->idFilterRemoved = true; |
|
148 | + } |
|
149 | + } |
|
150 | + |
|
151 | + /** |
|
152 | + * Get all conditions of the filters. |
|
153 | + * |
|
154 | + * @return array |
|
155 | + */ |
|
156 | + public function conditions() { |
|
157 | + $input = Input::all(); |
|
158 | + if ($this->filterCondition) { |
|
159 | + foreach ($this->filterCondition as $attr) { |
|
160 | + unset($input[$attr]); |
|
161 | + } |
|
162 | + } |
|
163 | + $inputs = array_dot($input); |
|
164 | + $inputs = array_filter($inputs, function ($input) { |
|
165 | + return $input !== '' && !is_null($input); |
|
166 | + }); |
|
167 | + |
|
168 | + if (empty($inputs)) { |
|
169 | + return []; |
|
170 | + } |
|
171 | + |
|
172 | + $params = []; |
|
173 | + |
|
174 | + foreach ($inputs as $key => $value) { |
|
175 | + array_set($params, $key, $value); |
|
176 | + } |
|
177 | + |
|
178 | + $conditions = []; |
|
179 | + |
|
180 | + $this->removeIDFilterIfNeeded(); |
|
181 | + |
|
182 | + foreach ($this->filters() as $filter) { |
|
183 | + $conditions[] = $filter->condition($params); |
|
184 | + } |
|
185 | + |
|
186 | + return array_filter($conditions); |
|
187 | + } |
|
188 | + |
|
189 | + /** |
|
190 | + * Add a filter to grid. |
|
191 | + * |
|
192 | + * @param AbstractFilter $filter |
|
193 | + * |
|
194 | + * @return AbstractFilter |
|
195 | + */ |
|
196 | + protected function addFilter(AbstractFilter $filter) { |
|
197 | + $filter->setParent($this); |
|
198 | + |
|
199 | + return $this->filters[] = $filter; |
|
200 | + } |
|
201 | + |
|
202 | + /** |
|
203 | + * Use a custom filter. |
|
204 | + * |
|
205 | + * @param AbstractFilter $filter |
|
206 | + * |
|
207 | + * @return AbstractFilter |
|
208 | + */ |
|
209 | + public function use (AbstractFilter $filter) { |
|
210 | + return $this->addFilter($filter); |
|
211 | + } |
|
212 | + |
|
213 | + /** |
|
214 | + * Get all filters. |
|
215 | + * |
|
216 | + * @return AbstractFilter[] |
|
217 | + */ |
|
218 | + public function filters() { |
|
219 | + return $this->filters; |
|
220 | + } |
|
221 | + |
|
222 | + /** |
|
223 | + * Execute the filter with conditions. |
|
224 | + * |
|
225 | + * @return array |
|
226 | + */ |
|
227 | + public function execute() { |
|
228 | + return $this->model->addConditions($this->conditions())->buildData(); |
|
229 | + } |
|
230 | + |
|
231 | + /** |
|
232 | + * @param callable $callback |
|
233 | + * @param int $count |
|
234 | + * |
|
235 | + * @return bool |
|
236 | + */ |
|
237 | + public function chunk(callable $callback, $count = 100) { |
|
238 | + return $this->model->addConditions($this->conditions())->chunk($callback, $count); |
|
239 | + } |
|
240 | + |
|
241 | + /** |
|
242 | + * Get the string contents of the filter view. |
|
243 | + * |
|
244 | + * @return \Illuminate\View\View|string |
|
245 | + */ |
|
246 | + public function render() { |
|
247 | + $this->removeIDFilterIfNeeded(); |
|
248 | + |
|
249 | + if (empty($this->filters)) { |
|
250 | + return ''; |
|
251 | + } |
|
252 | + |
|
253 | + $script = <<<'EOT' |
|
254 | 254 | |
255 | 255 | $("#filter-modal .submit").click(function () { |
256 | 256 | $("#filter-modal").modal('toggle'); |
@@ -259,65 +259,65 @@ discard block |
||
259 | 259 | }); |
260 | 260 | |
261 | 261 | EOT; |
262 | - Admin::script($script); |
|
263 | - |
|
264 | - return view($this->view)->with([ |
|
265 | - 'action' => $this->action ?: $this->urlWithoutFilters(), |
|
266 | - 'filters' => $this->filters, |
|
267 | - 'modalId' => $this->filterModalId, |
|
268 | - ]); |
|
269 | - } |
|
270 | - |
|
271 | - /** |
|
272 | - * Get url without filter queryString. |
|
273 | - * |
|
274 | - * @return string |
|
275 | - */ |
|
276 | - protected function urlWithoutFilters() { |
|
277 | - $columns = []; |
|
278 | - |
|
279 | - /** @var Filter\AbstractFilter $filter * */ |
|
280 | - foreach ($this->filters as $filter) { |
|
281 | - $columns[] = $filter->getColumn(); |
|
282 | - } |
|
283 | - |
|
284 | - /** @var \Illuminate\Http\Request $request * */ |
|
285 | - $request = Request::instance(); |
|
286 | - |
|
287 | - $query = $request->query(); |
|
288 | - array_forget($query, $columns); |
|
289 | - |
|
290 | - $question = $request->getBaseUrl() . $request->getPathInfo() == '/' ? '/?' : '?'; |
|
291 | - |
|
292 | - return count($request->query()) > 0 |
|
293 | - ? $request->url() . $question . http_build_query($query) |
|
294 | - : $request->fullUrl(); |
|
295 | - } |
|
296 | - |
|
297 | - /** |
|
298 | - * Generate a filter object and add to grid. |
|
299 | - * |
|
300 | - * @param string $method |
|
301 | - * @param array $arguments |
|
302 | - * |
|
303 | - * @return AbstractFilter|$this |
|
304 | - */ |
|
305 | - public function __call($method, $arguments) { |
|
306 | - if (in_array($method, $this->supports)) { |
|
307 | - $className = '\\Encore\\Admin\\Grid\\Filter\\' . ucfirst($method); |
|
308 | - |
|
309 | - return $this->addFilter(new $className(...$arguments)); |
|
310 | - } |
|
311 | - |
|
312 | - return $this; |
|
313 | - } |
|
314 | - |
|
315 | - /** |
|
316 | - * Get the string contents of the filter view. |
|
317 | - * |
|
318 | - * @return \Illuminate\View\View|string |
|
319 | - */ |
|
320 | - public function __toString() { |
|
321 | - return $this->render(); |
|
322 | - } |
|
262 | + Admin::script($script); |
|
263 | + |
|
264 | + return view($this->view)->with([ |
|
265 | + 'action' => $this->action ?: $this->urlWithoutFilters(), |
|
266 | + 'filters' => $this->filters, |
|
267 | + 'modalId' => $this->filterModalId, |
|
268 | + ]); |
|
269 | + } |
|
270 | + |
|
271 | + /** |
|
272 | + * Get url without filter queryString. |
|
273 | + * |
|
274 | + * @return string |
|
275 | + */ |
|
276 | + protected function urlWithoutFilters() { |
|
277 | + $columns = []; |
|
278 | + |
|
279 | + /** @var Filter\AbstractFilter $filter * */ |
|
280 | + foreach ($this->filters as $filter) { |
|
281 | + $columns[] = $filter->getColumn(); |
|
282 | + } |
|
283 | + |
|
284 | + /** @var \Illuminate\Http\Request $request * */ |
|
285 | + $request = Request::instance(); |
|
286 | + |
|
287 | + $query = $request->query(); |
|
288 | + array_forget($query, $columns); |
|
289 | + |
|
290 | + $question = $request->getBaseUrl() . $request->getPathInfo() == '/' ? '/?' : '?'; |
|
291 | + |
|
292 | + return count($request->query()) > 0 |
|
293 | + ? $request->url() . $question . http_build_query($query) |
|
294 | + : $request->fullUrl(); |
|
295 | + } |
|
296 | + |
|
297 | + /** |
|
298 | + * Generate a filter object and add to grid. |
|
299 | + * |
|
300 | + * @param string $method |
|
301 | + * @param array $arguments |
|
302 | + * |
|
303 | + * @return AbstractFilter|$this |
|
304 | + */ |
|
305 | + public function __call($method, $arguments) { |
|
306 | + if (in_array($method, $this->supports)) { |
|
307 | + $className = '\\Encore\\Admin\\Grid\\Filter\\' . ucfirst($method); |
|
308 | + |
|
309 | + return $this->addFilter(new $className(...$arguments)); |
|
310 | + } |
|
311 | + |
|
312 | + return $this; |
|
313 | + } |
|
314 | + |
|
315 | + /** |
|
316 | + * Get the string contents of the filter view. |
|
317 | + * |
|
318 | + * @return \Illuminate\View\View|string |
|
319 | + */ |
|
320 | + public function __toString() { |
|
321 | + return $this->render(); |
|
322 | + } |
|
323 | 323 | } |
@@ -161,7 +161,7 @@ discard block |
||
161 | 161 | } |
162 | 162 | } |
163 | 163 | $inputs = array_dot($input); |
164 | - $inputs = array_filter($inputs, function ($input) { |
|
164 | + $inputs = array_filter($inputs, function($input) { |
|
165 | 165 | return $input !== '' && !is_null($input); |
166 | 166 | }); |
167 | 167 | |
@@ -287,10 +287,10 @@ discard block |
||
287 | 287 | $query = $request->query(); |
288 | 288 | array_forget($query, $columns); |
289 | 289 | |
290 | - $question = $request->getBaseUrl() . $request->getPathInfo() == '/' ? '/?' : '?'; |
|
290 | + $question = $request->getBaseUrl().$request->getPathInfo() == '/' ? '/?' : '?'; |
|
291 | 291 | |
292 | 292 | return count($request->query()) > 0 |
293 | - ? $request->url() . $question . http_build_query($query) |
|
293 | + ? $request->url().$question.http_build_query($query) |
|
294 | 294 | : $request->fullUrl(); |
295 | 295 | } |
296 | 296 | |
@@ -304,7 +304,7 @@ discard block |
||
304 | 304 | */ |
305 | 305 | public function __call($method, $arguments) { |
306 | 306 | if (in_array($method, $this->supports)) { |
307 | - $className = '\\Encore\\Admin\\Grid\\Filter\\' . ucfirst($method); |
|
307 | + $className = '\\Encore\\Admin\\Grid\\Filter\\'.ucfirst($method); |
|
308 | 308 | |
309 | 309 | return $this->addFilter(new $className(...$arguments)); |
310 | 310 | } |