Skip to content

Commit

Permalink
Fix FILTER and COLUMNS headers to work with multiple datasets.
Browse files Browse the repository at this point in the history
  • Loading branch information
jdorn committed Nov 3, 2013
1 parent 713921d commit 27f23ef
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 18 deletions.
17 changes: 14 additions & 3 deletions classes/headers/ColumnsHeader.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
<?php
class ColumnsHeader extends HeaderBase {
public static function init($params, &$report) {
foreach($params as $column=>$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;
Expand Down Expand Up @@ -80,6 +88,9 @@ public static function parseShortcut($value) {
$i++;
}

return $params;
return array(
'dataset'=>$dataset,
'columns'=>$params
);
}
}
5 changes: 4 additions & 1 deletion classes/headers/FilterHeader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
54 changes: 40 additions & 14 deletions lib/PhpReports/Report.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 27f23ef

Please sign in to comment.