element.php 15 KB

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