/Zend/View/Helper/HeadTitle.php

https://github.com/MontmereLimited/ZendFramework-v1 · PHP · 222 lines · 102 code · 20 blank · 100 comment · 16 complexity · 808678d07550b2cb725f6af74b9b1731 MD5 · raw file

  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-2011 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @version $Id: HeadTitle.php 23775 2011-03-01 17:25:24Z ralph $
  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-2011 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. * @return Zend_View_Helper_HeadTitle
  63. */
  64. public function headTitle($title = null, $setType = null)
  65. {
  66. if (null === $setType) {
  67. $setType = (null === $this->getDefaultAttachOrder())
  68. ? Zend_View_Helper_Placeholder_Container_Abstract::APPEND
  69. : $this->getDefaultAttachOrder();
  70. }
  71. $title = (string) $title;
  72. if ($title !== '') {
  73. if ($setType == Zend_View_Helper_Placeholder_Container_Abstract::SET) {
  74. $this->set($title);
  75. } elseif ($setType == Zend_View_Helper_Placeholder_Container_Abstract::PREPEND) {
  76. $this->prepend($title);
  77. } else {
  78. $this->append($title);
  79. }
  80. }
  81. return $this;
  82. }
  83. /**
  84. * Set a default order to add titles
  85. *
  86. * @param string $setType
  87. */
  88. public function setDefaultAttachOrder($setType)
  89. {
  90. if (!in_array($setType, array(
  91. Zend_View_Helper_Placeholder_Container_Abstract::APPEND,
  92. Zend_View_Helper_Placeholder_Container_Abstract::SET,
  93. Zend_View_Helper_Placeholder_Container_Abstract::PREPEND
  94. ))) {
  95. // // // // // // // // require_once 'Zend/View/Exception.php';
  96. throw new Zend_View_Exception("You must use a valid attach order: 'PREPEND', 'APPEND' or 'SET'");
  97. }
  98. $this->_defaultAttachOrder = $setType;
  99. return $this;
  100. }
  101. /**
  102. * Get the default attach order, if any.
  103. *
  104. * @return mixed
  105. */
  106. public function getDefaultAttachOrder()
  107. {
  108. return $this->_defaultAttachOrder;
  109. }
  110. /**
  111. * Sets a translation Adapter for translation
  112. *
  113. * @param Zend_Translate|Zend_Translate_Adapter $translate
  114. * @return Zend_View_Helper_HeadTitle
  115. */
  116. public function setTranslator($translate)
  117. {
  118. if ($translate instanceof Zend_Translate_Adapter) {
  119. $this->_translator = $translate;
  120. } elseif ($translate instanceof Zend_Translate) {
  121. $this->_translator = $translate->getAdapter();
  122. } else {
  123. // // // // // // // // require_once 'Zend/View/Exception.php';
  124. $e = new Zend_View_Exception("You must set an instance of Zend_Translate or Zend_Translate_Adapter");
  125. $e->setView($this->view);
  126. throw $e;
  127. }
  128. return $this;
  129. }
  130. /**
  131. * Retrieve translation object
  132. *
  133. * If none is currently registered, attempts to pull it from the registry
  134. * using the key 'Zend_Translate'.
  135. *
  136. * @return Zend_Translate_Adapter|null
  137. */
  138. public function getTranslator()
  139. {
  140. if (null === $this->_translator) {
  141. // // // // // // // // require_once 'Zend/Registry.php';
  142. if (Zend_Registry::isRegistered('Zend_Translate')) {
  143. $this->setTranslator(Zend_Registry::get('Zend_Translate'));
  144. }
  145. }
  146. return $this->_translator;
  147. }
  148. /**
  149. * Enables translation
  150. *
  151. * @return Zend_View_Helper_HeadTitle
  152. */
  153. public function enableTranslation()
  154. {
  155. $this->_translate = true;
  156. return $this;
  157. }
  158. /**
  159. * Disables translation
  160. *
  161. * @return Zend_View_Helper_HeadTitle
  162. */
  163. public function disableTranslation()
  164. {
  165. $this->_translate = false;
  166. return $this;
  167. }
  168. /**
  169. * Turn helper into string
  170. *
  171. * @param string|null $indent
  172. * @param string|null $locale
  173. * @return string
  174. */
  175. public function toString($indent = null, $locale = null)
  176. {
  177. $indent = (null !== $indent)
  178. ? $this->getWhitespace($indent)
  179. : $this->getIndent();
  180. $items = array();
  181. if($this->_translate && $translator = $this->getTranslator()) {
  182. foreach ($this as $item) {
  183. $items[] = $translator->translate($item, $locale);
  184. }
  185. } else {
  186. foreach ($this as $item) {
  187. $items[] = $item;
  188. }
  189. }
  190. $separator = $this->getSeparator();
  191. $output = '';
  192. if(($prefix = $this->getPrefix())) {
  193. $output .= $prefix;
  194. }
  195. $output .= implode($separator, $items);
  196. if(($postfix = $this->getPostfix())) {
  197. $output .= $postfix;
  198. }
  199. $output = ($this->_autoEscape) ? $this->_escape($output) : $output;
  200. return $indent . '<title>' . $output . '</title>';
  201. }
  202. }