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