FormElement.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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-2008 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-2008 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. * Converts parameter arguments to an element info array.
  38. *
  39. * E.g, formExample($name, $value, $attribs, $options, $listsep) is
  40. * the same thing as formExample(array('name' => ...)).
  41. *
  42. * Note that you cannot pass a 'disable' param; you need to pass
  43. * it as an 'attribs' key.
  44. *
  45. * @access protected
  46. *
  47. * @return array An element info array with keys for name, value,
  48. * attribs, options, listsep, disable, and escape.
  49. */
  50. protected function _getInfo($name, $value = null, $attribs = null,
  51. $options = null, $listsep = null
  52. ) {
  53. // the baseline info. note that $name serves a dual purpose;
  54. // if an array, it's an element info array that will override
  55. // these baseline values. as such, ignore it for the 'name'
  56. // if it's an array.
  57. $info = array(
  58. 'name' => is_array($name) ? '' : $name,
  59. 'id' => is_array($name) ? '' : $name,
  60. 'value' => $value,
  61. 'attribs' => $attribs,
  62. 'options' => $options,
  63. 'listsep' => $listsep,
  64. 'disable' => false,
  65. 'escape' => true,
  66. );
  67. // override with named args
  68. if (is_array($name)) {
  69. // only set keys that are already in info
  70. foreach ($info as $key => $val) {
  71. if (isset($name[$key])) {
  72. $info[$key] = $name[$key];
  73. }
  74. }
  75. }
  76. // force attribs to an array, per note from Orjan Persson.
  77. settype($info['attribs'], 'array');
  78. // Normalize readonly tag
  79. if (isset($info['attribs']['readonly'])
  80. && $info['attribs']['readonly'] != 'readonly')
  81. {
  82. $info['attribs']['readonly'] = 'readonly';
  83. }
  84. // Disable attribute
  85. if (isset($info['attribs']['disable'])
  86. && is_scalar($info['attribs']['disable']))
  87. {
  88. // disable the element
  89. $info['disable'] = (bool)$info['attribs']['disable'];
  90. unset($info['attribs']['disable']);
  91. } elseif (isset($info['attribs']['disable'])
  92. && is_array($info['attribs']['disable']))
  93. {
  94. $info['disable'] = $info['attribs']['disable'];
  95. unset($info['attribs']['disable']);
  96. }
  97. // Set ID for element
  98. if (isset($info['attribs']['id'])) {
  99. $info['id'] = (string) $info['attribs']['id'];
  100. } elseif (!isset($info['attribs']['id']) && !empty($info['name'])) {
  101. $id = $info['name'];
  102. if (substr($id, -2) == '[]') {
  103. $id = substr($id, 0, strlen($id) - 2);
  104. }
  105. if (strstr($id, ']')) {
  106. $id = trim($id, ']');
  107. $id = str_replace('][', '-', $id);
  108. $id = str_replace('[', '-', $id);
  109. }
  110. $info['id'] = $id;
  111. }
  112. // Determine escaping from attributes
  113. if (isset($info['attribs']['escape'])) {
  114. $info['escape'] = (bool) $info['attribs']['escape'];
  115. }
  116. // Determine listsetp from attributes
  117. if (isset($info['attribs']['listsep'])) {
  118. $info['listsep'] = (string) $info['attribs']['listsep'];
  119. }
  120. // Remove attribs that might overwrite the other keys. We do this LAST
  121. // because we needed the other attribs values earlier.
  122. foreach ($info as $key => $val) {
  123. if (isset($info['attribs'][$key])) {
  124. unset($info['attribs'][$key]);
  125. }
  126. }
  127. // done!
  128. return $info;
  129. }
  130. /**
  131. * Creates a hidden element.
  132. *
  133. * We have this as a common method because other elements often
  134. * need hidden elements for their operation.
  135. *
  136. * @access protected
  137. *
  138. * @param $name The element name.
  139. *
  140. * @param $value The element value.
  141. *
  142. * @param $attribs Attributes for the element.
  143. *
  144. * @return string A hidden element.
  145. */
  146. protected function _hidden($name, $value = null, $attribs = null)
  147. {
  148. return '<input type="hidden"'
  149. . ' name="' . $this->view->escape($name) . '"'
  150. . ' value="' . $this->view->escape($value) . '"'
  151. . $this->_htmlAttribs($attribs) . $this->getClosingBracket();
  152. }
  153. }