PageRenderTime 50ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/blackriver/openx
PHP | 494 lines | 170 code | 51 blank | 273 comment | 17 complexity | c1d14dbe2bc45d32e70271de55573bd2 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-2007 The PHP Group
  20. * @license http://www.php.net/license/3_01.txt PHP License 3.01
  21. * @version CVS: $Id: element.php 22781 2008-07-16 16:52:20Z chris.nutting@openx.org $
  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.10
  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. return (strlen($value)? htmlspecialchars($value): '&nbsp;') .
  212. $this->_getPersistantData();
  213. } //end func getFrozenHtml
  214. // }}}
  215. // {{{ _getPersistantData()
  216. /**
  217. * Used by getFrozenHtml() to pass the element's value if _persistantFreeze is on
  218. *
  219. * @access private
  220. * @return string
  221. */
  222. function _getPersistantData()
  223. {
  224. if (!$this->_persistantFreeze) {
  225. return '';
  226. } else {
  227. $id = $this->getAttribute('id');
  228. return '<input' . $this->_getAttrString(array(
  229. 'type' => 'hidden',
  230. 'name' => $this->getName(),
  231. 'value' => $this->getValue()
  232. ) + (isset($id)? array('id' => $id): array())) . ' />';
  233. }
  234. }
  235. // }}}
  236. // {{{ isFrozen()
  237. /**
  238. * Returns whether or not the element is frozen
  239. *
  240. * @since 1.3
  241. * @access public
  242. * @return bool
  243. */
  244. function isFrozen()
  245. {
  246. return $this->_flagFrozen;
  247. } // end func isFrozen
  248. // }}}
  249. // {{{ setPersistantFreeze()
  250. /**
  251. * Sets wether an element value should be kept in an hidden field
  252. * when the element is frozen or not
  253. *
  254. * @param bool $persistant True if persistant value
  255. * @since 2.0
  256. * @access public
  257. * @return void
  258. */
  259. function setPersistantFreeze($persistant=false)
  260. {
  261. $this->_persistantFreeze = $persistant;
  262. } //end func setPersistantFreeze
  263. // }}}
  264. // {{{ setLabel()
  265. /**
  266. * Sets display text for the element
  267. *
  268. * @param string $label Display text for the element
  269. * @since 1.3
  270. * @access public
  271. * @return void
  272. */
  273. function setLabel($label)
  274. {
  275. $this->_label = $label;
  276. } //end func setLabel
  277. // }}}
  278. // {{{ getLabel()
  279. /**
  280. * Returns display text for the element
  281. *
  282. * @since 1.3
  283. * @access public
  284. * @return string
  285. */
  286. function getLabel()
  287. {
  288. return $this->_label;
  289. } //end func getLabel
  290. // }}}
  291. // {{{ _findValue()
  292. /**
  293. * Tries to find the element value from the values array
  294. *
  295. * @since 2.7
  296. * @access private
  297. * @return mixed
  298. */
  299. function _findValue(&$values)
  300. {
  301. if (empty($values)) {
  302. return null;
  303. }
  304. $elementName = $this->getName();
  305. if (isset($values[$elementName])) {
  306. return $values[$elementName];
  307. } elseif (strpos($elementName, '[')) {
  308. $myVar = "['" . str_replace(
  309. array('\\', '\'', ']', '['), array('\\\\', '\\\'', '', "']['"),
  310. $elementName
  311. ) . "']";
  312. return eval("return (isset(\$values$myVar)) ? \$values$myVar : null;");
  313. } else {
  314. return null;
  315. }
  316. } //end func _findValue
  317. // }}}
  318. // {{{ onQuickFormEvent()
  319. /**
  320. * Called by HTML_QuickForm whenever form event is made on this element
  321. *
  322. * @param string $event Name of event
  323. * @param mixed $arg event arguments
  324. * @param object &$caller calling object
  325. * @since 1.0
  326. * @access public
  327. * @return void
  328. */
  329. function onQuickFormEvent($event, $arg, &$caller)
  330. {
  331. switch ($event) {
  332. case 'createElement':
  333. $className = get_class($this);
  334. $this->$className($arg[0], $arg[1], $arg[2], $arg[3], $arg[4]);
  335. break;
  336. case 'addElement':
  337. $this->onQuickFormEvent('createElement', $arg, $caller);
  338. $this->onQuickFormEvent('updateValue', null, $caller);
  339. break;
  340. case 'updateValue':
  341. // constant values override both default and submitted ones
  342. // default values are overriden by submitted
  343. $value = $this->_findValue($caller->_constantValues);
  344. if (null === $value) {
  345. $value = $this->_findValue($caller->_submitValues);
  346. if (null === $value) {
  347. $value = $this->_findValue($caller->_defaultValues);
  348. }
  349. }
  350. if (null !== $value) {
  351. $this->setValue($value);
  352. }
  353. break;
  354. case 'setGroupValue':
  355. $this->setValue($arg);
  356. }
  357. return true;
  358. } // end func onQuickFormEvent
  359. // }}}
  360. // {{{ accept()
  361. /**
  362. * Accepts a renderer
  363. *
  364. * @param HTML_QuickForm_Renderer renderer object
  365. * @param bool Whether an element is required
  366. * @param string An error message associated with an element
  367. * @access public
  368. * @return void
  369. */
  370. function accept(&$renderer, $required=false, $error=null)
  371. {
  372. $renderer->renderElement($this, $required, $error);
  373. } // end func accept
  374. // }}}
  375. // {{{ _generateId()
  376. /**
  377. * Automatically generates and assigns an 'id' attribute for the element.
  378. *
  379. * Currently used to ensure that labels work on radio buttons and
  380. * checkboxes. Per idea of Alexander Radivanovich.
  381. *
  382. * @access private
  383. * @return void
  384. */
  385. function _generateId()
  386. {
  387. static $idx = 1;
  388. if (!$this->getAttribute('id')) {
  389. $this->updateAttributes(array('id' => 'qf_' . substr(md5(microtime() . $idx++), 0, 6)));
  390. }
  391. } // end func _generateId
  392. // }}}
  393. // {{{ exportValue()
  394. /**
  395. * Returns a 'safe' element's value
  396. *
  397. * @param array array of submitted values to search
  398. * @param bool whether to return the value as associative array
  399. * @access public
  400. * @return mixed
  401. */
  402. function exportValue(&$submitValues, $assoc = false)
  403. {
  404. $value = $this->_findValue($submitValues);
  405. if (null === $value) {
  406. $value = $this->getValue();
  407. }
  408. return $this->_prepareValue($value, $assoc);
  409. }
  410. // }}}
  411. // {{{ _prepareValue()
  412. /**
  413. * Used by exportValue() to prepare the value for returning
  414. *
  415. * @param mixed the value found in exportValue()
  416. * @param bool whether to return the value as associative array
  417. * @access private
  418. * @return mixed
  419. */
  420. function _prepareValue($value, $assoc)
  421. {
  422. if (null === $value) {
  423. return null;
  424. } elseif (!$assoc) {
  425. return $value;
  426. } else {
  427. $name = $this->getName();
  428. if (!strpos($name, '[')) {
  429. return array($name => $value);
  430. } else {
  431. $valueAry = array();
  432. $myIndex = "['" . str_replace(
  433. array('\\', '\'', ']', '['), array('\\\\', '\\\'', '', "']['"),
  434. $name
  435. ) . "']";
  436. eval("\$valueAry$myIndex = \$value;");
  437. return $valueAry;
  438. }
  439. }
  440. }
  441. // }}}
  442. } // end class HTML_QuickForm_element
  443. ?>