]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/WikiFormRich.php
just aesthetics
[SourceForge/phpwiki.git] / lib / plugin / WikiFormRich.php
1 <?php // -*-php-*-
2 rcs_id('$Id: WikiFormRich.php,v 1.3 2004-07-09 13:05:34 rurban Exp $');
3 /**
4  Copyright 1999, 2000, 2001, 2002, 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  Samples:
40    <?plugin WikiFormRich action=dumpserial method=GET 
41             checkbox[] name=include value="all" 
42             editbox[] name=directory value=DEFAULT_DUMP_DIR
43             editbox[] name=pages value=*
44             editbox[] name=exclude value="" ?>
45    <?plugin WikiFormRich action=dumphtml method=GET 
46             editbox[] name=directory value=HTML_DUMP_DIR
47             editbox[] name=pages value="*"
48             editbox[] name=exclude value="" ?>
49
50    <?plugin WikiFormRich action=loadfile method=GET 
51             editbox[]  name=source value=DEFAULT_WIKI_PGSRC
52             checkbox[] name=overwrite value=1
53             editbox[]  name=exclude value="" ?>
54  */
55 class WikiPlugin_WikiFormRich
56 extends WikiPlugin
57 {
58     function getName () {
59         return _("Provide generic WikiForm input buttons");
60     }
61
62     function getVersion() {
63         return preg_replace("/[Revision: $]/", '',
64                             "\$Revision: 1.3 $");
65     }
66
67     function getDefaultArguments() {
68         return array('action' => false,     // required argument
69                      'method' => 'POST',    // or GET
70                      'class'  => false,
71                      'buttontext' => false, // for the submit button
72                      'cancel' => false,     // boolean if the action supports cancel also
73                      );
74     }
75
76     function handle_plugin_args_cruft($argstr, $args) {
77         $allowed = array("editbox", "hidden", "checkbox", "radiobutton", "pulldown");
78         // no editbox[] = array(...) allowed (space)
79         $arg_array = preg_split("/\n/", $argstr);
80         // for security we should check this better
81         $arg = '';
82         for ($i = 0; $i < count($arg_array); $i++) {
83             if (preg_match("/^\s*(".join("|",$allowed).")\[\]\s+(.+)\s*$/", $arg_array[$i], $m)) {
84                 $name = $m[1];
85                 $this->inputbox[][$name] = array(); $i = count($this->inputbox) - 1;
86                 foreach (preg_split("/[\s]+/", $m[2]) as $attr_pair) {
87                     list($attr,$value) = preg_split("/\s*=\s*/", $attr_pair);
88                     if (preg_match('/^"(.*)"$/', $value, $m))
89                         $value = $m[1];
90                     elseif (defined($value))
91                         $value = constant($value);
92                     $this->inputbox[$i][$name][$attr] = $value;
93                 }
94                 //trigger_error("not yet finished");
95                 //eval('$this->inputbox[]["'.$m[1].'"]='.$m[2].';');
96             } else {
97                 trigger_error(sprintf("Invalid argument %s ignored",htmlentities($arg_array[$i])), 
98                               E_USER_WARNING);
99             }
100         }
101         return;
102     }
103
104     function run($dbi, $argstr, &$request, $basepage) {
105         extract($this->getArgs($argstr, $request));
106         if (empty($action)) {
107             return $this->error(fmt("A required argument '%s' is missing.","action"));
108         }
109         $form = HTML::form(array('action' => $request->getPostURL(),
110                                  'method' => $method,
111                                  'class'  => 'wikiadmin',
112                                  'accept-charset' => $GLOBALS['charset']),
113                            HiddenInputs(array('action' => $action,
114                                               'pagename' => $basepage)));
115         foreach ($this->inputbox as $inputbox) {
116             foreach ($inputbox as $inputtype => $input) {
117               switch($inputtype) {
118               case 'checkbox':
119                 $input['type'] = 'checkbox';
120                 if (empty($input['name']))
121                     return $this->error("A required argument '%s' is missing.","checkbox[][name]");
122                 if (empty($input['value'])) $input['value'] = 1;
123                 if (empty($input['text'])) 
124                     $input['text'] = gettext($input['name'])."=".$input['value'];
125                 $text = $input['text'];
126                 unset($input['text']);
127                 if (!empty($input['checked'])) $input['checked'] = 'checked';
128                 $form->pushContent(HTML::div(array('class' => $class), HTML::input($input), $text));
129                 break;
130               case 'editbox':
131                 $input['type'] = 'text';
132                 if (empty($input['name']))
133                     return $this->error("A required argument '%s' is missing.","editbox[][name]");
134                 if (empty($input['text'])) $input['text'] = gettext($input['name']);
135                 $text = $input['text'];
136                 unset($input['text']);
137                 $form->pushContent(HTML::div(array('class' => $class), HTML::input($input), $text));
138                 break;
139               case 'radiobutton':
140                 $input['type'] = 'radio';
141                 if (empty($input['name']))
142                     return $this->error("A required argument '%s' is missing.","radiobutton[][name]");
143                 if (empty($input['text'])) $input['text'] = gettext($input['name']);
144                 $text = $input['text'];
145                 unset($input['text']);
146                 if ($input['checked']) $input['checked'] = 'checked';
147                 $form->pushContent(HTML::div(array('class' => $class), HTML::input($input), $text));
148                 break;
149               case 'hidden':
150                 $input['type'] = 'hidden';
151                 if (empty($input['name']))
152                     return $this->error("A required argument '%s' is missing.","hidden[][name]");
153                 unset($input['text']);
154                 $form->pushContent(HTML::input($input));
155               case 'pulldown':
156                     return $this->error("Sorry, pulldown not yet supported");
157               }
158             }
159         }
160         if ($request->getArg('start_debug'))
161             $form->pushContent(HTML::input(array('name' => 'start_debug',
162                                                  'value' =>  $request->getArg('start_debug'),
163                                                  'type'  => 'hidden')));
164         if (empty($buttontext)) $buttontext = $action;
165         $submit = Button('submit:', $buttontext, $class);
166         if ($cancel) {
167             $form->pushContent(HTML::span(array('class' => $class),
168                                           $submit, Button('submit:cancel', _("Cancel"), $class)));
169         } else {
170             $form->pushContent(HTML::span(array('class' => $class),
171                                           $submit));
172         }
173         return $form;
174     }
175 };
176
177 // $Log: not supported by cvs2svn $
178 // Revision 1.2  2004/07/09 10:25:52  rurban
179 // fix the args parser
180 //
181 // Revision 1.1  2004/07/02 11:03:53  rurban
182 // renamed WikiFormMore to WikiFormRich: better syntax, no eval (safer)
183 //
184 // Revision 1.3  2004/07/01 13:59:25  rurban
185 // enhanced to allow arbitrary order of args and stricter eval checking
186 //
187 // Revision 1.2  2004/07/01 13:14:01  rurban
188 // desc only
189 //
190 // Revision 1.1  2004/07/01 13:11:53  rurban
191 // more generic forms
192 //
193 //
194
195 // For emacs users
196 // Local Variables:
197 // mode: php
198 // tab-width: 8
199 // c-basic-offset: 4
200 // c-hanging-comment-ender-p: nil
201 // indent-tabs-mode: nil
202 // End:
203 ?>