element.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  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 $button;
  41. private $customFrozenTemplate = '';
  42. protected $inputSize;
  43. /**
  44. * Label of the field
  45. * @var string
  46. */
  47. public $_label = '';
  48. /**
  49. * Label "for" a field... (Chamilo LMS customization)
  50. * @var string
  51. * @access private
  52. */
  53. public $_label_for = '';
  54. /**
  55. * Form element type
  56. * @var string
  57. * @since 1.0
  58. * @access private
  59. */
  60. public $_type = '';
  61. /**
  62. * Flag to tell if element is frozen
  63. * @var boolean
  64. * @since 1.0
  65. * @access private
  66. */
  67. public $_flagFrozen = false;
  68. /**
  69. * Does the element support persistant data when frozen
  70. * @var boolean
  71. * @since 1.3
  72. * @access private
  73. */
  74. public $_persistantFreeze = false;
  75. protected $columnsSize;
  76. /**
  77. * Class constructor
  78. *
  79. * @param string Name of the element
  80. * @param string|array Label(s) for the element
  81. * @param mixed Associative array of tag attributes or HTML attributes name="value" pairs
  82. *
  83. * @return void
  84. * @since 1.0
  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 boolean
  107. */
  108. public function getButton()
  109. {
  110. return $this->button;
  111. }
  112. /**
  113. * @param boolean $button
  114. */
  115. public function setButton($button): void
  116. {
  117. $this->button = $button;
  118. }
  119. /**
  120. * @return null
  121. */
  122. public function getColumnsSize()
  123. {
  124. return $this->columnsSize;
  125. }
  126. /**
  127. * @param null $columnsSize
  128. */
  129. public function setColumnsSize($columnsSize)
  130. {
  131. $this->columnsSize = $columnsSize;
  132. }
  133. /**
  134. * @return string
  135. */
  136. public function getLayout()
  137. {
  138. return $this->layout;
  139. }
  140. /**
  141. * @param string $layout
  142. */
  143. public function setLayout($layout)
  144. {
  145. $this->layout = $layout;
  146. }
  147. /**
  148. * @return string
  149. */
  150. public function getIcon()
  151. {
  152. return $this->icon;
  153. }
  154. /**
  155. * @return string
  156. */
  157. public function getIconToHtml()
  158. {
  159. return $this->icon;
  160. }
  161. /**
  162. * @param mixed $icon
  163. */
  164. public function setIcon($icon)
  165. {
  166. $this->icon = $icon;
  167. }
  168. /**
  169. * Returns the current API version
  170. *
  171. * @since 1.0
  172. * @access public
  173. * @return float
  174. */
  175. public function apiVersion()
  176. {
  177. return 3.2;
  178. }
  179. /**
  180. * Returns element type
  181. *
  182. * @since 1.0
  183. * @access public
  184. * @return string
  185. */
  186. public function getType()
  187. {
  188. return $this->_type;
  189. }
  190. /**
  191. * Sets the input field name
  192. *
  193. * @param string $name Input field name attribute
  194. * @since 1.0
  195. * @access public
  196. * @return void
  197. */
  198. public function setName($name)
  199. {
  200. }
  201. /**
  202. * Returns the element name
  203. *
  204. * @since 1.0
  205. * @access public
  206. * @return string
  207. */
  208. public function getName()
  209. {
  210. }
  211. /**
  212. * Sets the value of the form element
  213. *
  214. * @param string $value Default value of the form element
  215. * @since 1.0
  216. * @access public
  217. * @return void
  218. */
  219. public function setValue($value)
  220. {
  221. }
  222. /**
  223. * Returns the value of the form element
  224. *
  225. * @since 1.0
  226. * @access public
  227. * @return mixed
  228. */
  229. public function getValue()
  230. {
  231. return null;
  232. }
  233. /**
  234. * @return string
  235. */
  236. public function getCleanValue()
  237. {
  238. $value = $this->cleanValueFromParameter($this->getValue());
  239. return $value;
  240. }
  241. /**
  242. * @param string $value
  243. *
  244. * @return string
  245. */
  246. public function cleanValueFromParameter($value)
  247. {
  248. $value = @htmlspecialchars($value, ENT_COMPAT, HTML_Common::charset());
  249. return $value;
  250. }
  251. /**
  252. * Freeze the element so that only its value is returned
  253. *
  254. * @access public
  255. * @return void
  256. */
  257. public function freeze()
  258. {
  259. $this->_flagFrozen = true;
  260. }
  261. /**
  262. * Unfreezes the element so that it becomes editable
  263. *
  264. * @access public
  265. * @return void
  266. * @since 3.2.4
  267. */
  268. public function unfreeze()
  269. {
  270. $this->_flagFrozen = false;
  271. }
  272. /**
  273. * Returns the value of field without HTML tags
  274. *
  275. * @since 1.0
  276. * @access public
  277. * @return string
  278. */
  279. public function getFrozenHtml()
  280. {
  281. $value = $this->getValue();
  282. // Modified by Ivan Tcholakov, 16-MAR-2010.
  283. //return ('' != $value? htmlspecialchars($value): '&nbsp;') .
  284. // $this->_getPersistantData();
  285. if (!empty($value)) {
  286. $value = $this->getCleanValue();
  287. } else {
  288. $value = '&nbsp;';
  289. }
  290. $value .= $this->_getPersistantData();
  291. return '<span class="freeze">'.$value.'</span>';
  292. }
  293. /**
  294. * Used by getFrozenHtml() to pass the element's value if _persistantFreeze is on
  295. *
  296. * @access private
  297. * @return string
  298. */
  299. function _getPersistantData()
  300. {
  301. if (!$this->_persistantFreeze) {
  302. return '';
  303. } else {
  304. $id = $this->getAttribute('id');
  305. return '<input' . $this->_getAttrString(array(
  306. 'type' => 'hidden',
  307. 'name' => $this->getName(),
  308. 'value' => $this->getValue()
  309. ) + (isset($id)? array('id' => $id): array())) . ' />';
  310. }
  311. }
  312. /**
  313. * Returns whether or not the element is frozen
  314. *
  315. * @since 1.3
  316. * @access public
  317. * @return bool
  318. */
  319. public function isFrozen()
  320. {
  321. return $this->_flagFrozen;
  322. }
  323. /**
  324. * Sets wether an element value should be kept in an hidden field
  325. * when the element is frozen or not
  326. *
  327. * @param bool $persistant True if persistant value
  328. * @since 2.0
  329. * @access public
  330. * @return void
  331. */
  332. function setPersistantFreeze($persistant=false)
  333. {
  334. $this->_persistantFreeze = $persistant;
  335. }
  336. /**
  337. * Sets display text for the element
  338. *
  339. * @param string $label Display text for the element
  340. * @param string $label_for Optionally add a "for" attribute
  341. * @since 1.3
  342. * @access public
  343. * @return void
  344. */
  345. public function setLabel($label, $labelFor = null)
  346. {
  347. $this->_label = $label;
  348. if (!empty($labelFor)) {
  349. $this->_label_for = $labelFor;
  350. }
  351. }
  352. /**
  353. * Returns display text for the element
  354. *
  355. * @since 1.3
  356. * @access public
  357. * @return string
  358. */
  359. public function getLabel()
  360. {
  361. return $this->_label;
  362. }
  363. /**
  364. * Returns "for" attribute for the element
  365. *
  366. * @access public
  367. * @return string
  368. */
  369. function getLabelFor()
  370. {
  371. return $this->_label_for;
  372. }
  373. /**
  374. * Tries to find the element value from the values array
  375. *
  376. * @since 2.7
  377. * @access private
  378. * @return mixed
  379. */
  380. function _findValue(&$values)
  381. {
  382. if (empty($values)) {
  383. return null;
  384. }
  385. $elementName = $this->getName();
  386. if (isset($values[$elementName])) {
  387. return $values[$elementName];
  388. } elseif (strpos($elementName, '[')) {
  389. // Fix checkbox
  390. if ($this->_type === 'checkbox') {
  391. $attributeValue = $this->getAttribute('value');
  392. $elementNameCheckBox = str_replace('[]', '', $elementName);
  393. if (isset($values[$elementNameCheckBox]) &&
  394. is_array($values[$elementNameCheckBox])
  395. ) {
  396. if (in_array($attributeValue, $values[$elementNameCheckBox])) {
  397. return true;
  398. }
  399. return false;
  400. }
  401. }
  402. $replacedName = str_replace(
  403. array('\\', '\'', ']', '['),
  404. array('\\\\', '\\\'', '', "']['"),
  405. $elementName
  406. );
  407. $myVar = "['$replacedName']";
  408. return eval("return (isset(\$values$myVar)) ? \$values$myVar : null;");
  409. } else {
  410. return null;
  411. }
  412. }
  413. /**
  414. * Called by HTML_QuickForm whenever form event is made on this element
  415. *
  416. * @param string $event Name of event
  417. * @param mixed $arg event arguments
  418. * @param object &$caller calling object
  419. * @since 1.0
  420. * @access public
  421. * @return void
  422. */
  423. public function onQuickFormEvent($event, $arg, &$caller)
  424. {
  425. switch ($event) {
  426. case 'createElement':
  427. //$className = get_class($this);
  428. //$this->$className($arg[0], $arg[1], $arg[2], $arg[3], $arg[4], $arg[5], $arg[6]);
  429. break;
  430. case 'addElement':
  431. $this->onQuickFormEvent('createElement', $arg, $caller);
  432. $this->onQuickFormEvent('updateValue', null, $caller);
  433. break;
  434. case 'updateValue':
  435. // constant values override both default and submitted ones
  436. // default values are overriden by submitted
  437. $value = $this->_findValue($caller->_constantValues);
  438. if (null === $value) {
  439. $value = $this->_findValue($caller->_submitValues);
  440. if (null === $value) {
  441. $value = $this->_findValue($caller->_defaultValues);
  442. }
  443. }
  444. if (null !== $value) {
  445. $this->setValue($value);
  446. }
  447. break;
  448. case 'setGroupValue':
  449. $this->setValue($arg);
  450. }
  451. return true;
  452. }
  453. /**
  454. * Accepts a renderer
  455. *
  456. * @param HTML_QuickForm_Renderer renderer object
  457. * @param bool Whether an element is required
  458. * @param string An error message associated with an element
  459. * @access public
  460. * @return void
  461. */
  462. public function accept(&$renderer, $required=false, $error=null)
  463. {
  464. $renderer->renderElement($this, $required, $error);
  465. }
  466. /**
  467. * Automatically generates and assigns an 'id' attribute for the element.
  468. *
  469. * Currently used to ensure that labels work on radio buttons and
  470. * checkboxes. Per idea of Alexander Radivanovich.
  471. *
  472. * @access private
  473. * @return void
  474. */
  475. public function _generateId()
  476. {
  477. static $idx = 1;
  478. if (!$this->getAttribute('id')) {
  479. $this->updateAttributes(array('id' => 'qf_' . substr(md5(microtime() . $idx++), 0, 6)));
  480. }
  481. }
  482. /**
  483. * Returns a 'safe' element's value
  484. *
  485. * @param array array of submitted values to search
  486. * @param bool whether to return the value as associative array
  487. * @access public
  488. * @return mixed
  489. */
  490. public function exportValue(&$submitValues, $assoc = false)
  491. {
  492. $value = $this->_findValue($submitValues);
  493. if (null === $value) {
  494. $value = $this->getValue();
  495. }
  496. return $this->_prepareValue($value, $assoc);
  497. }
  498. /**
  499. * Used by exportValue() to prepare the value for returning
  500. *
  501. * @param mixed the value found in exportValue()
  502. * @param bool whether to return the value as associative array
  503. * @access private
  504. * @return mixed
  505. */
  506. public function _prepareValue($value, $assoc)
  507. {
  508. if (null === $value) {
  509. return null;
  510. } elseif (!$assoc) {
  511. return $value;
  512. } else {
  513. $name = $this->getName();
  514. if (!strpos($name, '[')) {
  515. return array($name => $value);
  516. } else {
  517. $valueAry = array();
  518. $myIndex = "['" . str_replace(
  519. array('\\', '\'', ']', '['), array('\\\\', '\\\'', '', "']['"),
  520. $name
  521. ) . "']";
  522. eval("\$valueAry$myIndex = \$value;");
  523. return $valueAry;
  524. }
  525. }
  526. }
  527. /**
  528. * @param mixed $template
  529. * @return HTML_QuickForm_element
  530. */
  531. public function setTemplate($template)
  532. {
  533. $this->template = $template;
  534. return $this;
  535. }
  536. /**
  537. * @return string
  538. */
  539. public function getCustomFrozenTemplate()
  540. {
  541. return $this->customFrozenTemplate;
  542. }
  543. /**
  544. * @param string $customFrozenTemplate
  545. * @return HTML_QuickForm_element
  546. */
  547. public function setCustomFrozenTemplate($customFrozenTemplate)
  548. {
  549. $this->customFrozenTemplate = $customFrozenTemplate;
  550. return $this;
  551. }
  552. /**
  553. * @return null
  554. */
  555. public function getInputSize()
  556. {
  557. return $this->inputSize;
  558. }
  559. /**
  560. * @param null $inputSize
  561. */
  562. public function setInputSize($inputSize)
  563. {
  564. $this->inputSize = $inputSize;
  565. }
  566. /**
  567. * @return array
  568. */
  569. public function calculateSize()
  570. {
  571. $size = $this->getColumnsSize();
  572. if (empty($size)) {
  573. $sizeTemp = $this->getInputSize();
  574. if (empty($size)) {
  575. $sizeTemp = 8;
  576. }
  577. $size = array(2, $sizeTemp, 2);
  578. } else {
  579. if (is_array($size)) {
  580. if (count($size) != 3) {
  581. $sizeTemp = $this->getInputSize();
  582. if (empty($size)) {
  583. $sizeTemp = 8;
  584. }
  585. $size = array(2, $sizeTemp, 2);
  586. }
  587. } else {
  588. // else just keep the $size array as received
  589. $size = array(2, (int) $size, 2);
  590. }
  591. }
  592. return $size;
  593. }
  594. }