Changeset 438:5197b0c2826f for inc/admin
- Timestamp:
- 06/29/11 11:07:52 (14 years ago)
- Branch:
- formfilters
- Children:
- 439:f6cd5149f3ad, 450:d6b8e3e74f10
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
inc/admin/class.dc.filter.php
r417 r438 22 22 # ***** END LICENSE BLOCK ***** 23 23 24 /** 25 @ingroup DC_CORE 26 @nosubgrouping 27 @brief Dotclear FilterSet class. 28 29 Dotclear FilterSet handles filters and columns when displaying items lists. 30 */ 24 31 class dcFilterSet { 25 32 26 protected $filters; 27 protected $form_prefix; 28 protected $action; 29 protected $hideform; 30 protected $columns_form; 31 32 public function __construct($action,$form_prefix="f_") { 33 protected $filters; /// <b>array</b> lists of defined filters 34 protected $form_prefix; /// <b>string</b> displayed form prefix 35 protected $action; /// <b>string</b> form action page 36 protected $hideform; /// <b>boolean</b> start form display hidden by default or not 37 protected $columns_form; /// <b>string</b> columns form 38 protected $name; /// <b>string</b> fieldset name 39 /** 40 Inits dcFilterSet object 41 42 @param core <b>dcCore</b> Dotclear core reference 43 @param form_prefix <b>string</b> form prefix to use for parameters 44 */ 45 public function __construct($name,$action,$form_prefix="f_") { 46 $this->name = $name; 33 47 $this->form_prefix=$form_prefix; 34 48 $this->filters = array(); … … 36 50 } 37 51 52 /** 53 Adds a new filter to list 54 55 @param filter <b>dcFilter</b> the filter to add 56 */ 38 57 public function addFilter (Filter $filter) { 39 58 $filter->setFormPrefix($this->form_prefix); … … 41 60 return $this; 42 61 } 43 44 // Retrieves filter values from context 62 63 /** 64 Saves user filters to preferences 65 */ 66 protected function saveFilters() { 67 $ser = array(); 68 $ws = $GLOBALS['core']->auth->user_prefs->addWorkspace('filters'); 69 foreach($this->filters as $filter) { 70 $ser[$filter->id]=$filter->serialize(); 71 } 72 $ws->put($this->name,serialize($ser),'string'); 73 } 74 75 /** 76 Loads user filters from preferences 77 */ 78 protected function loadFilters() { 79 $ws = $GLOBALS['core']->auth->user_prefs->addWorkspace('filters'); 80 81 $settings = !is_null($ws->{$this->name}) ? unserialize($ws->{$this->name}) : array(); 82 foreach($settings as $k => $v) { 83 $this->filters[$k]->unserialize($v); 84 } 85 } 86 87 /** 88 Updates filters values according to form_data 89 To be called before any call to display() or getForm() 90 91 @param form_data <b>array</b> form values (usually $_GET or $_POST) 92 */ 45 93 public function setValues ($form_data) { 46 94 $this->hideform = true; 47 if (isset($form_data['clear_filters'])) 95 if (isset($form_data['clear_filters'])) { 96 $this->saveFilters(); 48 97 return; 98 } 99 if (!isset($form_data['apply'])) { 100 $this->loadFilters(); 101 } 49 102 foreach ($this->filters as $filter) { 50 103 $filter->setValues ($form_data); 51 if ($filter->isEnabled()) 104 if ($filter->isEnabled()) { 52 105 $this->hideform=false; 53 } 54 if (isset($form_data['apply']) && (trim($form_data['apply']) == '+') 55 && isset($form_data['add_filter']) && isset($this->filters[$form_data['add_filter']])) { 56 $this->filters[$form_data['add_filter']]->add(); 57 $this->hideform=false; 58 } 59 } 60 106 } 107 } 108 if (isset($form_data['apply'])) { 109 if (trim($form_data['apply']) == '+' 110 && isset($form_data['add_filter']) 111 && isset($this->filters[$form_data['add_filter']])) { 112 $this->filters[$form_data['add_filter']]->add(); 113 $this->hideform=false; 114 } 115 } 116 $this->saveFilters(); 117 } 118 119 /** 120 Defines additional form in layout (right column) 121 122 @param html <b>string</b> the code to add 123 */ 61 124 public function setColumnsForm($html) 62 125 { … … 64 127 } 65 128 129 /** 130 Returns form fields as hidden fields 131 132 @return <b>string</b> the corresponding html code 133 */ 66 134 public function getFormFieldsAsHidden() { 67 135 $ret=''; … … 72 140 } 73 141 74 public function getForm($action,$extra_content,$method="get",$nb_cols=3) { 142 /** 143 Retrieves filterset generated form 144 145 @param method <b>string</b> form method to use (default: "get") 146 */ 147 public function getForm($method="get") { 75 148 $ret = ''; 76 149 … … 83 156 $ret .= 84 157 '<div class="two-cols">'. 85 '<form id="filters" action="'.$this->action.'" method=" get" id="filters-form"'.$formclass.'>'.158 '<form id="filters" action="'.$this->action.'" method="'.$method.'" id="filters-form"'.$formclass.'>'. 86 159 '<div class="col70">'. 87 160 '<h3>'.__('Entries filters').'</h3>'; … … 90 163 $form_combo=array(); 91 164 $form_combo['-']=''; 92 foreach ($this->filters as $filter) { 93 if ($filter->isEnabled()) { 94 $ret .= $filter->getFormLine(); 165 if (count($this->filters)) { 166 $ret .= '<ul>'; 167 foreach ($this->filters as $filter) { 168 if ($filter->isEnabled()) { 169 $ret .= $filter->getFormLine(); 170 } 171 $form_combo[$filter->desc]=$filter->id; 172 $count++; 95 173 } 96 $form_combo[$filter->desc]=$filter->id; 97 $count++; 174 $ret .= '</ul>'; 98 175 } 99 176 $ret .= … … 113 190 return $ret; 114 191 } 115 192 193 /** 194 Displays required fieldset http header 195 To be called in page header, of course. 196 */ 116 197 public function header() { 117 198 return dcPage::jsLoad('js/filters.js'); 118 199 } 200 201 202 /** 203 Displays the fieldset 204 */ 119 205 public function display() { 120 echo $this->getForm("#",""); 121 } 122 206 echo $this->getForm(); 207 } 208 209 /** 210 Applies fieldset and return resulting parameters for request 211 212 @param method <b>string</b> form method to use (default: "get") 213 @param method <b>string</b> form method to use (default: "get") 214 215 */ 123 216 public function applyFilters($params) { 124 217 $filtered = false; … … 135 228 136 229 230 /** 231 @ingroup DC_CORE 232 @nosubgrouping 233 @brief abstract filter class. 234 235 Dotclear Filter handles administration filters for each list 236 A filter fills in a parameter array, as defined in dcBlog class 237 */ 137 238 abstract class Filter { 138 public $id; 139 public $desc; 140 protected $request_param; 141 protected $enabled; 142 protected $values; 143 public $field_id; 144 145 239 public $id; ///< <b>string</b> field id (local to fieldset) 240 public $desc; ///< <b>string</b> field description 241 protected $request_param; ///< <b>string</b> resulting parameter array key 242 protected $enabled; ///< <b>string</b> true if filter is enabled 243 protected $values; ///< <b>array</b> possible filter values 244 public $field_id; ///< <b>string</b> field id (global to the page) 245 246 /** 247 Inits Filter object 248 249 @param id <b>string</b> field id 250 @param form_prefix <b>string</b> form prefix to use for parameters 251 */ 146 252 public function __construct ($id,$desc,$request_param) { 147 253 $this->id = $id; … … 153 259 } 154 260 261 /** 262 Get a field id 263 264 @param pos <b>integer</b> position of field, in case of multiple field (0 if only 1 field set, default value) 265 @return <b>string</b> The field ID 266 */ 155 267 protected function getFieldId($pos=0) { 156 268 if ($pos == 0) { … … 161 273 } 162 274 275 /** 276 Tells whether the filter is enabled or not 277 278 @return <b>boolean</b> true if enabled, false otherwise 279 */ 163 280 public function isEnabled() { 164 281 return $this->enabled; 165 282 } 166 283 284 /** 285 Adds the current filter to the list 286 */ 167 287 public function add() { 288 // By default here, only 1 value allowed. Simply enable the filter 168 289 $this->enabled = true; 169 290 } 170 291 292 /** 293 Defines form prefix for filter 294 295 @param prefix <b>string</b> the form prefix 296 */ 171 297 public function setFormPrefix($prefix) { 172 298 $this->field_id = $prefix.$this->id; 173 299 } 174 300 175 public abstract function getType(); 176 177 public function getFormFields() { 301 302 /** 303 Returns HTML code for form field 304 305 @param pos <b>integer</b> position of the field to display (in case of multiple values) 306 @return <b>string</b> the html code 307 */ 308 public function getFormFields($pos=0) { 178 309 return ''; 179 310 } 180 311 312 /** 313 Returns filter values il a serialized way (array) 314 315 @return <b>array</b> serialized data 316 */ 317 public function serialize() { 318 return array( 319 'values' => $this->values, 320 'enabled' => $this->enabled 321 ); 322 } 323 324 /** 325 Defines filter values from serialized data (array) 326 To be used in conjunction with serialize method 327 328 @param $data <b>array</b> serialized data to retrieve 329 */ 330 public function unserialize ($data) { 331 $this->values = $data['values']; 332 $this->enabled = $data['enabled']; 333 } 334 335 /** 336 Set filter values from form_data (usually $_GET) 337 @param $form_data <b>array</b> form data 338 */ 181 339 public function setValues($form_data) { 182 /* if (isset($form_data['c_'.$this->field_id])) {183 $this->enabled = true;184 }*/185 340 $count=0; 186 341 while (isset($form_data[$this->getFieldId($count)])) { 187 342 if (!isset($form_data['del_'.$this->getFieldId($count)])) { 188 $this->values[] = $form_data[$this->getFieldId($count)]; 189 $this->enabled = true; 343 $this->values[$count] = $form_data[$this->getFieldId($count)]; 344 } elseif (isset($this->values[$count])) { 345 unset($this->values[$count]); 190 346 } 191 347 $count++; 192 } 193 } 194 348 349 } 350 $this->values = array_values($this->values); 351 $this->enabled = (count($this->values)!=0); 352 } 353 354 /** 355 Returns form fields as hidden fields 356 357 @return <b>string</b> the corresponding html code 358 */ 195 359 public function getFormFieldAsHidden () { 196 360 $ret=''; … … 199 363 } 200 364 } 365 /** 366 Returns HTML code for the hole filter lines 367 368 @return <b>string</b> the html code 369 */ 370 201 371 public function getFormLine() { 202 372 $ret=""; 203 373 for ($cur=0; $cur < count($this->values); $cur++) { 204 $ret .= '< pid="'.$this->getFieldId($cur).'" class="line" title="'.$this->desc.'">'.374 $ret .= '<li id="'.$this->getFieldId($cur).'" class="line" title="'.$this->desc.'">'. 205 375 $this->getFormFields($cur). 206 376 '<input id="del_'.$this->getFieldId($cur).'" class="delete" '. 207 'type="submit" title="Delete th is filter" value=" - " name="del_'.$this->getFieldId($cur).'"/>'.208 '</ p>';377 'type="submit" title="Delete the following filter : " value=" - " name="del_'.$this->getFieldId($cur).'"/>'. 378 '</li>'; 209 379 } 210 380 return $ret; 211 381 } 212 382 383 /** 384 Convert filter values into a $param filter, used for the upcoming SQL request 385 386 @param <b>ArrayObject</b> the parameters array to enrich 387 */ 213 388 public function applyFilter($params) { 214 389 } … … 216 391 } 217 392 393 /** 394 @ingroup DC_CORE 395 @nosubgrouping 396 @brief abstract filter class. 397 398 Handle combo filter on admin side. Can be single or multi-valued 399 */ 218 400 class comboFilter extends Filter { 219 401 protected $options; … … 242 424 return "combo"; 243 425 } 426 427 public function serialize() { 428 $data = parent::serialize(); 429 $data['verb'] = $this->verb; 430 return $data; 431 } 432 433 public function unserialize ($data) { 434 parent::unserialize($data); 435 $this->verb = $data['verb']; 436 } 244 437 245 438 public function setValues($form_data) { … … 258 451 if ($pos == 0) { 259 452 $desc = $this->desc.' : '; 260 $labelclass=" ";453 $labelclass="filter-title"; 261 454 } else { 262 455 $desc = __('or'); 263 $labelclass = ' class="or"';456 $labelclass = 'or'; 264 457 }; 265 return '<span class=" filter-title">'.$desc.'</span>'.458 return '<span class="'.$labelclass.'">'.$desc.'</span>'. 266 459 (($pos == 0) 267 460 ?form::combo($this->field_id.'_v', … … 284 477 } 285 478 } 479 480 /** 481 @ingroup DC_CORE 482 @nosubgrouping 483 @brief abstract filter class. 484 485 Handle boolean filter on admin side. 486 */ 487 class booleanFilter extends Filter { 488 protected $options; 489 490 public function __construct($id,$desc,$request_param,$options,$extra=array()) { 491 parent::__construct($id,$desc,$request_param); 492 $this->options = $options; 493 $this->values=array(); 494 } 495 496 497 public function getType() { 498 return "boolean"; 499 } 500 public function add() { 501 parent::add(); 502 $this->values[]=$options[0]; 503 } 504 505 public function getFormFields($pos=0) { 506 return '<span class="'.$labelclass.'">'.$this->desc.'</span>'. 507 form::combo($this->getFieldId($pos),$this->options,$this->values[$pos], 508 '','',false,'title="'.__('Choose an option').'"'); 509 } 510 511 public function applyFilter($params) { 512 $params[$this->request_param]=$this->values[0]; 513 } 514 } 515 286 516 ?>
Note: See TracChangeset
for help on using the changeset viewer.