Dotclear

source: inc/admin/actions/class.dcaction.php @ 2055:699a9314513f

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

Sites map