]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/WikicreoleTable.php
No tabs
[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 ($this->is_wiki_numeric($cell)) {
122                         $html_row->pushContent(HTML::td(array('style' => "text-align:right"), TransformInline($cell, $basepage)));
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     // $cell is a number, possibly in bold, italics or underlined
134     private function is_wiki_numeric($cell)
135     {
136         return is_numeric(trim($cell, "*/_'"));
137     }
138
139     private function parse_row($line)
140     {
141         $line = str_replace('|', ' |', $line);
142
143         $bracket_link = "\\[ .*? [^]\s] .*? \\]";
144         $cell_content = "(?: [^[] | " . ESCAPE_CHAR . "\\[ | $bracket_link )*?";
145
146         preg_match_all("/(\\|+) \s* ($cell_content) \s* (?=\\||\$)/x",
147             $line, $matches, PREG_SET_ORDER);
148
149         $row = array();
150
151         foreach ($matches as $m) {
152             $cell = $m[2];
153             $row[] = $cell;
154         }
155         return $row;
156     }
157
158     /**
159      * Compute cell in spreadsheet table
160      * @param array $table: two-dimensional table
161      * @param int $i: first index of cell to compute
162      * @param int $j: second index of cell to compute
163      * @param int $imax: first table dimension
164      * @param int $jmax: second table dimension
165      * @return int
166      */
167     private function compute_table_cell($table, $i, $j, $imax, $jmax)
168     {
169
170         // What is implemented:
171         // @@=SUM(R)@@ : sum of cells in current row
172         // @@=SUM(C)@@ : sum of cells in current column
173         // @@=AVERAGE(R)@@ : average of cells in current row
174         // @@=AVERAGE(C)@@ : average of cells in current column
175         // @@=MAX(R)@@ : maximum value of cells in current row
176         // @@=MAX(C)@@ : maximum value of cells in current column
177         // @@=MIN(R)@@ : minimum value of cells in current row
178         // @@=MIN(C)@@ : minimum value of cells in current column
179         // @@=COUNT(R)@@ : number of cells in current row
180         //                (numeric or not, excluding headers and current cell)
181         // @@=COUNT(C)@@ : number of cells in current column
182         //                (numeric or not, excluding headers and current cell)
183
184         $result = 0;
185         $counter = 0;
186         $found = false;
187
188         if (strpos($table[$i][$j], "@@=SUM(C)@@") !== false) {
189             for ($index = 0; $index < $imax; $index++) {
190                 if (is_numeric($table[$index][$j])) {
191                     $result += $table[$index][$j];
192                 }
193             }
194             return str_replace("@@=SUM(C)@@", $result, $table[$i][$j]);
195
196         } elseif (strpos($table[$i][$j], "@@=SUM(R)@@") !== false) {
197             for ($index = 0; $index < $jmax; $index++) {
198                 if (is_numeric($table[$i][$index])) {
199                     $result += $table[$i][$index];
200                 }
201             }
202             return str_replace("@@=SUM(R)@@", $result, $table[$i][$j]);
203
204         } elseif (strpos($table[$i][$j], "@@=AVERAGE(C)@@") !== false) {
205             for ($index = 0; $index < $imax; $index++) {
206                 if (is_numeric($table[$index][$j])) {
207                     $result += $table[$index][$j];
208                     $counter++;
209                 }
210             }
211             $result = $result / $counter;
212             return str_replace("@@=AVERAGE(C)@@", $result, $table[$i][$j]);
213
214         } elseif (strpos($table[$i][$j], "@@=AVERAGE(R)@@") !== false) {
215             for ($index = 0; $index < $jmax; $index++) {
216                 if (is_numeric($table[$i][$index])) {
217                     $result += $table[$i][$index];
218                     $counter++;
219                 }
220             }
221             $result = $result / $counter;
222             return str_replace("@@=AVERAGE(R)@@", $result, $table[$i][$j]);
223
224         } elseif (strpos($table[$i][$j], "@@=MAX(C)@@") !== false) {
225             for ($index = 0; $index < $imax; $index++) {
226                 if (is_numeric($table[$index][$j])) {
227                     if (!$found) {
228                         $found = true;
229                         $result = $table[$index][$j];
230                     } else {
231                         $result = max($result, $table[$index][$j]);
232                     }
233                 }
234             }
235             if (!$found) {
236                 $result = "";
237             }
238             return str_replace("@@=MAX(C)@@", $result, $table[$i][$j]);
239
240         } elseif (strpos($table[$i][$j], "@@=MAX(R)@@") !== false) {
241             for ($index = 0; $index < $jmax; $index++) {
242                 if (is_numeric($table[$i][$index])) {
243                     if (!$found) {
244                         $found = true;
245                         $result = $table[$i][$index];
246                     } else {
247                         $result = max($result, $table[$i][$index]);
248                     }
249                 }
250             }
251             if (!$found) {
252                 $result = "";
253             }
254             return str_replace("@@=MAX(R)@@", $result, $table[$i][$j]);
255
256         } elseif (strpos($table[$i][$j], "@@=MIN(C)@@") !== false) {
257             for ($index = 0; $index < $imax; $index++) {
258                 if (is_numeric($table[$index][$j])) {
259                     if (!$found) {
260                         $found = true;
261                         $result = $table[$index][$j];
262                     } else {
263                         $result = min($result, $table[$index][$j]);
264                     }
265                 }
266             }
267             if (!$found) {
268                 $result = "";
269             }
270             return str_replace("@@=MIN(C)@@", $result, $table[$i][$j]);
271
272         } elseif (strpos($table[$i][$j], "@@=MIN(R)@@") !== false) {
273             for ($index = 0; $index < $jmax; $index++) {
274                 if (is_numeric($table[$i][$index])) {
275                     if (!$found) {
276                         $found = true;
277                         $result = $table[$i][$index];
278                     } else {
279                         $result = min($result, $table[$i][$index]);
280                     }
281                 }
282             }
283             if (!$found) {
284                 $result = "";
285             }
286             return str_replace("@@=MIN(R)@@", $result, $table[$i][$j]);
287
288         } elseif (strpos($table[$i][$j], "@@=COUNT(C)@@") !== false) {
289             for ($index = 0; $index < $imax; $index++) {
290                 // exclude header
291                 if (!string_starts_with(trim($table[$index][$j]), "=")) {
292                     $counter++;
293                 }
294             }
295             if (string_starts_with(trim($table[$i][$j]), "=")) {
296                 $result = $counter;
297             } else {
298                 $result = $counter - 1; // exclude self
299             }
300             return str_replace("@@=COUNT(C)@@", $result, $table[$i][$j]);
301
302         } elseif (strpos($table[$i][$j], "@@=COUNT(R)@@") !== false) {
303             for ($index = 0; $index < $jmax; $index++) {
304                 // exclude header
305                 if (!string_starts_with(trim($table[$i][$index]), "=")) {
306                     $counter++;
307                 }
308             }
309             if (string_starts_with(trim($table[$i][$j]), "=")) {
310                 $result = $counter;
311             } else {
312                 $result = $counter - 1; // exclude self
313             }
314             return str_replace("@@=COUNT(R)@@", $result, $table[$i][$j]);
315         }
316
317         return $table[$i][$j];
318     }
319
320 }
321
322 // Local Variables:
323 // mode: php
324 // tab-width: 8
325 // c-basic-offset: 4
326 // c-hanging-comment-ender-p: nil
327 // indent-tabs-mode: nil
328 // End: