2
0

FormElement.php 6.3 KB

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