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
102 return "WikiFormRich";
105 function getDescription()
107 return _("Provide generic WikiForm input buttons.");
110 function getDefaultArguments()
112 return array('action' => false, // required argument
113 'method' => 'post', // or get
114 'class' => 'wikiaction',
115 'buttontext' => false, // for the submit button. default: action
116 'cancel' => false, // boolean if the action supports cancel also
117 'nobr' => false, // "no break": linebreaks or not
121 /* TODO: support better block alignment: <br>, tables, indent
123 function handle_plugin_args_cruft($argstr, $args)
125 $allowed = array("editbox", "hidden", "checkbox", "radiobutton" /*deprecated*/,
126 "radio", "pulldown", "submit", "reset", "combobox");
127 // no editbox[] = array(...) allowed (space)
128 $arg_array = preg_split("/\n/", $argstr);
129 // for security we should check this better
130 for ($i = 0; $i < count($arg_array); $i++) {
131 //TODO: we require an name=value pair here, but submit may go without also.
132 if (preg_match("/^\s*(" . join("|", $allowed) . ")\[\](.*)$/", $arg_array[$i], $m)) {
133 $name = $m[1]; // one of the allowed input types
134 $this->inputbox[][$name] = array();
135 $j = count($this->inputbox) - 1;
136 $curargs = trim($m[2]);
137 // must match name=NAME and also value=<!plugin-list name !>
138 while (preg_match("/^(\w+?)=((?:\".*?\")|(?:\w+)|(?:\"?<!plugin-list.+?!>\"?))\s*/",
142 $curargs = substr($curargs, strlen($m[0]));
143 if (preg_match("/^\"(.*)\"$/", $value, $m))
145 if (in_array($name, array("pulldown", "checkbox", "radio", "radiobutton", "combobox"))
146 and preg_match('/^<!plugin-list.+!>$/', $value, $m)
147 ) // like pulldown[] name=test value=<!plugin-list BackLinks page=HomePage!>
149 $loader = new WikiPluginLoader();
152 $plugin_str = preg_replace(array("/^<!/", "/!>$/"), array("<?", "?>"), $value);
153 // will return a pagelist object! pulldown,checkbox,radiobutton
154 $value = $loader->expandPI($plugin_str, $GLOBALS['request'], $markup, $basepage);
155 if (isa($value, 'PageList'))
156 $value = $value->pageNames(); // apply limit
157 elseif (!is_array($value))
158 trigger_error(sprintf("Invalid argument %s ignored", htmlentities($arg_array[$i])),
160 } elseif (defined($value))
161 $value = constant($value);
162 $this->inputbox[$j][$name][$attr] = $value;
164 //trigger_error("not yet finished");
165 //eval('$this->inputbox[]["'.$m[1].'"]='.$m[2].';');
167 trigger_error(sprintf("Invalid argument %s ignored", htmlentities($arg_array[$i])),
174 function run($dbi, $argstr, &$request, $basepage)
176 extract($this->getArgs($argstr, $request));
177 if (empty($action)) {
178 return $this->error(fmt("A required argument ā%sā is missing.", "action"));
181 $form = HTML::form(array('action' => $request->getPostURL(),
182 'method' => strtolower($method),
183 'class' => 'wikiformrich',
184 'accept-charset' => 'UTF-8'),
185 HiddenInputs(array('action' => $action)));
186 $nbsp = HTML::raw(' ');
188 foreach ($this->inputbox as $inputbox) {
189 foreach ($inputbox as $inputtype => $input) {
190 if ($inputtype == 'radiobutton') $inputtype = 'radio'; // convert from older versions
191 $input['type'] = $inputtype;
193 if ($inputtype != 'submit') {
194 if (empty($input['name']))
195 return $this->error(fmt("A required argument ā%sā is missing.",
196 $inputtype . "[][name]"));
197 if (!isset($input['text'])) $input['text'] = gettext($input['name']);
198 $text = $input['text'];
199 unset($input['text']);
201 switch ($inputtype) {
202 case 'checkbox': // text right
204 if (empty($input['value'])) $input['value'] = 1;
205 if (is_array($input['value'])) {
206 $div = HTML::div(array('class' => $class));
207 $values = $input['value'];
208 $name = $input['name'];
209 $input['name'] = $inputtype == 'checkbox' ? $name . "[]" : $name;
210 foreach ($values as $val) {
211 $input['value'] = $val;
212 if ($request->getArg($name)) {
213 if ($request->getArg($name) == $val)
214 $input['checked'] = 'checked';
216 unset($input['checked']);
218 $div->pushContent(HTML::input($input), $nbsp, $val, $nbsp, "\n");
220 $div->pushContent(HTML::br());
222 $form->pushContent($div);
224 if (empty($input['checked'])) {
225 if ($request->getArg($input['name']))
226 $input['checked'] = 'checked';
228 $input['checked'] = 'checked';
231 $form->pushContent(HTML::input($input), $nbsp, $text, $nbsp);
233 $form->pushContent(HTML::div(array('class' => $class), HTML::input($input), $nbsp, $text));
236 case 'editbox': // text left
237 $input['type'] = 'text';
238 if (empty($input['value']) and ($s = $request->getArg($input['name'])))
239 $input['value'] = $s;
240 if (!empty($input['autocomplete']))
241 $this->do_autocomplete($form, $inputtype, $input, $input['value']);
243 $form->pushContent($text, $nbsp, HTML::input($input));
245 $form->pushContent(HTML::div(array('class' => $class), $text, $nbsp, HTML::input($input)));
247 case 'combobox': // text left
248 $input['autocomplete'] = 1;
250 $values = isset($input['value']) ? $input['value'] : '';
251 unset($input['value']);
252 unset($input['type']);
253 if (is_string($values)) $values = explode(",", $values);
254 if (!empty($input['autocomplete']))
255 $this->do_autocomplete($form, $inputtype, $input, $values);
256 $select = HTML::select($input);
257 if (empty($values) and ($s = $request->getArg($input['name']))) {
258 $select->pushContent(HTML::option(array('value' => $s), $s));
259 } elseif (is_array($values)) {
260 $name = $input['name'];
261 unset($input['name']);
262 foreach ($values as $val) {
263 $input = array('value' => $val);
264 if ($request->getArg($name)) {
265 if ($request->getArg($name) == $val)
266 $input['selected'] = 'selected';
268 unset($input['selected']);
270 //TODO: filter uneeded attributes
271 $select->pushContent(HTML::option($input, $val));
273 } else { // force empty option
274 $select->pushContent(HTML::option(array(), ''));
276 $form->pushContent($text, $nbsp, $select);
280 $form->pushContent(HTML::input($input));
282 // change the order of inputs, by explicitly placing a submit button here.
283 case 'submit': // text right (?)
284 //$input['type'] = 'submit';
285 if (empty($input['value'])) $input['value'] = $buttontext ? $buttontext : $action;
286 unset($input['text']);
287 if (empty($input['class'])) $input['class'] = $class;
289 $form->pushContent(HTML::input($input), $nbsp, $text, $nbsp);
291 $form->pushContent(HTML::div(array('class' => $class), HTML::input($input), $text));
292 // unset the default submit button
298 if ($request->getArg('start_debug'))
299 $form->pushContent(HTML::input
300 (array('name' => 'start_debug',
301 'value' => $request->getArg('start_debug'),
302 'type' => 'hidden')));
304 $form->pushContent(HiddenInputs(array('pagename' => $basepage)));
305 if (!$already_submit) {
306 if (empty($buttontext)) $buttontext = $action;
307 $submit = Button('submit:', $buttontext, $class);
309 $form->pushContent(HTML::span
310 (array('class' => $class),
312 Button('submit:cancel', _("Cancel"), $class)));
314 $form->pushContent(HTML::span(array('class' => $class),
321 private function do_autocomplete(&$form, $inputtype, &$input, &$values)
324 $input['class'] = "dropdown";
325 $input['acdropdown'] = "true";
326 //$input['autocomplete'] = "OFF";
327 $input['autocomplete_complete'] = "true";
328 // only match begin: autocomplete_matchbegin, or
329 $input['autocomplete_matchsubstring'] = "true";
330 if (empty($values)) {
331 if (isset($input['method']) && $input['method']) {
332 if (empty($input['args'])) {
333 if (preg_match("/^(.*?) (.*)$/", $input['method'], $m)) {
334 $input['method'] = $m[1];
335 $input['args'] = $m[2];
337 $input['args'] = null;
339 static $tmpArray = 'tmpArray00';
340 // deferred remote xmlrpc call
341 if (string_starts_with($input['method'], "dynxmlrpc:")) {
342 // how is server + method + args encoding parsed by acdropdown?
343 $input['autocomplete_list'] = substr($input['method'], 3);
345 $input['autocomplete_list'] .= (" " . $input['args']);
346 // static xmlrpc call, local only
347 } elseif (string_starts_with($input['method'], "xmlrpc:")) {
348 include_once 'lib/XmlRpcClient.php';
349 $values = wiki_xmlrpc_post(substr($input['method'], 7), $input['args']);
350 } elseif (string_starts_with($input['method'], "url:")) {
351 include_once 'lib/HttpClient.php';
352 $html = HttpClient::quickGet(substr($input['method'], 4));
353 //TODO: how to parse the HTML result into a list?
354 } elseif (string_starts_with($input['method'], "dynurl:")) {
355 $input['autocomplete_list'] = substr($input['method'], 3);
356 } elseif (string_starts_with($input['method'], "plugin:")) {
357 $dbi = $request->getDbh();
358 $pluginName = substr($input['method'], 7);
360 require_once 'lib/WikiPlugin.php';
361 $w = new WikiPluginLoader();
362 $p = $w->getPlugin($pluginName, false); // second arg?
364 trigger_error("invalid input['method'] " . $input['method'], E_USER_WARNING);
365 $pagelist = $p->run($dbi, @$input['args'], $request, $basepage);
367 if (is_object($pagelist) and isa($pagelist, 'PageList')) {
368 foreach ($pagelist->_pages as $page) {
369 if (is_object($page))
370 $values[] = $page->getName();
372 $values[] = (string)$page;
375 } elseif (string_starts_with($input['method'], "array:")) {
376 // some predefined values (e.g. in a template or themeinfo.php)
377 $input['autocomplete_list'] = $input['method'];
379 trigger_error("invalid input['method'] " . $input['method'], E_USER_WARNING);
381 if (empty($input['autocomplete_list'])) {
383 $input['autocomplete_list'] = "array:" . $tmpArray;
384 $svalues = empty($values) ? "" : join("','", $values);
385 $form->pushContent(JavaScript("var $tmpArray = new Array('" . $svalues . "')"));
387 if (count($values) == 1)
388 $input['value'] = $values[0];
390 $input['value'] = "";
391 unset($input['method']);
392 unset($input['args']);
393 //unset($input['autocomplete']);
394 } elseif ($s = $request->getArg($input['name']))
395 $input['value'] = $s;
405 // c-hanging-comment-ender-p: nil
406 // indent-tabs-mode: nil