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-2015 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
23/** Zend_View_Helper_Placeholder_Container_Standalone */
24
25/**
26 * Helper for setting and retrieving title element for HTML head
27 *
28 * @uses       Zend_View_Helper_Placeholder_Container_Standalone
29 * @package    Zend_View
30 * @subpackage Helper
31 * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
32 * @license    http://framework.zend.com/license/new-bsd     New BSD License
33 */
34class Zend_View_Helper_HeadTitle extends Zend_View_Helper_Placeholder_Container_Standalone
35{
36    /**
37     * Registry key for placeholder
38     * @var string
39     */
40    protected $_regKey = 'Zend_View_Helper_HeadTitle';
41
42    /**
43     * Whether or not auto-translation is enabled
44     * @var boolean
45     */
46    protected $_translate = false;
47
48    /**
49     * Translation object
50     *
51     * @var Zend_Translate_Adapter
52     */
53    protected $_translator;
54
55    /**
56     * Default title rendering order (i.e. order in which each title attached)
57     *
58     * @var string
59     */
60    protected $_defaultAttachOrder = null;
61
62    /**
63     * Retrieve placeholder for title element and optionally set state
64     *
65     * @param  string $title
66     * @param  string $setType
67     * @return Zend_View_Helper_HeadTitle
68     */
69    public function headTitle($title = null, $setType = null)
70    {
71        if (null === $setType) {
72            $setType = (null === $this->getDefaultAttachOrder())
73                     ? Zend_View_Helper_Placeholder_Container_Abstract::APPEND
74                     : $this->getDefaultAttachOrder();
75        }
76        $title = (string) $title;
77        if ($title !== '') {
78            if ($setType == Zend_View_Helper_Placeholder_Container_Abstract::SET) {
79                $this->set($title);
80            } elseif ($setType == Zend_View_Helper_Placeholder_Container_Abstract::PREPEND) {
81                $this->prepend($title);
82            } else {
83                $this->append($title);
84            }
85        }
86
87        return $this;
88    }
89
90    /**
91     * Set a default order to add titles
92     *
93     * @param string $setType
94     */
95    public function setDefaultAttachOrder($setType)
96    {
97        if (!in_array($setType, array(
98            Zend_View_Helper_Placeholder_Container_Abstract::APPEND,
99            Zend_View_Helper_Placeholder_Container_Abstract::SET,
100            Zend_View_Helper_Placeholder_Container_Abstract::PREPEND
101        ))) {
102            throw new Zend_View_Exception("You must use a valid attach order: 'PREPEND', 'APPEND' or 'SET'");
103        }
104
105        $this->_defaultAttachOrder = $setType;
106        return $this;
107    }
108
109    /**
110     * Get the default attach order, if any.
111     *
112     * @return mixed
113     */
114    public function getDefaultAttachOrder()
115    {
116        return $this->_defaultAttachOrder;
117    }
118
119    /**
120     * Sets a translation Adapter for translation
121     *
122     * @param  Zend_Translate|Zend_Translate_Adapter $translate
123     * @return Zend_View_Helper_HeadTitle
124     */
125    public function setTranslator($translate)
126    {
127        if ($translate instanceof Zend_Translate_Adapter) {
128            $this->_translator = $translate;
129        } elseif ($translate instanceof Zend_Translate) {
130            $this->_translator = $translate->getAdapter();
131        } else {
132            $e = new Zend_View_Exception("You must set an instance of Zend_Translate or Zend_Translate_Adapter");
133            $e->setView($this->view);
134            throw $e;
135        }
136        return $this;
137    }
138
139    /**
140     * Retrieve translation object
141     *
142     * If none is currently registered, attempts to pull it from the registry
143     * using the key 'Zend_Translate'.
144     *
145     * @return Zend_Translate_Adapter|null
146     */
147    public function getTranslator()
148    {
149        if (null === $this->_translator) {
150            if (Zend_Registry::isRegistered('Zend_Translate')) {
151                $this->setTranslator(Zend_Registry::get('Zend_Translate'));
152            }
153        }
154        return $this->_translator;
155    }
156
157    /**
158     * Enables translation
159     *
160     * @return Zend_View_Helper_HeadTitle
161     */
162    public function enableTranslation()
163    {
164        $this->_translate = true;
165        return $this;
166    }
167
168    /**
169     * Disables translation
170     *
171     * @return Zend_View_Helper_HeadTitle
172     */
173    public function disableTranslation()
174    {
175        $this->_translate = false;
176        return $this;
177    }
178
179    /**
180     * Turn helper into string
181     *
182     * @param  string|null $indent
183     * @param  string|null $locale
184     * @return string
185     */
186    public function toString($indent = null, $locale = null)
187    {
188        $indent = (null !== $indent)
189                ? $this->getWhitespace($indent)
190                : $this->getIndent();
191
192        $items = array();
193
194        if($this->_translate && $translator = $this->getTranslator()) {
195            foreach ($this as $item) {
196                $items[] = $translator->translate($item, $locale);
197            }
198        } else {
199            foreach ($this as $item) {
200                $items[] = $item;
201            }
202        }
203
204        $separator = $this->getSeparator();
205        $output = '';
206        if(($prefix = $this->getPrefix())) {
207            $output  .= $prefix;
208        }
209        $output .= implode($separator, $items);
210        if(($postfix = $this->getPostfix())) {
211            $output .= $postfix;
212        }
213
214        $output = ($this->_autoEscape) ? $this->_escape($output) : $output;
215
216        return $indent . '<title>' . $output . '</title>';
217    }
218}
219