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;
 13 
 14 use Grido\Components\Actions\Action;
 15 use Grido\Components\Columns\Column;
 16 use Grido\Components\Columns\Editable;
 17 use Grido\Components\Filters\Filter;
 18 use Grido\Helpers;
 19 
 20 /**
 21  * Container of grid components.
 22  *
 23  * @package     Grido
 24  * @subpackage  Components
 25  * @author      Petr Bugyík
 26  *
 27  */
 28 abstract class Container extends \Nette\Application\UI\Control
 29 {
 30     /** @var bool */
 31     protected $hasColumns;
 32 
 33     /** @var bool */
 34     protected $hasFilters;
 35 
 36     /** @var bool */
 37     protected $hasActions;
 38 
 39     /** @var bool */
 40     protected $hasOperation;
 41 
 42     /** @var bool */
 43     protected $hasExport;
 44 
 45     /**
 46      * Returns column component.
 47      * @param string $name
 48      * @param bool $need
 49      * @return Editable
 50      */
 51     public function getColumn($name, $need = TRUE)
 52     {
 53         return $this->hasColumns()
 54             ? $this->getComponent(Column::ID)->getComponent(Helpers::formatColumnName($name), $need)
 55             : NULL;
 56     }
 57 
 58     /**
 59      * Returns filter component.
 60      * @param string $name
 61      * @param bool $need
 62      * @return Filter
 63      */
 64     public function getFilter($name, $need = TRUE)
 65     {
 66         return $this->hasFilters()
 67             ? $this->getComponent(Filter::ID)->getComponent(Helpers::formatColumnName($name), $need)
 68             : NULL;
 69     }
 70 
 71     /**
 72      * Returns action component.
 73      * @param string $name
 74      * @param bool $need
 75      * @return Action
 76      */
 77     public function getAction($name, $need = TRUE)
 78     {
 79         return $this->hasActions()
 80             ? $this->getComponent(Action::ID)->getComponent($name, $need)
 81             : NULL;
 82     }
 83 
 84     /**
 85      * Returns operations component.
 86      * @param bool $need
 87      * @return Operation
 88      */
 89     public function getOperation($need = TRUE)
 90     {
 91         return $this->getComponent(Operation::ID, $need);
 92     }
 93 
 94     /**
 95      * Returns export component.
 96      * @param bool $need
 97      * @return Export
 98      */
 99     public function getExport($need = TRUE)
100     {
101         return $this->getComponent(Export::ID, $need);
102     }
103 
104     /**********************************************************************************************/
105 
106     /**
107      * @param bool $useCache
108      * @return bool
109      * @internal
110      */
111     public function hasColumns($useCache = TRUE)
112     {
113         $hasColumns = $this->hasColumns;
114 
115         if ($hasColumns === NULL || $useCache === FALSE) {
116             $container = $this->getComponent(Column::ID, FALSE);
117             $hasColumns = $container && count($container->getComponents()) > 0;
118             $this->hasColumns = $useCache ? $hasColumns : NULL;
119         }
120 
121         return $hasColumns;
122     }
123 
124     /**
125      * @param bool $useCache
126      * @return bool
127      * @internal
128      */
129     public function hasFilters($useCache = TRUE)
130     {
131         $hasFilters = $this->hasFilters;
132 
133         if ($hasFilters === NULL || $useCache === FALSE) {
134             $container = $this->getComponent(Filter::ID, FALSE);
135             $hasFilters = $container && count($container->getComponents()) > 0;
136             $this->hasFilters = $useCache ? $hasFilters : NULL;
137         }
138 
139         return $hasFilters;
140     }
141 
142     /**
143      * @param bool $useCache
144      * @return bool
145      * @internal
146      */
147     public function hasActions($useCache = TRUE)
148     {
149         $hasActions = $this->hasActions;
150 
151         if ($hasActions === NULL || $useCache === FALSE) {
152             $container = $this->getComponent(Action::ID, FALSE);
153             $hasActions = $container && count($container->getComponents()) > 0;
154             $this->hasActions = $useCache ? $hasActions : NULL;
155         }
156 
157         return $hasActions;
158     }
159 
160     /**
161      * @param bool $useCache
162      * @return bool
163      * @internal
164      */
165     public function hasOperation($useCache = TRUE)
166     {
167         $hasOperation = $this->hasOperation;
168 
169         if ($hasOperation === NULL || $useCache === FALSE) {
170             $hasOperation = (bool) $this->getComponent(Operation::ID, FALSE);
171             $this->hasOperation = $useCache ? $hasOperation : NULL;
172         }
173 
174         return $hasOperation;
175     }
176 
177     /**
178      * @param bool $useCache
179      * @return bool
180      * @internal
181      */
182     public function hasExport($useCache = TRUE)
183     {
184         $hasExport = $this->hasExport;
185 
186         if ($hasExport === NULL || $useCache === FALSE) {
187             $hasExport = (bool) $this->getComponent(Export::ID, FALSE);
188             $this->hasExport = $useCache ? $hasExport : NULL;
189         }
190 
191         return $hasExport;
192     }
193 
194     /**********************************************************************************************/
195 
196     /**
197      * @param string $name
198      * @param string $label
199      * @return Columns\Text
200      */
201     public function addColumnText($name, $label)
202     {
203         return new Columns\Text($this, $name, $label);
204     }
205 
206     /**
207      * @deprecated
208      */
209     public function addColumnMail($name, $label)
210     {
211         trigger_error(__METHOD__ . '() is deprecated; use addColumnEmail() instead.', E_USER_DEPRECATED);
212 
213         return $this->addColumnEmail($name, $label);
214     }
215 
216     /**
217      * @param string $name
218      * @param string $label
219      * @return Columns\Email
220      */
221     public function addColumnEmail($name, $label)
222     {
223         return new Columns\Email($this, $name, $label);
224     }
225 
226     /**
227      * @deprecated
228      */
229     public function addColumnHref($name, $label)
230     {
231         trigger_error(__METHOD__ . '() is deprecated; use addColumnLink() instead.', E_USER_DEPRECATED);
232 
233         return new Columns\Link($this, $name, $label);
234     }
235 
236     /**
237      * @param string $name
238      * @param string $label
239      * @return Columns\Link
240      */
241     public function addColumnLink($name, $label)
242     {
243         return new Columns\Link($this, $name, $label);
244     }
245 
246     /**
247      * @param string $name
248      * @param string $label
249      * @param string $dateFormat
250      * @return Columns\Date
251      */
252     public function addColumnDate($name, $label, $dateFormat = NULL)
253     {
254         return new Columns\Date($this, $name, $label, $dateFormat);
255     }
256 
257     /**
258      * @param string $name
259      * @param string $label
260      * @param int $decimals number of decimal points
261      * @param string $decPoint separator for the decimal point
262      * @param string $thousandsSep thousands separator
263      * @return Columns\Number
264      */
265     public function addColumnNumber($name, $label, $decimals = NULL, $decPoint = NULL, $thousandsSep = NULL)
266     {
267         return new Columns\Number($this, $name, $label, $decimals, $decPoint, $thousandsSep);
268     }
269 
270     /**********************************************************************************************/
271 
272     /**
273      * @param string $name
274      * @param string $label
275      * @return Filters\Text
276      */
277     public function addFilterText($name, $label)
278     {
279         return new Filters\Text($this, $name, $label);
280     }
281 
282     /**
283      * @param string $name
284      * @param string $label
285      * @return Filters\Date
286      */
287     public function addFilterDate($name, $label)
288     {
289         return new Filters\Date($this, $name, $label);
290     }
291 
292     /**
293      * @param string $name
294      * @param string $label
295      * @return Filters\DateRange
296      */
297     public function addFilterDateRange($name, $label)
298     {
299         return new Filters\DateRange($this, $name, $label);
300     }
301 
302     /**
303      * @param string $name
304      * @param string $label
305      * @return Filters\Check
306      */
307     public function addFilterCheck($name, $label)
308     {
309         return new Filters\Check($this, $name, $label);
310     }
311 
312     /**
313      * @param string $name
314      * @param string $label
315      * @param array $items
316      * @return Filters\Select
317      */
318     public function addFilterSelect($name, $label, array $items = NULL)
319     {
320         return new Filters\Select($this, $name, $label, $items);
321     }
322 
323     /**
324      * @param string $name
325      * @param string $label
326      * @return Filters\Number
327      */
328     public function addFilterNumber($name, $label)
329     {
330         return new Filters\Number($this, $name, $label);
331     }
332 
333     /**
334      * @param string $name
335      * @param \Nette\Forms\IControl $formControl
336      * @return Filters\Custom
337      */
338     public function addFilterCustom($name, \Nette\Forms\IControl $formControl)
339     {
340         return new Filters\Custom($this, $name, NULL, $formControl);
341     }
342 
343     /**********************************************************************************************/
344 
345     /**
346      * @param string $name
347      * @param string $label
348      * @param string $destination
349      * @param array $args
350      * @return Actions\Href
351      */
352     public function addActionHref($name, $label, $destination = NULL, array $args = NULL)
353     {
354         return new Actions\Href($this, $name, $label, $destination, $args);
355     }
356 
357     /**
358      * @param string $name
359      * @param string $label
360      * @param callback $onClick
361      * @return Actions\Event
362      */
363     public function addActionEvent($name, $label, $onClick = NULL)
364     {
365         return new Actions\Event($this, $name, $label, $onClick);
366     }
367 
368     /**********************************************************************************************/
369 
370     /**
371      * @param array $operations
372      * @param callback $onSubmit - callback after operation submit
373      * @return Operation
374      */
375     public function setOperation(array $operations, $onSubmit)
376     {
377         return new Operation($this, $operations, $onSubmit);
378     }
379 
380     /**
381      * @param string $label of exporting file
382      * @return Export
383      */
384     public function setExport($label = NULL)
385     {
386         return new Export($this, $label);
387     }
388 
389     /**
390      * Sets all columns as editable.
391      * Callback is optional for user implementation of method for saving modified data.
392      * @param callback $callback function($id, $newValue, $oldValue, Editable $column) {}
393      * @return \Grido\Grid
394      */
395     public function setEditableColumns($callback = NULL)
396     {
397         $this->onRegistered[] = function(\Grido\Grid $grid) use ($callback) {
398             if (!$grid->hasColumns()) {
399                 return;
400             }
401 
402             foreach ($grid->getComponent(Column::ID)->getComponents() as $column) {
403                 if ($column instanceof Editable && !$column->isEditableDisabled() && !$column->editableCallback) {
404                     $column->setEditable($callback);
405                 }
406             }
407         };
408 
409         return $this;
410     }
411 }
412 
Grido@master API documentation generated by ApiGen