Changeset 755:38c4f75f3dd5
- Timestamp:
- 11/10/11 10:03:55 (14 years ago)
- Branch:
- formfilters
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
admin/posts.php
r523 r755 151 151 $core->callBehavior('adminPostsFilters',$filterSet); 152 152 153 $filterSet->set FormValues($_GET);153 $filterSet->setup($_GET,$_POST); 154 154 155 155 # Get posts … … 171 171 } 172 172 173 $filterSet->set ColumnsForm($post_list->getColumnsForm());173 $filterSet->setExtraData($post_list->getColumnsForm()); 174 174 175 175 /* DISPLAY -
inc/admin/class.dc.filter.php
r580 r755 20 20 class dcFilterSet { 21 21 22 protected $filters; /// <b>array</b> lists of defined filters 22 protected $lfilters; /// <b>array</b> lists of defined filters 23 protected $efilters; /// <b>array</b> lists of defined filters 23 24 protected $form_prefix; /// <b>string</b> displayed form prefix 24 25 protected $action; /// <b>string</b> form action page 25 26 protected $hideform; /// <b>boolean</b> start form display hidden by default or not 26 protected $ columns_form; /// <b>string</b> columns form27 protected $extra_data; /// <b>string</b> columns form 27 28 protected $name; /// <b>string</b> fieldset name 29 28 30 /** 29 31 Inits dcFilterSet object … … 35 37 $this->name = $name; 36 38 $this->form_prefix=$form_prefix; 37 $this->filters = array(); 39 $this->lfilters = new ArrayObject(); 40 $this->efilters = new ArrayObject(); 38 41 $this->action = $action; 39 } 40 42 $this->extra_data = ''; 43 } 44 41 45 /** 42 46 Adds a new filter to list … … 46 50 public function addFilter (Filter $filter) { 47 51 $filter->setFormPrefix($this->form_prefix); 48 $this->filters[$filter->id] = $filter; 52 $filter->setFilterSet($this); 53 $this->efilters[$filter->id] = $filter; 54 $this->lfilters[$filter->id] = clone $filter; 49 55 return $this; 50 56 } … … 56 62 $ser = array(); 57 63 $ws = $GLOBALS['core']->auth->user_prefs->addWorkspace('filters'); 58 foreach($this-> filters as $filter) {64 foreach($this->lfilters as $filter) { 59 65 $ser[$filter->id]=$filter->serialize(); 60 66 } … … 70 76 $settings = !is_null($ws->{$this->name}) ? unserialize($ws->{$this->name}) : array(); 71 77 foreach($settings as $k => $v) { 72 $this-> filters[$k]->unserialize($v);78 $this->lfilters[$k]->unserialize($v); 73 79 } 74 80 } … … 80 86 @param form_data <b>array</b> form values (usually $_GET or $_POST) 81 87 */ 82 p ublic function setFormValues ($form_data) {88 protected function initializeFromData ($filters, $form_data) { 83 89 $this->hideform = true; 84 if (isset($form_data['clear_filters'])) { 85 $this->saveFilters(); 86 return; 87 } 88 if (!isset($form_data['apply'])) { 89 $this->loadFilters(); 90 } 91 foreach ($this->filters as $filter) { 92 $filter->setFormValues ($form_data); 93 if ($filter->isEnabled()) { 94 $this->hideform=false; 95 } 96 } 97 if (isset($form_data['apply'])) { 98 if (trim($form_data['apply']) == '+' 99 && isset($form_data['add_filter']) 100 && isset($this->filters[$form_data['add_filter']])) { 101 $this->filters[$form_data['add_filter']]->add(); 102 $this->hideform=false; 103 } 104 } 105 $this->saveFilters(); 90 foreach ($filters as $filter) { 91 $filter->initializeFromData ($form_data); 92 } 106 93 } 107 94 … … 111 98 @param html <b>string</b> the code to add 112 99 */ 113 public function set ColumnsForm($html)100 public function setExtraData($html) 114 101 { 115 $this-> columns_form= $html;102 $this->extra_data = $html; 116 103 } 117 104 … … 123 110 public function getFormFieldsAsHidden() { 124 111 $ret=''; 125 foreach ($this-> filters as $filter) {112 foreach ($this->lfilters as $filter) { 126 113 $ret.= $filter->getFormFieldAsHidden(); 127 114 } … … 130 117 131 118 /** 119 Sets up filterset from $get and $post parameters 120 121 */ 122 public function setup($get,$post) { 123 /* Use cases : 124 (1) $post not empty for formfilter fields : 125 * efilters are set from $post 126 * lfilters are set from $get 127 * keep filters div shown 128 (2) $post empty : 129 * both efilters and lfilters are set from $get 130 * hide filter div 131 */ 132 $action = false; 133 $allowed_actions = array('clear_filters','add','del_','apply','reset'); 134 // Fetch each $post parameter to see whether filters are concerned. 135 // Only 1 action at a time is allowed. 136 foreach ($post as $k => $v) { 137 if (strpos($k,$this->form_prefix)===0) { 138 $tmp = substr($k,strlen($this->form_prefix)); 139 foreach ($allowed_actions as $a) { 140 if (strpos($tmp,$a)===0) { 141 $action = $tmp; 142 break; 143 } 144 } 145 } 146 } 147 if ($action !== false) { 148 // Use case (1) 149 if ($action != 'clear_filters' && $action != 'reset') { 150 $this->initializeFromData($this->efilters,$post); 151 if ($action == 'add'){ 152 if (isset($post['add_filter']) 153 && isset($this->efilters[$post['add_filter']])) { 154 echo 'addadd'; 155 $this->efilters[$post['add_filter']]->add(); 156 } 157 } elseif (strpos($action,'del_') === 0) { 158 159 $count = preg_match('#del_(.+)_([0-9]+)#',$action,$match); 160 if (($count == 1) && isset($this->efilters[$match[1]])) { 161 $this->efilters[$match[1]]->remove($match[2]); 162 } 163 } elseif ($action=="apply") { 164 http::redirect($this->action.'?'.http_build_query($this->getFiltersAsParams($this->efilters),'','&')); 165 exit; 166 } 167 } 168 if (isset($post[$this->form_prefix."query"])) { 169 parse_str($post[$this->form_prefix."query"],$out); 170 $this->initializeFromData($this->lfilters,$out); 171 if ($action == 'reset') { 172 $this->initializeFromData($this->efilters,$out); 173 } 174 } 175 $this->hideform=false; 176 } else { 177 $this->initializeFromData($this->efilters, $get); 178 $this->initializeFromData($this->lfilters, $get); 179 } 180 } 181 /** 132 182 Retrieves filterset generated form 133 183 134 184 @param method <b>string</b> form method to use (default: "get") 135 185 */ 136 public function getForm( $method="get") {186 public function getForm() { 137 187 $ret = ''; 138 188 … … 142 192 $formclass=''; 143 193 } 144 $ret .= '<p><img alt="" src="images/minus.png" /> <a href="#" id="toggle-filters">'.__('Toggle filters and display options').'</a></p>'; 194 $ret .= '<p><img alt="" src="images/minus.png" /> '. 195 '<a href="#" id="toggle-filters">'. 196 __('Toggle filters and display options'). 197 '</a></p>'; 198 145 199 $ret .= 146 200 '<div class="two-cols">'. 147 '<form id="filters" action="'.$this->action.'" method=" '.$method.'"'.$formclass.'>'.201 '<form id="filters" action="'.$this->action.'" method="post"'.$formclass.'>'. 148 202 '<div class="col70">'. 149 '<h3>'.__('Entries filters').'</h3>'; 150 203 '<h3>'.__('Entries filters').'</h3>'. 204 '<table summary="'.__('Query filters').'" id="tfilters">'. 205 '<tbody>'; 151 206 $count=0; 152 207 $form_combo=array(); 153 208 $form_combo['-']=''; 154 if (count($this->filters)) { 155 $ret .= '<ul>'; 156 foreach ($this->filters as $filter) { 209 if (count($this->efilters)) { 210 foreach ($this->efilters as $filter) { 157 211 if ($filter->isEnabled()) { 158 212 $ret .= $filter->getFormLine(); … … 161 215 $count++; 162 216 } 163 $ret .= '</ul>'; 164 } 165 $ret .= 166 '<p class="clear"><input class="delete" type="submit" value="'.__('Delete all filters').'" name="clear_filters" /></p>'. 217 } 218 $ret .= '</tbody></table>'. 219 '<p class="clear"><input class="delete" type="submit" value="'.__('Delete all filters').'" name="'. 220 $this->form_prefix.'clear_filters" />'. 221 ' <input type="submit" value="'.__('Reset').'" name="'. 222 $this->form_prefix.'reset" /></p>'. 167 223 '<h3 class="margintop">'.__('Add a filter').'</h3>'. 168 224 '<p id="available_filters">'. 169 225 form::combo("add_filter",$form_combo). 170 '<input type="submit" value=" + " title="'.__('Add this filter').'" name=" apply" />'.226 '<input type="submit" value=" + " title="'.__('Add this filter').'" name="'.$this->form_prefix.'add" />'. 171 227 '</p>'. 172 '</div>'. 173 '<div class="col30">'. 174 $this->columns_form. 175 '</div>'. 176 '<p class="clear margintop"><input type="submit" value="'.__('Apply filters and display options').'" name="apply" /></p>'. 177 178 '</form></div>'; 228 '</div>'; 229 if ($this->extra_data != '') { 230 $ret .= 231 '<div class="col30">'. 232 $this->extra_data. 233 '</div>'; 234 } 235 $ret .= 236 '<p class="clear margintop">'. 237 '<input type="submit" value="'.__('Apply filters and display options'). 238 '" name="'.$this->form_prefix.'apply" /></p>'. 239 form::hidden($this->form_prefix."query",http_build_query($this->getFiltersAsParams($this->lfilters))). 240 $GLOBALS['core']->formNonce(). 241 '</form>'. 242 '</div>'; 243 return $ret; 244 } 245 /** 246 Retrieves the filters values as parameters 247 248 @param filters <b>array</b> list of concerned filters 249 250 @return <b>array</b> the array of parameters 251 252 */ 253 254 protected function getFiltersAsParams($filters) { 255 $arr = new ArrayObject(); 256 foreach ($filters as $f) { 257 if ($f->isEnabled()) 258 $f->fillQS($arr); 259 } 260 return $arr; 261 } 262 263 protected function displayFilters($filters) { 264 $ret = '<ul>'; 265 foreach ($filters as $f) { 266 if ($f->isEnabled()) 267 $ret .= '<li>'.$f->getAsText().'</li>'."\n"; 268 } 269 $ret .= '</ul>'; 179 270 return $ret; 180 271 } … … 205 296 public function applyFilters($params) { 206 297 $filtered = false; 207 foreach ($this-> filters as $filter) {298 foreach ($this->lfilters as $filter) { 208 299 if ($filter->isEnabled()) { 209 300 $filter->applyFilter($params); … … 214 305 } 215 306 307 public function getDelName($field_id,$pos) { 308 return $this->form_prefix.'del_'.$field_id.'_'.$pos; 309 } 216 310 } 217 311 … … 226 320 */ 227 321 abstract class Filter { 322 public $filterset; ///<b>string</b> filterset parent 228 323 public $id; ///<b>string</b> field id (local to fieldset) 229 324 public $name; ///<b>string</b> filter name … … 251 346 252 347 /** 348 Defines the filterset containing this filter 349 350 @param prefix <b>dcFilterset</b> the filterset 351 */ 352 public function setFilterSet($fs) { 353 $this->filterset = $fs; 354 } 355 356 /** 357 Defines form prefix for filter 358 359 @param prefix <b>string</b> the form prefix 360 */ 361 public function setFormPrefix($prefix) { 362 $this->field_id = $prefix.$this->id; 363 } 364 365 /** 253 366 Get a field id 254 367 … … 282 395 283 396 /** 284 Defines form prefix forfilter285 286 @param prefix <b>string</b> the form prefix287 */288 public function setFormPrefix($prefix) {289 $this->field_id = $prefix.$this->id;290 }291 397 Removes a value from filter 398 */ 399 public function remove($pos) { 400 if (isset($this->values[$pos])) { 401 array_splice($this->values,$pos,1); 402 $this->enabled = (count($this->values)!=0); 403 } 404 } 292 405 293 406 /** 294 407 Returns HTML code for form field 295 408 296 @param pos <b>integer</b> position of the field to display (in case of multiple values) 409 @param pos <b>integer</b> position of the field to display 410 (in case of multiple values) 297 411 @return <b>string</b> the html code 298 412 */ 299 public function getFormFields($pos=0) { 300 return ''; 301 } 302 303 /** 304 Returns filter values il a serialized way (array) 305 306 @return <b>array</b> serialized data 307 */ 308 public function serialize() { 309 return array( 310 'values' => $this->values, 311 'enabled' => $this->enabled 312 ); 313 } 314 315 /** 316 Defines filter values from serialized data (array) 317 To be used in conjunction with serialize method 318 319 @param $data <b>array</b> serialized data to retrieve 320 */ 321 public function unserialize ($data) { 322 $this->values = $data['values']; 323 $this->enabled = $data['enabled']; 324 } 325 326 /** 327 Set filter values from form_data (usually $_GET) 328 @param $form_data <b>array</b> form data 329 */ 330 public function setFormValues($form_data) { 413 abstract protected function getFormFields($pos=0); 414 415 /** 416 Extract values from data (data being an array, such as $_GET or $_POST) 417 418 @param $data <b>array</b> data to parse 419 @return <b>array</b> field values 420 421 */ 422 protected function getValuesFromData($data) { 331 423 $count=0; 332 while (isset($form_data[$this->getFieldId($count)])) { 333 if (!isset($form_data['del_'.$this->getFieldId($count)])) { 334 $this->values[$count] = $form_data[$this->getFieldId($count)]; 335 } elseif (isset($this->values[$count])) { 336 unset($this->values[$count]); 337 } 424 $arr = array(); 425 while (isset($data[$this->getFieldId($count)])) { 426 $arr[$count] = $data[$this->getFieldId($count)]; 338 427 $count++; 339 340 } 341 $this->values = array_values($this->values); 428 } 429 return $arr; 430 } 431 432 public function initializeFromData($form_data) { 433 $this->values = $this->getValuesFromData($form_data); 342 434 $this->enabled = (count($this->values)!=0); 343 435 } 344 436 345 /** 437 /** 438 Returns HTML code for the hole filter lines 439 440 @return <b>string</b> the html code 441 */ 442 443 public function getFormLine() { 444 $ret=''; 445 for ($cur=0; $cur < count($this->values); $cur++) { 446 $ret .= '<tr class="'.$this->id.'">'; 447 $del_id = $this->filterset->getDelName($this->id,$cur); 448 $ret .= '<td><input id="'.$del_id.'" class="delete" '. 449 'type="submit" title="Delete the following filter : " value=" - " name="'.$del_id.'"/></td>'. 450 $this->getFormFields($cur); 451 $ret .= '</tr>'; 452 } 453 return $ret; 454 } 455 456 public function fillQS($arr) { 457 for ($cur=0; $cur < count($this->values); $cur++) { 458 $arr[$this->getFieldId($cur)]=$this->values[$cur]; 459 } 460 } 461 462 /** 346 463 Returns form fields as hidden fields 347 464 … … 354 471 } 355 472 } 356 /**357 Returns HTML code for the hole filter lines358 359 @return <b>string</b> the html code360 */361 362 public function getFormLine() {363 $ret="";364 for ($cur=0; $cur < count($this->values); $cur++) {365 $ret .= '<li id="'.$this->getFieldId($cur).'" class="line" title="'.$this->desc.'">'.366 $this->getFormFields($cur).367 '<input id="del_'.$this->getFieldId($cur).'" class="delete" '.368 'type="submit" title="Delete the following filter : " value=" - " name="del_'.$this->getFieldId($cur).'"/>'.369 '</li>';370 }371 return $ret;372 }373 473 374 474 /** … … 387 487 return $this->values; 388 488 } 489 490 public abstract function getAsText(); 491 389 492 390 493 } … … 412 515 } 413 516 517 protected function getValuesFromData($data) { 518 $val = parent::getValuesFromData($data); 519 if (isset($data[$this->field_id.'_v'])) { 520 $verb = $data[$this->field_id.'_v']; 521 } else { 522 $verb = "is"; 523 } 524 $arr = array( 525 'values' => $val, 526 'verb' => $verb 527 ); 528 return $arr; 529 } 530 414 531 public function add() { 415 532 parent::add(); … … 434 551 } 435 552 436 public function setFormValues($form_data) { 437 parent::setFormValues($form_data); 438 if (isset($form_data[$this->field_id."_v"])) { 439 $this->verb = ($form_data[$this->field_id."_v"] == 'is') ? 'is' : 'isnot'; 440 } 553 public function initializeFromData($form_data) { 554 $arr = $this->getValuesFromData($form_data); 555 $this->values = $arr['values']; 556 $this->verb = $arr['verb']; 557 $this->enabled = (count($this->values) != 0); 558 } 559 560 public function getFormFields($pos=0) { 561 if ($pos == 0) { 562 $ret = '<td id="'.$this->getFieldId($cur).'" title="'.$this->desc.'" class="filter-title">'. 563 ''.$this->desc.' : </td>'. 564 '<td>'. 565 form::combo($this->field_id.'_v', 566 array(__('is')=>'is',__('is not')=>'isnot'),$this->verb,'','', 567 false,'title="'.sprintf(__('%s is or is not'),$this->desc).'"'). 568 '</td>'; 569 } else { 570 $ret = '<td id="'.$this->getFieldId($cur).'" title="or" colspan="2" class="or">'. 571 __('or').' : </td>'; 572 }; 573 $ret .= '<td>'.form::combo($this->getFieldId($pos),$this->options,$this->values[$pos], 574 '','',false,'title="'.__('Choose an option').'"').'</td>'; 575 return $ret; 441 576 } 442 577 … … 444 579 return parent::getFormFieldAsHidden().form::hidden($this->field_id."_v",$this->verb); 445 580 } 446 447 public function getFormFields($pos=0) { 581 582 public function fillQS($arr) { 583 parent::fillQS($arr); 448 584 449 if ($pos == 0) { 450 $desc = $this->desc.' : '; 451 $labelclass="filter-title"; 452 } else { 453 $desc = __('or'); 454 $labelclass = 'or'; 455 }; 456 return '<span class="'.$labelclass.'">'.$desc.'</span>'. 457 (($pos == 0) 458 ?form::combo($this->field_id.'_v', 459 array(__('is')=>'is',__('is not')=>'isnot'),$this->verb,'','', 460 false,'title="'.sprintf(__('%s is or is not'),$this->desc).'"') 461 :''). 462 form::combo($this->getFieldId($pos),$this->options,$this->values[$pos], 463 '','',false,'title="'.__('Choose an option').'"'); 585 $arr[$this->field_id.'_v']=$this->verb; 464 586 } 465 587 … … 473 595 else 474 596 $params[$attr]=$this->values; 597 } 598 599 public function getValues() { 600 return array_merge($this->values,array($this->field_id.'_v',$this->verb)); 601 } 602 603 public function getAsText() { 604 return sprintf("%s %s %s",$this->desc,$this->verb,join(',',$this->values)); 475 605 } 476 606 } … … 502 632 503 633 public function getFormFields($pos=0) { 504 return '< span class="'.$labelclass.'">'.$this->desc.'</span>'.634 return '<td><span class="'.$labelclass.'">'.$this->desc.'</span></td><td>'. 505 635 form::combo($this->getFieldId($pos),$this->options,$this->values[$pos], 506 '','',false,'title="'.__('Choose an option').'"') ;636 '','',false,'title="'.__('Choose an option').'"').'</td>'; 507 637 } 508 638 509 639 public function applyFilter($params) { 510 640 $params[$this->request_param]=$this->values[0]; 641 } 642 643 public function getAsText() { 644 return sprintf("%s %s",$this->desc,$this->values[0]); 511 645 } 512 646 } … … 546 680 } 547 681 548 public function getValue() { 549 $v = parent::getValue(); 550 return $v[0]; 682 public function getAsText() { 683 return sprintf("%s %s",$this->desc,$this->values[0]); 551 684 } 552 685
Note: See TracChangeset
for help on using the changeset viewer.