]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/WikiFormRich.php
Add () for new WikiPluginLoader
[SourceForge/phpwiki.git] / lib / plugin / WikiFormRich.php
1 <?php
2
3 /*
4  * Copyright 2004,2006,2007 $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 along
19  * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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=.. 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=..
34  * - submit[]
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.
44  *
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 !>
51
52 Samples:
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=""
68 submit[]
69 checkbox[] name=case_exact
70 checkbox[] name=regex >>
71 <<WikiFormRich action=FullTextSearch method=get class=wikiadmin nobr=1
72 editbox[] name=s text=""
73 submit[]
74 checkbox[] name=case_exact
75 checkbox[] name=regex >>
76 <<WikiFormRich action=FuzzyPages method=get class=wikiadmin nobr=1
77 editbox[] name=s text=""
78 submit[]
79 checkbox[] name=case_exact ?>
80 <<WikiFormRich action=AppendText buttontext="AddPlugin"
81 radio[] name=s value=<!plugin-list BackLinks page=WikiPlugin limit=10 !>
82 ?>
83 <<WikiFormRich action=AppendText buttontext="AddPlugin"
84 pulldown[] name=s text="Plugins: " value=<!plugin-list BackLinks page=WikiPlugin !>
85 ?>
86 <<WikiFormRich action=AppendText buttontext="AddCategory"
87 pulldown[] name=s text="Categories: " value=<!plugin-list TitleSearch s=Category !>
88 ?>
89 <<WikiFormRich action=SemanticSearch buttontext="AddRelation"
90 combobox[] name=relation text="Relation: " method=listRelations
91 ?>
92 <<WikiFormRich action=AppendText buttontext="InsertTemplate"
93 combobox[] name=s text="Template: " method=titleSearch args="Template/"
94 ?>
95  */
96
97 class WikiPlugin_WikiFormRich
98     extends WikiPlugin
99 {
100     function getName()
101     {
102         return "WikiFormRich";
103     }
104
105     function getDescription()
106     {
107         return _("Provide generic WikiForm input buttons.");
108     }
109
110     function getDefaultArguments()
111     {
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
118         );
119     }
120
121     /* TODO: support better block alignment: <br>, tables, indent
122      */
123     function handle_plugin_args_cruft($argstr, $args)
124     {
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*/",
139                     $curargs, $m)) {
140                     $attr = $m[1];
141                     $value = $m[2];
142                     $curargs = substr($curargs, strlen($m[0]));
143                     if (preg_match("/^\"(.*)\"$/", $value, $m))
144                         $value = $m[1];
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!>
148                     {
149                         $loader = new WikiPluginLoader();
150                         $markup = null;
151                         $basepage = null;
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])),
159                                 E_USER_WARNING);
160                     } elseif (defined($value))
161                         $value = constant($value);
162                     $this->inputbox[$j][$name][$attr] = $value;
163                 }
164                 //trigger_error("not yet finished");
165                 //eval('$this->inputbox[]["'.$m[1].'"]='.$m[2].';');
166             } else {
167                 trigger_error(sprintf("Invalid argument %s ignored", htmlentities($arg_array[$i])),
168                     E_USER_WARNING);
169             }
170         }
171         return;
172     }
173
174     function run($dbi, $argstr, &$request, $basepage)
175     {
176         extract($this->getArgs($argstr, $request));
177         if (empty($action)) {
178             return $this->error(fmt("A required argument ā€œ%sā€ is missing.", "action"));
179         }
180
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('&nbsp;');
187         $already_submit = 0;
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;
192                 $text = '';
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']);
200                 }
201                 switch ($inputtype) {
202                     case 'checkbox': // text right
203                     case 'radio':
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';
215                                     else
216                                         unset($input['checked']);
217                                 }
218                                 $div->pushContent(HTML::input($input), $nbsp, $val, $nbsp, "\n");
219                                 if (!$nobr)
220                                     $div->pushContent(HTML::br());
221                             }
222                             $form->pushContent($div);
223                         } else {
224                             if (empty($input['checked'])) {
225                                 if ($request->getArg($input['name']))
226                                     $input['checked'] = 'checked';
227                             } else {
228                                 $input['checked'] = 'checked';
229                             }
230                             if ($nobr)
231                                 $form->pushContent(HTML::input($input), $nbsp, $text, $nbsp);
232                             else
233                                 $form->pushContent(HTML::div(array('class' => $class), HTML::input($input), $nbsp, $text));
234                         }
235                         break;
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']);
242                         if ($nobr)
243                             $form->pushContent($text, $nbsp, HTML::input($input));
244                         else
245                             $form->pushContent(HTML::div(array('class' => $class), $text, $nbsp, HTML::input($input)));
246                         break;
247                     case 'combobox': // text left
248                         $input['autocomplete'] = 1;
249                     case 'pulldown':
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';
267                                     else
268                                         unset($input['selected']);
269                                 }
270                                 //TODO: filter uneeded attributes
271                                 $select->pushContent(HTML::option($input, $val));
272                             }
273                         } else { // force empty option
274                             $select->pushContent(HTML::option(array(), ''));
275                         }
276                         $form->pushContent($text, $nbsp, $select);
277                         break;
278                     case 'reset':
279                     case 'hidden':
280                         $form->pushContent(HTML::input($input));
281                         break;
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;
288                         if ($nobr)
289                             $form->pushContent(HTML::input($input), $nbsp, $text, $nbsp);
290                         else
291                             $form->pushContent(HTML::div(array('class' => $class), HTML::input($input), $text));
292                         // unset the default submit button
293                         $already_submit = 1;
294                         break;
295                 }
296             }
297         }
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')));
303         if (!USE_PATH_INFO)
304             $form->pushContent(HiddenInputs(array('pagename' => $basepage)));
305         if (!$already_submit) {
306             if (empty($buttontext)) $buttontext = $action;
307             $submit = Button('submit:', $buttontext, $class);
308             if ($cancel) {
309                 $form->pushContent(HTML::span
310                 (array('class' => $class),
311                     $submit,
312                     Button('submit:cancel', _("Cancel"), $class)));
313             } else {
314                 $form->pushContent(HTML::span(array('class' => $class),
315                     $submit));
316             }
317         }
318         return $form;
319     }
320
321     private function do_autocomplete(&$form, $inputtype, &$input, &$values)
322     {
323         global $request;
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];
336                     } else
337                         $input['args'] = null;
338                 }
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);
344                     if ($input['args'])
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);
359                     $basepage = '';
360                     require_once 'lib/WikiPlugin.php';
361                     $w = new WikiPluginLoader();
362                     $p = $w->getPlugin($pluginName, false); // second arg?
363                     if (!is_object($p))
364                         trigger_error("invalid input['method'] " . $input['method'], E_USER_WARNING);
365                     $pagelist = $p->run($dbi, @$input['args'], $request, $basepage);
366                     $values = array();
367                     if (is_object($pagelist) and isa($pagelist, 'PageList')) {
368                         foreach ($pagelist->_pages as $page) {
369                             if (is_object($page))
370                                 $values[] = $page->getName();
371                             else
372                                 $values[] = (string)$page;
373                         }
374                     }
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'];
378                 } else {
379                     trigger_error("invalid input['method'] " . $input['method'], E_USER_WARNING);
380                 }
381                 if (empty($input['autocomplete_list'])) {
382                     $tmpArray++;
383                     $input['autocomplete_list'] = "array:" . $tmpArray;
384                     $svalues = empty($values) ? "" : join("','", $values);
385                     $form->pushContent(JavaScript("var $tmpArray = new Array('" . $svalues . "')"));
386                 }
387                 if (count($values) == 1)
388                     $input['value'] = $values[0];
389                 else
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;
396         }
397         return true;
398     }
399 }
400
401 // Local Variables:
402 // mode: php
403 // tab-width: 8
404 // c-basic-offset: 4
405 // c-hanging-comment-ender-p: nil
406 // indent-tabs-mode: nil
407 // End: