element.php 13 KB

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