/library/Zend/Form/Decorator/ViewHelper.php

https://github.com/Exercise/zf2 · PHP · 261 lines · 135 code · 30 blank · 96 comment · 25 complexity · 42cf9ccb52b945b68bfa40adffb9e867 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_Form
  17. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. */
  20. /**
  21. * @namespace
  22. */
  23. namespace Zend\Form\Decorator;
  24. use Zend\Form\Element;
  25. /**
  26. * Zend_Form_Decorator_ViewHelper
  27. *
  28. * Decorate an element by using a view helper to render it.
  29. *
  30. * Accepts the following options:
  31. * - separator: string with which to separate passed in content and generated content
  32. * - placement: whether to append or prepend the generated content to the passed in content
  33. * - helper: the name of the view helper to use
  34. *
  35. * Assumes the view helper accepts three parameters, the name, value, and
  36. * optional attributes; these will be provided by the element.
  37. *
  38. * @uses \Zend\Form\Decorator\AbstractDecorator
  39. * @uses \Zend\Form\Decorator\Exception
  40. * @category Zend
  41. * @package Zend_Form
  42. * @subpackage Decorator
  43. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  44. * @license http://framework.zend.com/license/new-bsd New BSD License
  45. * @version $Id$
  46. */
  47. class ViewHelper extends AbstractDecorator
  48. {
  49. /**
  50. * Element types that represent buttons
  51. * @var array
  52. */
  53. protected $_buttonTypes = array(
  54. 'Zend\Form\Element\Button',
  55. 'Zend\Form\Element\Reset',
  56. 'Zend\Form\Element\Submit',
  57. );
  58. /**
  59. * View helper to use when rendering
  60. * @var string
  61. */
  62. protected $_helper;
  63. /**
  64. * Set view helper to use when rendering
  65. *
  66. * @param string $helper
  67. * @return Zend_Form_Decorator_Element_ViewHelper
  68. */
  69. public function setHelper($helper)
  70. {
  71. $this->_helper = (string) $helper;
  72. return $this;
  73. }
  74. /**
  75. * Retrieve view helper for rendering element
  76. *
  77. * @return string
  78. */
  79. public function getHelper()
  80. {
  81. if (null === $this->_helper) {
  82. $options = $this->getOptions();
  83. if (isset($options['helper'])) {
  84. $this->setHelper($options['helper']);
  85. $this->removeOption('helper');
  86. } else {
  87. $element = $this->getElement();
  88. if (null !== $element) {
  89. if (null !== ($helper = $element->getAttrib('helper'))) {
  90. $this->setHelper($helper);
  91. } else {
  92. $type = $element->getType();
  93. if ($pos = strrpos($type, '\\')) {
  94. $type = substr($type, $pos + 1);
  95. }
  96. $this->setHelper('form' . ucfirst($type));
  97. }
  98. }
  99. }
  100. }
  101. return $this->_helper;
  102. }
  103. /**
  104. * Get name
  105. *
  106. * If element is a Zend_Form_Element, will attempt to namespace it if the
  107. * element belongs to an array.
  108. *
  109. * @return string
  110. */
  111. public function getName()
  112. {
  113. if (null === ($element = $this->getElement())) {
  114. return '';
  115. }
  116. $name = $element->getName();
  117. if (!$element instanceof Element) {
  118. return $name;
  119. }
  120. if (null !== ($belongsTo = $element->getBelongsTo())) {
  121. $name = $belongsTo . '['
  122. . $name
  123. . ']';
  124. }
  125. if ($element->isArray()) {
  126. $name .= '[]';
  127. }
  128. return $name;
  129. }
  130. /**
  131. * Retrieve element attributes
  132. *
  133. * Set id to element name and/or array item.
  134. *
  135. * @return array
  136. */
  137. public function getElementAttribs()
  138. {
  139. if (null === ($element = $this->getElement())) {
  140. return null;
  141. }
  142. $attribs = $element->getAttribs();
  143. if (isset($attribs['helper'])) {
  144. unset($attribs['helper']);
  145. }
  146. if (method_exists($element, 'getSeparator')) {
  147. if (null !== ($listsep = $element->getSeparator())) {
  148. $attribs['listsep'] = $listsep;
  149. }
  150. }
  151. if (isset($attribs['id'])) {
  152. return $attribs;
  153. }
  154. $id = $element->getName();
  155. if ($element instanceof Element) {
  156. if (null !== ($belongsTo = $element->getBelongsTo())) {
  157. $belongsTo = preg_replace('/\[([^\]]+)\]/', '-$1', $belongsTo);
  158. $id = $belongsTo . '-' . $id;
  159. }
  160. }
  161. $element->setAttrib('id', $id);
  162. $attribs['id'] = $id;
  163. return $attribs;
  164. }
  165. /**
  166. * Get value
  167. *
  168. * If element type is one of the button types, returns the label.
  169. *
  170. * @param \Zend\Form\Element $element
  171. * @return string|null
  172. */
  173. public function getValue($element)
  174. {
  175. if (!$element instanceof Element) {
  176. return null;
  177. }
  178. foreach ($this->_buttonTypes as $type) {
  179. if ($element instanceof $type) {
  180. if (stristr($type, 'button')) {
  181. $element->content = $element->getLabel();
  182. return null;
  183. }
  184. return $element->getLabel();
  185. }
  186. }
  187. return $element->getValue();
  188. }
  189. /**
  190. * Render an element using a view helper
  191. *
  192. * Determine view helper from 'viewHelper' option, or, if none set, from
  193. * the element type. Then call as
  194. * helper($element->getName(), $element->getValue(), $element->getAttribs())
  195. *
  196. * @param string $content
  197. * @return string
  198. * @throws \Zend\Form\Decorator\Exception if element or view are not registered
  199. */
  200. public function render($content)
  201. {
  202. $element = $this->getElement();
  203. $view = $element->getView();
  204. if (null === $view) {
  205. throw new Exception('ViewHelper decorator cannot render without a registered view object');
  206. }
  207. if (method_exists($element, 'getMultiOptions')) {
  208. $element->getMultiOptions();
  209. }
  210. $helper = $this->getHelper();
  211. $separator = $this->getSeparator();
  212. $value = $this->getValue($element);
  213. $attribs = $this->getElementAttribs();
  214. $name = $element->getFullyQualifiedName();
  215. $id = $element->getId();
  216. $attribs['id'] = $id;
  217. $helperObject = $view->getHelper($helper);
  218. if (method_exists($helperObject, 'setTranslator')) {
  219. $helperObject->setTranslator($element->getTranslator());
  220. }
  221. $elementContent = $view->$helper($name, $value, $attribs, $element->options);
  222. switch ($this->getPlacement()) {
  223. case self::APPEND:
  224. return $content . $separator . $elementContent;
  225. case self::PREPEND:
  226. return $elementContent . $separator . $content;
  227. default:
  228. return $elementContent;
  229. }
  230. }
  231. }