element.php 15 KB

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