element.php 13 KB

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