2
0

HeadTitle.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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-2009 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-2009 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. throw new Zend_View_Exception("You must set an instance of Zend_Translate or Zend_Translate_Adapter");
  88. }
  89. return $this;
  90. }
  91. /*
  92. * Retrieve translation object
  93. *
  94. * If none is currently registered, attempts to pull it from the registry
  95. * using the key 'Zend_Translate'.
  96. *
  97. * @return Zend_Translate_Adapter|null
  98. */
  99. public function getTranslator()
  100. {
  101. if (null === $this->_translator) {
  102. require_once 'Zend/Registry.php';
  103. if (Zend_Registry::isRegistered('Zend_Translate')) {
  104. $this->setTranslator(Zend_Registry::get('Zend_Translate'));
  105. }
  106. }
  107. return $this->_translator;
  108. }
  109. /**
  110. * Enables translation
  111. *
  112. * @return Zend_View_Helper_HeadTitle
  113. */
  114. public function enableTranslation()
  115. {
  116. $this->_translate = true;
  117. return $this;
  118. }
  119. /**
  120. * Disables translation
  121. *
  122. * @return Zend_View_Helper_HeadTitle
  123. */
  124. public function disableTranslation()
  125. {
  126. $this->_translate = false;
  127. return $this;
  128. }
  129. /**
  130. * Turn helper into string
  131. *
  132. * @param string|null $indent
  133. * @param string|null $locale
  134. * @return string
  135. */
  136. public function toString($indent = null, $locale = null)
  137. {
  138. $indent = (null !== $indent)
  139. ? $this->getWhitespace($indent)
  140. : $this->getIndent();
  141. $items = array();
  142. if($this->_translate && $translator = $this->getTranslator()) {
  143. foreach ($this as $item) {
  144. $items[] = $translator->translate($item, $locale);
  145. }
  146. } else {
  147. foreach ($this as $item) {
  148. $items[] = $item;
  149. }
  150. }
  151. $separator = $this->getSeparator();
  152. $output = '';
  153. if(($prefix = $this->getPrefix())) {
  154. $output .= $prefix;
  155. }
  156. $output .= implode($separator, $items);
  157. if(($postfix = $this->getPostfix())) {
  158. $output .= $postfix;
  159. }
  160. $output = ($this->_autoEscape) ? $this->_escape($output) : $output;
  161. return $indent . '<title>' . $output . '</title>';
  162. }
  163. }