Text   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 142
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Test Coverage

Coverage 81.82%

Importance

Changes 0
Metric Value
wmc 16
lcom 2
cbo 4
dl 0
loc 142
ccs 36
cts 44
cp 0.8182
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setSuggestion() 0 20 1
A setSuggestionLimit() 0 5 1
A setSuggestionCallback() 0 5 1
A getSuggestionLimit() 0 4 1
A getSuggestionCallback() 0 4 1
A getSuggestionColumn() 0 4 1
B handleSuggest() 0 31 9
A getFormControl() 0 7 1
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