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