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