Dotclear

source: inc/admin/actions/class.dcaction.php @ 2243:86b0bd224843

Revision 2243:86b0bd224843, 10.0 KB checked in by Dsls, 12 years ago (diff)

Compatibility for actionspage : handle redir if set, avoid plugins bugs when specifying "%" in URLs by disbling entry selection persistence.

RevLine 
[1796]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
[1806]14/**
15* dcActionsPage -- handler for action page on selected entries
16*
17*/
18abstract class dcActionsPage
[1796]19{
[1806]20     /** @var string form submit uri */
[1796]21     protected $uri;
[1806]22     /** @var dcCore dotclear core instance */
[1796]23     protected $core;
[1806]24     /** @var array action combo box */
[1796]25     protected $combo;
[1806]26     /** @var array list of defined actions (callbacks) */
[1796]27     protected $actions;
[1806]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 */
[1796]31     protected $rs;
[1806]32     /** @var array redirection $_GET arguments, if any (does not contain ids by default, ids may be merged to it) */
[1796]33     protected $redir_args;
[1806]34     /** @var array list of $_POST fields used to build the redirection  */
[1796]35     protected $redirect_fields;
[2063]36     /** @var string redirection anchor if any  */
37     protected $redir_anchor;
38
[1806]39     /** @var string current action, if any */
[1796]40     protected $action;
[1807]41     /** @var array list of url parameters (usually $_POST) */
42     protected $from;
[1905]43     /** @var string form field name for "entries" (usually "entries") */
44     protected $field_entries;
[1796]45     
[1806]46     /** @var string title for checkboxes list, if displayed */
47     protected $cb_title;
48     
[2055]49     /** @var string title for caller page title */
50     protected $caller_title;
[2072]51
52     /** @var boolean true if we are acting inside a plugin (different handling of begin/endpage) */
53     protected $in_plugin;   
[2243]54     
55     /** @var boolean true if we enable to keep selection when redirecting */
56     protected $enable_redir_selection; 
[2072]57
[1806]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     */
[1903]68     public function __construct($core,$uri,$redirect_args=array()) {
[1796]69          $this->core = $core;
70          $this->actions = new ArrayObject();
71          $this->combo = array();
72          $this->uri = $uri;
[1903]73          $this->redir_args = $redirect_args;
[1796]74          $this->redirect_fields = array();
75          $this->action = '';
[1806]76          $this->cb_title = __('Title');
[1903]77          $this->entries = array();
[1807]78          $this->from = new ArrayObject($_POST);
[1905]79          $this->field_entries = 'entries';
[2055]80          $this->caller_title = __('Entries');
[2063]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          }
[2072]87          $u=explode('?',$_SERVER['REQUEST_URI']);
88          $this->in_plugin = (strpos($u[0],'plugin.php') !== false);
[2243]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;
[1796]102     }
103     
[1806]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     */
[1796]116     public function addAction ($actions,$callback) {
117          foreach ($actions as $k => $a) {
[1806]118               // Check each case of combo definition
119               // Store form values in $values
[1796]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) {
[1905]127                    $values = array($a->value);
[1796]128                    $this->combo[$k] = $a->value;
129               } else {
[1905]130                    $values = array($a);
[1796]131                    $this->combo[$k] = $a;
132               }
[1806]133               // Associate each potential value to the callback
[1796]134               foreach ($values as $v) {
135                    $this->actions[$v]=$callback;
136               }
137          }
[1806]138          return $this;
[1796]139     }
140     
[1806]141    /**
142     * getCombo - returns the actions combo, useable through form::combo
143     *
144     * @access public
145      *
146     * @return array the actions combo
147     */
[1796]148     public function getCombo() {
149          return $this->combo;
150     }
151     
[1806]152     
153    /**
154     * getIDS() - returns the list of selected entries
155     *
156     * @access public
157      *
158     * @return array the list
159     */
[1796]160     public function getIDs() {
[1806]161          return array_keys($this->entries);
[1796]162     }
[1806]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() {
[1796]172          $ret = '';
[2015]173          foreach  ($this->entries as $id=>$v) {
[1905]174               $ret .= form::hidden($this->field_entries.'[]',$id);
[1796]175          }
176          return $ret;
177     }
[1806]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     */   
[1796]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     }
[1806]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     */
[1796]209     public function getRS() {
210          return $this->rs;
211     }
212     
[1806]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) {
[1796]223          foreach ($this->redirect_fields as $p) {
224               if (isset($from[$p])) {
225                    $redir_args[$p] = $from[$p];
226               }
227          }
228     }
[1806]229
[2055]230
[1806]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     */
[2169]243     public function getRedirection($with_selected_entries=false,$params=array()) {
[1796]244          $redir_args = array_merge($params,$this->redir_args);
[2243]245          if (isset($redir_args['redir'])) {
246               unset($redir_args['redir']);
247          }
248         
249          if ($with_selected_entries && $this->enable_redir_selection) {
[1905]250               $redir_args[$this->field_entries] = array_keys($this->entries);
[1796]251          }
[2063]252          return $this->uri.'?'.http_build_query($redir_args).$this->redir_anchor;
[1796]253     }
[2039]254     
[1806]255     /**
256     * redirect - redirects to redirection page
257     *
258      * @see getRedirection for arguments details
259      *
260     * @access public
261     */
[2169]262     public function redirect($with_selected_entries=false,$params=array()) {
263          http::redirect($this->getRedirection($with_selected_entries,$params));
[2048]264          exit;
[1796]265     }   
266     
[1806]267     /**
[2039]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     /**
[2055]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     /**
[1806]290     * getAction - returns current action, if any
291     *
292     * @access public
293      *
294     * @return string the action
295     */
[1796]296     public function getAction() {
297          return $this->action;
298     }
[1806]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     */
[1796]308     public function process() {
[1807]309
310          $this->setupRedir($this->from);
311          $this->fetchEntries($this->from); 
312          if (isset($this->from['action'])) {
313               $this->action = $this->from['action'];
[1796]314               try {
315                    $performed=false;
316                    foreach ($this->actions as $k=>$v) {
[1807]317                         if ($this->from['action']==$k) {
[1796]318                              $performed = true;
[1807]319                              call_user_func($v,$this->core,$this,$this->from);
[1796]320                         }
321                    }
322                    if ($performed) {
[2048]323                         return true;
[1796]324                    }
325               } catch (Exception $e) {
326                    $this->error($e);
[2055]327                    return true;
[1796]328               }
329          }
330     }
[1806]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>'.
[1905]348                    form::checkbox(array($this->field_entries.'[]'),$id,true,'','').'</td>'.
[1806]349                    '<td>'.   $title.'</td></tr>';
350          }
351          $ret .= '</table>';
352          return $ret;
353     }
[1796]354     
[1806]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
[1796]378}
379
Note: See TracBrowser for help on using the repository browser.

Sites map