@@ -426,12 +426,6 @@ private static function _filterTestInPeriodDateSet($cellValue,$monthSet)
426
426
return FALSE ;
427
427
}
428
428
429
- private static function _filterTypeTopTenFilters ($ cellValue ,$ testSet )
430
- {
431
- echo 'CALLING _filterTypeTopTenFilters ' ,PHP_EOL ;
432
- return TRUE ;
433
- }
434
-
435
429
/**
436
430
* Search/Replace arrays to convert Excel wildcard syntax to a regexp syntax for preg_matching
437
431
*
@@ -448,7 +442,7 @@ private static function _filterTypeTopTenFilters($cellValue,$testSet)
448
442
* @param PHPExcel_Worksheet_AutoFilter_Column $filterColumn
449
443
* @return mixed[]
450
444
*/
451
- private function _dynamicFilterDateRange ($ dynamicRuleType , $ filterColumn )
445
+ private function _dynamicFilterDateRange ($ dynamicRuleType , & $ filterColumn )
452
446
{
453
447
$ rDateType = PHPExcel_Calculation_Functions::getReturnDateType ();
454
448
PHPExcel_Calculation_Functions::setReturnDateType (PHPExcel_Calculation_Functions::RETURNDATE_PHP_NUMERIC );
@@ -541,7 +535,7 @@ private function _dynamicFilterDateRange($dynamicRuleType, $filterColumn)
541
535
542
536
// Set the filter column rule attributes ready for writing
543
537
$ filterColumn ->setAttributes (array ( 'val ' => $ val ,
544
- 'maxVal ' , $ maxVal
538
+ 'maxVal ' => $ maxVal
545
539
)
546
540
);
547
541
@@ -562,6 +556,22 @@ private function _dynamicFilterDateRange($dynamicRuleType, $filterColumn)
562
556
);
563
557
}
564
558
559
+ private function _calculateTopTenValue ($ columnID ,$ startRow ,$ endRow ,$ ruleType ,$ ruleValue ) {
560
+ $ range = $ columnID .$ startRow .': ' .$ columnID .$ endRow ;
561
+ $ dataValues = PHPExcel_Calculation_Functions::flattenArray (
562
+ $ this ->_workSheet ->rangeToArray ($ range ,NULL ,TRUE ,FALSE )
563
+ );
564
+
565
+ $ dataValues = array_filter ($ dataValues );
566
+ if ($ ruleType == PHPExcel_Worksheet_AutoFilter_Column_Rule::AUTOFILTER_COLUMN_RULE_TOPTEN_TOP ) {
567
+ rsort ($ dataValues );
568
+ } else {
569
+ sort ($ dataValues );
570
+ }
571
+
572
+ return array_pop (array_slice ($ dataValues ,0 ,$ ruleValue ));
573
+ }
574
+
565
575
/**
566
576
* Apply the AutoFilter rules to the AutoFilter Range
567
577
*
@@ -573,7 +583,7 @@ public function showHideRows()
573
583
list ($ rangeStart ,$ rangeEnd ) = PHPExcel_Cell::rangeBoundaries ($ this ->_range );
574
584
575
585
// The heading row should always be visible
576
- echo 'AutoFilter Heading Row ' ,$ rangeStart [1 ],' is always SHOWN ' ,PHP_EOL ;
586
+ // echo 'AutoFilter Heading Row ',$rangeStart[1],' is always SHOWN',PHP_EOL;
577
587
$ this ->_workSheet ->getRowDimension ($ rangeStart [1 ])->setVisible (TRUE );
578
588
579
589
$ columnFilterTests = array ();
@@ -717,32 +727,49 @@ public function showHideRows()
717
727
$ ruleValues = array ();
718
728
$ dataRowCount = $ rangeEnd [1 ] - $ rangeStart [1 ];
719
729
foreach ($ rules as $ rule ) {
720
- var_dump ($ rule );
721
730
// We should only ever have one Dynamic Filter Rule anyway
722
731
$ toptenRuleType = $ rule ->getGrouping ();
723
732
$ ruleValue = $ rule ->getValue ();
733
+ $ ruleOperator = $ rule ->getOperator ();
734
+ }
735
+ if ($ ruleOperator === PHPExcel_Worksheet_AutoFilter_Column_Rule::AUTOFILTER_COLUMN_RULE_TOPTEN_PERCENT ) {
736
+ $ ruleValue = floor ($ ruleValue * ($ dataRowCount / 100 ));
724
737
}
725
- var_dump ($ toptenRuleType );
726
- var_dump ($ ruleValue );
738
+ if ($ ruleValue < 1 ) $ ruleValue = 1 ;
739
+ if ($ ruleValue > 500 ) $ ruleValue = 500 ;
740
+
741
+ $ maxVal = $ this ->_calculateTopTenValue ($ columnID ,$ rangeStart [1 ]+1 ,$ rangeEnd [1 ],$ toptenRuleType ,$ ruleValue );
742
+
743
+ $ operator = ($ toptenRuleType == PHPExcel_Worksheet_AutoFilter_Column_Rule::AUTOFILTER_COLUMN_RULE_TOPTEN_TOP )
744
+ ? PHPExcel_Worksheet_AutoFilter_Column_Rule::AUTOFILTER_COLUMN_RULE_GREATERTHANOREQUAL
745
+ : PHPExcel_Worksheet_AutoFilter_Column_Rule::AUTOFILTER_COLUMN_RULE_LESSTHANOREQUAL ;
746
+ $ ruleValues [] = array ( 'operator ' => $ operator ,
747
+ 'value ' => $ maxVal
748
+ );
727
749
$ columnFilterTests [$ columnID ] = array (
728
- 'method ' => '_filterTypeTopTenFilters ' ,
729
- 'arguments ' => $ ruleValues
750
+ 'method ' => '_filterTestInCustomDataSet ' ,
751
+ 'arguments ' => array ( 'filterRules ' => $ ruleValues ,
752
+ 'join ' => PHPExcel_Worksheet_AutoFilter_Column::AUTOFILTER_COLUMN_JOIN_OR
753
+ )
754
+ );
755
+ $ filterColumn ->setAttributes (
756
+ array ('maxVal ' => $ maxVal )
730
757
);
731
758
break ;
732
759
}
733
760
}
734
761
735
- echo 'Column Filter Test CRITERIA ' ,PHP_EOL ;
736
- var_dump ($ columnFilterTests );
737
-
762
+ // echo 'Column Filter Test CRITERIA',PHP_EOL;
763
+ // var_dump($columnFilterTests);
764
+ //
738
765
// Execute the column tests for each row in the autoFilter range to determine show/hide,
739
766
for ($ row = $ rangeStart [1 ]+1 ; $ row <= $ rangeEnd [1 ]; ++$ row ) {
740
- echo 'Testing Row = ' ,$ row ,PHP_EOL ;
767
+ // echo 'Testing Row = ',$row,PHP_EOL;
741
768
$ result = TRUE ;
742
769
foreach ($ columnFilterTests as $ columnID => $ columnFilterTest ) {
743
- echo 'Testing cell ' ,$ columnID .$ row ,PHP_EOL ;
770
+ // echo 'Testing cell ',$columnID.$row,PHP_EOL;
744
771
$ cellValue = $ this ->_workSheet ->getCell ($ columnID .$ row )->getCalculatedValue ();
745
- echo 'Value is ' ,$ cellValue ,PHP_EOL ;
772
+ // echo 'Value is ',$cellValue,PHP_EOL;
746
773
// Execute the filter test
747
774
$ result = $ result &&
748
775
call_user_func_array (
@@ -752,13 +779,13 @@ public function showHideRows()
752
779
$ columnFilterTest ['arguments ' ]
753
780
)
754
781
);
755
- echo (($ result ) ? 'VALID ' : 'INVALID ' ),PHP_EOL ;
782
+ // echo (($result) ? 'VALID' : 'INVALID'),PHP_EOL;
756
783
// If filter test has resulted in FALSE, exit the loop straightaway rather than running any more tests
757
784
if (!$ result )
758
785
break ;
759
786
}
760
787
// Set show/hide for the row based on the result of the autoFilter result
761
- echo (($ result ) ? 'SHOW ' : 'HIDE ' ),PHP_EOL ;
788
+ // echo (($result) ? 'SHOW' : 'HIDE'),PHP_EOL;
762
789
$ this ->_workSheet ->getRowDimension ($ row )->setVisible ($ result );
763
790
}
764
791
0 commit comments