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