PageRenderTime 51ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://github.com/zendframework/zf2
PHP | 261 lines | 194 code | 6 blank | 61 comment | 7 complexity | c99ef60225f546ea2694b83b9f5ea62f MD5 | raw file
Possible License(s): BSD-3-Clause
  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-2012 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-2012 Zend Technologies USA Inc. (http://www.zend.com)
  44. * @license http://framework.zend.com/license/new-bsd New BSD License
  45. */
  46. class ViewHelper extends AbstractDecorator
  47. {
  48. /**
  49. * Element types that represent buttons
  50. * @var array
  51. */
  52. protected $_buttonTypes = array(
  53. 'Zend\Form\Element\Button',
  54. 'Zend\Form\Element\Reset',
  55. 'Zend\Form\Element\Submit',
  56. );
  57. /**
  58. * View helper to use when rendering
  59. * @var string
  60. */
  61. protected $_helper;
  62. /**
  63. * Set view helper to use when rendering
  64. *
  65. * @param string $helper
  66. * @return Zend_Form_Decorator_Element_ViewHelper
  67. */
  68. public function setHelper($helper)
  69. {
  70. $this->_helper = (string) $helper;
  71. return $this;
  72. }
  73. /**
  74. * Retrieve view helper for rendering element
  75. *
  76. * @return string
  77. */
  78. public function getHelper()
  79. {
  80. if (null === $this->_helper) {
  81. $options = $this->getOptions();
  82. if (isset($options['helper'])) {
  83. $this->setHelper($options['helper']);
  84. $this->removeOption('helper');
  85. } else {
  86. $element = $this->getElement();
  87. if (null !== $element) {
  88. if (null !== ($helper = $element->getAttrib('helper'))) {
  89. $this->setHelper($helper);
  90. } else {
  91. $type = $element->getType();
  92. if ($pos = strrpos($type, '\\')) {
  93. $type = substr($type, $pos + 1);
  94. }
  95. $this->setHelper('form' . ucfirst($type));
  96. }
  97. }
  98. }
  99. }
  100. return $this->_helper;
  101. }
  102. /**
  103. * Get name
  104. *
  105. * If element is a Zend_Form_Element, will attempt to namespace it if the
  106. * element belongs to an array.
  107. *
  108. * @return string
  109. */
  110. public function getName()
  111. {
  112. if (null === ($element = $this->getElement())) {
  113. return '';
  114. }
  115. $name = $element->getName();
  116. if (!$element instanceof Element) {
  117. return $name;
  118. }
  119. if (null !== ($belongsTo = $element->getBelongsTo())) {
  120. $name = $belongsTo . '['
  121. . $name
  122. . ']';
  123. }
  124. if ($element->isArray()) {
  125. $name .= '[]';
  126. }
  127. return $name;
  128. }
  129. /**
  130. * Retrieve element attributes
  131. *
  132. * Set id to element name and/or array item.
  133. *
  134. * @return array
  135. */
  136. public function getElementAttribs()
  137. {
  138. if (null === ($element = $this->getElement())) {
  139. return null;
  140. }
  141. $attribs = $element->getAttribs();
  142. if (isset($attribs['helper'])) {
  143. unset($attribs['helper']);
  144. }
  145. if (method_exists($element, 'getSeparator')) {
  146. if (null !== ($listsep = $element->getSeparator())) {
  147. $attribs['listsep'] = $listsep;
  148. }
  149. }
  150. if (isset($attribs['id'])) {
  151. return $attribs;
  152. }
  153. $id = $element->getName();
  154. if ($element instanceof Element) {
  155. if (null !== ($belongsTo = $element->getBelongsTo())) {
  156. $belongsTo = preg_replace('/\[([^\]]+)\]/', '-$1', $belongsTo);
  157. $id = $belongsTo . '-' . $id;
  158. }
  159. }
  160. $element->setAttrib('id', $id);
  161. $attribs['id'] = $id;
  162. return $attribs;
  163. }
  164. /**
  165. * Get value
  166. *
  167. * If element type is one of the button types, returns the label.
  168. *
  169. * @param \Zend\Form\Element $element
  170. * @return string|null
  171. */
  172. public function getValue($element)
  173. {
  174. if (!$element instanceof Element) {
  175. return null;
  176. }
  177. foreach ($this->_buttonTypes as $type) {
  178. if ($element instanceof $type) {
  179. if (stristr($type, 'button')) {
  180. $element->content = $element->getLabel();
  181. return null;
  182. }
  183. return $element->getLabel();
  184. }
  185. }
  186. return $element->getValue();
  187. }
  188. /**
  189. * Render an element using a view helper
  190. *
  191. * Determine view helper from 'viewHelper' option, or, if none set, from
  192. * the element type. Then call as
  193. * helper($element->getName(), $element->getValue(), $element->getAttribs())
  194. *
  195. * @param string $content
  196. * @return string
  197. * @throws \Zend\Form\Decorator\Exception if element or view are not registered
  198. */
  199. public function render($content)
  200. {
  201. $element = $this->getElement();
  202. $view = $element->getView();
  203. if (null === $view) {
  204. throw new Exception\UnexpectedValueException('ViewHelper decorator cannot render without a registered view object');
  205. }
  206. if (method_exists($element, 'getMultiOptions')) {
  207. $element->getMultiOptions();
  208. }
  209. $helper = $this->getHelper();
  210. $separator = $this->getSeparator();
  211. $value = $this->getValue($element);
  212. $attribs = $this->getElementAttribs();
  213. $name = $element->getFullyQualifiedName();
  214. $id = $element->getId();
  215. $attribs['id'] = $id;
  216. $helperObject = $view->plugin($helper);
  217. if (method_exists($helperObject, 'setTranslator')) {
  218. $helperObject->setTranslator($element->getTranslator());
  219. }
  220. $helper = $view->plugin($helper);
  221. $elementContent = $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. }