1 | <?php |
||
31 | abstract class Action extends \Grido\Components\Component |
||
32 | 1 | { |
|
33 | const ID = 'actions'; |
||
34 | |||
35 | /** @var Html <a> html tag */ |
||
36 | protected $elementPrototype; |
||
37 | |||
38 | /** @var callback for custom rendering */ |
||
39 | protected $customRender; |
||
40 | |||
41 | 1 | /** @var string - name of primary key f.e.: link->('Article:edit', array($primaryKey => 1)) */ |
|
42 | protected $primaryKey; |
||
43 | |||
44 | /** @var callback for disabling */ |
||
45 | protected $disable; |
||
46 | |||
47 | /** @var string */ |
||
48 | protected $options; |
||
49 | |||
50 | /** |
||
51 | * @param \Grido\Grid $grid |
||
52 | * @param string $name |
||
53 | * @param string $label |
||
54 | */ |
||
55 | public function __construct($grid, $name, $label) |
||
62 | |||
63 | /** |
||
64 | * Sets html element. |
||
65 | * @param Html $elementPrototype |
||
66 | * @return Action |
||
67 | */ |
||
68 | public function setElementPrototype(Html $elementPrototype) |
||
73 | |||
74 | /** |
||
75 | * Sets callback for custom rendering. |
||
76 | * @param callback |
||
77 | * @return Action |
||
78 | 1 | */ |
|
79 | public function setCustomRender($callback) |
||
84 | |||
85 | /** |
||
86 | * Sets primary key. |
||
87 | * @param string $primaryKey |
||
88 | * @return Action |
||
89 | */ |
||
90 | public function setPrimaryKey($primaryKey) |
||
95 | |||
96 | /** |
||
97 | * Sets callback for disable. |
||
98 | * Callback should return TRUE if the action is not allowed for current item. |
||
99 | * @param callback |
||
100 | * @return Action |
||
101 | */ |
||
102 | public function setDisable($callback) |
||
103 | { |
||
104 | $this->disable = $callback; |
||
105 | return $this; |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * Sets client side confirm. |
||
110 | * @param string|callback $confirm |
||
111 | * @return Action |
||
112 | */ |
||
113 | public function setConfirm($confirm) |
||
114 | { |
||
115 | $this->setOption('confirm', $confirm); |
||
116 | return $this; |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * Sets name of icon. |
||
121 | * @param string $name |
||
122 | * @return Action |
||
123 | */ |
||
124 | public function setIcon($name) |
||
125 | { |
||
126 | $this->setOption('icon', $name); |
||
127 | return $this; |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * Sets user-specific option. |
||
132 | * @param string $key |
||
133 | * @param mixed $value |
||
134 | * @return Action |
||
135 | */ |
||
136 | public function setOption($key, $value) |
||
137 | { |
||
138 | if ($value === NULL) { |
||
139 | unset($this->options[$key]); |
||
140 | |||
141 | } else { |
||
142 | $this->options[$key] = $value; |
||
143 | } |
||
144 | |||
145 | return $this; |
||
146 | } |
||
147 | |||
148 | /**********************************************************************************************/ |
||
149 | |||
150 | /** |
||
151 | * Returns element prototype (<a> html tag). |
||
152 | * @return Html |
||
153 | * @throws Exception |
||
154 | */ |
||
155 | public function getElementPrototype() |
||
169 | |||
170 | /** |
||
171 | * @return string |
||
172 | * @internal |
||
173 | */ |
||
174 | public function getPrimaryKey() |
||
182 | |||
183 | /** |
||
184 | * @param mixed $row |
||
185 | * @return Html |
||
186 | * @internal |
||
187 | */ |
||
188 | public function getElement($row) |
||
189 | { |
||
190 | 1 | $element = clone $this->getElementPrototype(); |
|
191 | |||
192 | 1 | if ($confirm = $this->getOption('confirm')) { |
|
193 | $confirm = is_callable($confirm) |
||
194 | ? call_user_func_array($confirm, [$row]) |
||
195 | : $confirm; |
||
196 | |||
197 | $element->data['grido-confirm'] = is_array($confirm) |
||
198 | ? vsprintf($this->translate(array_shift($confirm)), $confirm) |
||
199 | : $this->translate($confirm); |
||
200 | } |
||
201 | |||
202 | 1 | return $element; |
|
203 | } |
||
204 | |||
205 | /** |
||
206 | * Returns user-specific option. |
||
207 | * @param string $key |
||
208 | * @param mixed $default |
||
209 | * @return mixed |
||
210 | */ |
||
211 | public function getOption($key, $default = NULL) |
||
217 | |||
218 | /** |
||
219 | * Returns user-specific options. |
||
220 | * @return array |
||
221 | */ |
||
222 | public function getOptions() |
||
226 | |||
227 | /**********************************************************************************************/ |
||
228 | |||
229 | /** |
||
230 | * @param mixed $row |
||
231 | * @throws Exception |
||
232 | * @return void |
||
233 | */ |
||
234 | public function render($row) |
||
249 | } |
||
250 |