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\Actions;
 13 
 14 use Nette\Utils\Html;
 15 
 16 /**
 17  * Action on one row.
 18  *
 19  * @package     Grido
 20  * @subpackage  Components\Actions
 21  * @author      Petr Bugyík
 22  *
 23  * @property-read Html $element
 24  * @property-write Html $elementPrototype
 25  * @property-write callback $customRender
 26  * @property-write callback $disable
 27  * @property string $primaryKey
 28  * @property string $options
 29  */
 30 abstract class Action extends \Grido\Components\Component
 31 {
 32     const ID = 'actions';
 33 
 34     /** @var Html <a> html tag */
 35     protected $elementPrototype;
 36 
 37     /** @var callback for custom rendering */
 38     protected $customRender;
 39 
 40     /** @var string - name of primary key f.e.: link->('Article:edit', array($primaryKey => 1)) */
 41     protected $primaryKey;
 42 
 43     /** @var callback for disabling */
 44     protected $disable;
 45 
 46     /** @var string */
 47     protected $options;
 48 
 49     /**
 50      * @param \Grido\Grid $grid
 51      * @param string $name
 52      * @param string $label
 53      */
 54     public function __construct($grid, $name, $label)
 55     {
 56         $this->addComponentToGrid($grid, $name);
 57 
 58         $this->type = get_class($this);
 59         $this->label = $this->translate($label);
 60     }
 61 
 62     /**
 63      * Sets html element.
 64      * @param Html $elementPrototype
 65      * @return Action
 66      */
 67     public function setElementPrototype(Html $elementPrototype)
 68     {
 69         $this->elementPrototype = $elementPrototype;
 70         return $this;
 71     }
 72 
 73     /**
 74      * Sets callback for custom rendering.
 75      * @param callback
 76      * @return Action
 77      */
 78     public function setCustomRender($callback)
 79     {
 80         $this->customRender = $callback;
 81         return $this;
 82     }
 83 
 84     /**
 85      * Sets primary key.
 86      * @param string $primaryKey
 87      * @return Action
 88      */
 89     public function setPrimaryKey($primaryKey)
 90     {
 91         $this->primaryKey = $primaryKey;
 92         return $this;
 93     }
 94 
 95     /**
 96      * Sets callback for disable.
 97      * Callback should return TRUE if the action is not allowed for current item.
 98      * @param callback
 99      * @return Action
100      */
101     public function setDisable($callback)
102     {
103         $this->disable = $callback;
104         return $this;
105     }
106 
107     /**
108      * Sets client side confirm.
109      * @param string|callback $confirm
110      * @return Action
111      */
112     public function setConfirm($confirm)
113     {
114         $this->setOption('confirm', $confirm);
115         return $this;
116     }
117 
118     /**
119      * Sets name of icon.
120      * @param string $name
121      * @return Action
122      */
123     public function setIcon($name)
124     {
125         $this->setOption('icon', $name);
126         return $this;
127     }
128 
129     /**
130      * Sets user-specific option.
131      * @param string $key
132      * @param mixed $value
133      * @return Action
134      */
135     public function setOption($key, $value)
136     {
137         if ($value === NULL) {
138             unset($this->options[$key]);
139 
140         } else {
141             $this->options[$key] = $value;
142         }
143 
144         return $this;
145     }
146 
147     /**********************************************************************************************/
148 
149     /**
150      * Returns element prototype (<a> html tag).
151      * @return Html
152      * @throws \Exception
153      */
154     public function getElementPrototype()
155     {
156         if ($this->elementPrototype === NULL) {
157             $this->elementPrototype = Html::el('a')
158                 ->setClass(array('grid-action-' . $this->getName()))
159                 ->setText($this->label);
160         }
161 
162         if (isset($this->elementPrototype->class) && is_string($this->elementPrototype->class)) {
163             $this->elementPrototype->class = (array) $this->elementPrototype->class;
164         } elseif (isset($this->elementPrototype->class) && !is_array($this->elementPrototype->class)) {
165             throw new \Exception('Attribute class must be string or array.');
166         }
167 
168         return $this->elementPrototype;
169     }
170 
171     /**
172      * @return string
173      * @internal
174      */
175     public function getPrimaryKey()
176     {
177         if ($this->primaryKey === NULL) {
178             $this->primaryKey = $this->grid->getPrimaryKey();
179         }
180 
181         return $this->primaryKey;
182     }
183 
184     /**
185      * @param mixed $row
186      * @return Html
187      * @internal
188      */
189     public function getElement($row)
190     {
191         $element = clone $this->getElementPrototype();
192 
193         if ($confirm = $this->getOption('confirm')) {
194             $confirm = is_callable($confirm)
195                 ? callback($confirm)->invokeArgs(array($row))
196                 : $confirm;
197 
198             $element->data['grido-confirm'] = is_array($confirm)
199                 ? vsprintf($this->translate(array_shift($confirm)), $confirm)
200                 : $this->translate($confirm);
201         }
202 
203         return $element;
204     }
205 
206     /**
207      * Returns user-specific option.
208      * @param string $key
209      * @param mixed $default
210      * @return mixed
211      */
212     public function getOption($key, $default = NULL)
213     {
214         return isset($this->options[$key])
215             ? $this->options[$key]
216             : $default;
217     }
218 
219     /**
220      * Returns user-specific options.
221      * @return array
222      */
223     public function getOptions()
224     {
225         return $this->options;
226     }
227 
228     /**********************************************************************************************/
229 
230     /**
231      * @param mixed $row
232      * @throws \InvalidArgumentException
233      * @return void
234      */
235     public function render($row)
236     {
237         if (!$row || ($this->disable && callback($this->disable)->invokeArgs(array($row)))) {
238             return;
239         }
240 
241         $element = $this->getElement($row);
242 
243         if ($this->customRender) {
244             echo callback($this->customRender)->invokeArgs(array($row, $element));
245             return;
246         }
247 
248         echo $element->render();
249     }
250 }
251 
Grido@master API documentation generated by ApiGen