element.php 14 KB

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