File indexing completed on 2024-12-22 05:36:42
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 * @subpackage Decorator 0018 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0019 * @license http://framework.zend.com/license/new-bsd New BSD License 0020 */ 0021 0022 /** Zend_Form_Decorator_Abstract */ 0023 // require_once 'Zend/Form/Decorator/Abstract.php'; 0024 0025 /** 0026 * Zend_Form_Decorator_Label 0027 * 0028 * Accepts the options: 0029 * - separator: separator to use between label and content (defaults to PHP_EOL) 0030 * - placement: whether to append or prepend label to content (defaults to prepend) 0031 * - tag: if set, used to wrap the label in an additional HTML tag 0032 * - tagClass: if tag option is set, used to add a class to the label wrapper 0033 * - opt(ional)Prefix: a prefix to the label to use when the element is optional 0034 * - opt(ional)Suffix: a suffix to the label to use when the element is optional 0035 * - req(uired)Prefix: a prefix to the label to use when the element is required 0036 * - req(uired)Suffix: a suffix to the label to use when the element is required 0037 * 0038 * Any other options passed will be used as HTML attributes of the label tag. 0039 * 0040 * @category Zend 0041 * @package Zend_Form 0042 * @subpackage Decorator 0043 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0044 * @license http://framework.zend.com/license/new-bsd New BSD License 0045 * @version $Id$ 0046 */ 0047 class Zend_Form_Decorator_Label extends Zend_Form_Decorator_Abstract 0048 { 0049 /** 0050 * Placement constants 0051 */ 0052 const IMPLICIT = 'IMPLICIT'; 0053 const IMPLICIT_PREPEND = 'IMPLICIT_PREPEND'; 0054 const IMPLICIT_APPEND = 'IMPLICIT_APPEND'; 0055 0056 /** 0057 * Default placement: prepend 0058 * @var string 0059 */ 0060 protected $_placement = 'PREPEND'; 0061 0062 /** 0063 * HTML tag with which to surround label 0064 * @var string 0065 */ 0066 protected $_tag; 0067 0068 /** 0069 * Class for the HTML tag with which to surround label 0070 * @var string 0071 */ 0072 protected $_tagClass; 0073 0074 /** 0075 * Set element ID 0076 * 0077 * @param string $id 0078 * @return Zend_Form_Decorator_Label 0079 */ 0080 public function setId($id) 0081 { 0082 $this->setOption('id', $id); 0083 return $this; 0084 } 0085 0086 /** 0087 * Retrieve element ID (used in 'for' attribute) 0088 * 0089 * If none set in decorator, looks first for element 'id' attribute, and 0090 * defaults to element name. 0091 * 0092 * @return string 0093 */ 0094 public function getId() 0095 { 0096 $id = $this->getOption('id'); 0097 if (null === $id) { 0098 if (null !== ($element = $this->getElement())) { 0099 $id = $element->getId(); 0100 $this->setId($id); 0101 } 0102 } 0103 0104 return $id; 0105 } 0106 0107 /** 0108 * Set HTML tag with which to surround label 0109 * 0110 * @param string $tag 0111 * @return Zend_Form_Decorator_Label 0112 */ 0113 public function setTag($tag) 0114 { 0115 if (empty($tag)) { 0116 $this->_tag = null; 0117 } else { 0118 $this->_tag = (string) $tag; 0119 } 0120 0121 $this->removeOption('tag'); 0122 0123 return $this; 0124 } 0125 0126 /** 0127 * Get HTML tag, if any, with which to surround label 0128 * 0129 * @return string 0130 */ 0131 public function getTag() 0132 { 0133 if (null === $this->_tag) { 0134 $tag = $this->getOption('tag'); 0135 if (null !== $tag) { 0136 $this->removeOption('tag'); 0137 $this->setTag($tag); 0138 } 0139 return $tag; 0140 } 0141 0142 return $this->_tag; 0143 } 0144 0145 /** 0146 * Set the class to apply to the HTML tag with which to surround label 0147 * 0148 * @param string $tagClass 0149 * @return Zend_Form_Decorator_Label 0150 */ 0151 public function setTagClass($tagClass) 0152 { 0153 if (empty($tagClass)) { 0154 $this->_tagClass = null; 0155 } else { 0156 $this->_tagClass = (string) $tagClass; 0157 } 0158 0159 $this->removeOption('tagClass'); 0160 0161 return $this; 0162 } 0163 0164 /** 0165 * Get the class to apply to the HTML tag, if any, with which to surround label 0166 * 0167 * @return void 0168 */ 0169 public function getTagClass() 0170 { 0171 if (null === $this->_tagClass) { 0172 $tagClass = $this->getOption('tagClass'); 0173 if (null !== $tagClass) { 0174 $this->removeOption('tagClass'); 0175 $this->setTagClass($tagClass); 0176 } 0177 } 0178 0179 return $this->_tagClass; 0180 } 0181 0182 /** 0183 * Get class with which to define label 0184 * 0185 * Appends either 'optional' or 'required' to class, depending on whether 0186 * or not the element is required. 0187 * 0188 * @return string 0189 */ 0190 public function getClass() 0191 { 0192 $class = ''; 0193 $element = $this->getElement(); 0194 0195 $decoratorClass = $this->getOption('class'); 0196 if (!empty($decoratorClass)) { 0197 $class .= ' ' . $decoratorClass; 0198 } 0199 0200 $type = $element->isRequired() ? 'required' : 'optional'; 0201 0202 if (!strstr($class, $type)) { 0203 $class .= ' ' . $type; 0204 $class = trim($class); 0205 } 0206 0207 return $class; 0208 } 0209 0210 /** 0211 * Load an optional/required suffix/prefix key 0212 * 0213 * @param string $key 0214 * @return void 0215 */ 0216 protected function _loadOptReqKey($key) 0217 { 0218 if (!isset($this->$key)) { 0219 $value = $this->getOption($key); 0220 $this->$key = (string) $value; 0221 if (null !== $value) { 0222 $this->removeOption($key); 0223 } 0224 } 0225 } 0226 0227 /** 0228 * Overloading 0229 * 0230 * Currently overloads: 0231 * 0232 * - getOpt(ional)Prefix() 0233 * - getOpt(ional)Suffix() 0234 * - getReq(uired)Prefix() 0235 * - getReq(uired)Suffix() 0236 * - setOpt(ional)Prefix() 0237 * - setOpt(ional)Suffix() 0238 * - setReq(uired)Prefix() 0239 * - setReq(uired)Suffix() 0240 * 0241 * @param string $method 0242 * @param array $args 0243 * @return mixed 0244 * @throws Zend_Form_Exception for unsupported methods 0245 */ 0246 public function __call($method, $args) 0247 { 0248 $tail = substr($method, -6); 0249 $head = substr($method, 0, 3); 0250 if (in_array($head, array('get', 'set')) 0251 && (('Prefix' == $tail) || ('Suffix' == $tail)) 0252 ) { 0253 $position = substr($method, -6); 0254 $type = strtolower(substr($method, 3, 3)); 0255 switch ($type) { 0256 case 'req': 0257 $key = 'required' . $position; 0258 break; 0259 case 'opt': 0260 $key = 'optional' . $position; 0261 break; 0262 default: 0263 // require_once 'Zend/Form/Exception.php'; 0264 throw new Zend_Form_Exception(sprintf('Invalid method "%s" called in Label decorator, and detected as type %s', $method, $type)); 0265 } 0266 0267 switch ($head) { 0268 case 'set': 0269 if (0 === count($args)) { 0270 // require_once 'Zend/Form/Exception.php'; 0271 throw new Zend_Form_Exception(sprintf('Method "%s" requires at least one argument; none provided', $method)); 0272 } 0273 $value = array_shift($args); 0274 $this->$key = $value; 0275 return $this; 0276 case 'get': 0277 default: 0278 if (null === ($element = $this->getElement())) { 0279 $this->_loadOptReqKey($key); 0280 } elseif (isset($element->$key)) { 0281 $this->$key = (string) $element->$key; 0282 } else { 0283 $this->_loadOptReqKey($key); 0284 } 0285 return $this->$key; 0286 } 0287 } 0288 0289 // require_once 'Zend/Form/Exception.php'; 0290 throw new Zend_Form_Exception(sprintf('Invalid method "%s" called in Label decorator', $method)); 0291 } 0292 0293 /** 0294 * Get label to render 0295 * 0296 * @return string 0297 */ 0298 public function getLabel() 0299 { 0300 if (null === ($element = $this->getElement())) { 0301 return ''; 0302 } 0303 0304 $label = $element->getLabel(); 0305 $label = trim($label); 0306 0307 if (empty($label)) { 0308 return ''; 0309 } 0310 0311 $optPrefix = $this->getOptPrefix(); 0312 $optSuffix = $this->getOptSuffix(); 0313 $reqPrefix = $this->getReqPrefix(); 0314 $reqSuffix = $this->getReqSuffix(); 0315 $separator = $this->getSeparator(); 0316 0317 if (!empty($label)) { 0318 if ($element->isRequired()) { 0319 $label = $reqPrefix . $label . $reqSuffix; 0320 } else { 0321 $label = $optPrefix . $label . $optSuffix; 0322 } 0323 } 0324 0325 return $label; 0326 } 0327 0328 /** 0329 * Determine if label should append, prepend or implicit content 0330 * 0331 * @return string 0332 */ 0333 public function getPlacement() 0334 { 0335 $placement = $this->_placement; 0336 if (null !== ($placementOpt = $this->getOption('placement'))) { 0337 $placementOpt = strtoupper($placementOpt); 0338 switch ($placementOpt) { 0339 case self::APPEND: 0340 case self::PREPEND: 0341 case self::IMPLICIT: 0342 case self::IMPLICIT_PREPEND: 0343 case self::IMPLICIT_APPEND: 0344 $placement = $this->_placement = $placementOpt; 0345 break; 0346 case false: 0347 $placement = $this->_placement = null; 0348 break; 0349 default: 0350 break; 0351 } 0352 $this->removeOption('placement'); 0353 } 0354 0355 return $placement; 0356 } 0357 0358 /** 0359 * Render a label 0360 * 0361 * @param string $content 0362 * @return string 0363 */ 0364 public function render($content) 0365 { 0366 $element = $this->getElement(); 0367 $view = $element->getView(); 0368 if (null === $view) { 0369 return $content; 0370 } 0371 0372 $label = $this->getLabel(); 0373 $separator = $this->getSeparator(); 0374 $placement = $this->getPlacement(); 0375 $tag = $this->getTag(); 0376 $tagClass = $this->getTagClass(); 0377 $id = $this->getId(); 0378 $class = $this->getClass(); 0379 $options = $this->getOptions(); 0380 0381 0382 if (empty($label) && empty($tag)) { 0383 return $content; 0384 } 0385 0386 if (!empty($label)) { 0387 $options['class'] = $class; 0388 $label = trim($label); 0389 0390 switch ($placement) { 0391 case self::IMPLICIT: 0392 // Break was intentionally omitted 0393 0394 case self::IMPLICIT_PREPEND: 0395 $options['escape'] = false; 0396 $options['disableFor'] = true; 0397 0398 $label = $view->formLabel( 0399 $element->getFullyQualifiedName(), 0400 $label . $separator . $content, 0401 $options 0402 ); 0403 break; 0404 0405 case self::IMPLICIT_APPEND: 0406 $options['escape'] = false; 0407 $options['disableFor'] = true; 0408 0409 $label = $view->formLabel( 0410 $element->getFullyQualifiedName(), 0411 $content . $separator . $label, 0412 $options 0413 ); 0414 break; 0415 0416 case self::APPEND: 0417 // Break was intentionally omitted 0418 0419 case self::PREPEND: 0420 // Break was intentionally omitted 0421 0422 default: 0423 $label = $view->formLabel( 0424 $element->getFullyQualifiedName(), 0425 $label, 0426 $options 0427 ); 0428 break; 0429 } 0430 } else { 0431 $label = ' '; 0432 } 0433 0434 if (null !== $tag) { 0435 // require_once 'Zend/Form/Decorator/HtmlTag.php'; 0436 $decorator = new Zend_Form_Decorator_HtmlTag(); 0437 if (null !== $this->_tagClass) { 0438 $decorator->setOptions(array('tag' => $tag, 0439 'id' => $id . '-label', 0440 'class' => $tagClass)); 0441 } else { 0442 $decorator->setOptions(array('tag' => $tag, 0443 'id' => $id . '-label')); 0444 } 0445 0446 $label = $decorator->render($label); 0447 } 0448 0449 switch ($placement) { 0450 case self::APPEND: 0451 return $content . $separator . $label; 0452 0453 case self::PREPEND: 0454 return $label . $separator . $content; 0455 0456 case self::IMPLICIT: 0457 // Break was intentionally omitted 0458 0459 case self::IMPLICIT_PREPEND: 0460 // Break was intentionally omitted 0461 0462 case self::IMPLICIT_APPEND: 0463 return $label; 0464 } 0465 } 0466 }