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