ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Subtotal.php
Go to the documentation of this file.
1 <?php
2 
4 
8 
9 class Subtotal
10 {
15  protected static function filterHiddenArgs($cellReference, $args): array
16  {
17  return array_filter(
18  $args,
19  function ($index) use ($cellReference) {
20  [, $row, ] = explode('.', $index);
21 
22  return $cellReference->getWorksheet()->getRowDimension($row)->getVisible();
23  },
24  ARRAY_FILTER_USE_KEY
25  );
26  }
27 
32  protected static function filterFormulaArgs($cellReference, $args): array
33  {
34  return array_filter(
35  $args,
36  function ($index) use ($cellReference) {
37  [, $row, $column] = explode('.', $index);
38  $retVal = true;
39  if ($cellReference->getWorksheet()->cellExists($column . $row)) {
40  //take this cell out if it contains the SUBTOTAL or AGGREGATE functions in a formula
41  $isFormula = $cellReference->getWorksheet()->getCell($column . $row)->isFormula();
42  $cellFormula = !preg_match('/^=.*\b(SUBTOTAL|AGGREGATE)\s*\(/i', $cellReference->getWorksheet()->getCell($column . $row)->getValue());
43 
44  $retVal = !$isFormula || $cellFormula;
45  }
46 
47  return $retVal;
48  },
49  ARRAY_FILTER_USE_KEY
50  );
51  }
52 
54  private const CALL_FUNCTIONS = [
55  1 => [Statistical\Averages::class, 'average'],
56  [Statistical\Counts::class, 'COUNT'], // 2
57  [Statistical\Counts::class, 'COUNTA'], // 3
58  [Statistical\Maximum::class, 'max'], // 4
59  [Statistical\Minimum::class, 'min'], // 5
60  [Operations::class, 'product'], // 6
61  [Statistical\StandardDeviations::class, 'STDEV'], // 7
62  [Statistical\StandardDeviations::class, 'STDEVP'], // 8
63  [Sum::class, 'sumIgnoringStrings'], // 9
64  [Statistical\Variances::class, 'VAR'], // 10
65  [Statistical\Variances::class, 'VARP'], // 11
66  ];
67 
84  public static function evaluate($functionType, ...$args)
85  {
86  $cellReference = array_pop($args);
87  $aArgs = Functions::flattenArrayIndexed($args);
88 
89  try {
90  $subtotal = (int) Helpers::validateNumericNullBool($functionType);
91  } catch (Exception $e) {
92  return $e->getMessage();
93  }
94 
95  // Calculate
96  if ($subtotal > 100) {
97  $aArgs = self::filterHiddenArgs($cellReference, $aArgs);
98  $subtotal -= 100;
99  }
100 
101  $aArgs = self::filterFormulaArgs($cellReference, $aArgs);
102  if (array_key_exists($subtotal, self::CALL_FUNCTIONS)) {
104  $call = self::CALL_FUNCTIONS[$subtotal];
105 
106  return call_user_func_array($call, $aArgs);
107  }
108 
109  return Functions::VALUE();
110  }
111 }
static validateNumericNullBool($number)
Many functions accept null/false/true argument treated as 0/0/1.
Definition: Helpers.php:27
static filterFormulaArgs($cellReference, $args)
Definition: Subtotal.php:32
$index
Definition: metadata.php:60
static flattenArrayIndexed($array)
Convert a multi-dimensional array to a simple 1-dimensional array, but retain an element of indexing...
Definition: Functions.php:616
$row
static filterHiddenArgs($cellReference, $args)
Definition: Subtotal.php:15