|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the Grido (https://github.com/o5/grido) |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright (c) 2011 Petr Bugyík (http://petr.bugyik.cz) |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view |
|
9
|
|
|
* the file LICENSE.md that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Grido\Components\Filters; |
|
13
|
|
|
|
|
14
|
|
|
use Grido\Exception; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Text input filter. |
|
18
|
|
|
* |
|
19
|
|
|
* @package Grido |
|
20
|
|
|
* @subpackage Components\Filters |
|
21
|
|
|
* @author Petr Bugyík |
|
22
|
|
|
* |
|
23
|
|
|
* @property int $suggestionLimit |
|
24
|
|
|
* @property-write callback $suggestionCallback |
|
25
|
|
|
*/ |
|
26
|
|
|
class Text extends Filter |
|
27
|
1 |
|
{ |
|
28
|
|
|
/** @var string */ |
|
29
|
|
|
protected $condition = 'LIKE ?'; |
|
30
|
|
|
|
|
31
|
|
|
/** @var string */ |
|
32
|
|
|
protected $formatValue = '%%value%'; |
|
33
|
|
|
|
|
34
|
|
|
/** @var bool */ |
|
35
|
|
|
protected $suggestion = FALSE; |
|
36
|
|
|
|
|
37
|
|
|
/** @var mixed */ |
|
38
|
|
|
protected $suggestionColumn; |
|
39
|
|
|
|
|
40
|
|
|
/** @var int */ |
|
41
|
|
|
protected $suggestionLimit = 10; |
|
42
|
|
|
|
|
43
|
|
|
/** @var callback */ |
|
44
|
|
|
protected $suggestionCallback; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Allows suggestion. |
|
48
|
|
|
* @param mixed $column |
|
49
|
|
|
* @return Text |
|
50
|
|
|
*/ |
|
51
|
|
|
public function setSuggestion($column = NULL) |
|
52
|
|
|
{ |
|
53
|
1 |
|
$this->suggestion = TRUE; |
|
54
|
1 |
|
$this->suggestionColumn = $column; |
|
55
|
|
|
|
|
56
|
1 |
|
$prototype = $this->getControl()->getControlPrototype(); |
|
57
|
1 |
|
$prototype->attrs['autocomplete'] = 'off'; |
|
58
|
1 |
|
$prototype->class[] = 'suggest'; |
|
59
|
|
|
|
|
60
|
1 |
|
$this->grid->onRender[] = function() use ($prototype) { |
|
61
|
1 |
|
$replacement = '-query-'; |
|
62
|
1 |
|
$prototype->setAttribute('data-grido-suggest-replacement', $replacement); |
|
63
|
1 |
|
$prototype->setAttribute('data-grido-suggest-limit', $this->suggestionLimit); |
|
64
|
1 |
|
$prototype->setAttribute('data-grido-suggest-handler', $this->link('suggest!', [ |
|
65
|
|
|
'query' => $replacement |
|
66
|
1 |
|
])); |
|
67
|
1 |
|
}; |
|
68
|
|
|
|
|
69
|
1 |
|
return $this; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Sets a limit for suggestion select. |
|
74
|
|
|
* @param int $limit |
|
75
|
|
|
* @return \Grido\Components\Filters\Text |
|
76
|
|
|
*/ |
|
77
|
|
|
public function setSuggestionLimit($limit) |
|
78
|
|
|
{ |
|
79
|
|
|
$this->suggestionLimit = (int) $limit; |
|
80
|
|
|
return $this; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Sets custom data callback. |
|
85
|
|
|
* @param callback $callback |
|
86
|
|
|
* @return \Grido\Components\Filters\Text |
|
87
|
|
|
*/ |
|
88
|
|
|
public function setSuggestionCallback($callback) |
|
89
|
|
|
{ |
|
90
|
1 |
|
$this->suggestionCallback = $callback; |
|
91
|
1 |
|
return $this; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/**********************************************************************************************/ |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @return int |
|
98
|
|
|
*/ |
|
99
|
|
|
public function getSuggestionLimit() |
|
100
|
|
|
{ |
|
101
|
|
|
return $this->suggestionLimit; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @return callback |
|
106
|
|
|
*/ |
|
107
|
|
|
public function getSuggestionCallback() |
|
108
|
|
|
{ |
|
109
|
|
|
return $this->suggestionCallback; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @return string |
|
114
|
|
|
*/ |
|
115
|
|
|
public function getSuggestionColumn() |
|
116
|
|
|
{ |
|
117
|
|
|
return $this->suggestionColumn; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @param string $query - value from input |
|
122
|
|
|
* @internal |
|
123
|
|
|
* @throws Exception |
|
124
|
|
|
*/ |
|
125
|
|
|
public function handleSuggest($query) |
|
126
|
|
|
{ |
|
127
|
1 |
|
!empty($this->grid->onRegistered) && $this->grid->onRegistered($this->grid); |
|
128
|
1 |
|
$name = $this->getName(); |
|
129
|
|
|
|
|
130
|
1 |
|
if (!$this->getPresenter()->isAjax() || !$this->suggestion || $query == '') { |
|
131
|
|
|
$this->getPresenter()->terminate(); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
1 |
|
$actualFilter = $this->grid->getActualFilter(); |
|
135
|
1 |
|
if (isset($actualFilter[$name])) { |
|
136
|
1 |
|
unset($actualFilter[$name]); |
|
137
|
1 |
|
} |
|
138
|
|
|
|
|
139
|
1 |
|
$conditions = $this->grid->__getConditions($actualFilter); |
|
140
|
|
|
|
|
141
|
1 |
|
if ($this->suggestionCallback === NULL) { |
|
142
|
1 |
|
$conditions[] = $this->__getCondition($query); |
|
143
|
|
|
|
|
144
|
1 |
|
$column = $this->suggestionColumn ? $this->suggestionColumn : current($this->getColumn()); |
|
145
|
1 |
|
$items = $this->grid->model->suggest($column, $conditions, $this->suggestionLimit); |
|
146
|
|
|
|
|
147
|
1 |
|
} else { |
|
148
|
1 |
|
$items = call_user_func_array($this->suggestionCallback, [$query, $actualFilter, $conditions, $this]); |
|
149
|
1 |
|
if (!is_array($items)) { |
|
150
|
|
|
throw new Exception('Items must be an array.'); |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
1 |
|
$this->getPresenter()->sendResponse(new \Nette\Application\Responses\JsonResponse($items)); |
|
155
|
1 |
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* @return \Nette\Forms\Controls\TextInput |
|
159
|
|
|
*/ |
|
160
|
|
|
protected function getFormControl() |
|
161
|
|
|
{ |
|
162
|
1 |
|
$control = new \Nette\Forms\Controls\TextInput($this->label); |
|
163
|
1 |
|
$control->getControlPrototype()->class[] = 'text'; |
|
164
|
|
|
|
|
165
|
1 |
|
return $control; |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
|