| 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 | |
|---|
| 13 | if (!defined('DC_RC_PATH')) { return; } |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | /** |
|---|
| 17 | * dcFilterSet -- filter handling object |
|---|
| 18 | * |
|---|
| 19 | * @uses dcForm |
|---|
| 20 | * |
|---|
| 21 | */ |
|---|
| 22 | class dcFilterSet extends dcForm { |
|---|
| 23 | /** @var array list of variable filters */ |
|---|
| 24 | protected $filters; |
|---|
| 25 | /** @var array list of static filters */ |
|---|
| 26 | protected $static_filters; |
|---|
| 27 | /** @var array list of all filters (union of the 2 previous props) */ |
|---|
| 28 | protected $all_filters; |
|---|
| 29 | /** @var string prefix to be used for all fields */ |
|---|
| 30 | protected $form_prefix; |
|---|
| 31 | /** @var string action to perform upon form submission */ |
|---|
| 32 | protected $action; |
|---|
| 33 | /** @var boolean start form display hidden by default or not */ |
|---|
| 34 | protected $hide_filterset; |
|---|
| 35 | /** @var string filterset name */ |
|---|
| 36 | protected $name; |
|---|
| 37 | /** @var dcCore dotclear core object */ |
|---|
| 38 | protected $core; |
|---|
| 39 | /** @var boolean true if content is filtered */ |
|---|
| 40 | protected $filtered; |
|---|
| 41 | |
|---|
| 42 | /** |
|---|
| 43 | * __init__ - class static initialiser (called at the very bottom of this |
|---|
| 44 | * page) |
|---|
| 45 | * |
|---|
| 46 | * @param mixed $env the twig environment. |
|---|
| 47 | * |
|---|
| 48 | * @access public |
|---|
| 49 | * @static |
|---|
| 50 | * |
|---|
| 51 | */ |
|---|
| 52 | public static function __init__($env) { |
|---|
| 53 | // filterset widgets are defined in a separate file |
|---|
| 54 | $env->getExtension('dc_form')->addTemplate( |
|---|
| 55 | '@forms/formfilter_layout.html.twig' |
|---|
| 56 | ); |
|---|
| 57 | |
|---|
| 58 | $env->addFunction( |
|---|
| 59 | new Twig_SimpleFunction( |
|---|
| 60 | 'filterset', |
|---|
| 61 | 'dcFilterSet::renderFilterSet', |
|---|
| 62 | array( |
|---|
| 63 | 'is_safe' => array('html'), |
|---|
| 64 | 'needs_context' => true, |
|---|
| 65 | 'needs_environment' => true |
|---|
| 66 | ))); |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | /** |
|---|
| 70 | * __construct -- constructor |
|---|
| 71 | * |
|---|
| 72 | * @param dcCore $core dotclear core instance. |
|---|
| 73 | * @param string $name filterset name. |
|---|
| 74 | * @param string $action form action. |
|---|
| 75 | * @param string $form_prefix form prefix. |
|---|
| 76 | * |
|---|
| 77 | * @access public |
|---|
| 78 | * |
|---|
| 79 | * @return mixed Value. |
|---|
| 80 | */ |
|---|
| 81 | public function __construct($core,$name,$action,$form_prefix="f_"){ |
|---|
| 82 | $this->form_prefix=$form_prefix; |
|---|
| 83 | $this->filters = new ArrayObject(); |
|---|
| 84 | $this->static_filters = new ArrayObject(); |
|---|
| 85 | $this->all_filters = new ArrayObject(); |
|---|
| 86 | $this->action = $action; |
|---|
| 87 | $this->filtered = false; |
|---|
| 88 | parent::__construct($core,$name,$action,'POST'); |
|---|
| 89 | //$this->id = "filters"; |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | |
|---|
| 93 | /** |
|---|
| 94 | * renderFilterSet -- binding to twig function "filterset" |
|---|
| 95 | * renders a filterset given its name & context |
|---|
| 96 | * @param mixed $env Twig environment (passed by Twig template). |
|---|
| 97 | * @param mixed $context Context (passed by Twig template). |
|---|
| 98 | * @param mixed $name filterset name. |
|---|
| 99 | * @param array $attributes filterset attributes. |
|---|
| 100 | * |
|---|
| 101 | * @access public |
|---|
| 102 | * @static |
|---|
| 103 | * |
|---|
| 104 | * @return mixed Value. |
|---|
| 105 | */ |
|---|
| 106 | public static function renderFilterSet($env,$context,$name, |
|---|
| 107 | $attributes=array()) { |
|---|
| 108 | $context['filtersetname']=$name; |
|---|
| 109 | echo $env->getExtension('dc_form')->renderWidget( |
|---|
| 110 | 'filterset', |
|---|
| 111 | $context |
|---|
| 112 | ); |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | |
|---|
| 116 | /** |
|---|
| 117 | * setup - sets up the form filter from http context |
|---|
| 118 | * |
|---|
| 119 | * @access public |
|---|
| 120 | * |
|---|
| 121 | * @return mixed Value. |
|---|
| 122 | */ |
|---|
| 123 | public function setup() { |
|---|
| 124 | $form_combo = array(); |
|---|
| 125 | $form_combo['-'] = ''; |
|---|
| 126 | foreach ($this->all_filters as $filter) { |
|---|
| 127 | $filter->init(); |
|---|
| 128 | } |
|---|
| 129 | foreach ($this->filters as $filter) { |
|---|
| 130 | $form_combo[$filter->id]=$filter->name; |
|---|
| 131 | } |
|---|
| 132 | $p = $this->form_prefix; |
|---|
| 133 | $this |
|---|
| 134 | ->addField ( |
|---|
| 135 | new dcFieldCombo ($p.'add_filter','',$form_combo, |
|---|
| 136 | array())) |
|---|
| 137 | ->addField ( |
|---|
| 138 | new dcFieldSubmit($p.'add',__('Add this filter'), |
|---|
| 139 | array())) |
|---|
| 140 | ->addField ( |
|---|
| 141 | new dcFieldSubmit($p.'clear_filters',__('Delete all filters'), |
|---|
| 142 | array())) |
|---|
| 143 | ->addField ( |
|---|
| 144 | new dcFieldSubmit($p.'apply', |
|---|
| 145 | __('Apply filters and display options'), |
|---|
| 146 | array())) |
|---|
| 147 | ->addField ( |
|---|
| 148 | new dcFieldSubmit($p.'reset',__('Reset'), |
|---|
| 149 | array())) |
|---|
| 150 | ; |
|---|
| 151 | $this->setupFields(); |
|---|
| 152 | /* Since we have specific handling for actions |
|---|
| 153 | (for instance "del_*" fields), we do not take advantage of |
|---|
| 154 | submitfields features, actions are performed manually here. |
|---|
| 155 | |
|---|
| 156 | Use cases : |
|---|
| 157 | (1) $_POST not empty for formfilter fields : |
|---|
| 158 | * filters are set from $_POST |
|---|
| 159 | * applied filters values are set from $_GET |
|---|
| 160 | * keep filters div shown |
|---|
| 161 | (2) $_POST empty : |
|---|
| 162 | * both filters fields & applied values are set from $_GET |
|---|
| 163 | * hide filter div |
|---|
| 164 | */ |
|---|
| 165 | $action = false; |
|---|
| 166 | //$allowed_actions = array('clear_filters','add','del_.*','apply','reset'); |
|---|
| 167 | $allowed_actions = '#^(clear_filters|add|del_.*|apply|reset)$#'; |
|---|
| 168 | |
|---|
| 169 | // Fetch each $_POST parameter to see whether filters are concerned. |
|---|
| 170 | // Only 1 action at a time is allowed. |
|---|
| 171 | foreach ($_POST as $k => $v) { |
|---|
| 172 | if (strpos($k,$this->form_prefix)===0) { |
|---|
| 173 | $tmp = substr($k,strlen($this->form_prefix)); |
|---|
| 174 | $count = preg_match($allowed_actions,$tmp,$match); |
|---|
| 175 | if ($count==1) { |
|---|
| 176 | $action = $match[1]; |
|---|
| 177 | break; |
|---|
| 178 | } |
|---|
| 179 | } |
|---|
| 180 | } |
|---|
| 181 | $this->hide_filterset = true; |
|---|
| 182 | if ($action !== false) { |
|---|
| 183 | // Use case (1) |
|---|
| 184 | if ($action != 'clear_filters' && $action != 'reset') { |
|---|
| 185 | // initialize fields from $_POST |
|---|
| 186 | $this->setupEditFilters($this->all_filters,$_POST); |
|---|
| 187 | if ($action == 'add'){ |
|---|
| 188 | // Add a new filter |
|---|
| 189 | $fname = $p.'add_filter'; |
|---|
| 190 | if (isset($_POST[$fname]) |
|---|
| 191 | && isset($this->filters[$_POST[$fname]])) { |
|---|
| 192 | $this->filters[$_POST[$fname]]->add(); |
|---|
| 193 | } |
|---|
| 194 | $this->hide_filterset = false; |
|---|
| 195 | } elseif (strpos($action,'del_') === 0) { |
|---|
| 196 | // Remove a filter |
|---|
| 197 | $count = preg_match('#del_(.+)_([0-9]+)#',$action,$match); |
|---|
| 198 | if (($count == 1) && isset($this->filters[$match[1]])) { |
|---|
| 199 | $this->filters[$match[1]]->remove($match[2]); |
|---|
| 200 | } |
|---|
| 201 | $this->hide_filterset = false; |
|---|
| 202 | } elseif ($action=="apply") { |
|---|
| 203 | // Apply all filters |
|---|
| 204 | // ==> store filter to preferences and redirect to |
|---|
| 205 | // page with filter as $_GET attributes |
|---|
| 206 | $data = $this->saveFilters(); |
|---|
| 207 | $query = http_build_query($data,'','&'); |
|---|
| 208 | if ($query != '') { |
|---|
| 209 | $query = (strpos($this->action,'?') === false ? '?' : '&').$query; |
|---|
| 210 | } |
|---|
| 211 | http::redirect($this->action.$query); |
|---|
| 212 | exit; |
|---|
| 213 | } |
|---|
| 214 | } |
|---|
| 215 | // Form as been submitted with POST method, retrieve |
|---|
| 216 | // applied filter values from "query" field |
|---|
| 217 | if (isset($_POST[$p."query"])) { |
|---|
| 218 | parse_str($_POST[$p."query"],$out); |
|---|
| 219 | $this->setupAppliedValues($this->all_filters,$out); |
|---|
| 220 | if ($action == 'reset') { |
|---|
| 221 | // RESET action => set fields from query field |
|---|
| 222 | $this->setupEditFilters($this->all_filters,$out); |
|---|
| 223 | } elseif ($action == 'clear_filters') { |
|---|
| 224 | $this->setupEditFilters($this->static_filters,$out); |
|---|
| 225 | foreach ($this->filters as $f) { |
|---|
| 226 | $f->cleanup(); |
|---|
| 227 | } |
|---|
| 228 | } |
|---|
| 229 | } |
|---|
| 230 | } else { |
|---|
| 231 | // Use case (2) : GET method; set both filters and applied values |
|---|
| 232 | // from GET args or from settings if no GET args. |
|---|
| 233 | $load_from_settings = true; |
|---|
| 234 | foreach($_GET as $k=>$v) { |
|---|
| 235 | if (strpos($k,$this->form_prefix)===0) { |
|---|
| 236 | $load_from_settings=false; |
|---|
| 237 | break; |
|---|
| 238 | } |
|---|
| 239 | } |
|---|
| 240 | $get = $_GET; |
|---|
| 241 | if ($load_from_settings) { |
|---|
| 242 | $get = array_merge($this->loadFilters(),$get); |
|---|
| 243 | } |
|---|
| 244 | $this->setupEditFilters($this->all_filters,$get); |
|---|
| 245 | |
|---|
| 246 | $this->setupAppliedValues($this->all_filters,$get); |
|---|
| 247 | } |
|---|
| 248 | foreach ($this->static_filters as $f) { |
|---|
| 249 | if (!$f->isEnabled()) { |
|---|
| 250 | $f->add(); |
|---|
| 251 | } |
|---|
| 252 | } |
|---|
| 253 | $queryParams = $this->getAppliedFilterValues(); |
|---|
| 254 | $this->addField( |
|---|
| 255 | new dcFieldHidden($this->form_prefix.'query', |
|---|
| 256 | http_build_query($queryParams))); |
|---|
| 257 | // form context is set through a global twig variable |
|---|
| 258 | $this->core->tpl->addGlobal( |
|---|
| 259 | 'filterset_'.$this->name, |
|---|
| 260 | $this->getContext()); |
|---|
| 261 | } |
|---|
| 262 | |
|---|
| 263 | public function getURLParams() { |
|---|
| 264 | return $this->getAppliedFilterValues()->getArrayCopy(); |
|---|
| 265 | } |
|---|
| 266 | |
|---|
| 267 | /** |
|---|
| 268 | * saveFilters - save user defined filters into preferences |
|---|
| 269 | * |
|---|
| 270 | * @access protected |
|---|
| 271 | */ |
|---|
| 272 | protected function saveFilters() { |
|---|
| 273 | $ser = array(); |
|---|
| 274 | $ws = $GLOBALS['core']->auth->user_prefs->addWorkspace('filters'); |
|---|
| 275 | $data= $this->serialize(); |
|---|
| 276 | $ws->put($this->name,serialize($data->getArrayCopy()),'string'); |
|---|
| 277 | return $data; |
|---|
| 278 | } |
|---|
| 279 | |
|---|
| 280 | /** |
|---|
| 281 | * loadFilters - load user filters from preferences |
|---|
| 282 | * |
|---|
| 283 | * @access protected |
|---|
| 284 | * |
|---|
| 285 | * @return mixed Value. |
|---|
| 286 | */ |
|---|
| 287 | protected function loadFilters() { |
|---|
| 288 | $ws = $GLOBALS['core']->auth->user_prefs->addWorkspace('filters'); |
|---|
| 289 | $data = (!is_null($ws->{$this->name})) |
|---|
| 290 | ? unserialize($ws->{$this->name}) |
|---|
| 291 | : array(); |
|---|
| 292 | if (is_array($data)) |
|---|
| 293 | return $data; |
|---|
| 294 | else |
|---|
| 295 | return array(); |
|---|
| 296 | } |
|---|
| 297 | |
|---|
| 298 | /** |
|---|
| 299 | * setupEditFilters - Updates filters fields according to form_data |
|---|
| 300 | * To be called before any call to display() or getForm() |
|---|
| 301 | * |
|---|
| 302 | * @param array $filters list of filters to update |
|---|
| 303 | * @param array $form_data form values (usually $_GET or $_POST) |
|---|
| 304 | * |
|---|
| 305 | * @access protected |
|---|
| 306 | * |
|---|
| 307 | * @return mixed Value. |
|---|
| 308 | */ |
|---|
| 309 | protected function setupEditFilters ($filters,$form_data) { |
|---|
| 310 | foreach ($filters as $filter) { |
|---|
| 311 | $filter->setupFields ($form_data); |
|---|
| 312 | } |
|---|
| 313 | } |
|---|
| 314 | |
|---|
| 315 | /** |
|---|
| 316 | * setupAppliedValues - Updates filters applied values according to |
|---|
| 317 | * form_data |
|---|
| 318 | * |
|---|
| 319 | * @param array $filters list of filters to update |
|---|
| 320 | * @param array $form_data form values (usually $_GET or $_POST) |
|---|
| 321 | * |
|---|
| 322 | * @access protected |
|---|
| 323 | * |
|---|
| 324 | * @return mixed Value. |
|---|
| 325 | */ |
|---|
| 326 | protected function setupAppliedValues ($filters,$form_data) { |
|---|
| 327 | foreach ($filters as $filter) { |
|---|
| 328 | $filter->setupAppliedValue ($form_data); |
|---|
| 329 | } |
|---|
| 330 | } |
|---|
| 331 | |
|---|
| 332 | /** |
|---|
| 333 | * serialize - retrieves filter applied values in a serialized form |
|---|
| 334 | * |
|---|
| 335 | * @access protected |
|---|
| 336 | * |
|---|
| 337 | * @return ArrayObject serialized data. |
|---|
| 338 | */ |
|---|
| 339 | protected function serialize() { |
|---|
| 340 | $arr = new ArrayObject(); |
|---|
| 341 | foreach ($this->filters as $f) { |
|---|
| 342 | if ($f->isEnabled()) { |
|---|
| 343 | $f->serialize($arr); |
|---|
| 344 | } |
|---|
| 345 | } |
|---|
| 346 | foreach ($this->static_filters as $f) { |
|---|
| 347 | $f->serialize($arr); |
|---|
| 348 | } |
|---|
| 349 | return $arr; |
|---|
| 350 | } |
|---|
| 351 | |
|---|
| 352 | /** |
|---|
| 353 | * addFilter - registers a new filter in filterset |
|---|
| 354 | * |
|---|
| 355 | * @param mixed \dcFilter the filter. |
|---|
| 356 | * |
|---|
| 357 | * @access public |
|---|
| 358 | * |
|---|
| 359 | * @return dcFilterSet the filterset (enabling to chain addFilter). |
|---|
| 360 | */ |
|---|
| 361 | public function addFilter (dcFilter $filter) { |
|---|
| 362 | $filter->setFormPrefix($this->form_prefix); |
|---|
| 363 | $filter->setFilterSet($this); |
|---|
| 364 | $this->all_filters[$filter->id] = $filter; |
|---|
| 365 | if ($filter->isStatic()) { |
|---|
| 366 | $this->static_filters[$filter->id] = $filter; |
|---|
| 367 | } else { |
|---|
| 368 | $this->filters[$filter->id] = $filter; |
|---|
| 369 | } |
|---|
| 370 | return $this; |
|---|
| 371 | } |
|---|
| 372 | |
|---|
| 373 | /** |
|---|
| 374 | * getContext - retrieves current filterset context |
|---|
| 375 | * (to be given to twig template) |
|---|
| 376 | * |
|---|
| 377 | * @access public |
|---|
| 378 | * |
|---|
| 379 | * @return array the context |
|---|
| 380 | */ |
|---|
| 381 | public function getContext() { |
|---|
| 382 | $fcontext = new ArrayObject(); |
|---|
| 383 | $sfcontext = new ArrayObject(); |
|---|
| 384 | foreach ($this->filters as $f) { |
|---|
| 385 | if($f->isEnabled()) { |
|---|
| 386 | $f->appendFilterContext($fcontext); |
|---|
| 387 | } |
|---|
| 388 | } |
|---|
| 389 | foreach ($this->static_filters as $f) { |
|---|
| 390 | $f->appendFilterContext($sfcontext); |
|---|
| 391 | } |
|---|
| 392 | return array( |
|---|
| 393 | 'active_filters' => $fcontext, |
|---|
| 394 | 'static_filters' => $sfcontext, |
|---|
| 395 | 'hide_filters' => $this->hide_filterset, |
|---|
| 396 | 'prefix' => $this->form_prefix); |
|---|
| 397 | } |
|---|
| 398 | |
|---|
| 399 | /** |
|---|
| 400 | * getAppliedFilterValues - retrieves the list of applied filter values |
|---|
| 401 | * |
|---|
| 402 | * @access protected |
|---|
| 403 | * |
|---|
| 404 | * @return ArrayObject the list of applied values |
|---|
| 405 | */ |
|---|
| 406 | protected function getAppliedFilterValues() { |
|---|
| 407 | $arr = new ArrayObject(); |
|---|
| 408 | foreach ($this->all_filters as $f) { |
|---|
| 409 | if ($f->isApplied()) |
|---|
| 410 | $f->updateAppliedValues($arr); |
|---|
| 411 | } |
|---|
| 412 | return $arr; |
|---|
| 413 | } |
|---|
| 414 | |
|---|
| 415 | /** |
|---|
| 416 | * applyFilters -- applies filterset |
|---|
| 417 | * |
|---|
| 418 | * @param mixed $params the parameters to update. |
|---|
| 419 | * |
|---|
| 420 | * @access public |
|---|
| 421 | * |
|---|
| 422 | * @return mixed true if at least 1 filter has been applied, false otherwise |
|---|
| 423 | */ |
|---|
| 424 | public function applyFilters($params) { |
|---|
| 425 | foreach ($this->all_filters as $filter) { |
|---|
| 426 | if ($filter->isApplied()) { |
|---|
| 427 | $filter->applyFilter($params); |
|---|
| 428 | $this->filtered = true; |
|---|
| 429 | } |
|---|
| 430 | } |
|---|
| 431 | return $this->filtered; |
|---|
| 432 | } |
|---|
| 433 | |
|---|
| 434 | /** |
|---|
| 435 | * buildFieldName -- builds a field name given a verb, an id and a position |
|---|
| 436 | * takes the form prefix into consideration |
|---|
| 437 | * @param mixed $verb the verb to use (ex : "del") |
|---|
| 438 | * @param mixed $field_id the field id |
|---|
| 439 | * @param mixed $pos the field position |
|---|
| 440 | * |
|---|
| 441 | * @access public |
|---|
| 442 | * |
|---|
| 443 | * @return mixed the field name |
|---|
| 444 | */ |
|---|
| 445 | public function buildFieldName($verb,$field_id,$pos) { |
|---|
| 446 | return $this->form_prefix.$verb.'_'.$field_id.'_'.$pos; |
|---|
| 447 | } |
|---|
| 448 | |
|---|
| 449 | } |
|---|
| 450 | |
|---|
| 451 | |
|---|
| 452 | /** |
|---|
| 453 | * dcFilter -- base filter class |
|---|
| 454 | * |
|---|
| 455 | * A filter can be edited while being applied with other values |
|---|
| 456 | * that enables to keep the list of applied filters (to display a list of items) |
|---|
| 457 | * while modifing the filter itself. |
|---|
| 458 | * Therefore it contains : |
|---|
| 459 | * * a Field that tracks the currently edited filter |
|---|
| 460 | * * an applied values that tracks the currently applied filter |
|---|
| 461 | * |
|---|
| 462 | */ |
|---|
| 463 | abstract class dcFilter { |
|---|
| 464 | /** @var dcFilterSet filterset parent */ |
|---|
| 465 | public $filterset; |
|---|
| 466 | /** @var string filter id */ |
|---|
| 467 | public $id; |
|---|
| 468 | /** @var string filter name */ |
|---|
| 469 | public $name; |
|---|
| 470 | /** @var string filter description */ |
|---|
| 471 | public $desc; |
|---|
| 472 | /** @var string filter id (including form prefix) */ |
|---|
| 473 | public $filter_id; |
|---|
| 474 | /** @var string resulting parameter array key */ |
|---|
| 475 | protected $request_param; |
|---|
| 476 | /** @var dcField edited field */ |
|---|
| 477 | protected $field; |
|---|
| 478 | /** @var array currently applied values */ |
|---|
| 479 | protected $avalues; |
|---|
| 480 | /** @var boolean true if field is static */ |
|---|
| 481 | protected $static; |
|---|
| 482 | /** @var array generic field options */ |
|---|
| 483 | protected $options; |
|---|
| 484 | /** @var boolean true if field can have multiple values */ |
|---|
| 485 | protected $multiple; |
|---|
| 486 | |
|---|
| 487 | /** |
|---|
| 488 | * __construct -- filter constructor |
|---|
| 489 | * |
|---|
| 490 | * @param mixed $id filter id. |
|---|
| 491 | * @param mixed $name filter name. |
|---|
| 492 | * @param mixed $desc filter description. |
|---|
| 493 | * @param mixed $request_param request parameter (see dcBlog::getPosts for |
|---|
| 494 | * instance). |
|---|
| 495 | * @param array $options filter options. |
|---|
| 496 | * |
|---|
| 497 | * @access public |
|---|
| 498 | * |
|---|
| 499 | * @return mixed Value. |
|---|
| 500 | */ |
|---|
| 501 | public function __construct ($id,$name,$desc,$request_param,$options=array()) { |
|---|
| 502 | $this->id = $id; |
|---|
| 503 | $this->name =$name; |
|---|
| 504 | $this->desc = $desc; |
|---|
| 505 | $this->request_param = $request_param; |
|---|
| 506 | $this->avalues = array(); |
|---|
| 507 | $this->filter_id = $this->id; |
|---|
| 508 | $this->static = false; |
|---|
| 509 | $this->multiple = false; |
|---|
| 510 | $this->options = $options; |
|---|
| 511 | if (isset($options['static']) && $options['static']) { |
|---|
| 512 | $this->static = true; |
|---|
| 513 | } |
|---|
| 514 | if (isset($options['multiple']) && $options['multiple']) { |
|---|
| 515 | $this->multiple = true; |
|---|
| 516 | } |
|---|
| 517 | } |
|---|
| 518 | |
|---|
| 519 | |
|---|
| 520 | /** |
|---|
| 521 | * parseData - Extract values from data (data being an array, such as $_GET |
|---|
| 522 | * or $_POST) ; does not update any field, only return parsed |
|---|
| 523 | * data |
|---|
| 524 | * |
|---|
| 525 | * @param mixed $data input data. |
|---|
| 526 | * |
|---|
| 527 | * @access protected |
|---|
| 528 | * |
|---|
| 529 | * @return array an array containing parsed data. |
|---|
| 530 | */ |
|---|
| 531 | protected function parseData($data) { |
|---|
| 532 | $arr = $this->field->parseValues($data); |
|---|
| 533 | return array('values' => $arr); |
|---|
| 534 | } |
|---|
| 535 | |
|---|
| 536 | /** |
|---|
| 537 | * isStatic -- returns whether the filter is static or not |
|---|
| 538 | * |
|---|
| 539 | * @access public |
|---|
| 540 | * |
|---|
| 541 | * @return boolean true if the filter is static. |
|---|
| 542 | */ |
|---|
| 543 | public function isStatic() { |
|---|
| 544 | return $this->static; |
|---|
| 545 | } |
|---|
| 546 | |
|---|
| 547 | /** |
|---|
| 548 | * setupFields -- sets up filter fielt from $_GET or $_POST |
|---|
| 549 | * |
|---|
| 550 | * @param mixed $data input data (either $_GET or $_POST). |
|---|
| 551 | * |
|---|
| 552 | * @access public |
|---|
| 553 | * |
|---|
| 554 | * @return mixed Value. |
|---|
| 555 | */ |
|---|
| 556 | public function setupFields($data) { |
|---|
| 557 | $this->field->setup($data); |
|---|
| 558 | } |
|---|
| 559 | |
|---|
| 560 | |
|---|
| 561 | /** |
|---|
| 562 | * init -- initializes filter (called after setup) |
|---|
| 563 | * |
|---|
| 564 | * @access public |
|---|
| 565 | * |
|---|
| 566 | * @return mixed Value. |
|---|
| 567 | */ |
|---|
| 568 | public function init() { |
|---|
| 569 | } |
|---|
| 570 | |
|---|
| 571 | /** |
|---|
| 572 | * cleanup - resets filter field to its default value |
|---|
| 573 | * |
|---|
| 574 | * @access public |
|---|
| 575 | * |
|---|
| 576 | * @return mixed Value. |
|---|
| 577 | */ |
|---|
| 578 | public function cleanup() { |
|---|
| 579 | $this->field->setValues(array()); |
|---|
| 580 | } |
|---|
| 581 | |
|---|
| 582 | /** |
|---|
| 583 | * setupAppliedValue -- defines field applied values from data |
|---|
| 584 | * |
|---|
| 585 | * @param mixed $data data to retrieve values from ($_GET or $_POST). |
|---|
| 586 | * |
|---|
| 587 | * @access public |
|---|
| 588 | * |
|---|
| 589 | * @return mixed Value. |
|---|
| 590 | */ |
|---|
| 591 | public function setupAppliedValue($data) { |
|---|
| 592 | $this->avalues = $this->parseData($data); |
|---|
| 593 | } |
|---|
| 594 | |
|---|
| 595 | public function updateAppliedValues($arr) { |
|---|
| 596 | $arr[$this->filter_id] = $this->avalues['values']; |
|---|
| 597 | } |
|---|
| 598 | |
|---|
| 599 | /** |
|---|
| 600 | * setFilterSet -- sets filterSet for filter |
|---|
| 601 | * |
|---|
| 602 | * @param mixed \dcFilterset Description. |
|---|
| 603 | * |
|---|
| 604 | * @access public |
|---|
| 605 | * |
|---|
| 606 | */ |
|---|
| 607 | public function setFilterSet(dcFilterset $fs) { |
|---|
| 608 | $this->filterset = $fs; |
|---|
| 609 | } |
|---|
| 610 | |
|---|
| 611 | /** |
|---|
| 612 | * setFormPrefix -- sets filter form prefix |
|---|
| 613 | * |
|---|
| 614 | * @param string $prefix the form prefix. |
|---|
| 615 | * |
|---|
| 616 | * @access public |
|---|
| 617 | * |
|---|
| 618 | * @return mixed Value. |
|---|
| 619 | */ |
|---|
| 620 | public function setFormPrefix($prefix) { |
|---|
| 621 | $this->filter_id = $prefix.$this->id; |
|---|
| 622 | } |
|---|
| 623 | |
|---|
| 624 | /** |
|---|
| 625 | * isEnabled -- Tells whether the filter is enabled or not (ie field has |
|---|
| 626 | * at least 1 value defined) |
|---|
| 627 | * |
|---|
| 628 | * @access public |
|---|
| 629 | * |
|---|
| 630 | * @return mixed true if the filter is enabled. |
|---|
| 631 | */ |
|---|
| 632 | public function isEnabled() { |
|---|
| 633 | return count($this->field) != 0; |
|---|
| 634 | } |
|---|
| 635 | |
|---|
| 636 | protected abstract function addValue($value=NULL); |
|---|
| 637 | |
|---|
| 638 | /** |
|---|
| 639 | * add -- adds a value for the filter |
|---|
| 640 | * |
|---|
| 641 | * @access public |
|---|
| 642 | * |
|---|
| 643 | * @return mixed Value. |
|---|
| 644 | */ |
|---|
| 645 | public function add() { |
|---|
| 646 | if (count($this->field) > 1 && !$this->multiple) |
|---|
| 647 | return; |
|---|
| 648 | $this->addValue(); |
|---|
| 649 | } |
|---|
| 650 | |
|---|
| 651 | /** |
|---|
| 652 | * remove -- Removes a value from filter |
|---|
| 653 | * |
|---|
| 654 | * @param mixed $pos value position. |
|---|
| 655 | * |
|---|
| 656 | * @access public |
|---|
| 657 | * |
|---|
| 658 | * @return mixed Value. |
|---|
| 659 | */ |
|---|
| 660 | public function remove($pos) { |
|---|
| 661 | $values = $this->field->getValues(); |
|---|
| 662 | if (isset($values[$pos])) { |
|---|
| 663 | $this->field->delValue($pos); |
|---|
| 664 | } |
|---|
| 665 | |
|---|
| 666 | } |
|---|
| 667 | |
|---|
| 668 | abstract protected function appendContextLine($ctx,$pos); |
|---|
| 669 | |
|---|
| 670 | /** |
|---|
| 671 | * appendFilterContext -- appends current filter context to the given |
|---|
| 672 | * context. |
|---|
| 673 | * A filter context consists in a list of array elements, one for |
|---|
| 674 | * each field line displayed. |
|---|
| 675 | * If a field has multiple values, there will be as many lines as values |
|---|
| 676 | * |
|---|
| 677 | * The twig template then iterates through the array to display |
|---|
| 678 | * each and every line |
|---|
| 679 | * @param mixed $ctx the context to enrich |
|---|
| 680 | * |
|---|
| 681 | * @access public |
|---|
| 682 | * |
|---|
| 683 | * @return mixed Value. |
|---|
| 684 | */ |
|---|
| 685 | public function appendFilterContext($ctx) { |
|---|
| 686 | foreach ($this->field->getValues() as $cur => $f) { |
|---|
| 687 | /* |
|---|
| 688 | * each line of context has the following properties : |
|---|
| 689 | * * lineclass : <tr> class to use |
|---|
| 690 | * * 'del_id' : delete input field name to delete current value |
|---|
| 691 | * * other field-type specific values that are set from |
|---|
| 692 | * appendContextLine method |
|---|
| 693 | */ |
|---|
| 694 | $line = new ArrayObject(); |
|---|
| 695 | $line['lineclass'] = $this->id; |
|---|
| 696 | $line['del_id'] = $this->filterset->buildFieldName('del',$this->id,$cur); |
|---|
| 697 | // Create the delete field for this line |
|---|
| 698 | $del = new dcFieldSubmit( |
|---|
| 699 | $this->filterset->buildFieldName('del',$this->id,$cur), |
|---|
| 700 | '-', |
|---|
| 701 | array( |
|---|
| 702 | 'attr' => array( |
|---|
| 703 | 'title' => __('Delete the following filter'))) |
|---|
| 704 | ); |
|---|
| 705 | $this->filterset->addField($del); |
|---|
| 706 | $this->appendContextLine($line,$cur); |
|---|
| 707 | $ctx[]=$line; |
|---|
| 708 | } |
|---|
| 709 | } |
|---|
| 710 | |
|---|
| 711 | /** |
|---|
| 712 | * serialize - serializes field value into given array |
|---|
| 713 | * |
|---|
| 714 | * @param mixed $arr the context to update. |
|---|
| 715 | * |
|---|
| 716 | * @access public |
|---|
| 717 | * |
|---|
| 718 | * @return mixed Value. |
|---|
| 719 | */ |
|---|
| 720 | public function serialize($arr) { |
|---|
| 721 | if (count($this->fields) == 1) { |
|---|
| 722 | $arr[$this->filter_id]=$this->field->getValue(); |
|---|
| 723 | } else { |
|---|
| 724 | $arr[$this->filter_id]=$this->field->getValues(); |
|---|
| 725 | } |
|---|
| 726 | } |
|---|
| 727 | |
|---|
| 728 | /** |
|---|
| 729 | * isApplied -- returns true when the filter is applied |
|---|
| 730 | * (ie. has at least 1 applied value) |
|---|
| 731 | * @access public |
|---|
| 732 | * |
|---|
| 733 | * @return mixed Value. |
|---|
| 734 | */ |
|---|
| 735 | public function isApplied(){ |
|---|
| 736 | return (count($this->avalues['values']) != 0); |
|---|
| 737 | } |
|---|
| 738 | |
|---|
| 739 | /** |
|---|
| 740 | * applyFilter -- Converts filter values into a $param filter, used for the |
|---|
| 741 | * upcoming SQL request |
|---|
| 742 | * |
|---|
| 743 | * @param mixed $params Description. |
|---|
| 744 | * |
|---|
| 745 | * @access public |
|---|
| 746 | * |
|---|
| 747 | * @return mixed Value. |
|---|
| 748 | */ |
|---|
| 749 | public function applyFilter($params) { |
|---|
| 750 | return false; |
|---|
| 751 | } |
|---|
| 752 | |
|---|
| 753 | /** |
|---|
| 754 | * header -- tbd |
|---|
| 755 | * |
|---|
| 756 | * @access public |
|---|
| 757 | * |
|---|
| 758 | * @return mixed Value. |
|---|
| 759 | */ |
|---|
| 760 | public function header() { |
|---|
| 761 | return ''; |
|---|
| 762 | } |
|---|
| 763 | |
|---|
| 764 | /** |
|---|
| 765 | * getFields -- returns filter field(s) |
|---|
| 766 | * |
|---|
| 767 | * @access public |
|---|
| 768 | * |
|---|
| 769 | * @return dcField the filter field. |
|---|
| 770 | */ |
|---|
| 771 | public function getFields() { |
|---|
| 772 | return $this->field; |
|---|
| 773 | } |
|---|
| 774 | } |
|---|
| 775 | |
|---|
| 776 | /** |
|---|
| 777 | * dcFilterText - basic single field text filter |
|---|
| 778 | * |
|---|
| 779 | * @uses dcFilter |
|---|
| 780 | * |
|---|
| 781 | */ |
|---|
| 782 | class dcFilterText extends dcFilter { |
|---|
| 783 | |
|---|
| 784 | /** |
|---|
| 785 | * @see dcFilter::init() |
|---|
| 786 | */ |
|---|
| 787 | public function init() { |
|---|
| 788 | $this->field = new dcFieldText( |
|---|
| 789 | $this->filter_id, |
|---|
| 790 | NULL); |
|---|
| 791 | $this->filterset->addField($this->field); |
|---|
| 792 | $this->multiple = false; |
|---|
| 793 | } |
|---|
| 794 | |
|---|
| 795 | |
|---|
| 796 | /** |
|---|
| 797 | * @see dcFilter::appendContextLine() |
|---|
| 798 | */ |
|---|
| 799 | public function appendContextLine($line,$pos) { |
|---|
| 800 | /* |
|---|
| 801 | Extra data provided by this filter : |
|---|
| 802 | * ffield : field name |
|---|
| 803 | * display_inline : true if the field is static |
|---|
| 804 | * fwidget : name of the widget (filter_text) |
|---|
| 805 | * desc : filter description |
|---|
| 806 | */ |
|---|
| 807 | $line['ffield'] = $this->field->getName(); |
|---|
| 808 | if ($this->static) { |
|---|
| 809 | $line['display_inline'] = true; |
|---|
| 810 | } |
|---|
| 811 | |
|---|
| 812 | if ($pos == 0) { |
|---|
| 813 | $line['fwidget']='filter_text'; |
|---|
| 814 | $line['desc']=$this->desc; |
|---|
| 815 | }; |
|---|
| 816 | } |
|---|
| 817 | |
|---|
| 818 | /** |
|---|
| 819 | * @see dcFilter::addValue() |
|---|
| 820 | */ |
|---|
| 821 | protected function addValue($value=NULL) { |
|---|
| 822 | if ($value === NULL) { |
|---|
| 823 | $value = ''; |
|---|
| 824 | } |
|---|
| 825 | $this->field->addValue($value); |
|---|
| 826 | } |
|---|
| 827 | |
|---|
| 828 | /** |
|---|
| 829 | * @see dcFilter::applyFilter() |
|---|
| 830 | */ |
|---|
| 831 | public function applyFilter($params) { |
|---|
| 832 | $params[$this->request_param]=$this->avalues['values'][0]; |
|---|
| 833 | } |
|---|
| 834 | } |
|---|
| 835 | |
|---|
| 836 | /** |
|---|
| 837 | * dcFilterCombo -- combo filter |
|---|
| 838 | * |
|---|
| 839 | * Enables to filter through a list of values, can be used to check |
|---|
| 840 | * if a value is in a list of items |
|---|
| 841 | * |
|---|
| 842 | * @uses dcFilter |
|---|
| 843 | * |
|---|
| 844 | */ |
|---|
| 845 | class dcFilterCombo extends dcFilter { |
|---|
| 846 | /** @var combo the list of possible values in combo */ |
|---|
| 847 | protected $combo; |
|---|
| 848 | |
|---|
| 849 | /** |
|---|
| 850 | * @see dcFilter::__construct() |
|---|
| 851 | */ |
|---|
| 852 | public function __construct($id,$name,$desc,$request_param,$combo, |
|---|
| 853 | $options=array()) { |
|---|
| 854 | parent::__construct($id,$name,$desc,$request_param,$options); |
|---|
| 855 | $this->combo = $combo; |
|---|
| 856 | } |
|---|
| 857 | |
|---|
| 858 | /** |
|---|
| 859 | * @see dcFilter::init() |
|---|
| 860 | */ |
|---|
| 861 | public function init() { |
|---|
| 862 | $this->field = new dcFieldCombo( |
|---|
| 863 | $this->filter_id, |
|---|
| 864 | NULL, |
|---|
| 865 | $this->combo); |
|---|
| 866 | $this->filterset->addField($this->field); |
|---|
| 867 | } |
|---|
| 868 | |
|---|
| 869 | /** |
|---|
| 870 | * @see dcFilter::addValue() |
|---|
| 871 | */ |
|---|
| 872 | protected function addValue($value=NULL) { |
|---|
| 873 | if ($value === NULL) { |
|---|
| 874 | $value = current(array_keys($this->combo)); |
|---|
| 875 | } |
|---|
| 876 | $this->field->addValue($value); |
|---|
| 877 | } |
|---|
| 878 | |
|---|
| 879 | /** |
|---|
| 880 | * @see dcFilter::init() |
|---|
| 881 | */ |
|---|
| 882 | public function appendContextLine($line,$pos) { |
|---|
| 883 | /* |
|---|
| 884 | Extra data provided by this filter : |
|---|
| 885 | * ffield : field name |
|---|
| 886 | * display_inline : true if the field is static |
|---|
| 887 | * fwidget : name of the widget (filter_combo or filter_combo_cont) |
|---|
| 888 | * foffset : field value offset |
|---|
| 889 | * desc : filter description |
|---|
| 890 | Only the 1st item contains description. |
|---|
| 891 | */ |
|---|
| 892 | if ($this->static) { |
|---|
| 893 | $line['display_inline'] = true; |
|---|
| 894 | } |
|---|
| 895 | $line['ffield'] = $this->field->getName(); |
|---|
| 896 | $line['foffset'] = $pos; |
|---|
| 897 | if ($pos == 0) { |
|---|
| 898 | $line['fwidget']='filter_combo'; |
|---|
| 899 | $line['desc']=$this->desc; |
|---|
| 900 | } else { |
|---|
| 901 | $line['fwidget']='filter_combo_cont'; |
|---|
| 902 | }; |
|---|
| 903 | } |
|---|
| 904 | |
|---|
| 905 | /** |
|---|
| 906 | * @see dcFilter::applyFilter() |
|---|
| 907 | */ |
|---|
| 908 | public function applyFilter($params) { |
|---|
| 909 | $attr = $this->request_param; |
|---|
| 910 | if ($this->multiple) |
|---|
| 911 | $params[$attr]=$this->avalues['values']; |
|---|
| 912 | else |
|---|
| 913 | $params[$attr]=$this->avalues['values'][0]; |
|---|
| 914 | } |
|---|
| 915 | |
|---|
| 916 | } |
|---|
| 917 | |
|---|
| 918 | /** |
|---|
| 919 | * dcFilterRichCombo -- rich combo filter |
|---|
| 920 | * |
|---|
| 921 | * Same as dcFilterCombo, with the possibility to exclude a list of values |
|---|
| 922 | * |
|---|
| 923 | * @uses dcFilter |
|---|
| 924 | * |
|---|
| 925 | */ |
|---|
| 926 | class dcFilterRichCombo extends dcFilterCombo { |
|---|
| 927 | /** @var verb verb field ('is' or 'is not') */ |
|---|
| 928 | protected $verb; |
|---|
| 929 | |
|---|
| 930 | /** |
|---|
| 931 | * @see dcFilter::init() |
|---|
| 932 | */ |
|---|
| 933 | public function init() { |
|---|
| 934 | parent::init(); |
|---|
| 935 | $this->verb = new dcFieldCombo( |
|---|
| 936 | $this->filter_id.'_v', |
|---|
| 937 | 'is', |
|---|
| 938 | array( |
|---|
| 939 | 'is'=>__('is'), |
|---|
| 940 | 'isnot'=>__('is not')) |
|---|
| 941 | ); |
|---|
| 942 | $this->filterset->addField($this->verb); |
|---|
| 943 | } |
|---|
| 944 | |
|---|
| 945 | /** |
|---|
| 946 | * @see dcFilter::parseData() |
|---|
| 947 | */ |
|---|
| 948 | protected function parseData($data) { |
|---|
| 949 | $val = parent::parseData($data); |
|---|
| 950 | $v = $this->verb->parseValues($data); |
|---|
| 951 | if (isset($v[0]) && $v[0] === 'isnot') |
|---|
| 952 | $val['verb'] = 'isnot'; |
|---|
| 953 | else |
|---|
| 954 | $val['verb'] ='is'; |
|---|
| 955 | return $val; |
|---|
| 956 | } |
|---|
| 957 | |
|---|
| 958 | /** |
|---|
| 959 | * @see dcFilter::setupFields() |
|---|
| 960 | */ |
|---|
| 961 | public function setupFields($data) { |
|---|
| 962 | parent::setupFields($data); |
|---|
| 963 | $this->verb->setup($data); |
|---|
| 964 | } |
|---|
| 965 | |
|---|
| 966 | /** |
|---|
| 967 | * @see dcFilter::updateAppliedValues() |
|---|
| 968 | */ |
|---|
| 969 | public function updateAppliedValues($arr) { |
|---|
| 970 | parent::updateAppliedValues($arr); |
|---|
| 971 | $arr[$this->verb->getName()] = $this->verb->getValue(); |
|---|
| 972 | } |
|---|
| 973 | |
|---|
| 974 | /** |
|---|
| 975 | * @see dcFilter::appendContextLine() |
|---|
| 976 | */ |
|---|
| 977 | public function appendContextLine($line,$pos) { |
|---|
| 978 | parent::appendContextLine($line,$pos); |
|---|
| 979 | if ($pos == 0) { |
|---|
| 980 | $line['fverb'] = $this->verb->getName(); |
|---|
| 981 | $line['fwidget']='filter_richcombo'; |
|---|
| 982 | } |
|---|
| 983 | } |
|---|
| 984 | |
|---|
| 985 | /** |
|---|
| 986 | * @see dcFilter::serialize() |
|---|
| 987 | */ |
|---|
| 988 | public function serialize($arr) { |
|---|
| 989 | parent::serialize($arr); |
|---|
| 990 | $arr[$this->filter_id.'_v']=$this->verb->getValue(); |
|---|
| 991 | } |
|---|
| 992 | |
|---|
| 993 | /** |
|---|
| 994 | * @see dcFilter::applyFilter() |
|---|
| 995 | */ |
|---|
| 996 | public function applyFilter($params) { |
|---|
| 997 | parent::applyFilter($params); |
|---|
| 998 | $attr = $this->request_param; |
|---|
| 999 | if ($this->avalues['verb'] != "is") { |
|---|
| 1000 | $params[$attr."_not"] = true; |
|---|
| 1001 | } |
|---|
| 1002 | } |
|---|
| 1003 | |
|---|
| 1004 | } |
|---|
| 1005 | |
|---|
| 1006 | // Static initializer |
|---|
| 1007 | dcFilterSet::__init__($GLOBALS['core']->tpl); |
|---|
| 1008 | ?> |
|---|