Dotclear

source: inc/admin/actions/class.dcaction.php @ 1807:281c64233f6d

Revision 1807:281c64233f6d, 8.1 KB checked in by Dsls, 12 years ago (diff)

Made it possible to update $_POST internally.

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

Sites map