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