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) |
||
56 | { |
||
57 | 1 | $this->addComponentToGrid($grid, $name); |
|
58 | |||
59 | 1 | $this->type = get_class($this); |
|
60 | 1 | $this->label = $this->translate($label); |
|
61 | 1 | } |
|
62 | |||
63 | /** |
||
64 | * Sets html element. |
||
65 | * @param Html $elementPrototype |
||
66 | * @return Action |
||
67 | */ |
||
68 | public function setElementPrototype(Html $elementPrototype) |
||
69 | { |
||
70 | 1 | $this->elementPrototype = $elementPrototype; |
|
71 | 1 | return $this; |
|
72 | } |
||
73 | |||
74 | /** |
||
75 | * Sets callback for custom rendering. |
||
76 | * @param callback |
||
77 | * @return Action |
||
78 | 1 | */ |
|
79 | public function setCustomRender($callback) |
||
80 | { |
||
81 | 1 | $this->customRender = $callback; |
|
82 | 1 | return $this; |
|
83 | } |
||
84 | |||
85 | /** |
||
86 | * Sets primary key. |
||
87 | * @param string $primaryKey |
||
88 | * @return Action |
||
89 | */ |
||
90 | public function setPrimaryKey($primaryKey) |
||
91 | { |
||
92 | 1 | $this->primaryKey = $primaryKey; |
|
93 | 1 | return $this; |
|
94 | } |
||
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 | 1 | $this->disable = $callback; |
|
105 | 1 | 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 | 1 | $this->setOption('confirm', $confirm); |
|
116 | 1 | 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 | 1 | if ($value === NULL) { |
|
139 | unset($this->options[$key]); |
||
140 | |||
141 | } else { |
||
142 | 1 | $this->options[$key] = $value; |
|
143 | } |
||
144 | |||
145 | 1 | 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() |
||
156 | { |
||
157 | 1 | if ($this->elementPrototype === NULL) { |
|
158 | 1 | $this->elementPrototype = Html::el('a') |
|
159 | 1 | ->setClass(['grid-action-' . $this->getName()]) |
|
160 | 1 | ->setText($this->label); |
|
161 | 1 | } |
|
162 | |||
163 | 1 | if (isset($this->elementPrototype->class)) { |
|
164 | 1 | $this->elementPrototype->class = (array) $this->elementPrototype->class; |
|
165 | 1 | } |
|
166 | |||
167 | 1 | return $this->elementPrototype; |
|
168 | } |
||
169 | |||
170 | /** |
||
171 | * @return string |
||
172 | * @internal |
||
173 | */ |
||
174 | public function getPrimaryKey() |
||
175 | { |
||
176 | 1 | if ($this->primaryKey === NULL) { |
|
177 | 1 | $this->primaryKey = $this->grid->getPrimaryKey(); |
|
178 | 1 | } |
|
179 | |||
180 | 1 | return $this->primaryKey; |
|
181 | } |
||
182 | |||
183 | /** |
||
184 | * @param mixed $row |
||
185 | * @return Html |
||
186 | * @internal |
||
187 | */ |
||
188 | public function getElement($row) |
||
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) |
||
212 | { |
||
217 | |||
218 | /** |
||
219 | * Returns user-specific options. |
||
220 | * @return array |
||
221 | */ |
||
222 | public function getOptions() |
||
223 | { |
||
224 | return $this->options; |
||
225 | } |
||
226 | |||
227 | /**********************************************************************************************/ |
||
228 | |||
229 | /** |
||
230 | * @param mixed $row |
||
231 | * @throws Exception |
||
232 | * @return void |
||
233 | */ |
||
234 | public function render($row) |
||
249 | } |
||
250 |