PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/pear/HTML/QuickForm/element.php

https://bitbucket.org/kudutest1/moodlegit
PHP | 481 lines | 166 code | 50 blank | 265 comment | 18 complexity | 2aae3b42dbcf9b646ca3765b21bd470e MD5 | raw file
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP version 4.0 |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997, 1998, 1999, 2000, 2001 The PHP Group |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.0 of the PHP license, |
  9. // | that is bundled with this package in the file LICENSE, and is |
  10. // | available at through the world-wide-web at |
  11. // | http://www.php.net/license/2_02.txt. |
  12. // | If you did not receive a copy of the PHP license and are unable to |
  13. // | obtain it through the world-wide-web, please send a note to |
  14. // | license@php.net so we can mail you a copy immediately. |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Adam Daniel <adaniel1@eesus.jnj.com> |
  17. // | Bertrand Mansion <bmansion@mamasam.com> |
  18. // +----------------------------------------------------------------------+
  19. //
  20. // $Id$
  21. require_once('HTML/Common.php');
  22. /**
  23. * Base class for form elements
  24. *
  25. * @author Adam Daniel <adaniel1@eesus.jnj.com>
  26. * @author Bertrand Mansion <bmansion@mamasam.com>
  27. * @version 1.3
  28. * @since PHP4.04pl1
  29. * @access public
  30. * @abstract
  31. */
  32. class HTML_QuickForm_element extends HTML_Common
  33. {
  34. // {{{ properties
  35. /**
  36. * Label of the field
  37. * @var string
  38. * @since 1.3
  39. * @access private
  40. */
  41. var $_label = '';
  42. /**
  43. * Form element type
  44. * @var string
  45. * @since 1.0
  46. * @access private
  47. */
  48. var $_type = '';
  49. /**
  50. * Flag to tell if element is frozen
  51. * @var boolean
  52. * @since 1.0
  53. * @access private
  54. */
  55. var $_flagFrozen = false;
  56. /**
  57. * Does the element support persistant data when frozen
  58. * @var boolean
  59. * @since 1.3
  60. * @access private
  61. */
  62. var $_persistantFreeze = false;
  63. // }}}
  64. // {{{ constructor
  65. /**
  66. * Class constructor
  67. *
  68. * @param string Name of the element
  69. * @param mixed Label(s) for the element
  70. * @param mixed Associative array of tag attributes or HTML attributes name="value" pairs
  71. * @since 1.0
  72. * @access public
  73. * @return void
  74. */
  75. function HTML_QuickForm_element($elementName=null, $elementLabel=null, $attributes=null)
  76. {
  77. HTML_Common::HTML_Common($attributes);
  78. if (isset($elementName)) {
  79. $this->setName($elementName);
  80. }
  81. if (isset($elementLabel)) {
  82. $this->setLabel($elementLabel);
  83. }
  84. } //end constructor
  85. // }}}
  86. // {{{ apiVersion()
  87. /**
  88. * Returns the current API version
  89. *
  90. * @since 1.0
  91. * @access public
  92. * @return float
  93. */
  94. function apiVersion()
  95. {
  96. return 2.0;
  97. } // end func apiVersion
  98. // }}}
  99. // {{{ getType()
  100. /**
  101. * Returns element type
  102. *
  103. * @since 1.0
  104. * @access public
  105. * @return string
  106. */
  107. function getType()
  108. {
  109. return $this->_type;
  110. } // end func getType
  111. // }}}
  112. // {{{ setName()
  113. /**
  114. * Sets the input field name
  115. *
  116. * @param string $name Input field name attribute
  117. * @since 1.0
  118. * @access public
  119. * @return void
  120. */
  121. function setName($name)
  122. {
  123. // interface method
  124. } //end func setName
  125. // }}}
  126. // {{{ getName()
  127. /**
  128. * Returns the element name
  129. *
  130. * @since 1.0
  131. * @access public
  132. * @return string
  133. */
  134. function getName()
  135. {
  136. // interface method
  137. } //end func getName
  138. // }}}
  139. // {{{ setValue()
  140. /**
  141. * Sets the value of the form element
  142. *
  143. * @param string $value Default value of the form element
  144. * @since 1.0
  145. * @access public
  146. * @return void
  147. */
  148. function setValue($value)
  149. {
  150. // interface
  151. } // end func setValue
  152. // }}}
  153. // {{{ getValue()
  154. /**
  155. * Returns the value of the form element
  156. *
  157. * @since 1.0
  158. * @access public
  159. * @return mixed
  160. */
  161. function getValue()
  162. {
  163. // interface
  164. return null;
  165. } // end func getValue
  166. // }}}
  167. // {{{ freeze()
  168. /**
  169. * Freeze the element so that only its value is returned
  170. *
  171. * @access public
  172. * @return void
  173. */
  174. function freeze()
  175. {
  176. $this->_flagFrozen = true;
  177. } //end func freeze
  178. // }}}
  179. // {{{ unfreeze()
  180. /**
  181. * Unfreezes the element so that it becomes editable
  182. *
  183. * @access public
  184. * @return void
  185. * @since 3.2.4
  186. */
  187. function unfreeze()
  188. {
  189. $this->_flagFrozen = false;
  190. }
  191. // }}}
  192. // {{{ getFrozenHtml()
  193. /**
  194. * Returns the value of field without HTML tags
  195. *
  196. * @since 1.0
  197. * @access public
  198. * @return string
  199. */
  200. function getFrozenHtml()
  201. {
  202. $value = $this->getValue();
  203. return ('' != $value? htmlspecialchars($value): '&nbsp;') .
  204. $this->_getPersistantData();
  205. } //end func getFrozenHtml
  206. // }}}
  207. // {{{ _getPersistantData()
  208. /**
  209. * Used by getFrozenHtml() to pass the element's value if _persistantFreeze is on
  210. *
  211. * @access private
  212. * @return string
  213. */
  214. function _getPersistantData()
  215. {
  216. if (!$this->_persistantFreeze) {
  217. return '';
  218. } else {
  219. $id = $this->getAttribute('id');
  220. return '<input' . $this->_getAttrString(array(
  221. 'type' => 'hidden',
  222. 'name' => $this->getName(),
  223. 'value' => $this->getValue()
  224. ) + (isset($id)? array('id' => $id): array())) . ' />';
  225. }
  226. }
  227. // }}}
  228. // {{{ isFrozen()
  229. /**
  230. * Returns whether or not the element is frozen
  231. *
  232. * @since 1.3
  233. * @access public
  234. * @return bool
  235. */
  236. function isFrozen()
  237. {
  238. return $this->_flagFrozen;
  239. } // end func isFrozen
  240. // }}}
  241. // {{{ setPersistantFreeze()
  242. /**
  243. * Sets wether an element value should be kept in an hidden field
  244. * when the element is frozen or not
  245. *
  246. * @param bool $persistant True if persistant value
  247. * @since 2.0
  248. * @access public
  249. * @return void
  250. */
  251. function setPersistantFreeze($persistant=false)
  252. {
  253. $this->_persistantFreeze = $persistant;
  254. } //end func setPersistantFreeze
  255. // }}}
  256. // {{{ setLabel()
  257. /**
  258. * Sets display text for the element
  259. *
  260. * @param string $label Display text for the element
  261. * @since 1.3
  262. * @access public
  263. * @return void
  264. */
  265. function setLabel($label)
  266. {
  267. $this->_label = $label;
  268. } //end func setLabel
  269. // }}}
  270. // {{{ getLabel()
  271. /**
  272. * Returns display text for the element
  273. *
  274. * @since 1.3
  275. * @access public
  276. * @return string
  277. */
  278. function getLabel()
  279. {
  280. return $this->_label;
  281. } //end func getLabel
  282. // }}}
  283. // {{{ _findValue()
  284. /**
  285. * Tries to find the element value from the values array
  286. *
  287. * @since 2.7
  288. * @access private
  289. * @return mixed
  290. */
  291. function _findValue(&$values)
  292. {
  293. if (empty($values)) {
  294. return null;
  295. }
  296. $elementName = $this->getName();
  297. if (isset($values[$elementName])) {
  298. return $values[$elementName];
  299. } elseif (strpos($elementName, '[')) {
  300. $myVar = "['" . str_replace(array(']', '['), array('', "']['"), $elementName) . "']";
  301. return eval("return (isset(\$values$myVar)) ? \$values$myVar : null;");
  302. } else {
  303. return null;
  304. }
  305. } //end func _findValue
  306. // }}}
  307. // {{{ onQuickFormEvent()
  308. /**
  309. * Called by HTML_QuickForm whenever form event is made on this element
  310. *
  311. * @param string $event Name of event
  312. * @param mixed $arg event arguments
  313. * @param object $caller calling object
  314. * @since 1.0
  315. * @access public
  316. * @return void
  317. */
  318. function onQuickFormEvent($event, $arg, &$caller)
  319. {
  320. switch ($event) {
  321. case 'createElement':
  322. $className = get_class($this);
  323. $this->$className($arg[0], $arg[1], $arg[2], $arg[3], $arg[4]);
  324. break;
  325. case 'addElement':
  326. $this->onQuickFormEvent('createElement', $arg, $caller);
  327. $this->onQuickFormEvent('updateValue', null, $caller);
  328. break;
  329. case 'updateValue':
  330. // constant values override both default and submitted ones
  331. // default values are overriden by submitted
  332. $value = $this->_findValue($caller->_constantValues);
  333. if (null === $value) {
  334. $value = $this->_findValue($caller->_submitValues);
  335. if (null === $value) {
  336. $value = $this->_findValue($caller->_defaultValues);
  337. }
  338. }
  339. if (null !== $value) {
  340. $this->setValue($value);
  341. }
  342. break;
  343. case 'setGroupValue':
  344. $this->setValue($arg);
  345. }
  346. return true;
  347. } // end func onQuickFormEvent
  348. // }}}
  349. // {{{ accept()
  350. /**
  351. * Accepts a renderer
  352. *
  353. * @param object An HTML_QuickForm_Renderer object
  354. * @param bool Whether an element is required
  355. * @param string An error message associated with an element
  356. * @access public
  357. * @return void
  358. */
  359. function accept(&$renderer, $required=false, $error=null)
  360. {
  361. $renderer->renderElement($this, $required, $error);
  362. } // end func accept
  363. // }}}
  364. // {{{ _generateId()
  365. /**
  366. * Automatically generates and assigns an 'id' attribute for the element.
  367. *
  368. * Currently used to ensure that labels work on radio buttons and
  369. * checkboxes. Per idea of Alexander Radivanovich.
  370. *
  371. * @access private
  372. * @return void
  373. */
  374. function _generateId() {
  375. if ($this->getAttribute('id')) {
  376. return;
  377. }
  378. $id = $this->getName();
  379. $id = 'id_' . str_replace(array('qf_', '[', ']'), array('', '_', ''), $id);
  380. $id = clean_param($id, PARAM_ALPHANUMEXT);
  381. $this->updateAttributes(array('id' => $id));
  382. }
  383. // }}}
  384. // {{{ exportValue()
  385. /**
  386. * Returns a 'safe' element's value
  387. *
  388. * @param array array of submitted values to search
  389. * @param bool whether to return the value as associative array
  390. * @access public
  391. * @return mixed
  392. */
  393. function exportValue(&$submitValues, $assoc = false)
  394. {
  395. $value = $this->_findValue($submitValues);
  396. if (null === $value) {
  397. $value = $this->getValue();
  398. }
  399. return $this->_prepareValue($value, $assoc);
  400. }
  401. // }}}
  402. // {{{ _prepareValue()
  403. /**
  404. * Used by exportValue() to prepare the value for returning
  405. *
  406. * @param mixed the value found in exportValue()
  407. * @param bool whether to return the value as associative array
  408. * @access private
  409. * @return mixed
  410. */
  411. function _prepareValue($value, $assoc)
  412. {
  413. if (null === $value) {
  414. return null;
  415. } elseif (!$assoc) {
  416. return $value;
  417. } else {
  418. $name = $this->getName();
  419. if (!strpos($name, '[')) {
  420. return array($name => $value);
  421. } else {
  422. $valueAry = array();
  423. $myIndex = "['" . str_replace(array(']', '['), array('', "']['"), $name) . "']";
  424. eval("\$valueAry$myIndex = \$value;");
  425. return $valueAry;
  426. }
  427. }
  428. }
  429. // }}}
  430. } // end class HTML_QuickForm_element
  431. ?>