PageRenderTime 52ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/wiki/inc/form.php

https://github.com/gbby/folders2web
PHP | 956 lines | 403 code | 60 blank | 493 comment | 102 complexity | 94abae275596585faf2197d63cbfd068 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /**
  3. * DokuWiki XHTML Form
  4. *
  5. * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
  6. * @author Tom N Harris <tnharris@whoopdedo.org>
  7. */
  8. if(!defined('DOKU_INC')) die('meh.');
  9. /**
  10. * Class for creating simple HTML forms.
  11. *
  12. * The forms is built from a list of pseudo-tags (arrays with expected keys).
  13. * Every pseudo-tag must have the key '_elem' set to the name of the element.
  14. * When printed, the form class calls functions named 'form_$type' for each
  15. * element it contains.
  16. *
  17. * Standard practice is for non-attribute keys in a pseudo-element to start
  18. * with '_'. Other keys are HTML attributes that will be included in the element
  19. * tag. That way, the element output functions can pass the pseudo-element
  20. * directly to buildAttributes.
  21. *
  22. * See the form_make* functions later in this file.
  23. *
  24. * @author Tom N Harris <tnharris@whoopdedo.org>
  25. */
  26. class Doku_Form {
  27. // Form id attribute
  28. var $params = array();
  29. // Draw a border around form fields.
  30. // Adds <fieldset></fieldset> around the elements
  31. var $_infieldset = false;
  32. // Hidden form fields.
  33. var $_hidden = array();
  34. // Array of pseudo-tags
  35. var $_content = array();
  36. /**
  37. * Constructor
  38. *
  39. * Sets parameters and autoadds a security token. The old calling convention
  40. * with up to four parameters is deprecated, instead the first parameter
  41. * should be an array with parameters.
  42. *
  43. * @param mixed $params Parameters for the HTML form element; Using the
  44. * deprecated calling convention this is the ID
  45. * attribute of the form
  46. * @param string $action (optional, deprecated) submit URL, defaults to
  47. * current page
  48. * @param string $method (optional, deprecated) 'POST' or 'GET', default
  49. * is POST
  50. * @param string $enctype (optional, deprecated) Encoding type of the
  51. * data
  52. * @author Tom N Harris <tnharris@whoopdedo.org>
  53. */
  54. function Doku_Form($params, $action=false, $method=false, $enctype=false) {
  55. if(!is_array($params)) {
  56. $this->params = array('id' => $params);
  57. if ($action !== false) $this->params['action'] = $action;
  58. if ($method !== false) $this->params['method'] = strtolower($method);
  59. if ($enctype !== false) $this->params['enctype'] = $enctype;
  60. } else {
  61. $this->params = $params;
  62. }
  63. if (!isset($this->params['method'])) {
  64. $this->params['method'] = 'post';
  65. } else {
  66. $this->params['method'] = strtolower($this->params['method']);
  67. }
  68. if (!isset($this->params['action'])) {
  69. $this->params['action'] = '';
  70. }
  71. $this->addHidden('sectok', getSecurityToken());
  72. }
  73. /**
  74. * startFieldset
  75. *
  76. * Add <fieldset></fieldset> tags around fields.
  77. * Usually results in a border drawn around the form.
  78. *
  79. * @param string $legend Label that will be printed with the border.
  80. * @author Tom N Harris <tnharris@whoopdedo.org>
  81. */
  82. function startFieldset($legend) {
  83. if ($this->_infieldset) {
  84. $this->addElement(array('_elem'=>'closefieldset'));
  85. }
  86. $this->addElement(array('_elem'=>'openfieldset', '_legend'=>$legend));
  87. $this->_infieldset = true;
  88. }
  89. /**
  90. * endFieldset
  91. *
  92. * @author Tom N Harris <tnharris@whoopdedo.org>
  93. */
  94. function endFieldset() {
  95. if ($this->_infieldset) {
  96. $this->addElement(array('_elem'=>'closefieldset'));
  97. }
  98. $this->_infieldset = false;
  99. }
  100. /**
  101. * addHidden
  102. *
  103. * Adds a name/value pair as a hidden field.
  104. * The value of the field (but not the name) will be passed to
  105. * formText() before printing.
  106. *
  107. * @param string $name Field name.
  108. * @param string $value Field value. If null, remove a previously added field.
  109. * @author Tom N Harris <tnharris@whoopdedo.org>
  110. */
  111. function addHidden($name, $value) {
  112. if (is_null($value))
  113. unset($this->_hidden[$name]);
  114. else
  115. $this->_hidden[$name] = $value;
  116. }
  117. /**
  118. * addElement
  119. *
  120. * Appends a content element to the form.
  121. * The element can be either a pseudo-tag or string.
  122. * If string, it is printed without escaping special chars. *
  123. *
  124. * @param string $elem Pseudo-tag or string to add to the form.
  125. * @author Tom N Harris <tnharris@whoopdedo.org>
  126. */
  127. function addElement($elem) {
  128. $this->_content[] = $elem;
  129. }
  130. /**
  131. * insertElement
  132. *
  133. * Inserts a content element at a position.
  134. *
  135. * @param string $pos 0-based index where the element will be inserted.
  136. * @param string $elem Pseudo-tag or string to add to the form.
  137. * @author Tom N Harris <tnharris@whoopdedo.org>
  138. */
  139. function insertElement($pos, $elem) {
  140. array_splice($this->_content, $pos, 0, array($elem));
  141. }
  142. /**
  143. * replaceElement
  144. *
  145. * Replace with NULL to remove an element.
  146. *
  147. * @param int $pos 0-based index the element will be placed at.
  148. * @param string $elem Pseudo-tag or string to add to the form.
  149. * @author Tom N Harris <tnharris@whoopdedo.org>
  150. */
  151. function replaceElement($pos, $elem) {
  152. $rep = array();
  153. if (!is_null($elem)) $rep[] = $elem;
  154. array_splice($this->_content, $pos, 1, $rep);
  155. }
  156. /**
  157. * findElementByType
  158. *
  159. * Gets the position of the first of a type of element.
  160. *
  161. * @param string $type Element type to look for.
  162. * @return array pseudo-element if found, false otherwise
  163. * @author Tom N Harris <tnharris@whoopdedo.org>
  164. */
  165. function findElementByType($type) {
  166. foreach ($this->_content as $pos=>$elem) {
  167. if (is_array($elem) && $elem['_elem'] == $type)
  168. return $pos;
  169. }
  170. return false;
  171. }
  172. /**
  173. * findElementById
  174. *
  175. * Gets the position of the element with an ID attribute.
  176. *
  177. * @param string $id ID of the element to find.
  178. * @return array pseudo-element if found, false otherwise
  179. * @author Tom N Harris <tnharris@whoopdedo.org>
  180. */
  181. function findElementById($id) {
  182. foreach ($this->_content as $pos=>$elem) {
  183. if (is_array($elem) && isset($elem['id']) && $elem['id'] == $id)
  184. return $pos;
  185. }
  186. return false;
  187. }
  188. /**
  189. * findElementByAttribute
  190. *
  191. * Gets the position of the first element with a matching attribute value.
  192. *
  193. * @param string $name Attribute name.
  194. * @param string $value Attribute value.
  195. * @return array pseudo-element if found, false otherwise
  196. * @author Tom N Harris <tnharris@whoopdedo.org>
  197. */
  198. function findElementByAttribute($name, $value) {
  199. foreach ($this->_content as $pos=>$elem) {
  200. if (is_array($elem) && isset($elem[$name]) && $elem[$name] == $value)
  201. return $pos;
  202. }
  203. return false;
  204. }
  205. /**
  206. * getElementAt
  207. *
  208. * Returns a reference to the element at a position.
  209. * A position out-of-bounds will return either the
  210. * first (underflow) or last (overflow) element.
  211. *
  212. * @param int $pos 0-based index
  213. * @return arrayreference pseudo-element
  214. * @author Tom N Harris <tnharris@whoopdedo.org>
  215. */
  216. function &getElementAt($pos) {
  217. if ($pos < 0) $pos = count($this->_content) + $pos;
  218. if ($pos < 0) $pos = 0;
  219. if ($pos >= count($this->_content)) $pos = count($this->_content) - 1;
  220. return $this->_content[$pos];
  221. }
  222. /**
  223. * Return the assembled HTML for the form.
  224. *
  225. * Each element in the form will be passed to a function named
  226. * 'form_$type'. The function should return the HTML to be printed.
  227. *
  228. * @author Tom N Harris <tnharris@whoopdedo.org>
  229. */
  230. function getForm() {
  231. global $lang;
  232. $form = '';
  233. $this->params['accept-charset'] = $lang['encoding'];
  234. $form .= '<form ' . buildAttributes($this->params,false) . '><div class="no">' . DOKU_LF;
  235. if (!empty($this->_hidden)) {
  236. foreach ($this->_hidden as $name=>$value)
  237. $form .= form_hidden(array('name'=>$name, 'value'=>$value));
  238. }
  239. foreach ($this->_content as $element) {
  240. if (is_array($element)) {
  241. $elem_type = $element['_elem'];
  242. if (function_exists('form_'.$elem_type)) {
  243. $form .= call_user_func('form_'.$elem_type, $element).DOKU_LF;
  244. }
  245. } else {
  246. $form .= $element;
  247. }
  248. }
  249. if ($this->_infieldset) $form .= form_closefieldset().DOKU_LF;
  250. $form .= '</div></form>'.DOKU_LF;
  251. return $form;
  252. }
  253. /**
  254. * Print the assembled form
  255. *
  256. * wraps around getForm()
  257. */
  258. function printForm(){
  259. echo $this->getForm();
  260. }
  261. /**
  262. * Add a radio set
  263. *
  264. * This function adds a set of radio buttons to the form. If $_POST[$name]
  265. * is set, this radio is preselected, else the first radio button.
  266. *
  267. * @param string $name The HTML field name
  268. * @param array $entries An array of entries $value => $caption
  269. *
  270. * @author Adrian Lang <lang@cosmocode.de>
  271. */
  272. function addRadioSet($name, $entries) {
  273. $value = (isset($_POST[$name]) && isset($entries[$_POST[$name]])) ?
  274. $_POST[$name] : key($entries);
  275. foreach($entries as $val => $cap) {
  276. $data = ($value === $val) ? array('checked' => 'checked') : array();
  277. $this->addElement(form_makeRadioField($name, $val, $cap, '', '', $data));
  278. }
  279. }
  280. }
  281. /**
  282. * form_makeTag
  283. *
  284. * Create a form element for a non-specific empty tag.
  285. *
  286. * @param string $tag Tag name.
  287. * @param array $attrs Optional attributes.
  288. * @return array pseudo-tag
  289. * @author Tom N Harris <tnharris@whoopdedo.org>
  290. */
  291. function form_makeTag($tag, $attrs=array()) {
  292. $elem = array('_elem'=>'tag', '_tag'=>$tag);
  293. return array_merge($elem, $attrs);
  294. }
  295. /**
  296. * form_makeOpenTag
  297. *
  298. * Create a form element for a non-specific opening tag.
  299. * Remember to put a matching close tag after this as well.
  300. *
  301. * @param string $tag Tag name.
  302. * @param array $attrs Optional attributes.
  303. * @return array pseudo-tag
  304. * @author Tom N Harris <tnharris@whoopdedo.org>
  305. */
  306. function form_makeOpenTag($tag, $attrs=array()) {
  307. $elem = array('_elem'=>'opentag', '_tag'=>$tag);
  308. return array_merge($elem, $attrs);
  309. }
  310. /**
  311. * form_makeCloseTag
  312. *
  313. * Create a form element for a non-specific closing tag.
  314. * Careless use of this will result in invalid XHTML.
  315. *
  316. * @param string $tag Tag name.
  317. * @return array pseudo-tag
  318. * @author Tom N Harris <tnharris@whoopdedo.org>
  319. */
  320. function form_makeCloseTag($tag) {
  321. return array('_elem'=>'closetag', '_tag'=>$tag);
  322. }
  323. /**
  324. * form_makeWikiText
  325. *
  326. * Create a form element for a textarea containing wiki text.
  327. * Only one wikitext element is allowed on a page. It will have
  328. * a name of 'wikitext' and id 'wiki__text'. The text will
  329. * be passed to formText() before printing.
  330. *
  331. * @param string $text Text to fill the field with.
  332. * @param array $attrs Optional attributes.
  333. * @return array pseudo-tag
  334. * @author Tom N Harris <tnharris@whoopdedo.org>
  335. */
  336. function form_makeWikiText($text, $attrs=array()) {
  337. $elem = array('_elem'=>'wikitext', '_text'=>$text,
  338. 'class'=>'edit', 'cols'=>'80', 'rows'=>'10');
  339. return array_merge($elem, $attrs);
  340. }
  341. /**
  342. * form_makeButton
  343. *
  344. * Create a form element for an action button.
  345. * A title will automatically be generated using the value and
  346. * accesskey attributes, unless you provide one.
  347. *
  348. * @param string $type Type attribute. 'submit' or 'cancel'
  349. * @param string $act Wiki action of the button, will be used as the do= parameter
  350. * @param string $value (optional) Displayed label. Uses $act if not provided.
  351. * @param array $attrs Optional attributes.
  352. * @return array pseudo-tag
  353. * @author Tom N Harris <tnharris@whoopdedo.org>
  354. */
  355. function form_makeButton($type, $act, $value='', $attrs=array()) {
  356. if ($value == '') $value = $act;
  357. $elem = array('_elem'=>'button', 'type'=>$type, '_action'=>$act,
  358. 'value'=>$value, 'class'=>'button');
  359. if (!empty($attrs['accesskey']) && empty($attrs['title'])) {
  360. $attrs['title'] = $value . ' ['.strtoupper($attrs['accesskey']).']';
  361. }
  362. return array_merge($elem, $attrs);
  363. }
  364. /**
  365. * form_makeField
  366. *
  367. * Create a form element for a labelled input element.
  368. * The label text will be printed before the input.
  369. *
  370. * @param string $type Type attribute of input.
  371. * @param string $name Name attribute of the input.
  372. * @param string $value (optional) Default value.
  373. * @param string $class Class attribute of the label. If this is 'block',
  374. * then a line break will be added after the field.
  375. * @param string $label Label that will be printed before the input.
  376. * @param string $id ID attribute of the input. If set, the label will
  377. * reference it with a 'for' attribute.
  378. * @param array $attrs Optional attributes.
  379. * @return array pseudo-tag
  380. * @author Tom N Harris <tnharris@whoopdedo.org>
  381. */
  382. function form_makeField($type, $name, $value='', $label=null, $id='', $class='', $attrs=array()) {
  383. if (is_null($label)) $label = $name;
  384. $elem = array('_elem'=>'field', '_text'=>$label, '_class'=>$class,
  385. 'type'=>$type, 'id'=>$id, 'name'=>$name, 'value'=>$value);
  386. return array_merge($elem, $attrs);
  387. }
  388. /**
  389. * form_makeFieldRight
  390. *
  391. * Create a form element for a labelled input element.
  392. * The label text will be printed after the input.
  393. *
  394. * @see form_makeField
  395. * @author Tom N Harris <tnharris@whoopdedo.org>
  396. */
  397. function form_makeFieldRight($type, $name, $value='', $label=null, $id='', $class='', $attrs=array()) {
  398. if (is_null($label)) $label = $name;
  399. $elem = array('_elem'=>'fieldright', '_text'=>$label, '_class'=>$class,
  400. 'type'=>$type, 'id'=>$id, 'name'=>$name, 'value'=>$value);
  401. return array_merge($elem, $attrs);
  402. }
  403. /**
  404. * form_makeTextField
  405. *
  406. * Create a form element for a text input element with label.
  407. *
  408. * @see form_makeField
  409. * @author Tom N Harris <tnharris@whoopdedo.org>
  410. */
  411. function form_makeTextField($name, $value='', $label=null, $id='', $class='', $attrs=array()) {
  412. if (is_null($label)) $label = $name;
  413. $elem = array('_elem'=>'textfield', '_text'=>$label, '_class'=>$class,
  414. 'id'=>$id, 'name'=>$name, 'value'=>$value, 'class'=>'edit');
  415. return array_merge($elem, $attrs);
  416. }
  417. /**
  418. * form_makePasswordField
  419. *
  420. * Create a form element for a password input element with label.
  421. * Password elements have no default value, for obvious reasons.
  422. *
  423. * @see form_makeField
  424. * @author Tom N Harris <tnharris@whoopdedo.org>
  425. */
  426. function form_makePasswordField($name, $label=null, $id='', $class='', $attrs=array()) {
  427. if (is_null($label)) $label = $name;
  428. $elem = array('_elem'=>'passwordfield', '_text'=>$label, '_class'=>$class,
  429. 'id'=>$id, 'name'=>$name, 'class'=>'edit');
  430. return array_merge($elem, $attrs);
  431. }
  432. /**
  433. * form_makeFileField
  434. *
  435. * Create a form element for a file input element with label
  436. *
  437. * @see form_makeField
  438. * @author Michael Klier <chi@chimeric.de>
  439. */
  440. function form_makeFileField($name, $label=null, $id='', $class='', $attrs=array()) {
  441. if (is_null($label)) $label = $name;
  442. $elem = array('_elem'=>'filefield', '_text'=>$label, '_class'=>$class,
  443. 'id'=>$id, 'name'=>$name, 'class'=>'edit');
  444. return array_merge($elem, $attrs);
  445. }
  446. /**
  447. * form_makeCheckboxField
  448. *
  449. * Create a form element for a checkbox input element with label.
  450. * If $value is an array, a hidden field with the same name and the value
  451. * $value[1] is constructed as well.
  452. *
  453. * @see form_makeFieldRight
  454. * @author Tom N Harris <tnharris@whoopdedo.org>
  455. */
  456. function form_makeCheckboxField($name, $value='1', $label=null, $id='', $class='', $attrs=array()) {
  457. if (is_null($label)) $label = $name;
  458. if (is_null($value) || $value=='') $value='0';
  459. $elem = array('_elem'=>'checkboxfield', '_text'=>$label, '_class'=>$class,
  460. 'id'=>$id, 'name'=>$name, 'value'=>$value);
  461. return array_merge($elem, $attrs);
  462. }
  463. /**
  464. * form_makeRadioField
  465. *
  466. * Create a form element for a radio button input element with label.
  467. *
  468. * @see form_makeFieldRight
  469. * @author Tom N Harris <tnharris@whoopdedo.org>
  470. */
  471. function form_makeRadioField($name, $value='1', $label=null, $id='', $class='', $attrs=array()) {
  472. if (is_null($label)) $label = $name;
  473. if (is_null($value) || $value=='') $value='0';
  474. $elem = array('_elem'=>'radiofield', '_text'=>$label, '_class'=>$class,
  475. 'id'=>$id, 'name'=>$name, 'value'=>$value);
  476. return array_merge($elem, $attrs);
  477. }
  478. /**
  479. * form_makeMenuField
  480. *
  481. * Create a form element for a drop-down menu with label.
  482. * The list of values can be strings, arrays of (value,text),
  483. * or an associative array with the values as keys and labels as values.
  484. * An item is selected by supplying its value or integer index.
  485. * If the list of values is an associative array, the selected item must be
  486. * a string.
  487. *
  488. * @author Tom N Harris <tnharris@whoopdedo.org>
  489. */
  490. function form_makeMenuField($name, $values, $selected='', $label=null, $id='', $class='', $attrs=array()) {
  491. if (is_null($label)) $label = $name;
  492. $options = array();
  493. reset($values);
  494. // FIXME: php doesn't know the difference between a string and an integer
  495. if (is_string(key($values))) {
  496. foreach ($values as $val=>$text) {
  497. $options[] = array($val,$text, (!is_null($selected) && $val==$selected));
  498. }
  499. } else {
  500. if (is_integer($selected)) $selected = $values[$selected];
  501. foreach ($values as $val) {
  502. if (is_array($val))
  503. @list($val,$text) = $val;
  504. else
  505. $text = null;
  506. $options[] = array($val,$text,$val===$selected);
  507. }
  508. }
  509. $elem = array('_elem'=>'menufield', '_options'=>$options, '_text'=>$label, '_class'=>$class,
  510. 'id'=>$id, 'name'=>$name);
  511. return array_merge($elem, $attrs);
  512. }
  513. /**
  514. * form_makeListboxField
  515. *
  516. * Create a form element for a list box with label.
  517. * The list of values can be strings, arrays of (value,text),
  518. * or an associative array with the values as keys and labels as values.
  519. * Items are selected by supplying its value or an array of values.
  520. *
  521. * @author Tom N Harris <tnharris@whoopdedo.org>
  522. */
  523. function form_makeListboxField($name, $values, $selected='', $label=null, $id='', $class='', $attrs=array()) {
  524. if (is_null($label)) $label = $name;
  525. $options = array();
  526. reset($values);
  527. if (is_null($selected) || $selected == '')
  528. $selected = array();
  529. elseif (!is_array($selected))
  530. $selected = array($selected);
  531. // FIXME: php doesn't know the difference between a string and an integer
  532. if (is_string(key($values))) {
  533. foreach ($values as $val=>$text) {
  534. $options[] = array($val,$text,in_array($val,$selected));
  535. }
  536. } else {
  537. foreach ($values as $val) {
  538. if (is_array($val))
  539. @list($val,$text) = $val;
  540. else
  541. $text = null;
  542. $options[] = array($val,$text,in_array($val,$selected));
  543. }
  544. }
  545. $elem = array('_elem'=>'listboxfield', '_options'=>$options, '_text'=>$label, '_class'=>$class,
  546. 'id'=>$id, 'name'=>$name);
  547. return array_merge($elem, $attrs);
  548. }
  549. /**
  550. * form_tag
  551. *
  552. * Print the HTML for a generic empty tag.
  553. * Requires '_tag' key with name of the tag.
  554. * Attributes are passed to buildAttributes()
  555. *
  556. * @author Tom N Harris <tnharris@whoopdedo.org>
  557. */
  558. function form_tag($attrs) {
  559. return '<'.$attrs['_tag'].' '.buildAttributes($attrs,true).'/>';
  560. }
  561. /**
  562. * form_opentag
  563. *
  564. * Print the HTML for a generic opening tag.
  565. * Requires '_tag' key with name of the tag.
  566. * Attributes are passed to buildAttributes()
  567. *
  568. * @author Tom N Harris <tnharris@whoopdedo.org>
  569. */
  570. function form_opentag($attrs) {
  571. return '<'.$attrs['_tag'].' '.buildAttributes($attrs,true).'>';
  572. }
  573. /**
  574. * form_closetag
  575. *
  576. * Print the HTML for a generic closing tag.
  577. * Requires '_tag' key with name of the tag.
  578. * There are no attributes.
  579. *
  580. * @author Tom N Harris <tnharris@whoopdedo.org>
  581. */
  582. function form_closetag($attrs) {
  583. return '</'.$attrs['_tag'].'>';
  584. }
  585. /**
  586. * form_openfieldset
  587. *
  588. * Print the HTML for an opening fieldset tag.
  589. * Uses the '_legend' key.
  590. * Attributes are passed to buildAttributes()
  591. *
  592. * @author Tom N Harris <tnharris@whoopdedo.org>
  593. */
  594. function form_openfieldset($attrs) {
  595. $s = '<fieldset '.buildAttributes($attrs,true).'>';
  596. if (!is_null($attrs['_legend'])) $s .= '<legend>'.$attrs['_legend'].'</legend>';
  597. return $s;
  598. }
  599. /**
  600. * form_closefieldset
  601. *
  602. * Print the HTML for a closing fieldset tag.
  603. * There are no attributes.
  604. *
  605. * @author Tom N Harris <tnharris@whoopdedo.org>
  606. */
  607. function form_closefieldset() {
  608. return '</fieldset>';
  609. }
  610. /**
  611. * form_hidden
  612. *
  613. * Print the HTML for a hidden input element.
  614. * Uses only 'name' and 'value' attributes.
  615. * Value is passed to formText()
  616. *
  617. * @author Tom N Harris <tnharris@whoopdedo.org>
  618. */
  619. function form_hidden($attrs) {
  620. return '<input type="hidden" name="'.$attrs['name'].'" value="'.formText($attrs['value']).'" />';
  621. }
  622. /**
  623. * form_wikitext
  624. *
  625. * Print the HTML for the wiki textarea.
  626. * Requires '_text' with default text of the field.
  627. * Text will be passed to formText(), attributes to buildAttributes()
  628. *
  629. * @author Tom N Harris <tnharris@whoopdedo.org>
  630. */
  631. function form_wikitext($attrs) {
  632. // mandatory attributes
  633. unset($attrs['name']);
  634. unset($attrs['id']);
  635. return '<textarea name="wikitext" id="wiki__text" onKeyDown="if(event.keyCode==83 && event.ctrlKey) { event.returnValue = false; event.cancelBubble=true; this.form.elements[\'edbtn__save\'].click();return false; }" '
  636. .buildAttributes($attrs,true).'>'.DOKU_LF
  637. .formText($attrs['_text'])
  638. .'</textarea>';
  639. // return '<textarea name="wikitext" id="wiki__text" '
  640. // .buildAttributes($attrs,true).'>'.DOKU_LF
  641. // .formText($attrs['_text'])
  642. // .'</textarea>';
  643. }
  644. /**
  645. * form_button
  646. *
  647. * Print the HTML for a form button.
  648. * If '_action' is set, the button name will be "do[_action]".
  649. * Other attributes are passed to buildAttributes()
  650. *
  651. * @author Tom N Harris <tnharris@whoopdedo.org>
  652. */
  653. function form_button($attrs) {
  654. $p = (!empty($attrs['_action'])) ? 'name="do['.$attrs['_action'].']" ' : '';
  655. return '<input '.$p.buildAttributes($attrs,true).' />';
  656. }
  657. /**
  658. * form_field
  659. *
  660. * Print the HTML for a form input field.
  661. * _class : class attribute used on the label tag
  662. * _text : Text to display before the input. Not escaped.
  663. * Other attributes are passed to buildAttributes() for the input tag.
  664. *
  665. * @author Tom N Harris <tnharris@whoopdedo.org>
  666. */
  667. function form_field($attrs) {
  668. $s = '<label';
  669. if ($attrs['_class']) $s .= ' class="'.$attrs['_class'].'"';
  670. if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"';
  671. $s .= '><span>'.$attrs['_text'].'</span>';
  672. $s .= ' <input '.buildAttributes($attrs,true).' /></label>';
  673. if (preg_match('/(^| )block($| )/', $attrs['_class']))
  674. $s .= '<br />';
  675. return $s;
  676. }
  677. /**
  678. * form_fieldright
  679. *
  680. * Print the HTML for a form input field. (right-aligned)
  681. * _class : class attribute used on the label tag
  682. * _text : Text to display after the input. Not escaped.
  683. * Other attributes are passed to buildAttributes() for the input tag.
  684. *
  685. * @author Tom N Harris <tnharris@whoopdedo.org>
  686. */
  687. function form_fieldright($attrs) {
  688. $s = '<label';
  689. if ($attrs['_class']) $s .= ' class="'.$attrs['_class'].'"';
  690. if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"';
  691. $s .= '><input '.buildAttributes($attrs,true).' />';
  692. $s .= ' <span>'.$attrs['_text'].'</span></label>';
  693. if (preg_match('/(^| )block($| )/', $attrs['_class']))
  694. $s .= '<br />';
  695. return $s;
  696. }
  697. /**
  698. * form_textfield
  699. *
  700. * Print the HTML for a text input field.
  701. * _class : class attribute used on the label tag
  702. * _text : Text to display before the input. Not escaped.
  703. * Other attributes are passed to buildAttributes() for the input tag.
  704. *
  705. * @author Tom N Harris <tnharris@whoopdedo.org>
  706. */
  707. function form_textfield($attrs) {
  708. // mandatory attributes
  709. unset($attrs['type']);
  710. $s = '<label';
  711. if ($attrs['_class']) $s .= ' class="'.$attrs['_class'].'"';
  712. if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"';
  713. $s .= '><span>'.$attrs['_text'].'</span> ';
  714. $s .= '<input type="text" '.buildAttributes($attrs,true).' /></label>';
  715. if (preg_match('/(^| )block($| )/', $attrs['_class']))
  716. $s .= '<br />';
  717. return $s;
  718. }
  719. /**
  720. * form_passwordfield
  721. *
  722. * Print the HTML for a password input field.
  723. * _class : class attribute used on the label tag
  724. * _text : Text to display before the input. Not escaped.
  725. * Other attributes are passed to buildAttributes() for the input tag.
  726. *
  727. * @author Tom N Harris <tnharris@whoopdedo.org>
  728. */
  729. function form_passwordfield($attrs) {
  730. // mandatory attributes
  731. unset($attrs['type']);
  732. $s = '<label';
  733. if ($attrs['_class']) $s .= ' class="'.$attrs['_class'].'"';
  734. if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"';
  735. $s .= '><span>'.$attrs['_text'].'</span> ';
  736. $s .= '<input type="password" '.buildAttributes($attrs,true).' /></label>';
  737. if (preg_match('/(^| )block($| )/', $attrs['_class']))
  738. $s .= '<br />';
  739. return $s;
  740. }
  741. /**
  742. * form_filefield
  743. *
  744. * Print the HTML for a file input field.
  745. * _class : class attribute used on the label tag
  746. * _text : Text to display before the input. Not escaped
  747. * _maxlength : Allowed size in byte
  748. * _accept : Accepted mime-type
  749. * Other attributes are passed to buildAttributes() for the input tag
  750. *
  751. * @author Michael Klier <chi@chimeric.de>
  752. */
  753. function form_filefield($attrs) {
  754. $s = '<label';
  755. if ($attrs['_class']) $s .= ' class="'.$attrs['_class'].'"';
  756. if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"';
  757. $s .= '><span>'.$attrs['_text'].'</span> ';
  758. $s .= '<input type="file" '.buildAttributes($attrs,true);
  759. if (!empty($attrs['_maxlength'])) $s .= ' maxlength="'.$attrs['_maxlength'].'"';
  760. if (!empty($attrs['_accept'])) $s .= ' accept="'.$attrs['_accept'].'"';
  761. $s .= ' /></label>';
  762. if (preg_match('/(^| )block($| )/', $attrs['_class']))
  763. $s .= '<br />';
  764. return $s;
  765. }
  766. /**
  767. * form_checkboxfield
  768. *
  769. * Print the HTML for a checkbox input field.
  770. * _class : class attribute used on the label tag
  771. * _text : Text to display after the input. Not escaped.
  772. * Other attributes are passed to buildAttributes() for the input tag.
  773. * If value is an array, a hidden field with the same name and the value
  774. * $attrs['value'][1] is constructed as well.
  775. *
  776. * @author Tom N Harris <tnharris@whoopdedo.org>
  777. */
  778. function form_checkboxfield($attrs) {
  779. // mandatory attributes
  780. unset($attrs['type']);
  781. $s = '<label';
  782. if ($attrs['_class']) $s .= ' class="'.$attrs['_class'].'"';
  783. if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"';
  784. $s .= '>';
  785. if (is_array($attrs['value'])) {
  786. echo '<input type="hidden" name="' . hsc($attrs['name']) .'"'
  787. . ' value="' . hsc($attrs['value'][1]) . '" />';
  788. $attrs['value'] = $attrs['value'][0];
  789. }
  790. $s .= '<input type="checkbox" '.buildAttributes($attrs,true).' />';
  791. $s .= ' <span>'.$attrs['_text'].'</span></label>';
  792. if (preg_match('/(^| )block($| )/', $attrs['_class']))
  793. $s .= '<br />';
  794. return $s;
  795. }
  796. /**
  797. * form_radiofield
  798. *
  799. * Print the HTML for a radio button input field.
  800. * _class : class attribute used on the label tag
  801. * _text : Text to display after the input. Not escaped.
  802. * Other attributes are passed to buildAttributes() for the input tag.
  803. *
  804. * @author Tom N Harris <tnharris@whoopdedo.org>
  805. */
  806. function form_radiofield($attrs) {
  807. // mandatory attributes
  808. unset($attrs['type']);
  809. $s = '<label';
  810. if ($attrs['_class']) $s .= ' class="'.$attrs['_class'].'"';
  811. if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"';
  812. $s .= '><input type="radio" '.buildAttributes($attrs,true).' />';
  813. $s .= ' <span>'.$attrs['_text'].'</span></label>';
  814. if (preg_match('/(^| )block($| )/', $attrs['_class']))
  815. $s .= '<br />';
  816. return $s;
  817. }
  818. /**
  819. * form_menufield
  820. *
  821. * Print the HTML for a drop-down menu.
  822. * _options : Array of (value,text,selected) for the menu.
  823. * Text can be omitted. Text and value are passed to formText()
  824. * Only one item can be selected.
  825. * _class : class attribute used on the label tag
  826. * _text : Text to display before the menu. Not escaped.
  827. * Other attributes are passed to buildAttributes() for the input tag.
  828. *
  829. * @author Tom N Harris <tnharris@whoopdedo.org>
  830. */
  831. function form_menufield($attrs) {
  832. $attrs['size'] = '1';
  833. $s = '<label';
  834. if ($attrs['_class']) $s .= ' class="'.$attrs['_class'].'"';
  835. if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"';
  836. $s .= '><span>'.$attrs['_text'].'</span>';
  837. $s .= ' <select '.buildAttributes($attrs,true).'>'.DOKU_LF;
  838. if (!empty($attrs['_options'])) {
  839. $selected = false;
  840. $cnt = count($attrs['_options']);
  841. for($n=0; $n < $cnt; $n++){
  842. @list($value,$text,$select) = $attrs['_options'][$n];
  843. $p = '';
  844. if (!is_null($text))
  845. $p .= ' value="'.formText($value).'"';
  846. else
  847. $text = $value;
  848. if (!empty($select) && !$selected) {
  849. $p .= ' selected="selected"';
  850. $selected = true;
  851. }
  852. $s .= '<option'.$p.'>'.formText($text).'</option>';
  853. }
  854. } else {
  855. $s .= '<option></option>';
  856. }
  857. $s .= DOKU_LF.'</select></label>';
  858. if (preg_match('/(^| )block($| )/', $attrs['_class']))
  859. $s .= '<br />';
  860. return $s;
  861. }
  862. /**
  863. * form_listboxfield
  864. *
  865. * Print the HTML for a list box.
  866. * _options : Array of (value,text,selected) for the list.
  867. * Text can be omitted. Text and value are passed to formText()
  868. * _class : class attribute used on the label tag
  869. * _text : Text to display before the menu. Not escaped.
  870. * Other attributes are passed to buildAttributes() for the input tag.
  871. *
  872. * @author Tom N Harris <tnharris@whoopdedo.org>
  873. */
  874. function form_listboxfield($attrs) {
  875. $s = '<label';
  876. if ($attrs['_class']) $s .= ' class="'.$attrs['_class'].'"';
  877. if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"';
  878. $s .= '><span>'.$attrs['_text'].'</span> ';
  879. $s .= '<select '.buildAttributes($attrs,true).'>'.DOKU_LF;
  880. if (!empty($attrs['_options'])) {
  881. foreach ($attrs['_options'] as $opt) {
  882. @list($value,$text,$select) = $opt;
  883. $p = '';
  884. if(is_null($text)) $text = $value;
  885. $p .= ' value="'.formText($value).'"';
  886. if (!empty($select)) $p .= ' selected="selected"';
  887. $s .= '<option'.$p.'>'.formText($text).'</option>';
  888. }
  889. } else {
  890. $s .= '<option></option>';
  891. }
  892. $s .= DOKU_LF.'</select></label>';
  893. if (preg_match('/(^| )block($| )/', $attrs['_class']))
  894. $s .= '<br />';
  895. return $s;
  896. }