1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Encore\Admin\Grid\Column; |
4
|
|
|
|
5
|
|
|
use Encore\Admin\Admin; |
6
|
|
|
use Encore\Admin\Grid\Model; |
7
|
|
|
|
8
|
|
|
class RangeFilter extends Filter |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var string |
12
|
|
|
*/ |
13
|
|
|
protected $type; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* RangeFilter constructor. |
17
|
|
|
* |
18
|
|
|
* @param string $type |
19
|
|
|
*/ |
20
|
|
|
public function __construct($type) |
21
|
|
|
{ |
22
|
|
|
$this->type = $type; |
23
|
|
|
$this->class = [ |
24
|
|
|
'start' => uniqid('column-filter-start-'), |
25
|
|
|
'end' => uniqid('column-filter-end-'), |
26
|
|
|
]; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Add a binding to the query. |
31
|
|
|
* |
32
|
|
|
* @param mixed $value |
33
|
|
|
* |
34
|
|
|
* @param Model $model |
35
|
|
|
*/ |
36
|
|
|
public function addBinding($value, Model $model) |
37
|
|
|
{ |
38
|
|
|
$value = array_filter((array)$value); |
39
|
|
|
|
40
|
|
|
if (empty($value)) { |
41
|
|
|
return; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
if (!isset($value['start'])) { |
45
|
|
|
return $model->where($this->getColumnName(), '<', $value['end']); |
|
|
|
|
46
|
|
|
} elseif (!isset($value['end'])) { |
47
|
|
|
return $model->where($this->getColumnName(), '>', $value['start']); |
|
|
|
|
48
|
|
|
} else { |
49
|
|
|
return $model->whereBetween($this->getColumnName(), array_values($value)); |
|
|
|
|
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
protected function addScript() |
54
|
|
|
{ |
55
|
|
|
$options = [ |
56
|
|
|
'locale' => config('app.locale'), |
57
|
|
|
'allowInputToggle' => true, |
58
|
|
|
]; |
59
|
|
|
|
60
|
|
View Code Duplication |
if ($this->type == 'date') { |
|
|
|
|
61
|
|
|
$options['format'] = 'YYYY-MM-DD'; |
62
|
|
|
} elseif ($this->type == 'time') { |
63
|
|
|
$options['format'] = 'HH:mm:ss'; |
64
|
|
|
} elseif ($this->type == 'datetime') { |
65
|
|
|
$options['format'] = 'YYYY-MM-DD HH:mm:ss'; |
66
|
|
|
} else { |
67
|
|
|
return; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$options = json_encode($options); |
71
|
|
|
|
72
|
|
|
Admin::script("$('.{$this->class['start']},.{$this->class['end']}').datetimepicker($options);"); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Render this filter. |
77
|
|
|
* |
78
|
|
|
* @return string |
79
|
|
|
*/ |
80
|
|
|
public function render() |
81
|
|
|
{ |
82
|
|
|
$script = <<<SCRIPT |
83
|
|
|
$('.dropdown-menu input').click(function(e) { |
84
|
|
|
e.stopPropagation(); |
85
|
|
|
}); |
86
|
|
|
SCRIPT; |
87
|
|
|
|
88
|
|
|
Admin::script($script); |
89
|
|
|
|
90
|
|
|
$this->addScript(); |
91
|
|
|
|
92
|
|
|
$value = $this->getFilterValue(['start' => '', 'end' => '']); |
|
|
|
|
93
|
|
|
$active = empty(array_filter($value)) ? '' : 'text-yellow'; |
94
|
|
|
|
95
|
|
|
return <<<EOT |
96
|
|
|
<span class="dropdown"> |
97
|
|
|
<form action="{$this->getFormAction()}" pjax-container style="display: inline-block;"> |
98
|
|
|
<a href="javascript:void(0);" class="dropdown-toggle {$active}" data-toggle="dropdown"> |
99
|
|
|
<i class="fa fa-filter"></i> |
100
|
|
|
</a> |
101
|
|
|
<ul class="dropdown-menu" role="menu" style="padding: 10px;box-shadow: 0 2px 3px 0 rgba(0,0,0,.2);left: -70px;border-radius: 0;"> |
102
|
|
|
<li> |
103
|
|
|
<input type="text" class="form-control input-sm {$this->class['start']}" name="{$this->getColumnName()}[start]" value="{$value['start']}" autocomplete="off"/> |
104
|
|
|
</li> |
105
|
|
|
<li style="margin: 5px;"></li> |
106
|
|
|
<li> |
107
|
|
|
<input type="text" class="form-control input-sm {$this->class['start']}" name="{$this->getColumnName()}[end]" value="{$value['end']}" autocomplete="off"/> |
108
|
|
|
</li> |
109
|
|
|
<li class="divider"></li> |
110
|
|
|
<li class="text-right"> |
111
|
|
|
<button class="btn btn-sm btn-primary btn-flat column-filter-submit pull-left">{$this->trans('submit')}</button> |
112
|
|
|
<button class="btn btn-sm btn-default btn-flat column-filter-all">{$this->trans('reset')}</button> |
113
|
|
|
</li> |
114
|
|
|
</ul> |
115
|
|
|
</form> |
116
|
|
|
</span> |
117
|
|
|
EOT; |
118
|
|
|
} |
119
|
|
|
} |
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: