File indexing completed on 2024-12-22 05:37:13
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_View 0017 * @subpackage Helper 0018 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0019 * @version $Id$ 0020 * @license http://framework.zend.com/license/new-bsd New BSD License 0021 */ 0022 0023 /** Zend_View_Helper_Placeholder_Container_Standalone */ 0024 // require_once 'Zend/View/Helper/Placeholder/Container/Standalone.php'; 0025 0026 /** 0027 * Helper for setting and retrieving title element for HTML head 0028 * 0029 * @uses Zend_View_Helper_Placeholder_Container_Standalone 0030 * @package Zend_View 0031 * @subpackage Helper 0032 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0033 * @license http://framework.zend.com/license/new-bsd New BSD License 0034 */ 0035 class Zend_View_Helper_HeadTitle extends Zend_View_Helper_Placeholder_Container_Standalone 0036 { 0037 /** 0038 * Registry key for placeholder 0039 * @var string 0040 */ 0041 protected $_regKey = 'Zend_View_Helper_HeadTitle'; 0042 0043 /** 0044 * Whether or not auto-translation is enabled 0045 * @var boolean 0046 */ 0047 protected $_translate = false; 0048 0049 /** 0050 * Translation object 0051 * 0052 * @var Zend_Translate_Adapter 0053 */ 0054 protected $_translator; 0055 0056 /** 0057 * Default title rendering order (i.e. order in which each title attached) 0058 * 0059 * @var string 0060 */ 0061 protected $_defaultAttachOrder = null; 0062 0063 /** 0064 * Retrieve placeholder for title element and optionally set state 0065 * 0066 * @param string $title 0067 * @param string $setType 0068 * @return Zend_View_Helper_HeadTitle 0069 */ 0070 public function headTitle($title = null, $setType = null) 0071 { 0072 if (null === $setType) { 0073 $setType = (null === $this->getDefaultAttachOrder()) 0074 ? Zend_View_Helper_Placeholder_Container_Abstract::APPEND 0075 : $this->getDefaultAttachOrder(); 0076 } 0077 $title = (string) $title; 0078 if ($title !== '') { 0079 if ($setType == Zend_View_Helper_Placeholder_Container_Abstract::SET) { 0080 $this->set($title); 0081 } elseif ($setType == Zend_View_Helper_Placeholder_Container_Abstract::PREPEND) { 0082 $this->prepend($title); 0083 } else { 0084 $this->append($title); 0085 } 0086 } 0087 0088 return $this; 0089 } 0090 0091 /** 0092 * Set a default order to add titles 0093 * 0094 * @param string $setType 0095 */ 0096 public function setDefaultAttachOrder($setType) 0097 { 0098 if (!in_array($setType, array( 0099 Zend_View_Helper_Placeholder_Container_Abstract::APPEND, 0100 Zend_View_Helper_Placeholder_Container_Abstract::SET, 0101 Zend_View_Helper_Placeholder_Container_Abstract::PREPEND 0102 ))) { 0103 // require_once 'Zend/View/Exception.php'; 0104 throw new Zend_View_Exception("You must use a valid attach order: 'PREPEND', 'APPEND' or 'SET'"); 0105 } 0106 0107 $this->_defaultAttachOrder = $setType; 0108 return $this; 0109 } 0110 0111 /** 0112 * Get the default attach order, if any. 0113 * 0114 * @return mixed 0115 */ 0116 public function getDefaultAttachOrder() 0117 { 0118 return $this->_defaultAttachOrder; 0119 } 0120 0121 /** 0122 * Sets a translation Adapter for translation 0123 * 0124 * @param Zend_Translate|Zend_Translate_Adapter $translate 0125 * @return Zend_View_Helper_HeadTitle 0126 */ 0127 public function setTranslator($translate) 0128 { 0129 if ($translate instanceof Zend_Translate_Adapter) { 0130 $this->_translator = $translate; 0131 } elseif ($translate instanceof Zend_Translate) { 0132 $this->_translator = $translate->getAdapter(); 0133 } else { 0134 // require_once 'Zend/View/Exception.php'; 0135 $e = new Zend_View_Exception("You must set an instance of Zend_Translate or Zend_Translate_Adapter"); 0136 $e->setView($this->view); 0137 throw $e; 0138 } 0139 return $this; 0140 } 0141 0142 /** 0143 * Retrieve translation object 0144 * 0145 * If none is currently registered, attempts to pull it from the registry 0146 * using the key 'Zend_Translate'. 0147 * 0148 * @return Zend_Translate_Adapter|null 0149 */ 0150 public function getTranslator() 0151 { 0152 if (null === $this->_translator) { 0153 // require_once 'Zend/Registry.php'; 0154 if (Zend_Registry::isRegistered('Zend_Translate')) { 0155 $this->setTranslator(Zend_Registry::get('Zend_Translate')); 0156 } 0157 } 0158 return $this->_translator; 0159 } 0160 0161 /** 0162 * Enables translation 0163 * 0164 * @return Zend_View_Helper_HeadTitle 0165 */ 0166 public function enableTranslation() 0167 { 0168 $this->_translate = true; 0169 return $this; 0170 } 0171 0172 /** 0173 * Disables translation 0174 * 0175 * @return Zend_View_Helper_HeadTitle 0176 */ 0177 public function disableTranslation() 0178 { 0179 $this->_translate = false; 0180 return $this; 0181 } 0182 0183 /** 0184 * Turn helper into string 0185 * 0186 * @param string|null $indent 0187 * @param string|null $locale 0188 * @return string 0189 */ 0190 public function toString($indent = null, $locale = null) 0191 { 0192 $indent = (null !== $indent) 0193 ? $this->getWhitespace($indent) 0194 : $this->getIndent(); 0195 0196 $items = array(); 0197 0198 if($this->_translate && $translator = $this->getTranslator()) { 0199 foreach ($this as $item) { 0200 $items[] = $translator->translate($item, $locale); 0201 } 0202 } else { 0203 foreach ($this as $item) { 0204 $items[] = $item; 0205 } 0206 } 0207 0208 $separator = $this->getSeparator(); 0209 $output = ''; 0210 if(($prefix = $this->getPrefix())) { 0211 $output .= $prefix; 0212 } 0213 $output .= implode($separator, $items); 0214 if(($postfix = $this->getPostfix())) { 0215 $output .= $postfix; 0216 } 0217 0218 $output = ($this->_autoEscape) ? $this->_escape($output) : $output; 0219 0220 return $indent . '<title>' . $output . '</title>'; 0221 } 0222 }