1 <?php
2
3 4 5 6 7 8 9 10
11
12 namespace Grido\Components\Actions;
13
14 use Nette\Utils\Html;
15
16 17 18 19 20 21 22 23 24 25 26 27 28 29
30 abstract class Action extends \Grido\Components\Component
31 {
32 const ID = 'actions';
33
34
35 protected $elementPrototype;
36
37
38 protected $customRender;
39
40
41 protected $primaryKey;
42
43
44 protected $disable;
45
46
47 protected $options;
48
49 50 51 52 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 64 65 66
67 public function setElementPrototype(Html $elementPrototype)
68 {
69 $this->elementPrototype = $elementPrototype;
70 return $this;
71 }
72
73 74 75 76 77
78 public function setCustomRender($callback)
79 {
80 $this->customRender = $callback;
81 return $this;
82 }
83
84 85 86 87 88
89 public function setPrimaryKey($primaryKey)
90 {
91 $this->primaryKey = $primaryKey;
92 return $this;
93 }
94
95 96 97 98 99 100
101 public function setDisable($callback)
102 {
103 $this->disable = $callback;
104 return $this;
105 }
106
107 108 109 110 111
112 public function setConfirm($confirm)
113 {
114 $this->setOption('confirm', $confirm);
115 return $this;
116 }
117
118 119 120 121 122
123 public function setIcon($name)
124 {
125 $this->setOption('icon', $name);
126 return $this;
127 }
128
129 130 131 132 133 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 151 152 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 173 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 186 187 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 208 209 210 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 221 222
223 public function getOptions()
224 {
225 return $this->options;
226 }
227
228
229
230 231 232 233 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