Dotclear

source: inc/admin/actions/class.dcaction.php @ 1903:931d1495a1c7

Revision 1903:931d1495a1c7, 8.2 KB checked in by Dsls, 12 years ago (diff)

Added redirection default params, tuned pages plugin for new actions handling. See #1181

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 current action, if any */
37     protected $action;
38     /** @var array list of url parameters (usually $_POST) */
39     protected $from;
40     
41     /** @var string title for checkboxes list, if displayed */
42     protected $cb_title;
43     
44    /**
45     * Class constructor
46     *
47     * @param mixed  $core   dotclear core
48     * @param mixed  $uri   form uri
49     *
50     * @access public
51     *
52     * @return mixed Value.
53     */
54     public function __construct($core,$uri,$redirect_args=array()) {
55          $this->core = $core;
56          $this->actions = new ArrayObject();
57          $this->combo = array();
58          $this->uri = $uri;
59          $this->redir_args = $redirect_args;
60          $this->redirect_fields = array();
61          $this->action = '';
62          $this->cb_title = __('Title');
63          $this->entries = array();
64          $this->from = new ArrayObject($_POST);
65     }
66     
67    /**
68     * addAction - adds an action
69     *
70     * @param string $actions the actions names as if it was a standalone combo array.
71      *                              It will be merged with other actions.
72      *                              Can be bound to multiple values, if the same callback is to be called
73     * @param callback $callback the callback for the action.
74     *
75     * @access public
76      *
77     * @return dcActionsPage the actions page itself, enabling to chain addAction().
78     */
79     public function addAction ($actions,$callback) {
80          foreach ($actions as $k => $a) {
81               // Check each case of combo definition
82               // Store form values in $values
83               if (is_array($a)) {
84                    $values = array_values($a);
85                    if (!isset($this->combo[$k])) {
86                         $this->combo[$k]=array();
87                    }
88                    $this->combo[$k] = array_merge ($this->combo[$k],$a);
89               } elseif ($a instanceof formSelectOption) {
90                    $values = $a->value;
91                    $this->combo[$k] = $a->value;
92               } else {
93                    $values = $a;
94                    $this->combo[$k] = $a;
95               }
96               // Associate each potential value to the callback
97               foreach ($values as $v) {
98                    $this->actions[$v]=$callback;
99               }
100          }
101          return $this;
102     }
103     
104    /**
105     * getCombo - returns the actions combo, useable through form::combo
106     *
107     * @access public
108      *
109     * @return array the actions combo
110     */
111     public function getCombo() {
112          return $this->combo;
113     }
114     
115     
116    /**
117     * getIDS() - returns the list of selected entries
118     *
119     * @access public
120      *
121     * @return array the list
122     */
123     public function getIDs() {
124          return array_keys($this->entries);
125     }
126     
127    /**
128     * getIDS() - returns the list of selected entries as HTML hidden fields string
129     *
130     * @access public
131      *
132     * @return string the HTML code for hidden fields
133     */
134      public function getIDsHidden() {
135          $ret = '';
136          foreach  ($this->entries as $id->$v) {
137               $ret .= form::hidden('entries[]',$id);
138          }
139          return $ret;
140     }
141     
142    /**
143     * getHiddenFields() - returns all redirection parameters as HTML hidden fields
144     *
145     * @param boolean $with_ids if true, also include ids in HTML code
146      *
147     * @access public
148      *
149     * @return string the HTML code for hidden fields
150     */   
151     public function getHiddenFields($with_ids = false) {
152          $ret = '';
153          foreach ($this->redir_args as $k => $v) {
154               $ret .= form::hidden(array($k),$v);
155          }
156          if ($with_ids) {
157               $ret .= $this->getIDsHidden();
158          }
159          return $ret;
160     }
161     
162     
163     /**
164     * getRS() - get record from DB Query containing requested IDs
165     *
166     * @param boolean $with_ids if true, also include ids in HTML code
167      *
168     * @access public
169      *
170     * @return string the HTML code for hidden fields
171     */
172     public function getRS() {
173          return $this->rs;
174     }
175     
176     /**
177     * setupRedir - setup redirection arguments
178      *  by default, $_POST fields as defined in redirect_fields attributes
179      *  are set into redirect_args.
180     *
181     * @param array $from input to parse fields from (usually $_POST)
182      *
183     * @access protected
184     */
185     protected function setupRedir($from) {
186          foreach ($this->redirect_fields as $p) {
187               if (isset($from[$p])) {
188                    $redir_args[$p] = $from[$p];
189               }
190          }
191     }
192
193     /**
194     * getRedirection - returns redirection URL
195     *
196     * @param array $params extra parameters to append to redirection
197      *                            must be an array : each key is the name,
198      *                            each value is the wanted value
199     * @param boolean $with_selected_entries if true, add selected entries in url
200      *
201     * @access public
202      *
203     * @return string the redirection url
204     */
205     public function getRedirection($params=array(),$with_selected_entries=false) {
206          $redir_args = array_merge($params,$this->redir_args);
207          if ($with_selected_entries) {
208               $redir_args['entries'] = array_keys($this->entries);
209          }
210          return $this->uri.'?'.http_build_query($redir_args);
211     }
212
213     /**
214     * redirect - redirects to redirection page
215     *
216      * @see getRedirection for arguments details
217      *
218     * @access public
219     */
220     public function redirect($params=array(),$with_selected_entries=false) {
221          http::redirect($this->getRedirection($params,$with_selected_entries));
222     }   
223     
224     /**
225     * getAction - returns current action, if any
226     *
227      * @see getRedirection for arguments details
228      *
229     * @access public
230      *
231     * @return string the action
232     */
233     public function getAction() {
234          return $this->action;
235     }
236
237     /**
238     * process - proceeds action handling, if any
239      *             this method may issue an exit() if
240      *             an action is being processed. If it
241      *             returns, no action has been performed
242     *
243     * @access public
244     */
245     public function process() {
246
247          $this->setupRedir($this->from);
248          $this->fetchEntries($this->from); 
249          if (isset($this->from['action'])) {
250               $this->action = $this->from['action'];
251               try {
252                    $performed=false;
253                    foreach ($this->actions as $k=>$v) {
254                         if ($this->from['action']==$k) {
255                              $performed = true;
256                              call_user_func($v,$this->core,$this,$this->from);
257                         }
258                    }
259                    if ($performed) {
260                         exit;
261                    }
262               } catch (Exception $e) {
263                    $this->error($e);
264               }
265          }
266     }
267
268     /**
269     * getcheckboxes -returns html code for selected entries
270      *             as a table containing entries checkboxes
271     *
272     * @access public
273      *
274     * @return string the html code for checkboxes
275     */
276     public function getCheckboxes() {
277          $ret = 
278               '<table class="posts-list"><tr>'.
279               '<th colspan="2">'.$this->cb_title.'</th>'.
280               '</tr>';
281          foreach ($this->entries as $id=>$title) {
282               $ret .= 
283                    '<tr><td>'.
284                    form::checkbox(array('entries[]'),$id,true,'','').'</td>'.
285                    '<td>'.   $title.'</td></tr>';
286          }
287          $ret .= '</table>';
288          return $ret;
289     }
290     
291     /**
292     * beginPage, endPage - displays the beginning/ending of a page, if action does not redirects dirtectly
293     *
294      * These methods are called from the actions themselves.
295      *
296     * @param string $breadcrumb breadcrumb to display
297     * @param string $head    page header to include
298      *
299     * @access public
300     */
301     abstract public function beginPage($breadcrumb='',$head='');
302     abstract public function endPage();
303
304     /**
305     * fetchEntries - fills-in information by requesting into db
306      *   this method may setup the following attributes
307      *   * entries : list of entries (checked against permissions)
308      *      entries ids are array keys, values contain entry description (if relevant)
309     *   * rs : record given by db request
310     * @access protected
311     */
312      abstract protected function fetchEntries($from);
313
314}
315
Note: See TracBrowser for help on using the repository browser.

Sites map