|
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_View |
|
17 * @subpackage Helper |
|
18 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
19 * @version $Id: HeadTitle.php 23388 2010-11-19 00:37:55Z ramon $ |
|
20 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
21 */ |
|
22 |
|
23 /** Zend_View_Helper_Placeholder_Container_Standalone */ |
|
24 require_once 'Zend/View/Helper/Placeholder/Container/Standalone.php'; |
|
25 |
|
26 /** |
|
27 * Helper for setting and retrieving title element for HTML head |
|
28 * |
|
29 * @uses Zend_View_Helper_Placeholder_Container_Standalone |
|
30 * @package Zend_View |
|
31 * @subpackage Helper |
|
32 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
33 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
34 */ |
|
35 class Zend_View_Helper_HeadTitle extends Zend_View_Helper_Placeholder_Container_Standalone |
|
36 { |
|
37 /** |
|
38 * Registry key for placeholder |
|
39 * @var string |
|
40 */ |
|
41 protected $_regKey = 'Zend_View_Helper_HeadTitle'; |
|
42 |
|
43 /** |
|
44 * Whether or not auto-translation is enabled |
|
45 * @var boolean |
|
46 */ |
|
47 protected $_translate = false; |
|
48 |
|
49 /** |
|
50 * Translation object |
|
51 * |
|
52 * @var Zend_Translate_Adapter |
|
53 */ |
|
54 protected $_translator; |
|
55 |
|
56 /** |
|
57 * Default title rendering order (i.e. order in which each title attached) |
|
58 * |
|
59 * @var string |
|
60 */ |
|
61 protected $_defaultAttachOrder = null; |
|
62 |
|
63 /** |
|
64 * Retrieve placeholder for title element and optionally set state |
|
65 * |
|
66 * @param string $title |
|
67 * @param string $setType |
|
68 * @return Zend_View_Helper_HeadTitle |
|
69 */ |
|
70 public function headTitle($title = null, $setType = null) |
|
71 { |
|
72 if ($setType === null && $this->getDefaultAttachOrder() === null) { |
|
73 $setType = Zend_View_Helper_Placeholder_Container_Abstract::APPEND; |
|
74 } elseif ($setType === null && $this->getDefaultAttachOrder() !== null) { |
|
75 $setType = $this->getDefaultAttachOrder(); |
|
76 } |
|
77 $title = (string) $title; |
|
78 if ($title !== '') { |
|
79 if ($setType == Zend_View_Helper_Placeholder_Container_Abstract::SET) { |
|
80 $this->set($title); |
|
81 } elseif ($setType == Zend_View_Helper_Placeholder_Container_Abstract::PREPEND) { |
|
82 $this->prepend($title); |
|
83 } else { |
|
84 $this->append($title); |
|
85 } |
|
86 } |
|
87 |
|
88 return $this; |
|
89 } |
|
90 |
|
91 /** |
|
92 * Set a default order to add titles |
|
93 * |
|
94 * @param string $setType |
|
95 */ |
|
96 public function setDefaultAttachOrder($setType) |
|
97 { |
|
98 if (!in_array($setType, array( |
|
99 Zend_View_Helper_Placeholder_Container_Abstract::APPEND, |
|
100 Zend_View_Helper_Placeholder_Container_Abstract::SET, |
|
101 Zend_View_Helper_Placeholder_Container_Abstract::PREPEND |
|
102 ))) { |
|
103 require_once 'Zend/View/Exception.php'; |
|
104 throw new Zend_View_Exception("You must use a valid attach order: 'PREPEND', 'APPEND' or 'SET'"); |
|
105 } |
|
106 |
|
107 $this->_defaultAttachOrder = $setType; |
|
108 return $this; |
|
109 } |
|
110 |
|
111 /** |
|
112 * Get the default attach order, if any. |
|
113 * |
|
114 * @return mixed |
|
115 */ |
|
116 public function getDefaultAttachOrder() |
|
117 { |
|
118 return $this->_defaultAttachOrder; |
|
119 } |
|
120 |
|
121 /** |
|
122 * Sets a translation Adapter for translation |
|
123 * |
|
124 * @param Zend_Translate|Zend_Translate_Adapter $translate |
|
125 * @return Zend_View_Helper_HeadTitle |
|
126 */ |
|
127 public function setTranslator($translate) |
|
128 { |
|
129 if ($translate instanceof Zend_Translate_Adapter) { |
|
130 $this->_translator = $translate; |
|
131 } elseif ($translate instanceof Zend_Translate) { |
|
132 $this->_translator = $translate->getAdapter(); |
|
133 } else { |
|
134 require_once 'Zend/View/Exception.php'; |
|
135 $e = new Zend_View_Exception("You must set an instance of Zend_Translate or Zend_Translate_Adapter"); |
|
136 $e->setView($this->view); |
|
137 throw $e; |
|
138 } |
|
139 return $this; |
|
140 } |
|
141 |
|
142 /** |
|
143 * Retrieve translation object |
|
144 * |
|
145 * If none is currently registered, attempts to pull it from the registry |
|
146 * using the key 'Zend_Translate'. |
|
147 * |
|
148 * @return Zend_Translate_Adapter|null |
|
149 */ |
|
150 public function getTranslator() |
|
151 { |
|
152 if (null === $this->_translator) { |
|
153 require_once 'Zend/Registry.php'; |
|
154 if (Zend_Registry::isRegistered('Zend_Translate')) { |
|
155 $this->setTranslator(Zend_Registry::get('Zend_Translate')); |
|
156 } |
|
157 } |
|
158 return $this->_translator; |
|
159 } |
|
160 |
|
161 /** |
|
162 * Enables translation |
|
163 * |
|
164 * @return Zend_View_Helper_HeadTitle |
|
165 */ |
|
166 public function enableTranslation() |
|
167 { |
|
168 $this->_translate = true; |
|
169 return $this; |
|
170 } |
|
171 |
|
172 /** |
|
173 * Disables translation |
|
174 * |
|
175 * @return Zend_View_Helper_HeadTitle |
|
176 */ |
|
177 public function disableTranslation() |
|
178 { |
|
179 $this->_translate = false; |
|
180 return $this; |
|
181 } |
|
182 |
|
183 /** |
|
184 * Turn helper into string |
|
185 * |
|
186 * @param string|null $indent |
|
187 * @param string|null $locale |
|
188 * @return string |
|
189 */ |
|
190 public function toString($indent = null, $locale = null) |
|
191 { |
|
192 $indent = (null !== $indent) |
|
193 ? $this->getWhitespace($indent) |
|
194 : $this->getIndent(); |
|
195 |
|
196 $items = array(); |
|
197 |
|
198 if($this->_translate && $translator = $this->getTranslator()) { |
|
199 foreach ($this as $item) { |
|
200 $items[] = $translator->translate($item, $locale); |
|
201 } |
|
202 } else { |
|
203 foreach ($this as $item) { |
|
204 $items[] = $item; |
|
205 } |
|
206 } |
|
207 |
|
208 $separator = $this->getSeparator(); |
|
209 $output = ''; |
|
210 if(($prefix = $this->getPrefix())) { |
|
211 $output .= $prefix; |
|
212 } |
|
213 $output .= implode($separator, $items); |
|
214 if(($postfix = $this->getPostfix())) { |
|
215 $output .= $postfix; |
|
216 } |
|
217 |
|
218 $output = ($this->_autoEscape) ? $this->_escape($output) : $output; |
|
219 |
|
220 return $indent . '<title>' . $output . '</title>'; |
|
221 } |
|
222 } |