File indexing completed on 2025-03-02 05:29:24
0001 <?php 0002 /** 0003 * Zend Framework 0004 * 0005 * LICENSE 0006 * 0007 * This source file is subject to the new BSD license that is bundled 0008 * with this package in the file LICENSE.txt. 0009 * It is also available through the world-wide-web at this URL: 0010 * http://framework.zend.com/license/new-bsd 0011 * If you did not receive a copy of the license and are unable to 0012 * obtain it through the world-wide-web, please send an email 0013 * to license@zend.com so we can send you a copy immediately. 0014 * 0015 * @category Zend 0016 * @package Zend_Form 0017 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0018 * @license http://framework.zend.com/license/new-bsd New BSD License 0019 */ 0020 0021 /** Zend_Form_Decorator_Abstract */ 0022 // require_once 'Zend/Form/Decorator/Abstract.php'; 0023 0024 /** 0025 * Zend_Form_Decorator_ViewHelper 0026 * 0027 * Decorate an element by using a view helper to render it. 0028 * 0029 * Accepts the following options: 0030 * - separator: string with which to separate passed in content and generated content 0031 * - placement: whether to append or prepend the generated content to the passed in content 0032 * - helper: the name of the view helper to use 0033 * 0034 * Assumes the view helper accepts three parameters, the name, value, and 0035 * optional attributes; these will be provided by the element. 0036 * 0037 * @category Zend 0038 * @package Zend_Form 0039 * @subpackage Decorator 0040 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0041 * @license http://framework.zend.com/license/new-bsd New BSD License 0042 * @version $Id$ 0043 */ 0044 class Zend_Form_Decorator_ViewHelper extends Zend_Form_Decorator_Abstract 0045 { 0046 /** 0047 * Element types that represent buttons 0048 * @var array 0049 */ 0050 protected $_buttonTypes = array( 0051 'Zend_Form_Element_Button', 0052 'Zend_Form_Element_Reset', 0053 'Zend_Form_Element_Submit', 0054 ); 0055 0056 /** 0057 * View helper to use when rendering 0058 * @var string 0059 */ 0060 protected $_helper; 0061 0062 /** 0063 * Set view helper to use when rendering 0064 * 0065 * @param string $helper 0066 * @return Zend_Form_Decorator_Element_ViewHelper 0067 */ 0068 public function setHelper($helper) 0069 { 0070 $this->_helper = (string) $helper; 0071 return $this; 0072 } 0073 0074 /** 0075 * Retrieve view helper for rendering element 0076 * 0077 * @return string 0078 */ 0079 public function getHelper() 0080 { 0081 if (null === $this->_helper) { 0082 $options = $this->getOptions(); 0083 if (isset($options['helper'])) { 0084 $this->setHelper($options['helper']); 0085 $this->removeOption('helper'); 0086 } else { 0087 $element = $this->getElement(); 0088 if (null !== $element) { 0089 if (null !== ($helper = $element->getAttrib('helper'))) { 0090 $this->setHelper($helper); 0091 } else { 0092 $type = $element->getType(); 0093 if ($pos = strrpos($type, '_')) { 0094 $type = substr($type, $pos + 1); 0095 } 0096 $this->setHelper('form' . ucfirst($type)); 0097 } 0098 } 0099 } 0100 } 0101 0102 return $this->_helper; 0103 } 0104 0105 /** 0106 * Get name 0107 * 0108 * If element is a Zend_Form_Element, will attempt to namespace it if the 0109 * element belongs to an array. 0110 * 0111 * @return string 0112 */ 0113 public function getName() 0114 { 0115 if (null === ($element = $this->getElement())) { 0116 return ''; 0117 } 0118 0119 $name = $element->getName(); 0120 0121 if (!$element instanceof Zend_Form_Element) { 0122 return $name; 0123 } 0124 0125 if (null !== ($belongsTo = $element->getBelongsTo())) { 0126 $name = $belongsTo . '[' 0127 . $name 0128 . ']'; 0129 } 0130 0131 if ($element->isArray()) { 0132 $name .= '[]'; 0133 } 0134 0135 return $name; 0136 } 0137 0138 /** 0139 * Retrieve element attributes 0140 * 0141 * Set id to element name and/or array item. 0142 * 0143 * @return array 0144 */ 0145 public function getElementAttribs() 0146 { 0147 if (null === ($element = $this->getElement())) { 0148 return null; 0149 } 0150 0151 $attribs = $element->getAttribs(); 0152 if (isset($attribs['helper'])) { 0153 unset($attribs['helper']); 0154 } 0155 0156 if (method_exists($element, 'getSeparator')) { 0157 if (null !== ($listsep = $element->getSeparator())) { 0158 $attribs['listsep'] = $listsep; 0159 } 0160 } 0161 0162 if (isset($attribs['id'])) { 0163 return $attribs; 0164 } 0165 0166 $id = $element->getName(); 0167 0168 if ($element instanceof Zend_Form_Element) { 0169 if (null !== ($belongsTo = $element->getBelongsTo())) { 0170 $belongsTo = preg_replace('/\[([^\]]+)\]/', '-$1', $belongsTo); 0171 $id = $belongsTo . '-' . $id; 0172 } 0173 } 0174 0175 $element->setAttrib('id', $id); 0176 $attribs['id'] = $id; 0177 0178 return $attribs; 0179 } 0180 0181 /** 0182 * Get value 0183 * 0184 * If element type is one of the button types, returns the label. 0185 * 0186 * @param Zend_Form_Element $element 0187 * @return string|null 0188 */ 0189 public function getValue($element) 0190 { 0191 if (!$element instanceof Zend_Form_Element) { 0192 return null; 0193 } 0194 0195 foreach ($this->_buttonTypes as $type) { 0196 if ($element instanceof $type) { 0197 if (stristr($type, 'button')) { 0198 $element->content = $element->getLabel(); 0199 0200 return $element->getValue(); 0201 } 0202 return $element->getLabel(); 0203 } 0204 } 0205 0206 return $element->getValue(); 0207 } 0208 0209 /** 0210 * Render an element using a view helper 0211 * 0212 * Determine view helper from 'viewHelper' option, or, if none set, from 0213 * the element type. Then call as 0214 * helper($element->getName(), $element->getValue(), $element->getAttribs()) 0215 * 0216 * @param string $content 0217 * @return string 0218 * @throws Zend_Form_Decorator_Exception if element or view are not registered 0219 */ 0220 public function render($content) 0221 { 0222 $element = $this->getElement(); 0223 0224 $view = $element->getView(); 0225 if (null === $view) { 0226 // require_once 'Zend/Form/Decorator/Exception.php'; 0227 throw new Zend_Form_Decorator_Exception('ViewHelper decorator cannot render without a registered view object'); 0228 } 0229 0230 if (method_exists($element, 'getMultiOptions')) { 0231 $element->getMultiOptions(); 0232 } 0233 0234 $helper = $this->getHelper(); 0235 $separator = $this->getSeparator(); 0236 $value = $this->getValue($element); 0237 $attribs = $this->getElementAttribs(); 0238 $name = $element->getFullyQualifiedName(); 0239 $id = $element->getId(); 0240 $attribs['id'] = $id; 0241 0242 $helperObject = $view->getHelper($helper); 0243 if (method_exists($helperObject, 'setTranslator')) { 0244 $helperObject->setTranslator($element->getTranslator()); 0245 } 0246 0247 // Check list separator 0248 if (isset($attribs['listsep']) 0249 && in_array($helper, array('formMultiCheckbox', 'formRadio', 'formSelect')) 0250 ) { 0251 $listsep = $attribs['listsep']; 0252 unset($attribs['listsep']); 0253 0254 $elementContent = $view->$helper($name, $value, $attribs, $element->options, $listsep); 0255 } else { 0256 $elementContent = $view->$helper($name, $value, $attribs, $element->options); 0257 } 0258 0259 switch ($this->getPlacement()) { 0260 case self::APPEND: 0261 return $content . $separator . $elementContent; 0262 case self::PREPEND: 0263 return $elementContent . $separator . $content; 0264 default: 0265 return $elementContent; 0266 } 0267 } 0268 }