]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/WikicreoleTable.php
Add spreadsheet capability to Wikicreole tables
[SourceForge/phpwiki.git] / lib / plugin / WikicreoleTable.php
1 <?php // -*-php-*-
2 rcs_id('$Id$');
3 /**
4   WikicreoleTablePlugin
5   A PhpWiki plugin that allows insertion of tables using the Wikicreole
6   syntax.
7 */
8 /*
9  * Copyright (C) 2008 Alcatel-Lucent
10  *
11  * This file is part of PhpWiki.
12  *
13  * PhpWiki is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  *
18  * PhpWiki is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with PhpWiki; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26 */
27
28 /*
29  * Standard Alcatel-Lucent disclaimer for contributing to open source
30  *
31  * "The WikicreoleTablePlugin ("Contribution") has not been tested and/or 
32  * validated for release as or in products, combinations with products or
33  * other commercial use. Any use of the Contribution is entirely made at 
34  * the user's own responsibility and the user can not rely on any features,
35  * functionalities or performances Alcatel-Lucent has attributed to the 
36  * Contribution.
37  *
38  * THE CONTRIBUTION BY ALCATEL-LUCENT IS PROVIDED AS IS, WITHOUT WARRANTY 
39  * OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE 
40  * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, COMPLIANCE,
41  * NON-INTERFERENCE AND/OR INTERWORKING WITH THE SOFTWARE TO WHICH THE 
42  * CONTRIBUTION HAS BEEN MADE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL 
43  * ALCATEL-LUCENT BE LIABLE FOR ANY DAMAGES OR OTHER LIABLITY, WHETHER IN 
44  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 
45  * CONTRIBUTION OR THE USE OR OTHER DEALINGS IN THE CONTRIBUTION, WHETHER 
46  * TOGETHER WITH THE SOFTWARE TO WHICH THE CONTRIBUTION RELATES OR ON A STAND 
47  * ALONE BASIS."
48  */
49
50 class WikiPlugin_WikicreoleTable
51 extends WikiPlugin
52 {
53     function getName() {
54         return _("WikicreoleTable");
55     }
56
57     function getDescription() {
58       return _("Layout tables using the Wikicreole syntax.");
59     }
60
61     function getDefaultArguments() {
62         return array();
63     }
64
65     function getVersion() {
66         return preg_replace("/[Revision: $]/", '',
67                             "\$Revision$");
68     }
69
70     function handle_plugin_args_cruft($argstr, $args) {
71         return;
72     }
73
74     function run($dbi, $argstr, &$request, $basepage) {
75         global $WikiTheme;
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                 // trigger_error(sprintf(_("Line %s does not begin with a '|'."), $line), E_USER_WARNING);
93             } else {
94                 $table[] = $this->_parse_row($line, $basepage);
95             }
96         }
97
98         $nbrows = sizeof($table);
99         $nbcols = sizeof($table[0]);
100
101         for ($i=0; $i<$nbrows; $i++) {
102             for ($j=0; $j<$nbcols; $j++) {
103                 if ($table[$i][$j][0] == '@') {
104                     $table[$i][$j] = $this->_compute($table, $i, $j, $nbrows, $nbcols);
105                 }
106             }
107         }
108
109         $htmltable = HTML::table(array('class' => "bordered"));
110         foreach ($table as $row) {
111             $htmlrow = HTML::tr();
112             foreach ($row as $cell) {
113                 if ($cell[0] == '=') {
114                     $cell = trim(substr($cell, 1));
115                     $htmlrow->pushContent(HTML::th(TransformInline($cell, 2.0, $basepage)));
116                 } else {
117                     $htmlrow->pushContent(HTML::td(TransformInline($cell, 2.0, $basepage)));
118                 }
119             }
120             $htmltable->pushContent($htmlrow);
121         }
122         return $htmltable;
123     }
124
125     function _parse_row ($line, $basepage) {
126         $brkt_link = "\\[ .*? [^]\s] .*? \\]";
127         $cell_content  = "(?: [^[] | ".ESCAPE_CHAR."\\[ | $brkt_link )*?";
128         
129         preg_match_all("/(\\|+) \s* ($cell_content) \s* (?=\\||\$)/x",
130                        $line, $matches, PREG_SET_ORDER);
131
132         $row = array();
133
134         foreach ($matches as $m) {
135             $cell = $m[2];
136             $row[]= $cell;
137         }
138         return $row;
139     }
140
141     function _compute ($mytable, $i, $j, $imax, $jmax) {
142
143         // What is implemented:
144         // @@SUM(R)@@ : sum of cells in current row
145         // @@SUM(C)@@ : sum of cells in current column
146
147         $result=0;
148         if (trim($mytable[$i][$j]) == "@@SUM(C)@@") {
149             for ($index=0; $index<$imax; $index++) {
150                 $result += $mytable[$index][$j];
151             }
152         }
153         if (trim($mytable[$i][$j]) == "@@SUM(R)@@") {
154             for ($index=0; $index<$jmax; $index++) {
155                 $result += $mytable[$i][$index];
156             }
157         }
158         return $result;
159     }
160 }
161
162 // For emacs users
163 // Local Variables:
164 // mode: php
165 // tab-width: 8
166 // c-basic-offset: 4
167 // c-hanging-comment-ender-p: nil
168 // indent-tabs-mode: nil
169 // End:
170 ?>