4 * Copyright 2004,2006,2007 $ThePhpWikiProgrammingTeam
6 * This file is part of PhpWiki.
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.
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.
18 * You should have received a copy of the GNU General Public License along
19 * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * This is another replacement for MagicPhpWikiURL forms.
25 * Previously encoded with the "phpwiki:" syntax.
27 * Enhanced WikiForm to be more generic:
28 * - editbox[] name=.. value=.. text=.. autocomplete=1
29 * - checkbox[] name=.. value=0|1 checked text=..
30 * - radio[] name=.. value=.. text=..
31 * - pulldown[] name=.. value=.. selected=.. text=.. autocomplete=1
32 * - combobox[] name=.. value=.. text=.. method=.. args=..
33 * - hidden[] name=.. value=..
35 * - action, submit buttontext, optional cancel button (bool)
36 * - method=get or post, Default: post.
37 * Valid arguments for pulldown and editbox: autocomplete=1, Default: 0
38 * If autocomplete=1, additional arguments method and args may be used.
39 * If no method is given, value will be used to fill in the valid values.
40 * method="xmlrpc:server:name" or "url:http://server/wiki/method" or "array:jsvariable"
41 * or "plugin:pluginname"
42 * args are optional arguments, space separated, for the method.
43 * A combobox is a pulldown with autocomplete=1.
45 * @Author: Reini Urban
46 * Values which are constants are evaluated.
47 * The cancel button must be supported by the action.
48 * (just some wikiadmin actions so far)
49 * improve layout by: nobr=1
50 * some allow values as list from from <!plugin-list !>
53 <<WikiFormRich action=dumpserial method=get
54 checkbox[] name=include value="all"
55 editbox[] name=directory value=DEFAULT_DUMP_DIR
56 editbox[] name=pages value=*
57 editbox[] name=exclude value="" >>
58 <<WikiFormRich action=dumphtml method=get
59 editbox[] name=directory value=HTML_DUMP_DIR
60 editbox[] name=pages value="*"
61 editbox[] name=exclude value="" >>
62 <<WikiFormRich action=loadfile method=get
63 editbox[] name=source value=DEFAULT_WIKI_PGSRC
64 checkbox[] name=overwrite value=1
65 editbox[] name=exclude value="" >>
66 <<WikiFormRich action=TitleSearch method=get class=wikiadmin nobr=1
67 editbox[] name=s text=""
69 checkbox[] name=case_exact
70 checkbox[] name=regex >>
71 <<WikiFormRich action=FullTextSearch method=get class=wikiadmin nobr=1
72 editbox[] name=s text=""
74 checkbox[] name=case_exact
75 checkbox[] name=regex >>
76 <<WikiFormRich action=FuzzyPages method=get class=wikiadmin nobr=1
77 editbox[] name=s text=""
79 checkbox[] name=case_exact ?>
80 <<WikiFormRich action=AppendText buttontext="AddPlugin"
81 radio[] name=s value=<!plugin-list BackLinks page=WikiPlugin limit=10 !>
83 <<WikiFormRich action=AppendText buttontext="AddPlugin"
84 pulldown[] name=s text="Plugins: " value=<!plugin-list BackLinks page=WikiPlugin !>
86 <<WikiFormRich action=AppendText buttontext="AddCategory"
87 pulldown[] name=s text="Categories: " value=<!plugin-list TitleSearch s=Category !>
89 <<WikiFormRich action=SemanticSearch buttontext="AddRelation"
90 combobox[] name=relation text="Relation: " method=listRelations
92 <<WikiFormRich action=AppendText buttontext="InsertTemplate"
93 combobox[] name=s text="Template: " method=titleSearch args="Template/"
97 class WikiPlugin_WikiFormRich
100 function getDescription()
102 return _("Provide generic WikiForm input buttons.");
105 function getDefaultArguments()
107 return array('action' => false, // required argument
108 'method' => 'post', // or get
109 'class' => 'wikiaction',
110 'buttontext' => false, // for the submit button. default: action
111 'cancel' => false, // boolean if the action supports cancel also
112 'nobr' => false, // "no break": linebreaks or not
116 /* TODO: support better block alignment: <br>, tables, indent
118 function handle_plugin_args_cruft($argstr, $args)
120 $allowed = array("editbox", "hidden", "checkbox", "radiobutton" /*deprecated*/,
121 "radio", "pulldown", "submit", "reset", "combobox");
122 // no editbox[] = array(...) allowed (space)
123 $arg_array = preg_split("/\n/", $argstr);
124 // for security we should check this better
125 for ($i = 0; $i < count($arg_array); $i++) {
126 //TODO: we require an name=value pair here, but submit may go without also.
127 if (preg_match("/^\s*(" . join("|", $allowed) . ")\[\](.*)$/", $arg_array[$i], $m)) {
128 $name = $m[1]; // one of the allowed input types
129 $this->inputbox[][$name] = array();
130 $j = count($this->inputbox) - 1;
131 $curargs = trim($m[2]);
132 // must match name=NAME and also value=<!plugin-list name !>
133 while (preg_match("/^(\w+?)=((?:\".*?\")|(?:\w+)|(?:\"?<!plugin-list.+?!>\"?))\s*/",
137 $curargs = substr($curargs, strlen($m[0]));
138 if (preg_match("/^\"(.*)\"$/", $value, $m))
140 if (in_array($name, array("pulldown", "checkbox", "radio", "radiobutton", "combobox"))
141 and preg_match('/^<!plugin-list.+!>$/', $value, $m)
142 ) // like pulldown[] name=test value=<!plugin-list BackLinks page=HomePage!>
144 $loader = new WikiPluginLoader();
147 $plugin_str = preg_replace(array("/^<!/", "/!>$/"), array("<?", "?>"), $value);
148 // will return a pagelist object! pulldown,checkbox,radiobutton
149 $value = $loader->expandPI($plugin_str, $GLOBALS['request'], $markup, $basepage);
150 if (isa($value, 'PageList'))
151 $value = $value->pageNames(); // apply limit
152 elseif (!is_array($value))
153 trigger_error(sprintf("Invalid argument %s ignored", htmlentities($arg_array[$i])),
155 } elseif (defined($value))
156 $value = constant($value);
157 $this->inputbox[$j][$name][$attr] = $value;
159 //trigger_error("not yet finished");
160 //eval('$this->inputbox[]["'.$m[1].'"]='.$m[2].';');
162 trigger_error(sprintf("Invalid argument %s ignored", htmlentities($arg_array[$i])),
169 function run($dbi, $argstr, &$request, $basepage)
171 extract($this->getArgs($argstr, $request));
172 if (empty($action)) {
173 return $this->error(fmt("A required argument ā%sā is missing.", "action"));
176 $form = HTML::form(array('action' => $request->getPostURL(),
177 'method' => strtolower($method),
178 'class' => 'wikiformrich',
179 'accept-charset' => 'UTF-8'),
180 HiddenInputs(array('action' => $action)));
181 $nbsp = HTML::raw(' ');
183 foreach ($this->inputbox as $inputbox) {
184 foreach ($inputbox as $inputtype => $input) {
185 if ($inputtype == 'radiobutton') $inputtype = 'radio'; // convert from older versions
186 $input['type'] = $inputtype;
188 if ($inputtype != 'submit') {
189 if (empty($input['name']))
190 return $this->error(fmt("A required argument ā%sā is missing.",
191 $inputtype . "[][name]"));
192 if (!isset($input['text'])) $input['text'] = gettext($input['name']);
193 $text = $input['text'];
194 unset($input['text']);
196 switch ($inputtype) {
197 case 'checkbox': // text right
199 if (empty($input['value'])) $input['value'] = 1;
200 if (is_array($input['value'])) {
201 $div = HTML::div(array('class' => $class));
202 $values = $input['value'];
203 $name = $input['name'];
204 $input['name'] = $inputtype == 'checkbox' ? $name . "[]" : $name;
205 foreach ($values as $val) {
206 $input['value'] = $val;
207 if ($request->getArg($name)) {
208 if ($request->getArg($name) == $val)
209 $input['checked'] = 'checked';
211 unset($input['checked']);
213 $div->pushContent(HTML::input($input), $nbsp, $val, $nbsp, "\n");
215 $div->pushContent(HTML::br());
217 $form->pushContent($div);
219 if (empty($input['checked'])) {
220 if ($request->getArg($input['name']))
221 $input['checked'] = 'checked';
223 $input['checked'] = 'checked';
226 $form->pushContent(HTML::input($input), $nbsp, $text, $nbsp);
228 $form->pushContent(HTML::div(array('class' => $class), HTML::input($input), $nbsp, $text));
231 case 'editbox': // text left
232 $input['type'] = 'text';
233 if (empty($input['value']) and ($s = $request->getArg($input['name'])))
234 $input['value'] = $s;
235 if (!empty($input['autocomplete']))
236 $this->do_autocomplete($form, $inputtype, $input, $input['value']);
238 $form->pushContent($text, $nbsp, HTML::input($input));
240 $form->pushContent(HTML::div(array('class' => $class), $text, $nbsp, HTML::input($input)));
242 case 'combobox': // text left
243 $input['autocomplete'] = 1;
245 $values = isset($input['value']) ? $input['value'] : '';
246 unset($input['value']);
247 unset($input['type']);
248 if (is_string($values)) $values = explode(",", $values);
249 if (!empty($input['autocomplete']))
250 $this->do_autocomplete($form, $inputtype, $input, $values);
251 $select = HTML::select($input);
252 if (empty($values) and ($s = $request->getArg($input['name']))) {
253 $select->pushContent(HTML::option(array('value' => $s), $s));
254 } elseif (is_array($values)) {
255 $name = $input['name'];
256 unset($input['name']);
257 foreach ($values as $val) {
258 $input = array('value' => $val);
259 if ($request->getArg($name)) {
260 if ($request->getArg($name) == $val)
261 $input['selected'] = 'selected';
263 unset($input['selected']);
265 //TODO: filter uneeded attributes
266 $select->pushContent(HTML::option($input, $val));
268 } else { // force empty option
269 $select->pushContent(HTML::option(array(), ''));
271 $form->pushContent($text, $nbsp, $select);
275 $form->pushContent(HTML::input($input));
277 // change the order of inputs, by explicitly placing a submit button here.
278 case 'submit': // text right (?)
279 //$input['type'] = 'submit';
280 if (empty($input['value'])) $input['value'] = $buttontext ? $buttontext : $action;
281 unset($input['text']);
282 if (empty($input['class'])) $input['class'] = $class;
284 $form->pushContent(HTML::input($input), $nbsp, $text, $nbsp);
286 $form->pushContent(HTML::div(array('class' => $class), HTML::input($input), $text));
287 // unset the default submit button
293 if ($request->getArg('start_debug'))
294 $form->pushContent(HTML::input
295 (array('name' => 'start_debug',
296 'value' => $request->getArg('start_debug'),
297 'type' => 'hidden')));
299 $form->pushContent(HiddenInputs(array('pagename' => $basepage)));
300 if (!$already_submit) {
301 if (empty($buttontext)) $buttontext = $action;
302 $submit = Button('submit:', $buttontext, $class);
304 $form->pushContent(HTML::span
305 (array('class' => $class),
307 Button('submit:cancel', _("Cancel"), $class)));
309 $form->pushContent(HTML::span(array('class' => $class),
316 private function do_autocomplete(&$form, $inputtype, &$input, &$values)
319 $input['class'] = "dropdown";
320 $input['acdropdown'] = "true";
321 //$input['autocomplete'] = "OFF";
322 $input['autocomplete_complete'] = "true";
323 // only match begin: autocomplete_matchbegin, or
324 $input['autocomplete_matchsubstring'] = "true";
325 if (empty($values)) {
326 if (isset($input['method']) && $input['method']) {
327 if (empty($input['args'])) {
328 if (preg_match("/^(.*?) (.*)$/", $input['method'], $m)) {
329 $input['method'] = $m[1];
330 $input['args'] = $m[2];
332 $input['args'] = null;
334 static $tmpArray = 'tmpArray00';
335 // deferred remote xmlrpc call
336 if (string_starts_with($input['method'], "dynxmlrpc:")) {
337 // how is server + method + args encoding parsed by acdropdown?
338 $input['autocomplete_list'] = substr($input['method'], 3);
340 $input['autocomplete_list'] .= (" " . $input['args']);
341 // static xmlrpc call, local only
342 } elseif (string_starts_with($input['method'], "xmlrpc:")) {
343 include_once 'lib/XmlRpcClient.php';
344 $values = wiki_xmlrpc_post(substr($input['method'], 7), $input['args']);
345 } elseif (string_starts_with($input['method'], "url:")) {
346 include_once 'lib/HttpClient.php';
347 $html = HttpClient::quickGet(substr($input['method'], 4));
348 //TODO: how to parse the HTML result into a list?
349 } elseif (string_starts_with($input['method'], "dynurl:")) {
350 $input['autocomplete_list'] = substr($input['method'], 3);
351 } elseif (string_starts_with($input['method'], "plugin:")) {
352 $dbi = $request->getDbh();
353 $pluginName = substr($input['method'], 7);
355 require_once 'lib/WikiPlugin.php';
356 $w = new WikiPluginLoader();
357 $p = $w->getPlugin($pluginName, false); // second arg?
359 trigger_error("invalid input['method'] " . $input['method'], E_USER_WARNING);
360 $pagelist = $p->run($dbi, @$input['args'], $request, $basepage);
362 if (is_object($pagelist) and isa($pagelist, 'PageList')) {
363 foreach ($pagelist->_pages as $page) {
364 if (is_object($page))
365 $values[] = $page->getName();
367 $values[] = (string)$page;
370 } elseif (string_starts_with($input['method'], "array:")) {
371 // some predefined values (e.g. in a template or themeinfo.php)
372 $input['autocomplete_list'] = $input['method'];
374 trigger_error("invalid input['method'] " . $input['method'], E_USER_WARNING);
376 if (empty($input['autocomplete_list'])) {
378 $input['autocomplete_list'] = "array:" . $tmpArray;
379 $svalues = empty($values) ? "" : join("','", $values);
380 $form->pushContent(JavaScript("var $tmpArray = new Array('" . $svalues . "')"));
382 if (count($values) == 1)
383 $input['value'] = $values[0];
385 $input['value'] = "";
386 unset($input['method']);
387 unset($input['args']);
388 //unset($input['autocomplete']);
389 } elseif ($s = $request->getArg($input['name']))
390 $input['value'] = $s;
400 // c-hanging-comment-ender-p: nil
401 // indent-tabs-mode: nil