]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/WikicreoleTable.php
Move compute_tablecell to Wikicreoletable plugin
[SourceForge/phpwiki.git] / lib / plugin / WikicreoleTable.php
1 <?php // -*-php-*-
2 // $Id$
3 /*
4  * Copyright (C) 2008-2009, 2011 Marc-Etienne Vargenau, Alcatel-Lucent
5  *
6  * This file is part of PhpWiki.
7  *
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.
12  *
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.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with PhpWiki; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 /*
24  * Standard Alcatel-Lucent disclaimer for contributing to open source
25  *
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
31  * Contribution.
32  *
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
42  * ALONE BASIS."
43  */
44
45 /**
46  * WikicreoleTablePlugin
47  * A PhpWiki plugin that allows insertion of tables using the Wikicreole
48  * syntax.
49  */
50
51 class WikiPlugin_WikicreoleTable
52 extends WikiPlugin
53 {
54     function getName() {
55         return _("WikicreoleTable");
56     }
57
58     function getDescription() {
59       return _("Layout tables using the Wikicreole syntax.");
60     }
61
62     function getDefaultArguments() {
63         return array();
64     }
65
66     function handle_plugin_args_cruft($argstr, $args) {
67         return;
68     }
69
70     function run($dbi, $argstr, &$request, $basepage) {
71         global $WikiTheme;
72         include_once('lib/InlineParser.php');
73
74         $table = array();
75
76         $lines = preg_split('/\s*?\n\s*/', $argstr);
77
78         foreach ($lines as $line) {
79             if (!$line) {
80                 continue;
81             }
82             $line = trim($line);
83             // If line ends with a '|', remove it
84             if ($line[strlen($line)-1] == '|') {
85                 $line = substr($line, 0, -1);
86             }
87             if ($line[0] == '|') {
88                 $table[] = $this->_parse_row($line, $basepage);
89             }
90         }
91
92         $nbrows = sizeof($table);
93         // If table is empty, do not generate table markup
94         if ($nbrows == 0) {
95             return HTML::raw('');
96         }
97
98         // Number of columns is the number of cells in the longer row
99         $nbcols = 0;
100         for ($i=0; $i<$nbrows; $i++) {
101             $nbcols = max($nbcols, sizeof($table[$i]));
102         }
103
104         for ($i=0; $i<$nbrows; $i++) {
105             for ($j=0; $j<$nbcols; $j++) {
106                 if (!isset($table[$i][$j])) {
107                     $table[$i][$j] = '';
108                 } else if (preg_match('/@@/', $table[$i][$j])) {
109                     $table[$i][$j] = $this->_compute_tablecell($table, $i, $j, $nbrows, $nbcols);
110                 }
111             }
112         }
113
114         $htmltable = HTML::table(array('class' => "bordered"));
115         foreach ($table as $row) {
116             $htmlrow = HTML::tr();
117             foreach ($row as $cell) {
118                 if ($cell && $cell[0] == '=') {
119                     $cell = trim(substr($cell, 1));
120                     $htmlrow->pushContent(HTML::th(TransformInline($cell, 2.0, $basepage)));
121                 } else {
122                     if (is_numeric($cell)) {
123                         $htmlrow->pushContent(HTML::td(array('style' => "text-align:right"), $cell));
124                     } else {
125                         $htmlrow->pushContent(HTML::td(TransformInline($cell, 2.0, $basepage)));
126                     }
127                 }
128             }
129             $htmltable->pushContent($htmlrow);
130         }
131         return $htmltable;
132     }
133
134     function _parse_row ($line, $basepage) {
135         $brkt_link = "\\[ .*? [^]\s] .*? \\]";
136         $cell_content  = "(?: [^[] | ".ESCAPE_CHAR."\\[ | $brkt_link )*?";
137
138         preg_match_all("/(\\|+) \s* ($cell_content) \s* (?=\\||\$)/x",
139                        $line, $matches, PREG_SET_ORDER);
140
141         $row = array();
142
143         foreach ($matches as $m) {
144             $cell = $m[2];
145             $row[]= $cell;
146         }
147         return $row;
148     }
149
150     /**
151      * Compute cell in spreadsheet table
152      * $table: two-dimensional table
153      * $i and $j: indexes of cell to compute
154      * $imax and $jmax: table dimensions
155      */
156     function _compute_tablecell ($table, $i, $j, $imax, $jmax) {
157
158         // What is implemented:
159         // @@=SUM(R)@@ : sum of cells in current row
160         // @@=SUM(C)@@ : sum of cells in current column
161         // @@=AVERAGE(R)@@ : average of cells in current row
162         // @@=AVERAGE(C)@@ : average of cells in current column
163         // @@=MAX(R)@@ : maximum value of cells in current row
164         // @@=MAX(C)@@ : maximum value of cells in current column
165         // @@=MIN(R)@@ : minimum value of cells in current row
166         // @@=MIN(C)@@ : minimum value of cells in current column
167         // @@=COUNT(R)@@ : number of cells in current row
168         //                (numeric or not, excluding headers and current cell)
169         // @@=COUNT(C)@@ : number of cells in current column
170         //                (numeric or not, excluding headers and current cell)
171
172         $result=0;
173         $counter=0;
174         $found=false;
175
176         if (strpos($table[$i][$j], "@@=SUM(C)@@") !== false) {
177             for ($index=0; $index<$imax; $index++) {
178                 if (is_numeric($table[$index][$j])) {
179                     $result += $table[$index][$j];
180                 }
181             }
182             return str_replace("@@=SUM(C)@@", $result, $table[$i][$j]);
183
184         } else if (strpos($table[$i][$j], "@@=SUM(R)@@") !== false) {
185             for ($index=0; $index<$jmax; $index++) {
186                 if (is_numeric($table[$i][$index])) {
187                     $result += $table[$i][$index];
188                 }
189             }
190             return str_replace("@@=SUM(R)@@", $result, $table[$i][$j]);
191
192         } else if (strpos($table[$i][$j], "@@=AVERAGE(C)@@") !== false) {
193             for ($index=0; $index<$imax; $index++) {
194                 if (is_numeric($table[$index][$j])) {
195                     $result += $table[$index][$j];
196                     $counter++;
197                 }
198             }
199             $result=$result/$counter;
200             return str_replace("@@=AVERAGE(C)@@", $result, $table[$i][$j]);
201
202         } else if (strpos($table[$i][$j], "@@=AVERAGE(R)@@") !== false) {
203             for ($index=0; $index<$jmax; $index++) {
204                 if (is_numeric($table[$i][$index])) {
205                     $result += $table[$i][$index];
206                     $counter++;
207                 }
208             }
209             $result=$result/$counter;
210             return str_replace("@@=AVERAGE(R)@@", $result, $table[$i][$j]);
211
212         } else if (strpos($table[$i][$j], "@@=MAX(C)@@") !== false) {
213             for ($index=0; $index<$imax; $index++) {
214                 if (is_numeric($table[$index][$j])) {
215                     if (!$found) {
216                         $found=true;
217                         $result=$table[$index][$j];
218                     } else {
219                         $result = max($result, $table[$index][$j]);
220                     }
221                 }
222             }
223             if (!$found) {
224                 $result="";
225             }
226             return str_replace("@@=MAX(C)@@", $result, $table[$i][$j]);
227
228         } else if (strpos($table[$i][$j], "@@=MAX(R)@@") !== false) {
229             for ($index=0; $index<$jmax; $index++) {
230                 if (is_numeric($table[$i][$index])) {
231                     if (!$found) {
232                         $found=true;
233                         $result=$table[$i][$index];
234                     } else {
235                         $result = max($result, $table[$i][$index]);
236                     }
237                 }
238             }
239             if (!$found) {
240                 $result="";
241             }
242             return str_replace("@@=MAX(R)@@", $result, $table[$i][$j]);
243
244         } else if (strpos($table[$i][$j], "@@=MIN(C)@@") !== false) {
245             for ($index=0; $index<$imax; $index++) {
246                 if (is_numeric($table[$index][$j])) {
247                     if (!$found) {
248                         $found=true;
249                         $result=$table[$index][$j];
250                     } else {
251                         $result = min($result, $table[$index][$j]);
252                     }
253                 }
254             }
255             if (!$found) {
256                 $result="";
257             }
258             return str_replace("@@=MIN(C)@@", $result, $table[$i][$j]);
259
260         } else if (strpos($table[$i][$j], "@@=MIN(R)@@") !== false) {
261             for ($index=0; $index<$jmax; $index++) {
262                 if (is_numeric($table[$i][$index])) {
263                     if (!$found) {
264                         $found=true;
265                         $result=$table[$i][$index];
266                     } else {
267                         $result = min($result, $table[$i][$index]);
268                     }
269                 }
270             }
271             if (!$found) {
272                 $result="";
273             }
274             return str_replace("@@=MIN(R)@@", $result, $table[$i][$j]);
275
276         } else if (strpos($table[$i][$j], "@@=COUNT(C)@@") !== false) {
277             for ($index=0; $index<$imax; $index++) {
278                 // exclude header
279                 if (!string_starts_with(trim($table[$index][$j]), "=")) {
280                     $counter++;
281                 }
282             }
283             $result = $counter-1; // exclude self
284             return str_replace("@@=COUNT(C)@@", $result, $table[$i][$j]);
285
286         } else if (strpos($table[$i][$j], "@@=COUNT(R)@@") !== false) {
287             for ($index=0; $index<$jmax; $index++) {
288                 // exclude header
289                 if (!string_starts_with(trim($table[$i][$index]), "=")) {
290                     $counter++;
291                 }
292             }
293             $result = $counter-1; // exclude self
294             return str_replace("@@=COUNT(R)@@", $result, $table[$i][$j]);
295         }
296
297         return $table[$i][$j];
298     }
299
300 }
301
302 // Local Variables:
303 // mode: php
304 // tab-width: 8
305 // c-basic-offset: 4
306 // c-hanging-comment-ender-p: nil
307 // indent-tabs-mode: nil
308 // End:
309 ?>