From 27f23ef8794754fbf2c0b01ba2b488827e4382ee Mon Sep 17 00:00:00 2001 From: Jeremy Dorn Date: Sun, 3 Nov 2013 13:44:21 -0800 Subject: [PATCH] Fix FILTER and COLUMNS headers to work with multiple datasets. --- classes/headers/ColumnsHeader.php | 17 ++++++++-- classes/headers/FilterHeader.php | 5 ++- lib/PhpReports/Report.php | 54 +++++++++++++++++++++++-------- 3 files changed, 58 insertions(+), 18 deletions(-) diff --git a/classes/headers/ColumnsHeader.php b/classes/headers/ColumnsHeader.php index 5b2ff849..e01420f3 100644 --- a/classes/headers/ColumnsHeader.php +++ b/classes/headers/ColumnsHeader.php @@ -1,15 +1,23 @@ $options) { + foreach($params['columns'] as $column=>$options) { if(!isset($options['type'])) throw new Exception("Must specify column type for column $column"); $type = $options['type']; unset($options['type']); - $report->addFilter($column,$type,$options); + $report->addFilter($params['dataset'],$column,$type,$options); } } public static function parseShortcut($value) { + if(preg_match('/^[0-9]+\:/',$value)) { + $dataset = substr($value,0,strpos($value,':')); + $value = substr($value,strlen($dataset)+1); + } + else { + $dataset = 0; + } + $parts = explode(',',$value); $params = array(); $i = 1; @@ -80,6 +88,9 @@ public static function parseShortcut($value) { $i++; } - return $params; + return array( + 'dataset'=>$dataset, + 'columns'=>$params + ); } } diff --git a/classes/headers/FilterHeader.php b/classes/headers/FilterHeader.php index 39712e6c..f85de5c8 100644 --- a/classes/headers/FilterHeader.php +++ b/classes/headers/FilterHeader.php @@ -12,11 +12,14 @@ class FilterHeader extends HeaderBase { 'params'=>array( 'type'=>'object', 'default'=>array() + ), + 'dataset'=>array( + 'default'=>0 ) ); public static function init($params, &$report) { - $report->addFilter($params['column'],$params['filter'],$params['params']); + $report->addFilter($params['dataset'],$params['column'],$params['filter'],$params['params']); } //in format: column, params diff --git a/lib/PhpReports/Report.php b/lib/PhpReports/Report.php index 900f771a..f4a4af07 100644 --- a/lib/PhpReports/Report.php +++ b/lib/PhpReports/Report.php @@ -231,21 +231,47 @@ public function parseHeader($name,$value) { } } - public function addFilter($column, $type, $options) { - if(!isset($this->filters[$column])) $this->filters[$column] = array(); + public function addFilter($dataset, $column, $type, $options) { + // If adding for multiple datasets + if(is_array($dataset)) { + foreach($dataset as $d) { + $this->addFilter($d,$column,$type,$options); + } + } + // If adding for all datasets + else if($dataset === true) { + $this->addFilter('all',$column,$type,$options); + } + // If adding for a single dataset + else { + if(!isset($this->filters[$dataset])) $this->filters[$dataset] = array(); + if(!isset($this->filters[$dataset][$column])) $this->filters[$dataset][$column] = array(); + + $this->filters[$dataset][$column][$type] = $options; + } - $this->filters[$column][$type] = $options; } - protected function applyFilters($column, $value, $row) { - //no filters to apply - if(!isset($this->filters[$column])) return $value; + protected function applyFilters($dataset, $column, $value, $row) { + // First, apply filters for all datasets + if(isset($this->filters['all']) && isset($this->filters['all'][$column])) { + foreach($this->filters['all'][$column] as $type=>$options) { + $classname = $type.'Filter'; + $value = $classname::filter($value, $options, $this, $row); + + //if the column should not be displayed + if($value === false) return false; + } + } - foreach($this->filters[$column] as $type=>$options) { - $classname = $type.'Filter'; - $value = $classname::filter($value, $options, $this, $row); - - //if the column should not be displayed - if($value === false) return false; + // Then apply filters for this specific dataset + if(isset($this->filters[$dataset]) && isset($this->filters[$dataset][$column])) { + foreach($this->filters[$dataset][$column] as $type=>$options) { + $classname = $type.'Filter'; + $value = $classname::filter($value, $options, $this, $row); + + //if the column should not be displayed + if($value === false) return false; + } } return $value; @@ -455,9 +481,9 @@ protected function prepareRows($dataset) { $val = new ReportValue($i, $key, $value); //apply filters for the column key - $val = $this->applyFilters($key,$val,$row); + $val = $this->applyFilters($dataset,$key,$val,$row); //apply filters for the column position - if($val) $val = $this->applyFilters($i,$val,$row); + if($val) $val = $this->applyFilters($dataset,$i,$val,$row); if($val) { $rowval[] = $val;