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

/protected/vendors/Zend/Form/Decorator/ViewHelper.php

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