]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/WikiFormRich.php
just docs
[SourceForge/phpwiki.git] / lib / plugin / WikiFormRich.php
1 <?php // -*-php-*-
2 rcs_id('$Id: WikiFormRich.php,v 1.8 2004-11-24 10:58:50 rurban Exp $');
3 /**
4  Copyright 2004 $ThePhpWikiProgrammingTeam
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
19  along with PhpWiki; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 /**
24  * This is another replacement for MagicPhpWikiURL forms.
25  * Previously encoded with the "phpwiki:" syntax.
26  *
27  * Enhanced WikiForm to be more generic:
28  * - editbox[]          name=.. value=.. text=..
29  * - checkbox[]         name=.. value=0|1 checked text=..
30  * - radiobutton[]      name=.. value=.. text=..
31  * - pulldown[]         name=.. values=.. selected=.. text=..  (not yet!)
32  * - hidden[]           name=.. value=..
33  * - action, submit buttontext, optional cancel button (bool)
34  * - method=GET or POST ((Default: POST).
35  
36  * values which are constants are evaluated.
37  * The cancel button must be supported by the action. (which?)
38
39  * improve layout: nobr=1, class=wikiadmin
40  TODO:
41  * add pulldown, possibly with <!plugin-list !>
42
43  Samples:
44    <?plugin WikiFormRich action=dumpserial method=GET 
45             checkbox[] name=include value="all" 
46             editbox[] name=directory value=DEFAULT_DUMP_DIR
47             editbox[] name=pages value=*
48             editbox[] name=exclude value="" ?>
49    <?plugin WikiFormRich action=dumphtml method=GET 
50             editbox[] name=directory value=HTML_DUMP_DIR
51             editbox[] name=pages value="*"
52             editbox[] name=exclude value="" ?>
53    <?plugin WikiFormRich action=loadfile method=GET 
54             editbox[]  name=source value=DEFAULT_WIKI_PGSRC
55             checkbox[] name=overwrite value=1
56             editbox[]  name=exclude value="" ?>
57   <?plugin WikiFormRich action=TitleSearch method=GET class=wikiadmin nobr=1
58            editbox[] name=s text=""
59            checkbox[] name=case_exact
60           checkbox[] name=regex ?>
61   <?plugin WikiFormRich action=FullTextSearch method=GET class=wikiadmin nobr=1
62            editbox[] name=s text=""
63            checkbox[] name=case_exact
64            checkbox[] name=regex ?>
65   <?plugin WikiFormRich action=FuzzyPages method=GET class=wikiadmin nobr=1
66            editbox[] name=s text=""
67            checkbox[] name=case_exact ?>
68 */
69
70 class WikiPlugin_WikiFormRich
71 extends WikiPlugin
72 {
73     function getName () {
74         return "WikiFormRich";
75     }
76     function getDescription () {
77         return _("Provide generic WikiForm input buttons");
78     }
79     function getVersion() {
80         return preg_replace("/[Revision: $]/", '',
81                             "\$Revision: 1.8 $");
82     }
83     function getDefaultArguments() {
84         return array('action' => false,     // required argument
85                      'method' => 'POST',    // or GET
86                      'class'  => false,
87                      'buttontext' => false, // for the submit button. default: action
88                      'cancel' => false,     // boolean if the action supports cancel also
89                      'nobr' => false,       // "no break": linebreaks or not
90                      );
91     }
92
93     function handle_plugin_args_cruft($argstr, $args) {
94         $allowed = array("editbox", "hidden", "checkbox", "radiobutton", "pulldown");
95         // no editbox[] = array(...) allowed (space)
96         $arg_array = preg_split("/\n/", $argstr);
97         // for security we should check this better
98         $arg = '';
99         for ($i = 0; $i < count($arg_array); $i++) {
100             if (preg_match("/^\s*(".join("|",$allowed).")\[\]\s+(.+)\s*$/", $arg_array[$i], $m)) {
101                 $name = $m[1];
102                 $this->inputbox[][$name] = array(); $j = count($this->inputbox) - 1;
103                 foreach (preg_split("/[\s]+/", $m[2]) as $attr_pair) {
104                     list($attr, $value) = preg_split("/\s*=\s*/", $attr_pair);
105                     if (preg_match('/^"(.*)"$/', $value, $m))
106                         $value = $m[1];
107                     elseif (defined($value))
108                         $value = constant($value);
109                     $this->inputbox[$j][$name][$attr] = $value;
110                 }
111                 //trigger_error("not yet finished");
112                 //eval('$this->inputbox[]["'.$m[1].'"]='.$m[2].';');
113             } else {
114                 trigger_error(sprintf("Invalid argument %s ignored",htmlentities($arg_array[$i])), 
115                               E_USER_WARNING);
116             }
117         }
118         return;
119     }
120
121     function run($dbi, $argstr, &$request, $basepage) {
122         extract($this->getArgs($argstr, $request));
123         if (empty($action)) {
124             return $this->error(fmt("A required argument '%s' is missing.","action"));
125         }
126         $form = HTML::form(array('action' => $request->getPostURL(),
127                                  'method' => $method,
128                                  'class'  => 'wikiadmin',
129                                  'accept-charset' => $GLOBALS['charset']),
130                            HiddenInputs(array('action' => $action,
131                                               'pagename' => $basepage)));
132         if ($nobr) $nbsp = HTML::Raw('&nbsp;');
133         foreach ($this->inputbox as $inputbox) {
134             foreach ($inputbox as $inputtype => $input) {
135               switch($inputtype) {
136               case 'checkbox':
137                 $input['type'] = 'checkbox';
138                 if (empty($input['name']))
139                     return $this->error(fmt("A required argument '%s' is missing.",
140                                             "checkbox[][name]"));
141                 if (empty($input['value'])) $input['value'] = 1;
142                 if (!isset($input['text'])) 
143                     $input['text'] = gettext($input['name']); //."=".$input['value'];
144                 $text = $input['text'];
145                 unset($input['text']);
146                 if (empty($input['checked'])) {
147                     if ($request->getArg($input['name']))
148                         $input['checked'] = 'checked';
149                 } else {
150                     $input['checked'] = 'checked';
151                 }
152                 if ($nobr)
153                     $form->pushContent(HTML::input($input), $nbsp, $text, $nbsp);
154                 else
155                     $form->pushContent(HTML::div(array('class' => $class), HTML::input($input), $text));
156                 break;
157               case 'editbox':
158                 $input['type'] = 'text';
159                 if (empty($input['name']))
160                     return $this->error(fmt("A required argument '%s' is missing.",
161                                             "editbox[][name]"));
162                 if (!isset($input['text'])) $input['text'] = gettext($input['name']);
163                 $text = $input['text'];
164                 if (empty($input['value']) and ($s = $request->getArg($input['name'])))
165                     $input['value'] = $s;
166                 unset($input['text']);
167                 if ($nobr)
168                     $form->pushContent(HTML::input($input), $nbsp, $text, $nbsp);
169                 else
170                     $form->pushContent(HTML::div(array('class' => $class), HTML::input($input), $text));
171                 break;
172               case 'radiobutton':
173                 $input['type'] = 'radio';
174                 if (empty($input['name']))
175                     return $this->error(fmt("A required argument '%s' is missing.",
176                                             "radiobutton[][name]"));
177                 if (!isset($input['text'])) $input['text'] = gettext($input['name']);
178                 $text = $input['text'];
179                 unset($input['text']);
180                 if ($input['checked']) $input['checked'] = 'checked';
181                 if ($nobr)
182                     $form->pushContent(HTML::input($input), $nbsp, $text, $nbsp);
183                 else
184                     $form->pushContent(HTML::div(array('class' => $class), HTML::input($input), $text));
185                 break;
186               case 'hidden':
187                 $input['type'] = 'hidden';
188                 if (empty($input['name']))
189                     return $this->error(fmt("A required argument '%s' is missing.",
190                                             "hidden[][name]"));
191                 unset($input['text']);
192                 $form->pushContent(HTML::input($input));
193               case 'pulldown':
194                     return $this->error("Sorry, pulldown not yet supported");
195               }
196             }
197         }
198         if ($request->getArg('start_debug'))
199             $form->pushContent(HTML::input(array('name' => 'start_debug',
200                                                  'value' =>  $request->getArg('start_debug'),
201                                                  'type'  => 'hidden')));
202         if (empty($buttontext)) $buttontext = $action;
203         $submit = Button('submit:', $buttontext, $class);
204         if ($cancel) {
205             $form->pushContent(HTML::span(array('class' => $class),
206                                           $submit, Button('submit:cancel', _("Cancel"), $class)));
207         } else {
208             $form->pushContent(HTML::span(array('class' => $class),
209                                           $submit));
210         }
211         return $form;
212     }
213 };
214
215 // $Log: not supported by cvs2svn $
216 // Revision 1.7  2004/11/24 10:40:04  rurban
217 // better nobr, allow empty text=""
218 //
219 // Revision 1.5  2004/11/24 10:14:36  rurban
220 // fill-in request args as with plugin-form
221 //
222 // Revision 1.4  2004/11/23 15:17:20  rurban
223 // better support for case_exact search (not caseexact for consistency),
224 // plugin args simplification:
225 //   handle and explode exclude and pages argument in WikiPlugin::getArgs
226 //     and exclude in advance (at the sql level if possible)
227 //   handle sortby and limit from request override in WikiPlugin::getArgs
228 // ListSubpages: renamed pages to maxpages
229 //
230 // Revision 1.3  2004/07/09 13:05:34  rurban
231 // just aesthetics
232 //
233 // Revision 1.2  2004/07/09 10:25:52  rurban
234 // fix the args parser
235 //
236 // Revision 1.1  2004/07/02 11:03:53  rurban
237 // renamed WikiFormMore to WikiFormRich: better syntax, no eval (safer)
238 //
239 // Revision 1.3  2004/07/01 13:59:25  rurban
240 // enhanced to allow arbitrary order of args and stricter eval checking
241 //
242 // Revision 1.2  2004/07/01 13:14:01  rurban
243 // desc only
244 //
245 // Revision 1.1  2004/07/01 13:11:53  rurban
246 // more generic forms
247 //
248
249 // For emacs users
250 // Local Variables:
251 // mode: php
252 // tab-width: 8
253 // c-basic-offset: 4
254 // c-hanging-comment-ender-p: nil
255 // indent-tabs-mode: nil
256 // End:
257 ?>