element.php 13 KB

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