PageRenderTime 25ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/View/Helper/FormElement.php

https://gitlab.com/devtoannh/cafe
PHP | 204 lines | 93 code | 18 blank | 93 comment | 21 complexity | 973d46579e7e867a39322a6b20c17980 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-2011 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 24201 2011-07-05 16:22:04Z matthew $
  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-2011 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 Zend_Translate $translator
  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. // If all helper options are passed as an array, attribs may have
  112. // been as well
  113. if (null === $attribs) {
  114. $attribs = $info['attribs'];
  115. }
  116. }
  117. $attribs = (array)$attribs;
  118. // Normalize readonly tag
  119. if (array_key_exists('readonly', $attribs)) {
  120. $attribs['readonly'] = 'readonly';
  121. }
  122. // Disable attribute
  123. if (array_key_exists('disable', $attribs)) {
  124. if (is_scalar($attribs['disable'])) {
  125. // disable the element
  126. $info['disable'] = (bool)$attribs['disable'];
  127. } else if (is_array($attribs['disable'])) {
  128. $info['disable'] = $attribs['disable'];
  129. }
  130. }
  131. // Set ID for element
  132. if (array_key_exists('id', $attribs)) {
  133. $info['id'] = (string)$attribs['id'];
  134. } else if ('' !== $info['name']) {
  135. $info['id'] = trim(strtr($info['name'],
  136. array('[' => '-', ']' => '')), '-');
  137. }
  138. // Remove NULL name attribute override
  139. if (array_key_exists('name', $attribs) && is_null($attribs['name'])) {
  140. unset($attribs['name']);
  141. }
  142. // Override name in info if specified in attribs
  143. if (array_key_exists('name', $attribs) && $attribs['name'] != $info['name']) {
  144. $info['name'] = $attribs['name'];
  145. }
  146. // Determine escaping from attributes
  147. if (array_key_exists('escape', $attribs)) {
  148. $info['escape'] = (bool)$attribs['escape'];
  149. }
  150. // Determine listsetp from attributes
  151. if (array_key_exists('listsep', $attribs)) {
  152. $info['listsep'] = (string)$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 (array_key_exists($key, $attribs)) {
  158. unset($attribs[$key]);
  159. }
  160. }
  161. $info['attribs'] = $attribs;
  162. // done!
  163. return $info;
  164. }
  165. /**
  166. * Creates a hidden element.
  167. *
  168. * We have this as a common method because other elements often
  169. * need hidden elements for their operation.
  170. *
  171. * @access protected
  172. *
  173. * @param string $name The element name.
  174. * @param string $value The element value.
  175. * @param array $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. }