2
0

FormElement.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_View
  17. * @subpackage Helper
  18. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /**
  22. * @see Zend_View_Helper_HtmlElement
  23. */
  24. require_once 'Zend/View/Helper/HtmlElement.php';
  25. /**
  26. * Base helper for form elements. Extend this, don't use it on its own.
  27. *
  28. * @category Zend
  29. * @package Zend_View
  30. * @subpackage Helper
  31. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  32. * @license http://framework.zend.com/license/new-bsd New BSD License
  33. */
  34. abstract class Zend_View_Helper_FormElement extends Zend_View_Helper_HtmlElement
  35. {
  36. /**
  37. * @var Zend_Translate
  38. */
  39. protected $_translator;
  40. /**
  41. * Get translator
  42. *
  43. * @return Zend_Translate
  44. */
  45. public function getTranslator()
  46. {
  47. return $this->_translator;
  48. }
  49. /**
  50. * Set translator
  51. *
  52. * @param $translator|null Zend_Translate
  53. * @return Zend_View_Helper_FormElement
  54. */
  55. public function setTranslator($translator = null)
  56. {
  57. if (null === $translator) {
  58. $this->_translator = null;
  59. } elseif ($translator instanceof Zend_Translate_Adapter) {
  60. $this->_translator = $translator;
  61. } elseif ($translator instanceof Zend_Translate) {
  62. $this->_translator = $translator->getAdapter();
  63. } else {
  64. require_once 'Zend/Form/Exception.php';
  65. throw new Zend_Form_Exception('Invalid translator specified');
  66. }
  67. return $this;
  68. }
  69. /**
  70. * Converts parameter arguments to an element info array.
  71. *
  72. * E.g, formExample($name, $value, $attribs, $options, $listsep) is
  73. * the same thing as formExample(array('name' => ...)).
  74. *
  75. * Note that you cannot pass a 'disable' param; you need to pass
  76. * it as an 'attribs' key.
  77. *
  78. * @access protected
  79. *
  80. * @return array An element info array with keys for name, value,
  81. * attribs, options, listsep, disable, and escape.
  82. */
  83. protected function _getInfo($name, $value = null, $attribs = null,
  84. $options = null, $listsep = null
  85. ) {
  86. // the baseline info. note that $name serves a dual purpose;
  87. // if an array, it's an element info array that will override
  88. // these baseline values. as such, ignore it for the 'name'
  89. // if it's an array.
  90. $info = array(
  91. 'name' => is_array($name) ? '' : $name,
  92. 'id' => is_array($name) ? '' : $name,
  93. 'value' => $value,
  94. 'attribs' => $attribs,
  95. 'options' => $options,
  96. 'listsep' => $listsep,
  97. 'disable' => false,
  98. 'escape' => true,
  99. );
  100. // override with named args
  101. if (is_array($name)) {
  102. // only set keys that are already in info
  103. foreach ($info as $key => $val) {
  104. if (isset($name[$key])) {
  105. $info[$key] = $name[$key];
  106. }
  107. }
  108. }
  109. // force attribs to an array, per note from Orjan Persson.
  110. settype($info['attribs'], 'array');
  111. // Normalize readonly tag
  112. if (isset($info['attribs']['readonly'])
  113. && $info['attribs']['readonly'] != 'readonly')
  114. {
  115. $info['attribs']['readonly'] = 'readonly';
  116. }
  117. // Disable attribute
  118. if (isset($info['attribs']['disable'])
  119. && is_scalar($info['attribs']['disable']))
  120. {
  121. // disable the element
  122. $info['disable'] = (bool)$info['attribs']['disable'];
  123. unset($info['attribs']['disable']);
  124. } elseif (isset($info['attribs']['disable'])
  125. && is_array($info['attribs']['disable']))
  126. {
  127. $info['disable'] = $info['attribs']['disable'];
  128. unset($info['attribs']['disable']);
  129. }
  130. // Set ID for element
  131. if (isset($info['attribs']['id'])) {
  132. $info['id'] = (string) $info['attribs']['id'];
  133. } elseif (!isset($info['attribs']['id']) && !empty($info['name'])) {
  134. $id = $info['name'];
  135. if (substr($id, -2) == '[]') {
  136. $id = substr($id, 0, strlen($id) - 2);
  137. }
  138. if (strstr($id, ']')) {
  139. $id = trim($id, ']');
  140. $id = str_replace('][', '-', $id);
  141. $id = str_replace('[', '-', $id);
  142. }
  143. $info['id'] = $id;
  144. }
  145. // Determine escaping from attributes
  146. if (isset($info['attribs']['escape'])) {
  147. $info['escape'] = (bool) $info['attribs']['escape'];
  148. }
  149. // Determine listsetp from attributes
  150. if (isset($info['attribs']['listsep'])) {
  151. $info['listsep'] = (string) $info['attribs']['listsep'];
  152. }
  153. // Remove attribs that might overwrite the other keys. We do this LAST
  154. // because we needed the other attribs values earlier.
  155. foreach ($info as $key => $val) {
  156. if (isset($info['attribs'][$key])) {
  157. unset($info['attribs'][$key]);
  158. }
  159. }
  160. // done!
  161. return $info;
  162. }
  163. /**
  164. * Creates a hidden element.
  165. *
  166. * We have this as a common method because other elements often
  167. * need hidden elements for their operation.
  168. *
  169. * @access protected
  170. *
  171. * @param $name The element name.
  172. *
  173. * @param $value The element value.
  174. *
  175. * @param $attribs Attributes for the element.
  176. *
  177. * @return string A hidden element.
  178. */
  179. protected function _hidden($name, $value = null, $attribs = null)
  180. {
  181. return '<input type="hidden"'
  182. . ' name="' . $this->view->escape($name) . '"'
  183. . ' value="' . $this->view->escape($value) . '"'
  184. . $this->_htmlAttribs($attribs) . $this->getClosingBracket();
  185. }
  186. }