Dotclear

source: inc/admin/actions/class.dcaction.php @ 3707:3a350757c847

Revision 3707:3a350757c847, 11.6 KB checked in by franck <carnet.franck.paul@…>, 7 years ago (diff)

Use array form of optionnal parameters for form::checkbox() where is relevant, code formatting (PSR-2)

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

Sites map