HeadTitle.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. * Retrieve placeholder for title element and optionally set state
  53. *
  54. * @param string $title
  55. * @param string $setType
  56. * @param string $separator
  57. * @return Zend_View_Helper_HeadTitle
  58. */
  59. public function headTitle($title = null, $setType = Zend_View_Helper_Placeholder_Container_Abstract::APPEND)
  60. {
  61. $title = (string) $title;
  62. if ($title !== '') {
  63. if ($setType == Zend_View_Helper_Placeholder_Container_Abstract::SET) {
  64. $this->set($title);
  65. } elseif ($setType == Zend_View_Helper_Placeholder_Container_Abstract::PREPEND) {
  66. $this->prepend($title);
  67. } else {
  68. $this->append($title);
  69. }
  70. }
  71. return $this;
  72. }
  73. /**
  74. * Sets a translation Adapter for translation
  75. *
  76. * @param Zend_Translate|Zend_Translate_Adapter $translate
  77. * @return Zend_View_Helper_HeadTitle
  78. */
  79. public function setTranslator($translate)
  80. {
  81. if ($translate instanceof Zend_Translate_Adapter) {
  82. $this->_translator = $translate;
  83. } elseif ($translate instanceof Zend_Translate) {
  84. $this->_translator = $translate->getAdapter();
  85. } else {
  86. require_once 'Zend/View/Exception.php';
  87. $e = new Zend_View_Exception("You must set an instance of Zend_Translate or Zend_Translate_Adapter");
  88. $e->setView($this->view);
  89. throw $e;
  90. }
  91. return $this;
  92. }
  93. /*
  94. * Retrieve translation object
  95. *
  96. * If none is currently registered, attempts to pull it from the registry
  97. * using the key 'Zend_Translate'.
  98. *
  99. * @return Zend_Translate_Adapter|null
  100. */
  101. public function getTranslator()
  102. {
  103. if (null === $this->_translator) {
  104. require_once 'Zend/Registry.php';
  105. if (Zend_Registry::isRegistered('Zend_Translate')) {
  106. $this->setTranslator(Zend_Registry::get('Zend_Translate'));
  107. }
  108. }
  109. return $this->_translator;
  110. }
  111. /**
  112. * Enables translation
  113. *
  114. * @return Zend_View_Helper_HeadTitle
  115. */
  116. public function enableTranslation()
  117. {
  118. $this->_translate = true;
  119. return $this;
  120. }
  121. /**
  122. * Disables translation
  123. *
  124. * @return Zend_View_Helper_HeadTitle
  125. */
  126. public function disableTranslation()
  127. {
  128. $this->_translate = false;
  129. return $this;
  130. }
  131. /**
  132. * Turn helper into string
  133. *
  134. * @param string|null $indent
  135. * @param string|null $locale
  136. * @return string
  137. */
  138. public function toString($indent = null, $locale = null)
  139. {
  140. $indent = (null !== $indent)
  141. ? $this->getWhitespace($indent)
  142. : $this->getIndent();
  143. $items = array();
  144. if($this->_translate && $translator = $this->getTranslator()) {
  145. foreach ($this as $item) {
  146. $items[] = $translator->translate($item, $locale);
  147. }
  148. } else {
  149. foreach ($this as $item) {
  150. $items[] = $item;
  151. }
  152. }
  153. $separator = $this->getSeparator();
  154. $output = '';
  155. if(($prefix = $this->getPrefix())) {
  156. $output .= $prefix;
  157. }
  158. $output .= implode($separator, $items);
  159. if(($postfix = $this->getPostfix())) {
  160. $output .= $postfix;
  161. }
  162. $output = ($this->_autoEscape) ? $this->_escape($output) : $output;
  163. return $indent . '<title>' . $output . '</title>';
  164. }
  165. }