Dotclear

source: inc/admin/actions/class.dcaction.php @ 2072:783d0619e389

Revision 2072:783d0619e389, 9.3 KB checked in by Dsls, 12 years ago (diff)

Handled in-plugin / out-plugin action pages display (dcPage.open vs direct html), tag_posts.php now works with new actions.

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

Sites map