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