FormElement.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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-2010 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-2010 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/View/Exception.php';
  66. $e = new Zend_View_Exception('Invalid translator specified');
  67. $e->setView($this->view);
  68. throw $e;
  69. }
  70. return $this;
  71. }
  72. /**
  73. * Converts parameter arguments to an element info array.
  74. *
  75. * E.g, formExample($name, $value, $attribs, $options, $listsep) is
  76. * the same thing as formExample(array('name' => ...)).
  77. *
  78. * Note that you cannot pass a 'disable' param; you need to pass
  79. * it as an 'attribs' key.
  80. *
  81. * @access protected
  82. *
  83. * @return array An element info array with keys for name, value,
  84. * attribs, options, listsep, disable, and escape.
  85. */
  86. protected function _getInfo($name, $value = null, $attribs = null,
  87. $options = null, $listsep = null
  88. ) {
  89. // the baseline info. note that $name serves a dual purpose;
  90. // if an array, it's an element info array that will override
  91. // these baseline values. as such, ignore it for the 'name'
  92. // if it's an array.
  93. $info = array(
  94. 'name' => is_array($name) ? '' : $name,
  95. 'id' => is_array($name) ? '' : $name,
  96. 'value' => $value,
  97. 'attribs' => $attribs,
  98. 'options' => $options,
  99. 'listsep' => $listsep,
  100. 'disable' => false,
  101. 'escape' => true,
  102. );
  103. // override with named args
  104. if (is_array($name)) {
  105. // only set keys that are already in info
  106. foreach ($info as $key => $val) {
  107. if (isset($name[$key])) {
  108. $info[$key] = $name[$key];
  109. }
  110. }
  111. }
  112. $attribs = (array)$attribs;
  113. // Normalize readonly tag
  114. if (array_key_exists('readonly', $attribs)) {
  115. $attribs['readonly'] = 'readonly';
  116. }
  117. // Disable attribute
  118. if (array_key_exists('disable', $attribs)) {
  119. if (is_scalar($attribs['disable'])) {
  120. // disable the element
  121. $info['disable'] = (bool)$attribs['disable'];
  122. } else if (is_array($attribs['disable'])) {
  123. $info['disable'] = $attribs['disable'];
  124. }
  125. }
  126. // Set ID for element
  127. if (array_key_exists('id', $attribs)) {
  128. $info['id'] = (string)$attribs['id'];
  129. } else if ('' !== $info['name']) {
  130. $info['id'] = trim(strtr($info['name'],
  131. array('[' => '-', ']' => '')), '-');
  132. }
  133. // Determine escaping from attributes
  134. if (array_key_exists('escape', $attribs)) {
  135. $info['escape'] = (bool)$attribs['escape'];
  136. }
  137. // Determine listsetp from attributes
  138. if (array_key_exists('listsep', $attribs)) {
  139. $info['listsep'] = (string)$attribs['listsep'];
  140. }
  141. // Remove attribs that might overwrite the other keys. We do this LAST
  142. // because we needed the other attribs values earlier.
  143. foreach ($info as $key => $val) {
  144. if (array_key_exists($key, $attribs)) {
  145. unset($attribs[$key]);
  146. }
  147. }
  148. $info['attribs'] = $attribs;
  149. // done!
  150. return $info;
  151. }
  152. /**
  153. * Creates a hidden element.
  154. *
  155. * We have this as a common method because other elements often
  156. * need hidden elements for their operation.
  157. *
  158. * @access protected
  159. *
  160. * @param $name The element name.
  161. *
  162. * @param $value The element value.
  163. *
  164. * @param $attribs Attributes for the element.
  165. *
  166. * @return string A hidden element.
  167. */
  168. protected function _hidden($name, $value = null, $attribs = null)
  169. {
  170. return '<input type="hidden"'
  171. . ' name="' . $this->view->escape($name) . '"'
  172. . ' value="' . $this->view->escape($value) . '"'
  173. . $this->_htmlAttribs($attribs) . $this->getClosingBracket();
  174. }
  175. }