element.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  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. $myVar = "['" . str_replace(
  385. array('\\', '\'', ']', '['), array('\\\\', '\\\'', '', "']['"),
  386. $elementName
  387. ) . "']";
  388. return eval("return (isset(\$values$myVar)) ? \$values$myVar : null;");
  389. } else {
  390. return null;
  391. }
  392. } //end func _findValue
  393. // }}}
  394. // {{{ onQuickFormEvent()
  395. /**
  396. * Called by HTML_QuickForm whenever form event is made on this element
  397. *
  398. * @param string $event Name of event
  399. * @param mixed $arg event arguments
  400. * @param object &$caller calling object
  401. * @since 1.0
  402. * @access public
  403. * @return void
  404. */
  405. public function onQuickFormEvent($event, $arg, &$caller)
  406. {
  407. switch ($event) {
  408. case 'createElement':
  409. //$className = get_class($this);
  410. //$this->$className($arg[0], $arg[1], $arg[2], $arg[3], $arg[4], $arg[5], $arg[6]);
  411. break;
  412. case 'addElement':
  413. $this->onQuickFormEvent('createElement', $arg, $caller);
  414. $this->onQuickFormEvent('updateValue', null, $caller);
  415. break;
  416. case 'updateValue':
  417. // constant values override both default and submitted ones
  418. // default values are overriden by submitted
  419. $value = $this->_findValue($caller->_constantValues);
  420. if (null === $value) {
  421. $value = $this->_findValue($caller->_submitValues);
  422. if (null === $value) {
  423. $value = $this->_findValue($caller->_defaultValues);
  424. }
  425. }
  426. if (null !== $value) {
  427. $this->setValue($value);
  428. }
  429. break;
  430. case 'setGroupValue':
  431. $this->setValue($arg);
  432. }
  433. return true;
  434. }
  435. /**
  436. * Accepts a renderer
  437. *
  438. * @param HTML_QuickForm_Renderer renderer object
  439. * @param bool Whether an element is required
  440. * @param string An error message associated with an element
  441. * @access public
  442. * @return void
  443. */
  444. function accept(&$renderer, $required=false, $error=null)
  445. {
  446. $renderer->renderElement($this, $required, $error);
  447. }
  448. /**
  449. * Automatically generates and assigns an 'id' attribute for the element.
  450. *
  451. * Currently used to ensure that labels work on radio buttons and
  452. * checkboxes. Per idea of Alexander Radivanovich.
  453. *
  454. * @access private
  455. * @return void
  456. */
  457. function _generateId()
  458. {
  459. static $idx = 1;
  460. if (!$this->getAttribute('id')) {
  461. $this->updateAttributes(array('id' => 'qf_' . substr(md5(microtime() . $idx++), 0, 6)));
  462. }
  463. }
  464. /**
  465. * Returns a 'safe' element's value
  466. *
  467. * @param array array of submitted values to search
  468. * @param bool whether to return the value as associative array
  469. * @access public
  470. * @return mixed
  471. */
  472. function exportValue(&$submitValues, $assoc = false)
  473. {
  474. $value = $this->_findValue($submitValues);
  475. if (null === $value) {
  476. $value = $this->getValue();
  477. }
  478. return $this->_prepareValue($value, $assoc);
  479. }
  480. // }}}
  481. // {{{ _prepareValue()
  482. /**
  483. * Used by exportValue() to prepare the value for returning
  484. *
  485. * @param mixed the value found in exportValue()
  486. * @param bool whether to return the value as associative array
  487. * @access private
  488. * @return mixed
  489. */
  490. function _prepareValue($value, $assoc)
  491. {
  492. if (null === $value) {
  493. return null;
  494. } elseif (!$assoc) {
  495. return $value;
  496. } else {
  497. $name = $this->getName();
  498. if (!strpos($name, '[')) {
  499. return array($name => $value);
  500. } else {
  501. $valueAry = array();
  502. $myIndex = "['" . str_replace(
  503. array('\\', '\'', ']', '['), array('\\\\', '\\\'', '', "']['"),
  504. $name
  505. ) . "']";
  506. eval("\$valueAry$myIndex = \$value;");
  507. return $valueAry;
  508. }
  509. }
  510. }
  511. /**
  512. * @param mixed $template
  513. * @return HTML_QuickForm_element
  514. */
  515. public function setTemplate($template)
  516. {
  517. $this->template = $template;
  518. return $this;
  519. }
  520. /**
  521. * @return string
  522. */
  523. public function getCustomFrozenTemplate()
  524. {
  525. return $this->customFrozenTemplate;
  526. }
  527. /**
  528. * @param string $customFrozenTemplate
  529. * @return HTML_QuickForm_element
  530. */
  531. public function setCustomFrozenTemplate($customFrozenTemplate)
  532. {
  533. $this->customFrozenTemplate = $customFrozenTemplate;
  534. return $this;
  535. }
  536. }