PageRenderTime 63ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

/elgg/mod/dokuwiki/lib/dokuwiki/inc/form.php

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