2
0

HeadTitle.php 5.0 KB

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