HeadTitle.php 6.4 KB

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