/View/Helper/FormElement.php

https://github.com/massiveart/ZF-ZOOLU · PHP · 204 lines · 97 code · 15 blank · 92 comment · 18 complexity · bb5975e448d2ab4cb33329e3f501c6f8 MD5 · raw file

  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: FormElement.php 20096 2010-01-06 02:05:09Z bkarwin $
  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. // force attribs to an array, per note from Orjan Persson.
  113. settype($info['attribs'], 'array');
  114. // Normalize readonly tag
  115. if (isset($info['attribs']['readonly'])
  116. && $info['attribs']['readonly'] != 'readonly')
  117. {
  118. $info['attribs']['readonly'] = 'readonly';
  119. }
  120. // Disable attribute
  121. if (isset($info['attribs']['disable'])
  122. && is_scalar($info['attribs']['disable']))
  123. {
  124. // disable the element
  125. $info['disable'] = (bool)$info['attribs']['disable'];
  126. unset($info['attribs']['disable']);
  127. } elseif (isset($info['attribs']['disable'])
  128. && is_array($info['attribs']['disable']))
  129. {
  130. $info['disable'] = $info['attribs']['disable'];
  131. unset($info['attribs']['disable']);
  132. }
  133. // Set ID for element
  134. if (isset($info['attribs']['id'])) {
  135. $info['id'] = (string) $info['attribs']['id'];
  136. } elseif (!isset($info['attribs']['id']) && !empty($info['name'])) {
  137. $id = $info['name'];
  138. if (substr($id, -2) == '[]') {
  139. $id = substr($id, 0, strlen($id) - 2);
  140. }
  141. if (strstr($id, ']')) {
  142. $id = trim($id, ']');
  143. $id = str_replace('][', '-', $id);
  144. $id = str_replace('[', '-', $id);
  145. }
  146. $info['id'] = $id;
  147. }
  148. // Determine escaping from attributes
  149. if (isset($info['attribs']['escape'])) {
  150. $info['escape'] = (bool) $info['attribs']['escape'];
  151. }
  152. // Determine listsetp from attributes
  153. if (isset($info['attribs']['listsep'])) {
  154. $info['listsep'] = (string) $info['attribs']['listsep'];
  155. }
  156. // Remove attribs that might overwrite the other keys. We do this LAST
  157. // because we needed the other attribs values earlier.
  158. foreach ($info as $key => $val) {
  159. if (isset($info['attribs'][$key])) {
  160. unset($info['attribs'][$key]);
  161. }
  162. }
  163. // done!
  164. return $info;
  165. }
  166. /**
  167. * Creates a hidden element.
  168. *
  169. * We have this as a common method because other elements often
  170. * need hidden elements for their operation.
  171. *
  172. * @access protected
  173. *
  174. * @param $name The element name.
  175. *
  176. * @param $value The element value.
  177. *
  178. * @param $attribs Attributes for the element.
  179. *
  180. * @return string A hidden element.
  181. */
  182. protected function _hidden($name, $value = null, $attribs = null)
  183. {
  184. return '<input type="hidden"'
  185. . ' name="' . $this->view->escape($name) . '"'
  186. . ' value="' . $this->view->escape($value) . '"'
  187. . $this->_htmlAttribs($attribs) . $this->getClosingBracket();
  188. }
  189. }