[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 ----------------------------------------- |
---|
| 12 | if (!defined('DC_RC_PATH')) { return; } |
---|
| 13 | |
---|
[1806] | 14 | /** |
---|
| 15 | * dcActionsPage -- handler for action page on selected entries |
---|
| 16 | * |
---|
| 17 | */ |
---|
| 18 | abstract 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; |
---|
[1806] | 36 | /** @var string current action, if any */ |
---|
[1796] | 37 | protected $action; |
---|
[1807] | 38 | /** @var array list of url parameters (usually $_POST) */ |
---|
| 39 | protected $from; |
---|
[1905] | 40 | /** @var string form field name for "entries" (usually "entries") */ |
---|
| 41 | protected $field_entries; |
---|
[1796] | 42 | |
---|
[1806] | 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 | */ |
---|
[1903] | 56 | public function __construct($core,$uri,$redirect_args=array()) { |
---|
[1796] | 57 | $this->core = $core; |
---|
| 58 | $this->actions = new ArrayObject(); |
---|
| 59 | $this->combo = array(); |
---|
| 60 | $this->uri = $uri; |
---|
[1903] | 61 | $this->redir_args = $redirect_args; |
---|
[1796] | 62 | $this->redirect_fields = array(); |
---|
| 63 | $this->action = ''; |
---|
[1806] | 64 | $this->cb_title = __('Title'); |
---|
[1903] | 65 | $this->entries = array(); |
---|
[1807] | 66 | $this->from = new ArrayObject($_POST); |
---|
[1905] | 67 | $this->field_entries = 'entries'; |
---|
[1796] | 68 | } |
---|
| 69 | |
---|
[1806] | 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 | */ |
---|
[1796] | 82 | public function addAction ($actions,$callback) { |
---|
| 83 | foreach ($actions as $k => $a) { |
---|
[1806] | 84 | // Check each case of combo definition |
---|
| 85 | // Store form values in $values |
---|
[1796] | 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) { |
---|
[1905] | 93 | $values = array($a->value); |
---|
[1796] | 94 | $this->combo[$k] = $a->value; |
---|
| 95 | } else { |
---|
[1905] | 96 | $values = array($a); |
---|
[1796] | 97 | $this->combo[$k] = $a; |
---|
| 98 | } |
---|
[1806] | 99 | // Associate each potential value to the callback |
---|
[1796] | 100 | foreach ($values as $v) { |
---|
| 101 | $this->actions[$v]=$callback; |
---|
| 102 | } |
---|
| 103 | } |
---|
[1806] | 104 | return $this; |
---|
[1796] | 105 | } |
---|
| 106 | |
---|
[1806] | 107 | /** |
---|
| 108 | * getCombo - returns the actions combo, useable through form::combo |
---|
| 109 | * |
---|
| 110 | * @access public |
---|
| 111 | * |
---|
| 112 | * @return array the actions combo |
---|
| 113 | */ |
---|
[1796] | 114 | public function getCombo() { |
---|
| 115 | return $this->combo; |
---|
| 116 | } |
---|
| 117 | |
---|
[1806] | 118 | |
---|
| 119 | /** |
---|
| 120 | * getIDS() - returns the list of selected entries |
---|
| 121 | * |
---|
| 122 | * @access public |
---|
| 123 | * |
---|
| 124 | * @return array the list |
---|
| 125 | */ |
---|
[1796] | 126 | public function getIDs() { |
---|
[1806] | 127 | return array_keys($this->entries); |
---|
[1796] | 128 | } |
---|
[1806] | 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() { |
---|
[1796] | 138 | $ret = ''; |
---|
[2015] | 139 | foreach ($this->entries as $id=>$v) { |
---|
[1905] | 140 | $ret .= form::hidden($this->field_entries.'[]',$id); |
---|
[1796] | 141 | } |
---|
| 142 | return $ret; |
---|
| 143 | } |
---|
[1806] | 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 | */ |
---|
[1796] | 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 | } |
---|
[1806] | 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 | */ |
---|
[1796] | 175 | public function getRS() { |
---|
| 176 | return $this->rs; |
---|
| 177 | } |
---|
| 178 | |
---|
[1806] | 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) { |
---|
[1796] | 189 | foreach ($this->redirect_fields as $p) { |
---|
| 190 | if (isset($from[$p])) { |
---|
| 191 | $redir_args[$p] = $from[$p]; |
---|
| 192 | } |
---|
| 193 | } |
---|
| 194 | } |
---|
[1806] | 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 | */ |
---|
[1796] | 208 | public function getRedirection($params=array(),$with_selected_entries=false) { |
---|
| 209 | $redir_args = array_merge($params,$this->redir_args); |
---|
| 210 | if ($with_selected_entries) { |
---|
[1905] | 211 | $redir_args[$this->field_entries] = array_keys($this->entries); |
---|
[1796] | 212 | } |
---|
| 213 | return $this->uri.'?'.http_build_query($redir_args); |
---|
| 214 | } |
---|
[2039] | 215 | |
---|
[1806] | 216 | /** |
---|
| 217 | * redirect - redirects to redirection page |
---|
| 218 | * |
---|
| 219 | * @see getRedirection for arguments details |
---|
| 220 | * |
---|
| 221 | * @access public |
---|
| 222 | */ |
---|
[1796] | 223 | public function redirect($params=array(),$with_selected_entries=false) { |
---|
| 224 | http::redirect($this->getRedirection($params,$with_selected_entries)); |
---|
[2048] | 225 | exit; |
---|
[1796] | 226 | } |
---|
| 227 | |
---|
[1806] | 228 | /** |
---|
[2039] | 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 | /** |
---|
[1806] | 240 | * getAction - returns current action, if any |
---|
| 241 | * |
---|
| 242 | * @access public |
---|
| 243 | * |
---|
| 244 | * @return string the action |
---|
| 245 | */ |
---|
[1796] | 246 | public function getAction() { |
---|
| 247 | return $this->action; |
---|
| 248 | } |
---|
[1806] | 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 | */ |
---|
[1796] | 258 | public function process() { |
---|
[1807] | 259 | |
---|
| 260 | $this->setupRedir($this->from); |
---|
| 261 | $this->fetchEntries($this->from); |
---|
| 262 | if (isset($this->from['action'])) { |
---|
| 263 | $this->action = $this->from['action']; |
---|
[1796] | 264 | try { |
---|
| 265 | $performed=false; |
---|
| 266 | foreach ($this->actions as $k=>$v) { |
---|
[1807] | 267 | if ($this->from['action']==$k) { |
---|
[1796] | 268 | $performed = true; |
---|
[1807] | 269 | call_user_func($v,$this->core,$this,$this->from); |
---|
[1796] | 270 | } |
---|
| 271 | } |
---|
| 272 | if ($performed) { |
---|
[2048] | 273 | return true; |
---|
[1796] | 274 | } |
---|
| 275 | } catch (Exception $e) { |
---|
| 276 | $this->error($e); |
---|
| 277 | } |
---|
| 278 | } |
---|
| 279 | } |
---|
[1806] | 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>'. |
---|
[1905] | 297 | form::checkbox(array($this->field_entries.'[]'),$id,true,'','').'</td>'. |
---|
[1806] | 298 | '<td>'. $title.'</td></tr>'; |
---|
| 299 | } |
---|
| 300 | $ret .= '</table>'; |
---|
| 301 | return $ret; |
---|
| 302 | } |
---|
[1796] | 303 | |
---|
[1806] | 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 | |
---|
[1796] | 327 | } |
---|
| 328 | |
---|