Dotclear

source: inc/admin/actions/class.dcaction.php @ 2048:a23152342139

Revision 2048:a23152342139, 8.5 KB checked in by Dsls, 12 years ago (diff)

exit is a bit rough and not really ob_start compliand, made it smoother for plugins; Tuned pages plugin so that it works ... now

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

Sites map