Changeset 1152:fbd922f6ed09 for inc/admin/class.dc.filter.php
- Timestamp:
- 04/02/13 13:05:04 (12 years ago)
- Branch:
- twig
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
inc/admin/class.dc.filter.php
r1148 r1152 14 14 15 15 16 class dcFilterSetExtension extends Twig_Extension {17 private $filtersets;18 19 public function __construct($core)20 {21 $this->core = $core;22 $this->tpl = 'formfilter_layout.html.twig';23 }24 25 public function initRuntime(Twig_Environment $environment)26 {27 $this->core->tpl->getExtension('dc_form')->addTemplate($this->tpl);28 }29 30 31 public function getFunctions()32 {33 return array(34 'filterset' => new Twig_Function_Method(35 $this,36 'renderFilterSet',37 array(38 'is_safe' => array('html'),39 'needs_context' => true40 )));41 }42 43 44 45 public function renderFilterSet($context,$name,$attributes=array())46 {47 $context['filtersetname']=$name;48 echo $this->core->tpl->getExtension('dc_form')->renderBlock(49 'filterset',50 $context51 );52 }53 54 55 public function getName()56 {57 return 'dc_filterset';58 }59 60 }61 62 63 16 class dcFilterSet extends dcForm { 64 17 protected $filters; /// <b>array</b> lists of currently applied filters 18 protected $static_filters; 19 protected $all_filters; 65 20 protected $form_prefix; /// <b>string</b> displayed form prefix 66 21 protected $action; /// <b>string</b> form action page … … 69 24 protected $core; 70 25 26 public static function __init__($env) { 27 $env->getExtension('dc_form')->addTemplate('@forms/formfilter_layout.html.twig'); 28 $env->addFunction( 29 new Twig_SimpleFunction( 30 'filterset', 31 'dcFilterSet::renderFilterSet', 32 array( 33 'is_safe' => array('html'), 34 'needs_context' => true, 35 'needs_environment' => true 36 ))); 37 } 38 39 public static function renderFilterSet($env,$context,$name,$attributes=array()) 40 { 41 $context['filtersetname']=$name; 42 echo $env->getExtension('dc_form')->renderWidget( 43 'filterset', 44 $context 45 ); 46 } 71 47 /** 72 48 Inits dcFilterSet object … … 78 54 $this->form_prefix=$form_prefix; 79 55 $this->filters = new ArrayObject(); 56 $this->static_filters = new ArrayObject(); 57 $this->all_filters = new ArrayObject(); 80 58 $this->action = $action; 81 59 $this->filtered = false; … … 87 65 $form_combo = array(); 88 66 $form_combo['-'] = ''; 67 foreach ($this->all_filters as $filter) { 68 $filter->init(); 69 } 89 70 foreach ($this->filters as $filter) { 90 $filter->init();91 71 $form_combo[$filter->id]=$filter->name; 92 72 } … … 110 90 ; 111 91 $this->setupFields(); 112 $submitted = $this->getSubmittedFields();113 92 /* Use cases : 114 93 (1) $_POST not empty for formfilter fields : … … 130 109 $tmp = substr($k,strlen($this->form_prefix)); 131 110 $count = preg_match($allowed_actions,$tmp,$match); 132 echo $action;133 111 if ($count==1) { 134 112 $action = $match[1]; … … 137 115 } 138 116 } 117 139 118 if ($action !== false) { 140 119 // Use case (1) 141 120 if ($action != 'clear_filters' && $action != 'reset') { 142 $this->setupEditFilters($ _POST);121 $this->setupEditFilters($this->all_filters,$_POST); 143 122 if ($action == 'add'){ 144 if (isset($_POST[$p.'add_filter']) 145 && isset($this->filters[$_POST[$p.'add_filter']])) { 146 $this->filters[$_POST[$p.'add_filter']]->add(); 123 $fname = $p.'add_filter'; 124 if (isset($_POST[$fname]) 125 && isset($this->filters[$_POST[$fname]])) { 126 $this->filters[$_POST[$fname]]->add(); 147 127 } 148 128 } elseif (strpos($action,'del_') === 0) { 149 129 $count = preg_match('#del_(.+)_([0-9]+)#',$action,$match); 150 echo "action=".$action;151 130 if (($count == 1) && isset($this->filters[$match[1]])) { 152 131 $this->filters[$match[1]]->remove($match[2]); 153 echo "remove";154 132 } 155 133 } elseif ($action=="apply") { … … 165 143 if (isset($_POST[$p."query"])) { 166 144 parse_str($_POST[$p."query"],$out); 167 $this->setupAppliedFilters($ out);145 $this->setupAppliedFilters($this->all_filters,$out); 168 146 if ($action == 'reset') { 169 $this->setupEditFilters($out); 147 $this->setupEditFilters($this->all_filters,$out); 148 } elseif ($action == 'clear_filters') { 149 $this->setupEditFilters($this->static_filters,$out); 150 foreach ($this->filters as $f) { 151 $f->cleanup(); 152 } 170 153 } 171 154 } … … 184 167 $get = new ArrayObject(array_merge($this->loadFilters(),$get)); 185 168 } 186 $this->setupEditFilters($get); 187 $this->setupAppliedFilters($get); 188 } 189 $queryParams = $this->getListedFilters(); 169 $this->setupEditFilters($this->all_filters,$get); 170 171 $this->setupAppliedFilters($this->all_filters,$get); 172 } 173 foreach ($this->static_filters as $f) { 174 if (!$f->isEnabled()) { 175 $f->add(); 176 } 177 } 178 $queryParams = $this->getAppliedFilters(); 190 179 $this->addField( 191 180 new dcFieldHidden($this->form_prefix.'query', … … 224 213 @param form_data <b>array</b> form values (usually $_GET or $_POST) 225 214 */ 226 protected function setupEditFilters ($f orm_data) {215 protected function setupEditFilters ($filters,$form_data) { 227 216 $this->hideform = true; 228 foreach ($ this->filters as $filter) {217 foreach ($filters as $filter) { 229 218 $filter->setupFields ($form_data); 230 219 } 231 220 } 232 protected function setupAppliedFilters ($f orm_data) {233 foreach ($ this->filters as $filter) {221 protected function setupAppliedFilters ($filters,$form_data) { 222 foreach ($filters as $filter) { 234 223 $filter->setupAppliedFilter ($form_data); 235 224 } … … 251 240 } 252 241 } 242 foreach ($this->static_filters as $f) { 243 $f->serialize($arr); 244 } 253 245 return $arr; 254 246 } … … 261 253 $filter->setFormPrefix($this->form_prefix); 262 254 $filter->setFilterSet($this); 263 $this->filters[$filter->id] = $filter; 255 $this->all_filters[$filter->id] = $filter; 256 if ($filter->isStatic()) { 257 $this->static_filters[$filter->id] = $filter; 258 } else { 259 $this->filters[$filter->id] = $filter; 260 } 264 261 return $this; 265 262 } 263 266 264 public function getContext() { 267 265 $fcontext = new ArrayObject(); 266 $sfcontext = new ArrayObject(); 268 267 foreach ($this->filters as $f) { 269 268 if($f->isEnabled()) { … … 271 270 } 272 271 } 272 foreach ($this->static_filters as $f) { 273 $f->appendEditLines ($sfcontext); 274 } 273 275 return array( 274 276 'active_filters' => $fcontext, 277 'static_filters' => $sfcontext, 275 278 'prefix'=>$this->form_prefix); 276 279 } 277 280 278 protected function get ListedFilters() {281 protected function getAppliedFilters() { 279 282 $arr = new ArrayObject(); 280 foreach ($this-> filters as $f) {281 if ($f->is Enabled())283 foreach ($this->all_filters as $f) { 284 if ($f->isApplied()) 282 285 $f->updateAppliedValues($arr); 283 286 } … … 292 295 */ 293 296 public function applyFilters($params) { 294 foreach ($this-> filters as $filter) {297 foreach ($this->all_filters as $filter) { 295 298 if ($filter->isApplied()) { 296 299 $filter->applyFilter($params); … … 318 321 public $name; ///<b>string</b> filter name 319 322 public $desc; ///<b>string</b> field description 323 public $filter_id; ///<b>string</b> field id (global to the page) 320 324 protected $request_param; ///<b>string</b> resulting parameter array key 321 protected $enabled; ///<b>string</b> true if filter is enabled 322 protected $fields; ///<b>array</b> currently edited values 325 protected $field; ///<b>array</b> currently edited values 323 326 protected $avalues; ///<b>array</b> currently applied values 324 public $filter_id; ///<b>string</b> field id (global to the page) 325 326 public function __construct ($id,$name,$desc,$request_param) { 327 $this->id = $id; 328 $this->name=$name; 329 $this->desc = $desc; 330 $this->request_param = $request_param; 331 $this->enabled=false; 332 $this->avalues = array(); 333 $this->filter_id = $this->id; 334 $this->fields = array(); 335 } 336 337 public function setupFields($data) { 338 $val = $this->parseData($data); 339 $pos=0; 340 foreach($val['values'] as $v) { 341 $this->addField($pos,$v); 342 $pos++; 343 } 344 $this->enabled = ($pos> 0); 345 }// abstract public function setupFields($data); 346 347 348 public function init() { 327 protected $static; 328 protected $options; 329 protected $multiple; 330 331 public function __construct ($id,$name,$desc,$request_param,$options=array()) { 332 $this->id = $id; 333 $this->name =$name; 334 $this->desc = $desc; 335 $this->request_param = $request_param; 336 $this->avalues = array(); 337 $this->filter_id = $this->id; 338 $this->static = false; 339 $this->multiple = false; 340 $this->options = $options; 341 if (isset($options['static']) && $options['static']) { 342 $this->static = true; 343 } 344 if (isset($options['multiple']) && $options['multiple']) { 345 $this->multiple = true; 346 } 349 347 } 350 348 … … 357 355 */ 358 356 protected function parseData($data) { 359 $count=0; 360 $arr = array(); 361 while (isset($data[$this->getFieldId($count)])) { 362 $arr[] = $data[$this->getFieldId($count)]; 363 $count++; 364 } 357 $arr = $this->field->parseValues($data); 365 358 return array('values' => $arr); 366 359 } 367 360 361 public function isStatic() { 362 return $this->static; 363 } 364 365 public function setupFields($data) { 366 $this->field->setup($data); 367 } 368 369 370 public function init() { 371 } 372 373 public function cleanup() { 374 $this->field->setValues(array()); 375 } 376 368 377 public function setupAppliedFilter($data) { 369 378 $this->avalues = $this->parseData($data); … … 371 380 372 381 public function updateAppliedValues($arr) { 373 foreach ($this->avalues['values'] as $k=>$v) { 374 $arr[$this->getFieldID($k)]=$v; 375 } 382 $arr[$this->filter_id] = $this->avalues['values']; 376 383 } 377 384 … … 396 403 397 404 /** 398 Get a field id399 400 @param pos <b>integer</b> position of field, in case of multiple field (0 if only 1 field set, default value)401 @return <b>string</b> The field ID402 */403 protected function getFieldId($pos=0) {404 if ($pos == 0) {405 return $this->filter_id;406 } else {407 return $this->filter_id.'_'.$pos;408 }409 }410 411 412 /**413 405 Tells whether the filter is enabled or not 414 406 … … 416 408 */ 417 409 public function isEnabled() { 418 return $this->enabled;419 } 420 421 protected abstract function add Field($pos,$value=NULL);410 return count($this->field) != 0; 411 } 412 413 protected abstract function addValue($value=NULL); 422 414 423 415 /** … … 425 417 */ 426 418 public function add() { 427 $this->addField(count($this->fields)); 428 $this->enabled = true; 419 if (count($this->field) > 1 && !$this->multiple) 420 return; 421 $this->addValue(); 429 422 } 430 423 … … 433 426 */ 434 427 public function remove($pos) { 435 if (isset($this->fields[$pos])) { 436 $this->filterset->removeField($this->fields[$pos]); 437 array_splice($this->fields,$pos,1); 438 for ($cur=$pos; $cur<count($this->fields);$cur++) { 439 $this->filterset->renameField($this->fields[$cur],$this->getFieldID($cur)); 440 } 441 $this->enabled = (count($this->fields)!=0); 428 $values = $this->field->getValues(); 429 if (isset($values[$pos])) { 430 $this->field->delValue($pos); 442 431 } 443 432 … … 447 436 448 437 public function appendEditLines($ctx) { 449 foreach ($this->field sas $cur => $f) {438 foreach ($this->field->getValues() as $cur => $f) { 450 439 $line = new ArrayObject(); 451 440 $line['lineclass'] = $this->id; … … 465 454 466 455 public function serialize($arr) { 467 for ($cur=0; $cur < count($this->fields); $cur++) { 468 $arr[$this->getFieldId($cur)]=$this->fields[$cur]->getValue(); 456 if (count($this->fields) == 1) { 457 $arr[$this->filter_id]=$this->field->getValue(); 458 } else { 459 $arr[$this->filter_id]=$this->field->getValues(); 469 460 } 470 461 } … … 492 483 493 484 485 486 class dcFilterText extends dcFilter { 487 488 public function init() { 489 $this->field = new dcFieldText( 490 $this->filter_id, 491 NULL); 492 $this->filterset->addField($this->field); 493 $this->multiple = false; 494 } 495 496 497 public function appendSingleLine($line,$pos) { 498 $line['ffield'] = $this->field->getName(); 499 if ($this->static) { 500 $line['display_inline'] = true; 501 } 502 503 if ($pos == 0) { 504 $line['fwidget']='filter_text'; 505 $line['desc']=$this->desc; 506 }; 507 } 508 509 protected function addValue($value=NULL) { 510 if ($value === NULL) { 511 $value = ''; 512 } 513 $this->field->addValue($value); 514 } 515 516 public function applyFilter($params) { 517 $params[$this->request_param]=$this->avalues['values'][0]; 518 } 519 520 } 494 521 /** 495 522 @ingroup DC_CORE … … 499 526 Handle combo filter on admin side. Can be single or multi-valued 500 527 */ 501 class comboFilter extends dcFilter { 502 protected $options; 528 class dcFilterCombo extends dcFilter { 529 protected $combo; 530 531 public function __construct($id,$name,$desc,$request_param,$combo,$options=array()) { 532 parent::__construct($id,$name,$desc,$request_param,$options); 533 $this->combo = $combo; 534 } 535 public function init() { 536 $this->field = new dcFieldCombo( 537 $this->filter_id, 538 NULL, 539 $this->combo); 540 $this->filterset->addField($this->field); 541 } 542 543 protected function addValue($value=NULL) { 544 if ($value === NULL) { 545 $value = current($this->combo); 546 } 547 $this->field->addValue($value); 548 } 549 550 public function appendSingleLine($line,$pos) { 551 if ($this->static) { 552 $line['display_inline'] = true; 553 } 554 $line['ffield'] = $this->field->getName(); 555 $line['foffset'] = $pos; 556 if ($pos == 0) { 557 $line['fwidget']='filter_combo'; 558 $line['desc']=$this->desc; 559 } else { 560 $line['fwidget']='filter_combo_cont'; 561 }; 562 } 563 564 public function applyFilter($params) { 565 $attr = $this->request_param; 566 if ($this->multiple) 567 $params[$attr]=$this->avalues['values']; 568 else 569 $params[$attr]=$this->avalues['values'][0]; 570 } 571 572 } 573 574 /** 575 @ingroup DC_CORE 576 @nosubgrouping 577 @brief abstract filter class. 578 579 Handle combo filter on admin side. Can be single or multi-valued 580 */ 581 class dcFilterRichCombo extends dcFilterCombo { 503 582 protected $verb; 504 protected $extra;505 506 public function __construct($id,$name,$desc,$request_param,$options,$extra=array()) {507 parent::__construct($id,$name,$desc,$request_param);508 $this->options = $options;509 $this->extra = $extra;510 }511 583 512 584 public function init() { … … 519 591 'isnot'=>__('is not')) 520 592 ); 593 $this->filterset->addField($this->verb); 521 594 } 522 595 523 596 protected function parseData($data) { 524 597 $val = parent::parseData($data); 525 $val['verb'] = $this->verb->parseValue($data); 598 $v = $this->verb->parseValues($data); 599 if (isset($v[0]) && $v[0] === 'isnot') 600 $val['verb'] = 'isnot'; 601 else 602 $val['verb'] ='is'; 526 603 return $val; 527 604 } 528 605 529 protected function addField($pos,$value=NULL) {530 if ($value === NULL) {531 $value = current($this->options);532 }533 $f = new dcFieldCombo(534 $this->getFieldID($pos),535 $value,536 $this->options);537 $this->filterset->addField($f);538 $this->fields[]=$f;539 }540 541 606 public function setupFields($data) { 542 /*$val = $this->parseData($data);543 $this->verb->setup($data);544 $pos=0;545 foreach($val['values'] as $v) {546 $this->addField($pos,$v);547 }548 $this->enabled = (count($this->fields) != 0);*/549 607 parent::setupFields($data); 550 608 $this->verb->setup($data); 551 $this->filterset->addField($this->verb);552 } 609 } 610 553 611 public function updateAppliedValues($arr) { 554 612 parent::updateAppliedValues($arr); 555 if ($this->enabled) { 556 $arr[$this->verb->getName()] = $this->verb->getValue(); 557 } 558 } 559 public function add() { 560 if (isset($this->extra['singleval']) && (count($this->fields) > 0)) 561 return; 562 parent::add(); 613 $arr['verb'] = $this->verb->getValue(); 563 614 } 564 615 565 616 public function appendSingleLine($line,$pos) { 566 $f = $this->fields[$pos]; 567 $line['ffield'] = $f->getName(); 568 617 parent::appendSingleLine($line,$pos); 569 618 if ($pos == 0) { 570 $line['fwidget']='filter_combo';571 619 $line['fverb'] = $this->verb->getName(); 572 $line['desc']=$this->desc; 573 } else { 574 $line['fwidget']='filter_combo_cont'; 575 }; 620 $line['fwidget']='filter_richcombo'; 621 } 576 622 } 577 623 … … 582 628 583 629 public function applyFilter($params) { 630 parent::applyFilter($params); 584 631 $attr = $this->request_param; 585 632 if ($this->avalues['verb'] != "is") { 586 633 $params[$attr."_not"] = true; 587 634 } 588 if (isset($this->extra['singleval']))589 $params[$attr]=$this->avalues['values'][0];590 else591 $params[$attr]=$this->avalues['values'];592 635 } 593 636 594 637 } 595 638 596 class booleanFilter extends dcFilter { 597 protected $options; 598 protected $verb; 599 600 public function __construct($id,$name,$desc,$request_param,$options) { 601 parent::__construct($id,$name,$desc,$request_param); 602 $this->options = $options; 603 } 604 605 606 607 public function appendSingleLine($line,$pos) { 608 $f = $this->fields[$pos]; 609 $line['ffield'] = $f->getName(); 610 611 if ($pos == 0) { 612 $line['fwidget']='filter_boolean'; 613 $line['desc']=$this->desc; 614 }; 615 } 616 617 protected function addField($pos,$value=NULL) { 618 if (count($this->fields)>0) 619 return; 620 if ($value === NULL) { 621 $value = 1; 622 } 623 $f = new dcFieldCombo( 624 $this->getFieldID($pos), 625 $value, 626 $this->options); 627 $this->filterset->addField($f); 628 $this->fields[]=$f; 629 } 630 631 public function applyFilter($params) { 632 $params[$this->request_param]=$this->avalues['values'][0]; 633 } 634 635 } 636 637 class textFilter extends dcFilter { 638 protected $options; 639 protected $verb; 640 641 public function __construct($id,$name,$desc,$request_param,$options) { 642 parent::__construct($id,$name,$desc,$request_param); 643 $this->options = $options; 644 } 645 646 public function setupFields($data) { 647 $val = $this->parseData($data); 648 foreach($val['values'] as $k=>$v) { 649 $this->addField($k,$v); 650 } 651 $this->enabled = (count($this->fields) != 0); 652 } 653 654 public function appendSingleLine($line,$pos) { 655 $f = $this->fields[$pos]; 656 $line['ffield'] = $f->getName(); 657 658 if ($pos == 0) { 659 $line['fwidget']='filter_boolean'; 660 $line['desc']=$this->desc; 661 }; 662 } 663 664 protected function addField($pos,$value=NULL) { 665 if (count($this->fields)>0) 666 return; 667 if ($value === NULL) { 668 $value = ''; 669 } 670 $f = new dcFieldText( 671 $this->getFieldID($pos), 672 $value); 673 $this->filterset->addField($f); 674 $this->fields[]=$f; 675 } 676 677 public function applyFilter($params) { 678 $params[$this->request_param]=$this->avalues['values'][0]; 679 } 680 681 } 682 639 640 641 dcFilterSet::__init__($GLOBALS['core']->tpl); 683 642 ?>
Note: See TracChangeset
for help on using the changeset viewer.