]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/WikicreoleTable.php
Remove unused argument
[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 along
19  * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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         include_once('lib/InlineParser.php');
72
73         $table = array();
74
75         $lines = preg_split('/\s*?\n\s*/', $argstr);
76
77         foreach ($lines as $line) {
78             if (!$line) {
79                 continue;
80             }
81             $line = trim($line);
82             // If line ends with a '|', remove it
83             if ($line[strlen($line)-1] == '|') {
84                 $line = substr($line, 0, -1);
85             }
86             if ($line[0] == '|') {
87                 $table[] = $this->_parse_row($line);
88             }
89         }
90
91         $nbrows = sizeof($table);
92         // If table is empty, do not generate table markup
93         if ($nbrows == 0) {
94             return HTML::raw('');
95         }
96
97         // Number of columns is the number of cells in the longer row
98         $nbcols = 0;
99         for ($i=0; $i<$nbrows; $i++) {
100             $nbcols = max($nbcols, sizeof($table[$i]));
101         }
102
103         for ($i=0; $i<$nbrows; $i++) {
104             for ($j=0; $j<$nbcols; $j++) {
105                 if (!isset($table[$i][$j])) {
106                     $table[$i][$j] = '';
107                 } else if (preg_match('/@@/', $table[$i][$j])) {
108                     $table[$i][$j] = $this->_compute_tablecell($table, $i, $j, $nbrows, $nbcols);
109                 }
110             }
111         }
112
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)));
120                 } else {
121                     if (is_numeric($cell)) {
122                         $htmlrow->pushContent(HTML::td(array('style' => "text-align:right"), $cell));
123                     } else {
124                         $htmlrow->pushContent(HTML::td(TransformInline($cell, 2.0, $basepage)));
125                     }
126                 }
127             }
128             $htmltable->pushContent($htmlrow);
129         }
130         return $htmltable;
131     }
132
133     function _parse_row ($line) {
134         $brkt_link = "\\[ .*? [^]\s] .*? \\]";
135         $cell_content  = "(?: [^[] | ".ESCAPE_CHAR."\\[ | $brkt_link )*?";
136
137         preg_match_all("/(\\|+) \s* ($cell_content) \s* (?=\\||\$)/x",
138                        $line, $matches, PREG_SET_ORDER);
139
140         $row = array();
141
142         foreach ($matches as $m) {
143             $cell = $m[2];
144             $row[]= $cell;
145         }
146         return $row;
147     }
148
149     /**
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
154      */
155     function _compute_tablecell ($table, $i, $j, $imax, $jmax) {
156
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)
170
171         $result=0;
172         $counter=0;
173         $found=false;
174
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];
179                 }
180             }
181             return str_replace("@@=SUM(C)@@", $result, $table[$i][$j]);
182
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];
187                 }
188             }
189             return str_replace("@@=SUM(R)@@", $result, $table[$i][$j]);
190
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];
195                     $counter++;
196                 }
197             }
198             $result=$result/$counter;
199             return str_replace("@@=AVERAGE(C)@@", $result, $table[$i][$j]);
200
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];
205                     $counter++;
206                 }
207             }
208             $result=$result/$counter;
209             return str_replace("@@=AVERAGE(R)@@", $result, $table[$i][$j]);
210
211         } else if (strpos($table[$i][$j], "@@=MAX(C)@@") !== false) {
212             for ($index=0; $index<$imax; $index++) {
213                 if (is_numeric($table[$index][$j])) {
214                     if (!$found) {
215                         $found=true;
216                         $result=$table[$index][$j];
217                     } else {
218                         $result = max($result, $table[$index][$j]);
219                     }
220                 }
221             }
222             if (!$found) {
223                 $result="";
224             }
225             return str_replace("@@=MAX(C)@@", $result, $table[$i][$j]);
226
227         } else if (strpos($table[$i][$j], "@@=MAX(R)@@") !== false) {
228             for ($index=0; $index<$jmax; $index++) {
229                 if (is_numeric($table[$i][$index])) {
230                     if (!$found) {
231                         $found=true;
232                         $result=$table[$i][$index];
233                     } else {
234                         $result = max($result, $table[$i][$index]);
235                     }
236                 }
237             }
238             if (!$found) {
239                 $result="";
240             }
241             return str_replace("@@=MAX(R)@@", $result, $table[$i][$j]);
242
243         } else if (strpos($table[$i][$j], "@@=MIN(C)@@") !== false) {
244             for ($index=0; $index<$imax; $index++) {
245                 if (is_numeric($table[$index][$j])) {
246                     if (!$found) {
247                         $found=true;
248                         $result=$table[$index][$j];
249                     } else {
250                         $result = min($result, $table[$index][$j]);
251                     }
252                 }
253             }
254             if (!$found) {
255                 $result="";
256             }
257             return str_replace("@@=MIN(C)@@", $result, $table[$i][$j]);
258
259         } else if (strpos($table[$i][$j], "@@=MIN(R)@@") !== false) {
260             for ($index=0; $index<$jmax; $index++) {
261                 if (is_numeric($table[$i][$index])) {
262                     if (!$found) {
263                         $found=true;
264                         $result=$table[$i][$index];
265                     } else {
266                         $result = min($result, $table[$i][$index]);
267                     }
268                 }
269             }
270             if (!$found) {
271                 $result="";
272             }
273             return str_replace("@@=MIN(R)@@", $result, $table[$i][$j]);
274
275         } else if (strpos($table[$i][$j], "@@=COUNT(C)@@") !== false) {
276             for ($index=0; $index<$imax; $index++) {
277                 // exclude header
278                 if (!string_starts_with(trim($table[$index][$j]), "=")) {
279                     $counter++;
280                 }
281             }
282             $result = $counter-1; // exclude self
283             return str_replace("@@=COUNT(C)@@", $result, $table[$i][$j]);
284
285         } else if (strpos($table[$i][$j], "@@=COUNT(R)@@") !== false) {
286             for ($index=0; $index<$jmax; $index++) {
287                 // exclude header
288                 if (!string_starts_with(trim($table[$i][$index]), "=")) {
289                     $counter++;
290                 }
291             }
292             $result = $counter-1; // exclude self
293             return str_replace("@@=COUNT(R)@@", $result, $table[$i][$j]);
294         }
295
296         return $table[$i][$j];
297     }
298
299 }
300
301 // Local Variables:
302 // mode: php
303 // tab-width: 8
304 // c-basic-offset: 4
305 // c-hanging-comment-ender-p: nil
306 // indent-tabs-mode: nil
307 // End:
308 ?>