From ae7a4662769855dc6ed9c115809d472341d7ea04 Mon Sep 17 00:00:00 2001 From: vargenau Date: Fri, 7 Jan 2011 10:04:56 +0000 Subject: [PATCH] Move compute_tablecell to Wikicreoletable plugin git-svn-id: svn://svn.code.sf.net/p/phpwiki/code/trunk@7819 96ab9672-09ca-45d6-a79d-3d69d39ca109 --- lib/plugin/WikicreoleTable.php | 152 ++++++++++++++++++++++++++++++++- lib/stdlib.php | 149 -------------------------------- 2 files changed, 151 insertions(+), 150 deletions(-) diff --git a/lib/plugin/WikicreoleTable.php b/lib/plugin/WikicreoleTable.php index c42395cb0..f7d68aef0 100644 --- a/lib/plugin/WikicreoleTable.php +++ b/lib/plugin/WikicreoleTable.php @@ -106,7 +106,7 @@ extends WikiPlugin if (!isset($table[$i][$j])) { $table[$i][$j] = ''; } else if (preg_match('/@@/', $table[$i][$j])) { - $table[$i][$j] = compute_tablecell($table, $i, $j, $nbrows, $nbcols); + $table[$i][$j] = $this->_compute_tablecell($table, $i, $j, $nbrows, $nbcols); } } } @@ -147,6 +147,156 @@ extends WikiPlugin return $row; } + /** + * Compute cell in spreadsheet table + * $table: two-dimensional table + * $i and $j: indexes of cell to compute + * $imax and $jmax: table dimensions + */ + function _compute_tablecell ($table, $i, $j, $imax, $jmax) { + + // What is implemented: + // @@=SUM(R)@@ : sum of cells in current row + // @@=SUM(C)@@ : sum of cells in current column + // @@=AVERAGE(R)@@ : average of cells in current row + // @@=AVERAGE(C)@@ : average of cells in current column + // @@=MAX(R)@@ : maximum value of cells in current row + // @@=MAX(C)@@ : maximum value of cells in current column + // @@=MIN(R)@@ : minimum value of cells in current row + // @@=MIN(C)@@ : minimum value of cells in current column + // @@=COUNT(R)@@ : number of cells in current row + // (numeric or not, excluding headers and current cell) + // @@=COUNT(C)@@ : number of cells in current column + // (numeric or not, excluding headers and current cell) + + $result=0; + $counter=0; + $found=false; + + if (strpos($table[$i][$j], "@@=SUM(C)@@") !== false) { + for ($index=0; $index<$imax; $index++) { + if (is_numeric($table[$index][$j])) { + $result += $table[$index][$j]; + } + } + return str_replace("@@=SUM(C)@@", $result, $table[$i][$j]); + + } else if (strpos($table[$i][$j], "@@=SUM(R)@@") !== false) { + for ($index=0; $index<$jmax; $index++) { + if (is_numeric($table[$i][$index])) { + $result += $table[$i][$index]; + } + } + return str_replace("@@=SUM(R)@@", $result, $table[$i][$j]); + + } else if (strpos($table[$i][$j], "@@=AVERAGE(C)@@") !== false) { + for ($index=0; $index<$imax; $index++) { + if (is_numeric($table[$index][$j])) { + $result += $table[$index][$j]; + $counter++; + } + } + $result=$result/$counter; + return str_replace("@@=AVERAGE(C)@@", $result, $table[$i][$j]); + + } else if (strpos($table[$i][$j], "@@=AVERAGE(R)@@") !== false) { + for ($index=0; $index<$jmax; $index++) { + if (is_numeric($table[$i][$index])) { + $result += $table[$i][$index]; + $counter++; + } + } + $result=$result/$counter; + return str_replace("@@=AVERAGE(R)@@", $result, $table[$i][$j]); + + } else if (strpos($table[$i][$j], "@@=MAX(C)@@") !== false) { + for ($index=0; $index<$imax; $index++) { + if (is_numeric($table[$index][$j])) { + if (!$found) { + $found=true; + $result=$table[$index][$j]; + } else { + $result = max($result, $table[$index][$j]); + } + } + } + if (!$found) { + $result=""; + } + return str_replace("@@=MAX(C)@@", $result, $table[$i][$j]); + + } else if (strpos($table[$i][$j], "@@=MAX(R)@@") !== false) { + for ($index=0; $index<$jmax; $index++) { + if (is_numeric($table[$i][$index])) { + if (!$found) { + $found=true; + $result=$table[$i][$index]; + } else { + $result = max($result, $table[$i][$index]); + } + } + } + if (!$found) { + $result=""; + } + return str_replace("@@=MAX(R)@@", $result, $table[$i][$j]); + + } else if (strpos($table[$i][$j], "@@=MIN(C)@@") !== false) { + for ($index=0; $index<$imax; $index++) { + if (is_numeric($table[$index][$j])) { + if (!$found) { + $found=true; + $result=$table[$index][$j]; + } else { + $result = min($result, $table[$index][$j]); + } + } + } + if (!$found) { + $result=""; + } + return str_replace("@@=MIN(C)@@", $result, $table[$i][$j]); + + } else if (strpos($table[$i][$j], "@@=MIN(R)@@") !== false) { + for ($index=0; $index<$jmax; $index++) { + if (is_numeric($table[$i][$index])) { + if (!$found) { + $found=true; + $result=$table[$i][$index]; + } else { + $result = min($result, $table[$i][$index]); + } + } + } + if (!$found) { + $result=""; + } + return str_replace("@@=MIN(R)@@", $result, $table[$i][$j]); + + } else if (strpos($table[$i][$j], "@@=COUNT(C)@@") !== false) { + for ($index=0; $index<$imax; $index++) { + // exclude header + if (!string_starts_with(trim($table[$index][$j]), "=")) { + $counter++; + } + } + $result = $counter-1; // exclude self + return str_replace("@@=COUNT(C)@@", $result, $table[$i][$j]); + + } else if (strpos($table[$i][$j], "@@=COUNT(R)@@") !== false) { + for ($index=0; $index<$jmax; $index++) { + // exclude header + if (!string_starts_with(trim($table[$i][$index]), "=")) { + $counter++; + } + } + $result = $counter-1; // exclude self + return str_replace("@@=COUNT(R)@@", $result, $table[$i][$j]); + } + + return $table[$i][$j]; + } + } // Local Variables: diff --git a/lib/stdlib.php b/lib/stdlib.php index d2a972d15..43ee28604 100644 --- a/lib/stdlib.php +++ b/lib/stdlib.php @@ -100,7 +100,6 @@ parse_attributes($line) is_image ($filename) is_video ($filename) - compute_tablecell ($table, $i, $j, $imax, $jmax) function: linkExistingWikiWord($wikiword, $linktext, $version) moved to: lib/WikiTheme.php @@ -2483,154 +2482,6 @@ function is_video ($filename) { or string_ends_with(strtolower($filename), ".ogg"); } -/** - * Compute cell in spreadsheet table - * $table: two-dimensional table - * $i and $j: indexes of cell to compute - * $imax and $jmax: table dimensions - */ -function compute_tablecell ($table, $i, $j, $imax, $jmax) { - - // What is implemented: - // @@=SUM(R)@@ : sum of cells in current row - // @@=SUM(C)@@ : sum of cells in current column - // @@=AVERAGE(R)@@ : average of cells in current row - // @@=AVERAGE(C)@@ : average of cells in current column - // @@=MAX(R)@@ : maximum value of cells in current row - // @@=MAX(C)@@ : maximum value of cells in current column - // @@=MIN(R)@@ : minimum value of cells in current row - // @@=MIN(C)@@ : minimum value of cells in current column - // @@=COUNT(R)@@ : number of cells in current row - // (numeric or not, excluding headers and current cell) - // @@=COUNT(C)@@ : number of cells in current column - // (numeric or not, excluding headers and current cell) - - $result=0; - $counter=0; - $found=false; - - if (strpos($table[$i][$j], "@@=SUM(C)@@") !== false) { - for ($index=0; $index<$imax; $index++) { - if (is_numeric($table[$index][$j])) { - $result += $table[$index][$j]; - } - } - return str_replace("@@=SUM(C)@@", $result, $table[$i][$j]); - - } else if (strpos($table[$i][$j], "@@=SUM(R)@@") !== false) { - for ($index=0; $index<$jmax; $index++) { - if (is_numeric($table[$i][$index])) { - $result += $table[$i][$index]; - } - } - return str_replace("@@=SUM(R)@@", $result, $table[$i][$j]); - - } else if (strpos($table[$i][$j], "@@=AVERAGE(C)@@") !== false) { - for ($index=0; $index<$imax; $index++) { - if (is_numeric($table[$index][$j])) { - $result += $table[$index][$j]; - $counter++; - } - } - $result=$result/$counter; - return str_replace("@@=AVERAGE(C)@@", $result, $table[$i][$j]); - - } else if (strpos($table[$i][$j], "@@=AVERAGE(R)@@") !== false) { - for ($index=0; $index<$jmax; $index++) { - if (is_numeric($table[$i][$index])) { - $result += $table[$i][$index]; - $counter++; - } - } - $result=$result/$counter; - return str_replace("@@=AVERAGE(R)@@", $result, $table[$i][$j]); - - } else if (strpos($table[$i][$j], "@@=MAX(C)@@") !== false) { - for ($index=0; $index<$imax; $index++) { - if (is_numeric($table[$index][$j])) { - if (!$found) { - $found=true; - $result=$table[$index][$j]; - } else { - $result = max($result, $table[$index][$j]); - } - } - } - if (!$found) { - $result=""; - } - return str_replace("@@=MAX(C)@@", $result, $table[$i][$j]); - - } else if (strpos($table[$i][$j], "@@=MAX(R)@@") !== false) { - for ($index=0; $index<$jmax; $index++) { - if (is_numeric($table[$i][$index])) { - if (!$found) { - $found=true; - $result=$table[$i][$index]; - } else { - $result = max($result, $table[$i][$index]); - } - } - } - if (!$found) { - $result=""; - } - return str_replace("@@=MAX(R)@@", $result, $table[$i][$j]); - - } else if (strpos($table[$i][$j], "@@=MIN(C)@@") !== false) { - for ($index=0; $index<$imax; $index++) { - if (is_numeric($table[$index][$j])) { - if (!$found) { - $found=true; - $result=$table[$index][$j]; - } else { - $result = min($result, $table[$index][$j]); - } - } - } - if (!$found) { - $result=""; - } - return str_replace("@@=MIN(C)@@", $result, $table[$i][$j]); - - } else if (strpos($table[$i][$j], "@@=MIN(R)@@") !== false) { - for ($index=0; $index<$jmax; $index++) { - if (is_numeric($table[$i][$index])) { - if (!$found) { - $found=true; - $result=$table[$i][$index]; - } else { - $result = min($result, $table[$i][$index]); - } - } - } - if (!$found) { - $result=""; - } - return str_replace("@@=MIN(R)@@", $result, $table[$i][$j]); - - } else if (strpos($table[$i][$j], "@@=COUNT(C)@@") !== false) { - for ($index=0; $index<$imax; $index++) { - if (!string_starts_with(trim($table[$index][$j]), "=")) { // exclude header - $counter++; - } - } - $result = $counter-1; // exclude self - return str_replace("@@=COUNT(C)@@", $result, $table[$i][$j]); - - } else if (strpos($table[$i][$j], "@@=COUNT(R)@@") !== false) { - for ($index=0; $index<$jmax; $index++) { - if (!string_starts_with(trim($table[$i][$index]), "=")) { // exclude header - $counter++; - } - } - $result = $counter-1; // exclude self - return str_replace("@@=COUNT(R)@@", $result, $table[$i][$j]); - } - - return $table[$i][$j]; -} - /** * Remove accents from given text. */ -- 2.45.0