element.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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: element.php 20456 2009-05-10 17:27:44Z ivantcholakov $
  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. global $charset;
  204. return ('' != $value? htmlspecialchars($value, ENT_COMPAT, $charset): '&nbsp;') .
  205. $this->_getPersistantData();
  206. } //end func getFrozenHtml
  207. // }}}
  208. // {{{ _getPersistantData()
  209. /**
  210. * Used by getFrozenHtml() to pass the element's value if _persistantFreeze is on
  211. *
  212. * @access private
  213. * @return string
  214. */
  215. function _getPersistantData()
  216. {
  217. if (!$this->_persistantFreeze) {
  218. return '';
  219. } else {
  220. $id = $this->getAttribute('id');
  221. return '<input' . $this->_getAttrString(array(
  222. 'type' => 'hidden',
  223. 'name' => $this->getName(),
  224. 'value' => $this->getValue()
  225. ) + (isset($id)? array('id' => $id): array())) . ' />';
  226. }
  227. }
  228. // }}}
  229. // {{{ isFrozen()
  230. /**
  231. * Returns whether or not the element is frozen
  232. *
  233. * @since 1.3
  234. * @access public
  235. * @return bool
  236. */
  237. function isFrozen()
  238. {
  239. return $this->_flagFrozen;
  240. } // end func isFrozen
  241. // }}}
  242. // {{{ setPersistantFreeze()
  243. /**
  244. * Sets wether an element value should be kept in an hidden field
  245. * when the element is frozen or not
  246. *
  247. * @param bool $persistant True if persistant value
  248. * @since 2.0
  249. * @access public
  250. * @return void
  251. */
  252. function setPersistantFreeze($persistant=false)
  253. {
  254. $this->_persistantFreeze = $persistant;
  255. } //end func setPersistantFreeze
  256. // }}}
  257. // {{{ setLabel()
  258. /**
  259. * Sets display text for the element
  260. *
  261. * @param string $label Display text for the element
  262. * @since 1.3
  263. * @access public
  264. * @return void
  265. */
  266. function setLabel($label)
  267. {
  268. $this->_label = $label;
  269. } //end func setLabel
  270. // }}}
  271. // {{{ getLabel()
  272. /**
  273. * Returns display text for the element
  274. *
  275. * @since 1.3
  276. * @access public
  277. * @return string
  278. */
  279. function getLabel()
  280. {
  281. return $this->_label;
  282. } //end func getLabel
  283. // }}}
  284. // {{{ _findValue()
  285. /**
  286. * Tries to find the element value from the values array
  287. *
  288. * @since 2.7
  289. * @access private
  290. * @return mixed
  291. */
  292. function _findValue(&$values)
  293. {
  294. if (empty($values)) {
  295. return null;
  296. }
  297. $elementName = $this->getName();
  298. if (isset($values[$elementName])) {
  299. return $values[$elementName];
  300. } elseif (strpos($elementName, '[')) {
  301. $myVar = "['" . str_replace(
  302. array('\\', '\'', ']', '['), array('\\\\', '\\\'', '', "']['"),
  303. $elementName
  304. ) . "']";
  305. return eval("return (isset(\$values$myVar)) ? \$values$myVar : null;");
  306. } else {
  307. return null;
  308. }
  309. } //end func _findValue
  310. // }}}
  311. // {{{ onQuickFormEvent()
  312. /**
  313. * Called by HTML_QuickForm whenever form event is made on this element
  314. *
  315. * @param string $event Name of event
  316. * @param mixed $arg event arguments
  317. * @param object $caller calling object
  318. * @since 1.0
  319. * @access public
  320. * @return void
  321. */
  322. function onQuickFormEvent($event, $arg, &$caller)
  323. {
  324. switch ($event) {
  325. case 'createElement':
  326. $className = get_class($this);
  327. $this->$className($arg[0], $arg[1], $arg[2], $arg[3], $arg[4]);
  328. break;
  329. case 'addElement':
  330. $this->onQuickFormEvent('createElement', $arg, $caller);
  331. $this->onQuickFormEvent('updateValue', null, $caller);
  332. break;
  333. case 'updateValue':
  334. // constant values override both default and submitted ones
  335. // default values are overriden by submitted
  336. $value = $this->_findValue($caller->_constantValues);
  337. if (null === $value) {
  338. $value = $this->_findValue($caller->_submitValues);
  339. if (null === $value) {
  340. $value = $this->_findValue($caller->_defaultValues);
  341. }
  342. }
  343. if (null !== $value) {
  344. $this->setValue($value);
  345. }
  346. break;
  347. case 'setGroupValue':
  348. $this->setValue($arg);
  349. }
  350. return true;
  351. } // end func onQuickFormEvent
  352. // }}}
  353. // {{{ accept()
  354. /**
  355. * Accepts a renderer
  356. *
  357. * @param object An HTML_QuickForm_Renderer object
  358. * @param bool Whether an element is required
  359. * @param string An error message associated with an element
  360. * @access public
  361. * @return void
  362. */
  363. function accept(&$renderer, $required=false, $error=null)
  364. {
  365. $renderer->renderElement($this, $required, $error);
  366. } // end func accept
  367. // }}}
  368. // {{{ _generateId()
  369. /**
  370. * Automatically generates and assigns an 'id' attribute for the element.
  371. *
  372. * Currently used to ensure that labels work on radio buttons and
  373. * checkboxes. Per idea of Alexander Radivanovich.
  374. *
  375. * @access private
  376. * @return void
  377. */
  378. function _generateId()
  379. {
  380. static $idx = 1;
  381. if (!$this->getAttribute('id')) {
  382. $this->updateAttributes(array('id' => 'qf_' . substr(md5(microtime() . $idx++), 0, 6)));
  383. }
  384. } // end func _generateId
  385. // }}}
  386. // {{{ exportValue()
  387. /**
  388. * Returns a 'safe' element's value
  389. *
  390. * @param array array of submitted values to search
  391. * @param bool whether to return the value as associative array
  392. * @access public
  393. * @return mixed
  394. */
  395. function exportValue(&$submitValues, $assoc = false)
  396. {
  397. $value = $this->_findValue($submitValues);
  398. if (null === $value) {
  399. $value = $this->getValue();
  400. }
  401. return $this->_prepareValue($value, $assoc);
  402. }
  403. // }}}
  404. // {{{ _prepareValue()
  405. /**
  406. * Used by exportValue() to prepare the value for returning
  407. *
  408. * @param mixed the value found in exportValue()
  409. * @param bool whether to return the value as associative array
  410. * @access private
  411. * @return mixed
  412. */
  413. function _prepareValue($value, $assoc)
  414. {
  415. if (null === $value) {
  416. return null;
  417. } elseif (!$assoc) {
  418. return $value;
  419. } else {
  420. $name = $this->getName();
  421. if (!strpos($name, '[')) {
  422. return array($name => $value);
  423. } else {
  424. $valueAry = array();
  425. $myIndex = "['" . str_replace(
  426. array('\\', '\'', ']', '['), array('\\\\', '\\\'', '', "']['"),
  427. $name
  428. ) . "']";
  429. eval("\$valueAry$myIndex = \$value;");
  430. return $valueAry;
  431. }
  432. }
  433. }
  434. // }}}
  435. } // end class HTML_QuickForm_element
  436. ?>