Grido@master
  • Namespace
  • Class
  • Tree
  • Deprecated
  • Todo
  • Download

Namespaces

  • Grido
    • Components
      • Actions
      • Columns
      • Filters
    • DataSources
    • PropertyAccessors
    • Translations

Classes

  • Grido\Translations\FileTranslator
  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 /**
 15  * Text input filter.
 16  *
 17  * @package     Grido
 18  * @subpackage  Components\Filters
 19  * @author      Petr Bugyík
 20  *
 21  * @property int $suggestionLimit
 22  * @property-write callback $suggestionCallback
 23  */
 24 class Text extends Filter
 25 {
 26     /** @var string */
 27     protected $condition = 'LIKE ?';
 28 
 29     /** @var string */
 30     protected $formatValue = '%%value%';
 31 
 32     /** @var bool */
 33     protected $suggestion = FALSE;
 34 
 35     /** @var mixed */
 36     protected $suggestionColumn;
 37 
 38     /** @var int */
 39     protected $suggestionLimit = 10;
 40 
 41     /** @var callback */
 42     protected $suggestionCallback;
 43 
 44     /**
 45      * Allows suggestion.
 46      * @param mixed $column
 47      * @return Text
 48      */
 49     public function setSuggestion($column = NULL)
 50     {
 51         $this->suggestion = TRUE;
 52         $this->suggestionColumn = $column;
 53 
 54         $prototype = $this->getControl()->getControlPrototype();
 55         $prototype->attrs['autocomplete'] = 'off';
 56         $prototype->class[] = 'suggest';
 57 
 58         $filter = $this;
 59         $this->grid->onRender[] = function() use ($prototype, $filter) {
 60             $replacement = '-query-';
 61             $prototype->data['grido-suggest-replacement'] = $replacement;
 62             $prototype->data['grido-suggest-limit'] = $filter->suggestionLimit;
 63             $prototype->data['grido-suggest-handler'] = $filter->link('suggest!', array(
 64                 'query' => $replacement
 65             ));
 66         };
 67 
 68         return $this;
 69     }
 70 
 71     /**
 72      * Sets a limit for suggestion select.
 73      * @param int $limit
 74      * @return \Grido\Components\Filters\Text
 75      */
 76     public function setSuggestionLimit($limit)
 77     {
 78         $this->suggestionLimit = (int) $limit;
 79         return $this;
 80     }
 81 
 82     /**
 83      * Sets custom data callback.
 84      * @param callback $callback
 85      * @return \Grido\Components\Filters\Text
 86      */
 87     public function setSuggestionCallback($callback)
 88     {
 89         $this->suggestionCallback = $callback;
 90         return $this;
 91     }
 92 
 93     /**********************************************************************************************/
 94 
 95     /**
 96      * @return int
 97      */
 98     public function getSuggestionLimit()
 99     {
100         return $this->suggestionLimit;
101     }
102 
103     /**
104      * @return callback
105      */
106     public function getSuggestionCallback()
107     {
108         return $this->suggestionCallback;
109     }
110 
111     /**
112      * @return string
113      */
114     public function getSuggestionColumn()
115     {
116         return $this->suggestionColumn;
117     }
118 
119     /**
120      * @param string $query - value from input
121      * @internal
122      * @throws \Exception
123      */
124     public function handleSuggest($query)
125     {
126         $this->grid->onRegistered && $this->grid->onRegistered($this->grid);
127         $name = $this->getName();
128 
129         if (!$this->getPresenter()->isAjax() || !$this->suggestion || $query == '') {
130             $this->getPresenter()->terminate();
131         }
132 
133         $actualFilter = $this->grid->getActualFilter();
134         if (isset($actualFilter[$name])) {
135             unset($actualFilter[$name]);
136         }
137 
138         $conditions = $this->grid->__getConditions($actualFilter);
139 
140         if ($this->suggestionCallback === NULL) {
141             $conditions[] = $this->__getCondition($query);
142 
143             $column = $this->suggestionColumn ? $this->suggestionColumn : current($this->getColumn());
144             $items = $this->grid->model->suggest($column, $conditions, $this->suggestionLimit);
145 
146         } else {
147             $items = callback($this->suggestionCallback)->invokeArgs(array($query, $actualFilter, $conditions, $this));
148             if (!is_array($items)) {
149                 throw new \Exception('Items must be an array.');
150             }
151         }
152 
153         //sort items - first beginning of item is same as query, then case sensitive and case insensitive
154         $startsWith = $caseSensitive = $caseInsensitive = array();
155         foreach ($items as $item) {
156             if (stripos($item, $query) === 0) {
157                 $startsWith[] = $item;
158             } elseif (strpos($item, $query) !== FALSE) {
159                 $caseSensitive[] = $item;
160             } else {
161                 $caseInsensitive[] = $item;
162             }
163         }
164 
165         sort($startsWith);
166         sort($caseSensitive);
167         sort($caseInsensitive);
168 
169         $items = array_merge($startsWith, $caseSensitive, $caseInsensitive);
170         $this->getPresenter()->sendResponse(new \Nette\Application\Responses\JsonResponse($items));
171     }
172 
173     /**
174      * @return \Nette\Forms\Controls\TextInput
175      */
176     protected function getFormControl()
177     {
178         $control = new \Nette\Forms\Controls\TextInput($this->label);
179         $control->getControlPrototype()->class[] = 'text';
180 
181         return $control;
182     }
183 }
184 
Grido@master API documentation generated by ApiGen