HeadTitle.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. * @package Zend_View
  16. * @subpackage Helper
  17. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @version $Id: Placeholder.php 7078 2007-12-11 14:29:33Z matthew $
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /** Zend_View_Helper_Placeholder_Container_Standalone */
  22. require_once 'Zend/View/Helper/Placeholder/Container/Standalone.php';
  23. /**
  24. * Helper for setting and retrieving title element for HTML head
  25. *
  26. * @uses Zend_View_Helper_Placeholder_Container_Standalone
  27. * @package Zend_View
  28. * @subpackage Helper
  29. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  30. * @license http://framework.zend.com/license/new-bsd New BSD License
  31. */
  32. class Zend_View_Helper_HeadTitle extends Zend_View_Helper_Placeholder_Container_Standalone
  33. {
  34. /**
  35. * Registry key for placeholder
  36. * @var string
  37. */
  38. protected $_regKey = 'Zend_View_Helper_HeadTitle';
  39. /**
  40. * Whether or not auto-translation is enabled
  41. * @var boolean
  42. */
  43. protected $_translate = false;
  44. /**
  45. * Translation object
  46. *
  47. * @var Zend_Translate_Adapter
  48. */
  49. protected $_translator;
  50. /**
  51. * Retrieve placeholder for title element and optionally set state
  52. *
  53. * @param string $title
  54. * @param string $setType
  55. * @param string $separator
  56. * @return Zend_View_Helper_HeadTitle
  57. */
  58. public function headTitle($title = null, $setType = Zend_View_Helper_Placeholder_Container_Abstract::APPEND)
  59. {
  60. if ($title) {
  61. if ($setType == Zend_View_Helper_Placeholder_Container_Abstract::SET) {
  62. $this->set($title);
  63. } elseif ($setType == Zend_View_Helper_Placeholder_Container_Abstract::PREPEND) {
  64. $this->prepend($title);
  65. } else {
  66. $this->append($title);
  67. }
  68. }
  69. return $this;
  70. }
  71. /**
  72. * Sets a translation Adapter for translation
  73. *
  74. * @param Zend_Translate|Zend_Translate_Adapter $translate
  75. * @return Zend_View_Helper_HeadTitle
  76. */
  77. public function setTranslator($translate)
  78. {
  79. if ($translate instanceof Zend_Translate_Adapter) {
  80. $this->_translator = $translate;
  81. } elseif ($translate instanceof Zend_Translate) {
  82. $this->_translator = $translate->getAdapter();
  83. } else {
  84. require_once 'Zend/View/Exception.php';
  85. throw new Zend_View_Exception("You must set an instance of Zend_Translate or Zend_Translate_Adapter");
  86. }
  87. return $this;
  88. }
  89. /*
  90. * Retrieve translation object
  91. *
  92. * If none is currently registered, attempts to pull it from the registry
  93. * using the key 'Zend_Translate'.
  94. *
  95. * @return Zend_Translate_Adapter|null
  96. */
  97. public function getTranslator()
  98. {
  99. if (null === $this->_translator) {
  100. require_once 'Zend/Registry.php';
  101. if (Zend_Registry::isRegistered('Zend_Translate')) {
  102. $this->setTranslator(Zend_Registry::get('Zend_Translate'));
  103. }
  104. }
  105. return $this->_translator;
  106. }
  107. /**
  108. * Enables translation
  109. *
  110. * @return Zend_View_Helper_HeadTitle
  111. */
  112. public function enableTranslation()
  113. {
  114. $this->_translate = true;
  115. return $this;
  116. }
  117. /**
  118. * Disables translation
  119. *
  120. * @return Zend_View_Helper_HeadTitle
  121. */
  122. public function disableTranslation()
  123. {
  124. $this->_translate = false;
  125. return $this;
  126. }
  127. /**
  128. * Turn helper into string
  129. *
  130. * @param string|null $indent
  131. * @param string|null $locale
  132. * @return string
  133. */
  134. public function toString($indent = null, $locale = null)
  135. {
  136. $indent = (null !== $indent)
  137. ? $this->getWhitespace($indent)
  138. : $this->getIndent();
  139. $items = array();
  140. if($this->_translate && $translator = $this->getTranslator()) {
  141. foreach ($this as $item) {
  142. $items[] = $translator->translate($item, $locale);
  143. }
  144. } else {
  145. foreach ($this as $item) {
  146. $items[] = $item;
  147. }
  148. }
  149. $separator = $this->getSeparator();
  150. $output = '';
  151. if(($prefix = $this->getPrefix())) {
  152. $output .= $prefix;
  153. }
  154. $output .= implode($separator, $items);
  155. if(($postfix = $this->getPostfix())) {
  156. $output .= $postfix;
  157. }
  158. $output = ($this->_autoEscape) ? $this->_escape($output) : $output;
  159. return $indent . '<title>' . $output . '</title>';
  160. }
  161. }