PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/main/inc/lib/pear/HTML/QuickForm/element.php

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