1 | <?php |
---|
2 | /** |
---|
3 | * @package Dotclear |
---|
4 | * @subpackage Backend |
---|
5 | * |
---|
6 | * @copyright Olivier Meunier & Association Dotclear |
---|
7 | * @copyright GPL-2.0-only |
---|
8 | */ |
---|
9 | |
---|
10 | if (!defined('DC_RC_PATH')) {return;} |
---|
11 | |
---|
12 | /** |
---|
13 | * dcActionsPage -- handler for action page on selected entries |
---|
14 | * |
---|
15 | */ |
---|
16 | abstract class dcActionsPage |
---|
17 | { |
---|
18 | /** @var string form submit uri */ |
---|
19 | protected $uri; |
---|
20 | /** @var dcCore dotclear core instance */ |
---|
21 | protected $core; |
---|
22 | /** @var array action combo box */ |
---|
23 | protected $combo; |
---|
24 | /** @var array list of defined actions (callbacks) */ |
---|
25 | protected $actions; |
---|
26 | /** @var array selected entries (each key is the entry id, value contains the entry description) */ |
---|
27 | protected $entries; |
---|
28 | /** @var record record that challenges ids against permissions */ |
---|
29 | protected $rs; |
---|
30 | /** @var array redirection $_GET arguments, if any (does not contain ids by default, ids may be merged to it) */ |
---|
31 | protected $redir_args; |
---|
32 | /** @var array list of $_POST fields used to build the redirection */ |
---|
33 | protected $redirect_fields; |
---|
34 | /** @var string redirection anchor if any */ |
---|
35 | protected $redir_anchor; |
---|
36 | |
---|
37 | /** @var string current action, if any */ |
---|
38 | protected $action; |
---|
39 | /** @var array list of url parameters (usually $_POST) */ |
---|
40 | protected $from; |
---|
41 | /** @var string form field name for "entries" (usually "entries") */ |
---|
42 | protected $field_entries; |
---|
43 | |
---|
44 | /** @var string title for checkboxes list, if displayed */ |
---|
45 | protected $cb_title; |
---|
46 | |
---|
47 | /** @var string title for caller page title */ |
---|
48 | protected $caller_title; |
---|
49 | |
---|
50 | /** @var boolean true if we are acting inside a plugin (different handling of begin/endpage) */ |
---|
51 | protected $in_plugin; |
---|
52 | |
---|
53 | /** @var boolean true if we enable to keep selection when redirecting */ |
---|
54 | protected $enable_redir_selection; |
---|
55 | |
---|
56 | /** |
---|
57 | * Class constructor |
---|
58 | * |
---|
59 | * @param mixed $core dotclear core |
---|
60 | * @param mixed $uri form uri |
---|
61 | * |
---|
62 | * @access public |
---|
63 | * |
---|
64 | * @return mixed Value. |
---|
65 | */ |
---|
66 | public function __construct($core, $uri, $redirect_args = array()) |
---|
67 | { |
---|
68 | $this->core = $core; |
---|
69 | $this->actions = new ArrayObject(); |
---|
70 | $this->combo = array(); |
---|
71 | $this->uri = $uri; |
---|
72 | $this->redir_args = $redirect_args; |
---|
73 | $this->redirect_fields = array(); |
---|
74 | $this->action = ''; |
---|
75 | $this->cb_title = __('Title'); |
---|
76 | $this->entries = array(); |
---|
77 | $this->from = new ArrayObject($_POST); |
---|
78 | $this->field_entries = 'entries'; |
---|
79 | $this->caller_title = __('Entries'); |
---|
80 | if (isset($this->redir_args['_ANCHOR'])) { |
---|
81 | $this->redir_anchor = '#' . $this->redir_args['_ANCHOR']; |
---|
82 | unset($this->redir_args['_ANCHOR']); |
---|
83 | } else { |
---|
84 | $this->redir_anchor = ''; |
---|
85 | } |
---|
86 | $u = explode('?', $_SERVER['REQUEST_URI']); |
---|
87 | $this->in_plugin = (strpos($u[0], 'plugin.php') !== false); |
---|
88 | $this->enable_redir_selection = true; |
---|
89 | } |
---|
90 | |
---|
91 | /** |
---|
92 | * setEnableRedirSelection - define whether to keep selection when redirecting |
---|
93 | * Can be usefull to be disabled to preserve some compatibility. |
---|
94 | * |
---|
95 | * @param boolean $enable true to enable, false otherwise |
---|
96 | * |
---|
97 | * @access public |
---|
98 | */ |
---|
99 | public function setEnableRedirSelection($enable) |
---|
100 | { |
---|
101 | $this->enable_redir_selection = $enable; |
---|
102 | } |
---|
103 | |
---|
104 | /** |
---|
105 | * addAction - adds an action |
---|
106 | * |
---|
107 | * @param string $actions the actions names as if it was a standalone combo array. |
---|
108 | * It will be merged with other actions. |
---|
109 | * Can be bound to multiple values, if the same callback is to be called |
---|
110 | * @param callback $callback the callback for the action. |
---|
111 | * |
---|
112 | * @access public |
---|
113 | * |
---|
114 | * @return dcActionsPage the actions page itself, enabling to chain addAction(). |
---|
115 | */ |
---|
116 | public function addAction($actions, $callback) |
---|
117 | { |
---|
118 | foreach ($actions as $k => $a) { |
---|
119 | // Check each case of combo definition |
---|
120 | // Store form values in $values |
---|
121 | if (is_array($a)) { |
---|
122 | $values = array_values($a); |
---|
123 | if (!isset($this->combo[$k])) { |
---|
124 | $this->combo[$k] = array(); |
---|
125 | } |
---|
126 | $this->combo[$k] = array_merge($this->combo[$k], $a); |
---|
127 | } elseif ($a instanceof formSelectOption) { |
---|
128 | $values = array($a->value); |
---|
129 | $this->combo[$k] = $a->value; |
---|
130 | } else { |
---|
131 | $values = array($a); |
---|
132 | $this->combo[$k] = $a; |
---|
133 | } |
---|
134 | // Associate each potential value to the callback |
---|
135 | foreach ($values as $v) { |
---|
136 | $this->actions[$v] = $callback; |
---|
137 | } |
---|
138 | } |
---|
139 | return $this; |
---|
140 | } |
---|
141 | |
---|
142 | /** |
---|
143 | * getCombo - returns the actions combo, useable through form::combo |
---|
144 | * |
---|
145 | * @access public |
---|
146 | * |
---|
147 | * @return array the actions combo |
---|
148 | */ |
---|
149 | public function getCombo() |
---|
150 | { |
---|
151 | return $this->combo; |
---|
152 | } |
---|
153 | |
---|
154 | /** |
---|
155 | * getIDS() - returns the list of selected entries |
---|
156 | * |
---|
157 | * @access public |
---|
158 | * |
---|
159 | * @return array the list |
---|
160 | */ |
---|
161 | public function getIDs() |
---|
162 | { |
---|
163 | return array_keys($this->entries); |
---|
164 | } |
---|
165 | |
---|
166 | /** |
---|
167 | * getIDS() - returns the list of selected entries as HTML hidden fields string |
---|
168 | * |
---|
169 | * @access public |
---|
170 | * |
---|
171 | * @return string the HTML code for hidden fields |
---|
172 | */ |
---|
173 | public function getIDsHidden() |
---|
174 | { |
---|
175 | $ret = ''; |
---|
176 | foreach ($this->entries as $id => $v) { |
---|
177 | $ret .= form::hidden($this->field_entries . '[]', $id); |
---|
178 | } |
---|
179 | return $ret; |
---|
180 | } |
---|
181 | |
---|
182 | /** |
---|
183 | * getHiddenFields() - returns all redirection parameters as HTML hidden fields |
---|
184 | * |
---|
185 | * @param boolean $with_ids if true, also include ids in HTML code |
---|
186 | * |
---|
187 | * @access public |
---|
188 | * |
---|
189 | * @return string the HTML code for hidden fields |
---|
190 | */ |
---|
191 | public function getHiddenFields($with_ids = false) |
---|
192 | { |
---|
193 | $ret = ''; |
---|
194 | foreach ($this->redir_args as $k => $v) { |
---|
195 | $ret .= form::hidden(array($k), $v); |
---|
196 | } |
---|
197 | if ($with_ids) { |
---|
198 | $ret .= $this->getIDsHidden(); |
---|
199 | } |
---|
200 | return $ret; |
---|
201 | } |
---|
202 | |
---|
203 | /** |
---|
204 | * getRS() - get record from DB Query containing requested IDs |
---|
205 | * |
---|
206 | * @param boolean $with_ids if true, also include ids in HTML code |
---|
207 | * |
---|
208 | * @access public |
---|
209 | * |
---|
210 | * @return string the HTML code for hidden fields |
---|
211 | */ |
---|
212 | public function getRS() |
---|
213 | { |
---|
214 | return $this->rs; |
---|
215 | } |
---|
216 | |
---|
217 | /** |
---|
218 | * setupRedir - setup redirection arguments |
---|
219 | * by default, $_POST fields as defined in redirect_fields attributes |
---|
220 | * are set into redirect_args. |
---|
221 | * |
---|
222 | * @param array $from input to parse fields from (usually $_POST) |
---|
223 | * |
---|
224 | * @access protected |
---|
225 | */ |
---|
226 | protected function setupRedir($from) |
---|
227 | { |
---|
228 | foreach ($this->redirect_fields as $p) { |
---|
229 | if (isset($from[$p])) { |
---|
230 | $this->redir_args[$p] = $from[$p]; |
---|
231 | } |
---|
232 | } |
---|
233 | } |
---|
234 | |
---|
235 | /** |
---|
236 | * getRedirection - returns redirection URL |
---|
237 | * |
---|
238 | * @param array $params extra parameters to append to redirection |
---|
239 | * must be an array : each key is the name, |
---|
240 | * each value is the wanted value |
---|
241 | * @param boolean $with_selected_entries if true, add selected entries in url |
---|
242 | * |
---|
243 | * @access public |
---|
244 | * |
---|
245 | * @return string the redirection url |
---|
246 | */ |
---|
247 | public function getRedirection($with_selected_entries = false, $params = array()) |
---|
248 | { |
---|
249 | $redir_args = array_merge($params, $this->redir_args); |
---|
250 | if (isset($redir_args['redir'])) { |
---|
251 | unset($redir_args['redir']); |
---|
252 | } |
---|
253 | |
---|
254 | if ($with_selected_entries && $this->enable_redir_selection) { |
---|
255 | $redir_args[$this->field_entries] = array_keys($this->entries); |
---|
256 | } |
---|
257 | return $this->uri . '?' . http_build_query($redir_args) . $this->redir_anchor; |
---|
258 | } |
---|
259 | |
---|
260 | /** |
---|
261 | * redirect - redirects to redirection page |
---|
262 | * |
---|
263 | * @see getRedirection for arguments details |
---|
264 | * |
---|
265 | * @access public |
---|
266 | */ |
---|
267 | public function redirect($with_selected_entries = false, $params = array()) |
---|
268 | { |
---|
269 | http::redirect($this->getRedirection($with_selected_entries, $params)); |
---|
270 | exit; |
---|
271 | } |
---|
272 | |
---|
273 | /** |
---|
274 | * getURI - returns current form URI, if any |
---|
275 | * |
---|
276 | * @access public |
---|
277 | * |
---|
278 | * @return string the form URI |
---|
279 | */ |
---|
280 | public function getURI() |
---|
281 | { |
---|
282 | return $this->uri; |
---|
283 | } |
---|
284 | |
---|
285 | /** |
---|
286 | * getCallerTitle - returns current form URI, if any |
---|
287 | * |
---|
288 | * @access public |
---|
289 | * |
---|
290 | * @return string the form URI |
---|
291 | */ |
---|
292 | public function getCallerTitle() |
---|
293 | { |
---|
294 | return $this->caller_title; |
---|
295 | } |
---|
296 | |
---|
297 | /** |
---|
298 | * getAction - returns current action, if any |
---|
299 | * |
---|
300 | * @access public |
---|
301 | * |
---|
302 | * @return string the action |
---|
303 | */ |
---|
304 | public function getAction() |
---|
305 | { |
---|
306 | return $this->action; |
---|
307 | } |
---|
308 | |
---|
309 | /** |
---|
310 | * process - proceeds action handling, if any |
---|
311 | * this method may issue an exit() if |
---|
312 | * an action is being processed. If it |
---|
313 | * returns, no action has been performed |
---|
314 | * |
---|
315 | * @access public |
---|
316 | */ |
---|
317 | public function process() |
---|
318 | { |
---|
319 | |
---|
320 | $this->setupRedir($this->from); |
---|
321 | $this->fetchEntries($this->from); |
---|
322 | if (isset($this->from['action'])) { |
---|
323 | $this->action = $this->from['action']; |
---|
324 | try { |
---|
325 | $performed = false; |
---|
326 | foreach ($this->actions as $k => $v) { |
---|
327 | if ($this->from['action'] == $k) { |
---|
328 | $performed = true; |
---|
329 | call_user_func($v, $this->core, $this, $this->from); |
---|
330 | } |
---|
331 | } |
---|
332 | if ($performed) { |
---|
333 | return true; |
---|
334 | } |
---|
335 | } catch (Exception $e) { |
---|
336 | $this->error($e); |
---|
337 | return true; |
---|
338 | } |
---|
339 | } |
---|
340 | } |
---|
341 | |
---|
342 | /** |
---|
343 | * getcheckboxes -returns html code for selected entries |
---|
344 | * as a table containing entries checkboxes |
---|
345 | * |
---|
346 | * @access public |
---|
347 | * |
---|
348 | * @return string the html code for checkboxes |
---|
349 | */ |
---|
350 | public function getCheckboxes() |
---|
351 | { |
---|
352 | $ret = |
---|
353 | '<table class="posts-list"><tr>' . |
---|
354 | '<th colspan="2">' . $this->cb_title . '</th>' . |
---|
355 | '</tr>'; |
---|
356 | foreach ($this->entries as $id => $title) { |
---|
357 | $ret .= |
---|
358 | '<tr><td class="minimal">' . |
---|
359 | form::checkbox(array($this->field_entries . '[]'), $id, array( |
---|
360 | 'checked' => true |
---|
361 | )) . |
---|
362 | '</td>' . |
---|
363 | '<td>' . $title . '</td></tr>'; |
---|
364 | } |
---|
365 | $ret .= '</table>'; |
---|
366 | return $ret; |
---|
367 | } |
---|
368 | |
---|
369 | /** |
---|
370 | * beginPage, endPage - displays the beginning/ending of a page, if action does not redirects dirtectly |
---|
371 | * |
---|
372 | * These methods are called from the actions themselves. |
---|
373 | * |
---|
374 | * @param string $breadcrumb breadcrumb to display |
---|
375 | * @param string $head page header to include |
---|
376 | * |
---|
377 | * @access public |
---|
378 | */ |
---|
379 | abstract public function beginPage($breadcrumb = '', $head = ''); |
---|
380 | abstract public function endPage(); |
---|
381 | |
---|
382 | /** |
---|
383 | * fetchEntries - fills-in information by requesting into db |
---|
384 | * this method may setup the following attributes |
---|
385 | * * entries : list of entries (checked against permissions) |
---|
386 | * entries ids are array keys, values contain entry description (if relevant) |
---|
387 | * * rs : record given by db request |
---|
388 | * @access protected |
---|
389 | */ |
---|
390 | abstract protected function fetchEntries($from); |
---|
391 | |
---|
392 | } |
---|