4 * Copyright (C) 2008-2009, 2011 Marc-Etienne Vargenau, Alcatel-Lucent
6 * This file is part of PhpWiki.
8 * PhpWiki is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * PhpWiki is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * Standard Alcatel-Lucent disclaimer for contributing to open source
26 * "The WikicreoleTablePlugin ("Contribution") has not been tested and/or
27 * validated for release as or in products, combinations with products or
28 * other commercial use. Any use of the Contribution is entirely made at
29 * the user's own responsibility and the user can not rely on any features,
30 * functionalities or performances Alcatel-Lucent has attributed to the
33 * THE CONTRIBUTION BY ALCATEL-LUCENT IS PROVIDED AS IS, WITHOUT WARRANTY
34 * OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
35 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, COMPLIANCE,
36 * NON-INTERFERENCE AND/OR INTERWORKING WITH THE SOFTWARE TO WHICH THE
37 * CONTRIBUTION HAS BEEN MADE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL
38 * ALCATEL-LUCENT BE LIABLE FOR ANY DAMAGES OR OTHER LIABLITY, WHETHER IN
39 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
40 * CONTRIBUTION OR THE USE OR OTHER DEALINGS IN THE CONTRIBUTION, WHETHER
41 * TOGETHER WITH THE SOFTWARE TO WHICH THE CONTRIBUTION RELATES OR ON A STAND
46 * WikicreoleTablePlugin
47 * A PhpWiki plugin that allows insertion of tables using the Wikicreole
51 class WikiPlugin_WikicreoleTable
55 return _("WikicreoleTable");
58 function getDescription() {
59 return _("Layout tables using the Wikicreole syntax.");
62 function getDefaultArguments() {
66 function handle_plugin_args_cruft($argstr, $args) {
70 function run($dbi, $argstr, &$request, $basepage) {
71 include_once('lib/InlineParser.php');
75 $lines = preg_split('/\s*?\n\s*/', $argstr);
77 foreach ($lines as $line) {
82 // If line ends with a '|', remove it
83 if ($line[strlen($line)-1] == '|') {
84 $line = substr($line, 0, -1);
86 if ($line[0] == '|') {
87 $table[] = $this->_parse_row($line);
91 $nbrows = sizeof($table);
92 // If table is empty, do not generate table markup
97 // Number of columns is the number of cells in the longer row
99 for ($i=0; $i<$nbrows; $i++) {
100 $nbcols = max($nbcols, sizeof($table[$i]));
103 for ($i=0; $i<$nbrows; $i++) {
104 for ($j=0; $j<$nbcols; $j++) {
105 if (!isset($table[$i][$j])) {
107 } else if (preg_match('/@@/', $table[$i][$j])) {
108 $table[$i][$j] = $this->_compute_tablecell($table, $i, $j, $nbrows, $nbcols);
113 $htmltable = HTML::table(array('class' => "bordered"));
114 foreach ($table as $row) {
115 $htmlrow = HTML::tr();
116 foreach ($row as $cell) {
117 if ($cell && $cell[0] == '=') {
118 $cell = trim(substr($cell, 1));
119 $htmlrow->pushContent(HTML::th(TransformInline($cell, 2.0, $basepage)));
121 if (is_numeric($cell)) {
122 $htmlrow->pushContent(HTML::td(array('style' => "text-align:right"), $cell));
124 $htmlrow->pushContent(HTML::td(TransformInline($cell, 2.0, $basepage)));
128 $htmltable->pushContent($htmlrow);
133 function _parse_row ($line) {
134 $brkt_link = "\\[ .*? [^]\s] .*? \\]";
135 $cell_content = "(?: [^[] | ".ESCAPE_CHAR."\\[ | $brkt_link )*?";
137 preg_match_all("/(\\|+) \s* ($cell_content) \s* (?=\\||\$)/x",
138 $line, $matches, PREG_SET_ORDER);
142 foreach ($matches as $m) {
150 * Compute cell in spreadsheet table
151 * $table: two-dimensional table
152 * $i and $j: indexes of cell to compute
153 * $imax and $jmax: table dimensions
155 function _compute_tablecell ($table, $i, $j, $imax, $jmax) {
157 // What is implemented:
158 // @@=SUM(R)@@ : sum of cells in current row
159 // @@=SUM(C)@@ : sum of cells in current column
160 // @@=AVERAGE(R)@@ : average of cells in current row
161 // @@=AVERAGE(C)@@ : average of cells in current column
162 // @@=MAX(R)@@ : maximum value of cells in current row
163 // @@=MAX(C)@@ : maximum value of cells in current column
164 // @@=MIN(R)@@ : minimum value of cells in current row
165 // @@=MIN(C)@@ : minimum value of cells in current column
166 // @@=COUNT(R)@@ : number of cells in current row
167 // (numeric or not, excluding headers and current cell)
168 // @@=COUNT(C)@@ : number of cells in current column
169 // (numeric or not, excluding headers and current cell)
175 if (strpos($table[$i][$j], "@@=SUM(C)@@") !== false) {
176 for ($index=0; $index<$imax; $index++) {
177 if (is_numeric($table[$index][$j])) {
178 $result += $table[$index][$j];
181 return str_replace("@@=SUM(C)@@", $result, $table[$i][$j]);
183 } else if (strpos($table[$i][$j], "@@=SUM(R)@@") !== false) {
184 for ($index=0; $index<$jmax; $index++) {
185 if (is_numeric($table[$i][$index])) {
186 $result += $table[$i][$index];
189 return str_replace("@@=SUM(R)@@", $result, $table[$i][$j]);
191 } else if (strpos($table[$i][$j], "@@=AVERAGE(C)@@") !== false) {
192 for ($index=0; $index<$imax; $index++) {
193 if (is_numeric($table[$index][$j])) {
194 $result += $table[$index][$j];
198 $result=$result/$counter;
199 return str_replace("@@=AVERAGE(C)@@", $result, $table[$i][$j]);
201 } else if (strpos($table[$i][$j], "@@=AVERAGE(R)@@") !== false) {
202 for ($index=0; $index<$jmax; $index++) {
203 if (is_numeric($table[$i][$index])) {
204 $result += $table[$i][$index];
208 $result=$result/$counter;
209 return str_replace("@@=AVERAGE(R)@@", $result, $table[$i][$j]);
211 } else if (strpos($table[$i][$j], "@@=MAX(C)@@") !== false) {
212 for ($index=0; $index<$imax; $index++) {
213 if (is_numeric($table[$index][$j])) {
216 $result=$table[$index][$j];
218 $result = max($result, $table[$index][$j]);
225 return str_replace("@@=MAX(C)@@", $result, $table[$i][$j]);
227 } else if (strpos($table[$i][$j], "@@=MAX(R)@@") !== false) {
228 for ($index=0; $index<$jmax; $index++) {
229 if (is_numeric($table[$i][$index])) {
232 $result=$table[$i][$index];
234 $result = max($result, $table[$i][$index]);
241 return str_replace("@@=MAX(R)@@", $result, $table[$i][$j]);
243 } else if (strpos($table[$i][$j], "@@=MIN(C)@@") !== false) {
244 for ($index=0; $index<$imax; $index++) {
245 if (is_numeric($table[$index][$j])) {
248 $result=$table[$index][$j];
250 $result = min($result, $table[$index][$j]);
257 return str_replace("@@=MIN(C)@@", $result, $table[$i][$j]);
259 } else if (strpos($table[$i][$j], "@@=MIN(R)@@") !== false) {
260 for ($index=0; $index<$jmax; $index++) {
261 if (is_numeric($table[$i][$index])) {
264 $result=$table[$i][$index];
266 $result = min($result, $table[$i][$index]);
273 return str_replace("@@=MIN(R)@@", $result, $table[$i][$j]);
275 } else if (strpos($table[$i][$j], "@@=COUNT(C)@@") !== false) {
276 for ($index=0; $index<$imax; $index++) {
278 if (!string_starts_with(trim($table[$index][$j]), "=")) {
282 $result = $counter-1; // exclude self
283 return str_replace("@@=COUNT(C)@@", $result, $table[$i][$j]);
285 } else if (strpos($table[$i][$j], "@@=COUNT(R)@@") !== false) {
286 for ($index=0; $index<$jmax; $index++) {
288 if (!string_starts_with(trim($table[$i][$index]), "=")) {
292 $result = $counter-1; // exclude self
293 return str_replace("@@=COUNT(R)@@", $result, $table[$i][$j]);
296 return $table[$i][$j];
305 // c-hanging-comment-ender-p: nil
306 // indent-tabs-mode: nil