]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/WikicreoleTable.php
Merge OldTextFormattingRules into TextFormattingRules; rename underscore plugins
[SourceForge/phpwiki.git] / lib / plugin / WikicreoleTable.php
1 <?php
2
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 getDescription()
55     {
56         return _("Layout tables using the Wikicreole syntax.");
57     }
58
59     function getDefaultArguments()
60     {
61         return array();
62     }
63
64     function handle_plugin_args_cruft($argstr, $args)
65     {
66         return;
67     }
68
69     function run($dbi, $argstr, &$request, $basepage)
70     {
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         $nb_rows = sizeof($table);
92         // If table is empty, do not generate table markup
93         if ($nb_rows == 0) {
94             return HTML::raw('');
95         }
96
97         // Number of columns is the number of cells in the longer row
98         $nb_cols = 0;
99         for ($i = 0; $i < $nb_rows; $i++) {
100             $nb_cols = max($nb_cols, sizeof($table[$i]));
101         }
102
103         for ($i = 0; $i < $nb_rows; $i++) {
104             for ($j = 0; $j < $nb_cols; $j++) {
105                 if (!isset($table[$i][$j])) {
106                     $table[$i][$j] = '';
107                 } elseif (preg_match('/@@/', $table[$i][$j])) {
108                     $table[$i][$j] = $this->compute_table_cell($table, $i, $j, $nb_rows, $nb_cols);
109                 }
110             }
111         }
112
113         $html_table = HTML::table(array('class' => "bordered"));
114         foreach ($table as $row) {
115             $html_row = HTML::tr();
116             foreach ($row as $cell) {
117                 if ($cell && $cell[0] == '=') {
118                     $cell = trim(substr($cell, 1));
119                     $html_row->pushContent(HTML::th(TransformInline($cell, $basepage)));
120                 } else {
121                     if (is_numeric($cell)) {
122                         $html_row->pushContent(HTML::td(array('style' => "text-align:right"), $cell));
123                     } else {
124                         $html_row->pushContent(HTML::td(TransformInline($cell, $basepage)));
125                     }
126                 }
127             }
128             $html_table->pushContent($html_row);
129         }
130         return $html_table;
131     }
132
133     private function parse_row($line)
134     {
135         $bracket_link = "\\[ .*? [^]\s] .*? \\]";
136         $cell_content = "(?: [^[] | " . ESCAPE_CHAR . "\\[ | $bracket_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     private function compute_table_cell($table, $i, $j, $imax, $jmax)
157     {
158
159         // What is implemented:
160         // @@=SUM(R)@@ : sum of cells in current row
161         // @@=SUM(C)@@ : sum of cells in current column
162         // @@=AVERAGE(R)@@ : average of cells in current row
163         // @@=AVERAGE(C)@@ : average of cells in current column
164         // @@=MAX(R)@@ : maximum value of cells in current row
165         // @@=MAX(C)@@ : maximum value of cells in current column
166         // @@=MIN(R)@@ : minimum value of cells in current row
167         // @@=MIN(C)@@ : minimum value of cells in current column
168         // @@=COUNT(R)@@ : number of cells in current row
169         //                (numeric or not, excluding headers and current cell)
170         // @@=COUNT(C)@@ : number of cells in current column
171         //                (numeric or not, excluding headers and current cell)
172
173         $result = 0;
174         $counter = 0;
175         $found = false;
176
177         if (strpos($table[$i][$j], "@@=SUM(C)@@") !== false) {
178             for ($index = 0; $index < $imax; $index++) {
179                 if (is_numeric($table[$index][$j])) {
180                     $result += $table[$index][$j];
181                 }
182             }
183             return str_replace("@@=SUM(C)@@", $result, $table[$i][$j]);
184
185         } elseif (strpos($table[$i][$j], "@@=SUM(R)@@") !== false) {
186             for ($index = 0; $index < $jmax; $index++) {
187                 if (is_numeric($table[$i][$index])) {
188                     $result += $table[$i][$index];
189                 }
190             }
191             return str_replace("@@=SUM(R)@@", $result, $table[$i][$j]);
192
193         } elseif (strpos($table[$i][$j], "@@=AVERAGE(C)@@") !== false) {
194             for ($index = 0; $index < $imax; $index++) {
195                 if (is_numeric($table[$index][$j])) {
196                     $result += $table[$index][$j];
197                     $counter++;
198                 }
199             }
200             $result = $result / $counter;
201             return str_replace("@@=AVERAGE(C)@@", $result, $table[$i][$j]);
202
203         } elseif (strpos($table[$i][$j], "@@=AVERAGE(R)@@") !== false) {
204             for ($index = 0; $index < $jmax; $index++) {
205                 if (is_numeric($table[$i][$index])) {
206                     $result += $table[$i][$index];
207                     $counter++;
208                 }
209             }
210             $result = $result / $counter;
211             return str_replace("@@=AVERAGE(R)@@", $result, $table[$i][$j]);
212
213         } elseif (strpos($table[$i][$j], "@@=MAX(C)@@") !== false) {
214             for ($index = 0; $index < $imax; $index++) {
215                 if (is_numeric($table[$index][$j])) {
216                     if (!$found) {
217                         $found = true;
218                         $result = $table[$index][$j];
219                     } else {
220                         $result = max($result, $table[$index][$j]);
221                     }
222                 }
223             }
224             if (!$found) {
225                 $result = "";
226             }
227             return str_replace("@@=MAX(C)@@", $result, $table[$i][$j]);
228
229         } elseif (strpos($table[$i][$j], "@@=MAX(R)@@") !== false) {
230             for ($index = 0; $index < $jmax; $index++) {
231                 if (is_numeric($table[$i][$index])) {
232                     if (!$found) {
233                         $found = true;
234                         $result = $table[$i][$index];
235                     } else {
236                         $result = max($result, $table[$i][$index]);
237                     }
238                 }
239             }
240             if (!$found) {
241                 $result = "";
242             }
243             return str_replace("@@=MAX(R)@@", $result, $table[$i][$j]);
244
245         } elseif (strpos($table[$i][$j], "@@=MIN(C)@@") !== false) {
246             for ($index = 0; $index < $imax; $index++) {
247                 if (is_numeric($table[$index][$j])) {
248                     if (!$found) {
249                         $found = true;
250                         $result = $table[$index][$j];
251                     } else {
252                         $result = min($result, $table[$index][$j]);
253                     }
254                 }
255             }
256             if (!$found) {
257                 $result = "";
258             }
259             return str_replace("@@=MIN(C)@@", $result, $table[$i][$j]);
260
261         } elseif (strpos($table[$i][$j], "@@=MIN(R)@@") !== false) {
262             for ($index = 0; $index < $jmax; $index++) {
263                 if (is_numeric($table[$i][$index])) {
264                     if (!$found) {
265                         $found = true;
266                         $result = $table[$i][$index];
267                     } else {
268                         $result = min($result, $table[$i][$index]);
269                     }
270                 }
271             }
272             if (!$found) {
273                 $result = "";
274             }
275             return str_replace("@@=MIN(R)@@", $result, $table[$i][$j]);
276
277         } elseif (strpos($table[$i][$j], "@@=COUNT(C)@@") !== false) {
278             for ($index = 0; $index < $imax; $index++) {
279                 // exclude header
280                 if (!string_starts_with(trim($table[$index][$j]), "=")) {
281                     $counter++;
282                 }
283             }
284             $result = $counter - 1; // exclude self
285             return str_replace("@@=COUNT(C)@@", $result, $table[$i][$j]);
286
287         } elseif (strpos($table[$i][$j], "@@=COUNT(R)@@") !== false) {
288             for ($index = 0; $index < $jmax; $index++) {
289                 // exclude header
290                 if (!string_starts_with(trim($table[$i][$index]), "=")) {
291                     $counter++;
292                 }
293             }
294             $result = $counter - 1; // exclude self
295             return str_replace("@@=COUNT(R)@@", $result, $table[$i][$j]);
296         }
297
298         return $table[$i][$j];
299     }
300
301 }
302
303 // Local Variables:
304 // mode: php
305 // tab-width: 8
306 // c-basic-offset: 4
307 // c-hanging-comment-ender-p: nil
308 // indent-tabs-mode: nil
309 // End: