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