EXTMVC:
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / View / TX_EXTMVC_View_AbstractView.php
1 <?php
2
3 /* *
4 * This script belongs to the FLOW3 framework. *
5 * *
6 * It is free software; you can redistribute it and/or modify it under *
7 * the terms of the GNU Lesser General Public License as published by the *
8 * Free Software Foundation, either version 3 of the License, or (at your *
9 * option) any later version. *
10 * *
11 * This script is distributed in the hope that it will be useful, but *
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
13 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser *
14 * General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU Lesser General Public *
17 * License along with the script. *
18 * If not, see http://www.gnu.org/licenses/lgpl.html *
19 * *
20 * The TYPO3 project - inspiring people to share! *
21 * */
22
23 require_once(t3lib_extMgm::extPath('extmvc') . 'Classes/View/TX_EXTMVC_View_ViewInterface.php');
24
25 /**
26 * An abstract View
27 *
28 * @version $Id:$
29 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
30 */
31 abstract class TX_EXTMVC_View_AbstractView implements TX_EXTMVC_View_ViewInterface {
32
33 /**
34 * @var TX_EXTMVC_Request
35 */
36 protected $request;
37
38 /**
39 * @var array of TX_EXTMVC_View_Helper_HelperInterface
40 */
41 protected $viewHelpers;
42
43 /**
44 * @var array
45 */
46 protected $contextVariables = array();
47
48 /**
49 * @var string
50 */
51 protected $languagePath = 'Resources/Language/';
52
53 /**
54 * Local Language content
55 *
56 * @var string
57 **/
58 protected $LOCAL_LANG = array();
59
60 /**
61 * Local Language content charset for individual labels (overriding)
62 *
63 * @var string
64 **/
65 protected $LOCAL_LANG_charset = array();
66
67 /**
68 * Key of the language to use
69 *
70 * @var string
71 **/
72 protected $languageKey = 'default';
73
74 /**
75 * Pointer to alternative fall-back language to use
76 *
77 * @var string
78 **/
79 protected $LLkey = 'default';
80 var $altLLkey=''; // .
81 var $LLtestPrefix=''; // You can set this during development to some value that makes it easy for you to spot all labels that ARe delivered by the getLL function.
82 var $LLtestPrefixAlt=''; // Save as LLtestPrefix, but additional prefix for the alternative value in getLL() function calls
83
84 /**
85 * Constructs the view
86 */
87 public function __construct() {
88 }
89
90 /**
91 * Initializes the view after all dependencies have been injected
92 *
93 * @return void
94 */
95 public function initializeObject() {
96 $this->initializeView();
97 $this->initializeLocalization();
98 }
99
100 /**
101 * Sets the current request
102 *
103 * @param TX_EXTMVC_Request $request
104 * @return void
105 */
106 public function setRequest(TX_EXTMVC_Request $request) {
107 $this->request = $request;
108 }
109
110 /**
111 * Returns an View Helper instance.
112 * View Helpers must implement the interface TX_EXTMVC_View_Helper_HelperInterface
113 *
114 * @param string $viewHelperClassName the full name of the View Helper Class including
115 * @return TX_EXTMVC_View_Helper_HelperInterface The View Helper instance
116 */
117 public function getViewHelper($viewHelperClassName) {
118 if (!isset($this->viewHelpers[$viewHelperClassName])) {
119 $viewHelper = t3lib_div::makeInstance($viewHelperClassName);
120 if (!$viewHelper instanceof TX_EXTMVC_View_Helper_HelperInterface) {
121 throw new TX_EXTMVC_Exception_InvalidViewHelper('View Helpers must implement interface "TX_EXTMVC_View_Helper_HelperInterface"', 1222895456);
122 }
123 $viewHelper->setRequest($this->request);
124 $this->viewHelpers[$viewHelperClassName] = $viewHelper;
125 }
126 return $this->viewHelpers[$viewHelperClassName];
127 }
128
129 /**
130 * Initializes this view.
131 *
132 * Override this method for initializing your concrete view implementation.
133 *
134 * @return void
135 */
136 protected function initializeView() {
137 }
138
139 /**
140 * Loads local-language values by looking for a "locallang.php" file in the plugin class directory ($this->scriptRelPath) and if found includes it.
141 * Also locallang values set in the TypoScript property "_LOCAL_LANG" are merged onto the values found in the "locallang.php" file.
142 *
143 * @return void
144 */
145 protected function initializeLocalization() {
146 $languageFilePath = t3lib_extMgm::extPath(strtolower($this->request->getControllerExtensionKey())) . $this->languagePath . 'locallang.php';
147
148 if ($GLOBALS['TSFE']->config['config']['language']) {
149 $this->languageKey = $GLOBALS['TSFE']->config['config']['language'];
150 if ($GLOBALS['TSFE']->config['config']['language_alt']) {
151 $this->alternativeLanguageKey = $GLOBALS['TSFE']->config['config']['language_alt'];
152 }
153 }
154
155 // Read the strings in the required charset (since TYPO3 4.2)
156 $this->LOCAL_LANG = t3lib_div::readLLfile($languageFilePath, $this->languageKey, $GLOBALS['TSFE']->renderCharset);
157 if ($this->alternativeLanguageKey) {
158 $tempLOCAL_LANG = t3lib_div::readLLfile($languageFilePath, $this->alternativeLanguageKey);
159 $this->LOCAL_LANG = array_merge(is_array($this->LOCAL_LANG) ? $this->LOCAL_LANG : array(), $tempLOCAL_LANG);
160 }
161
162 // TODO Overlaying labels from TypoScript (including fictitious language keys for non-system languages!):
163 if (is_array($this->conf['_LOCAL_LANG.'])) {
164 reset($this->conf['_LOCAL_LANG.']);
165 while(list($k,$lA)=each($this->conf['_LOCAL_LANG.'])) {
166 if (is_array($lA)) {
167 $k = substr($k,0,-1);
168 foreach($lA as $llK => $llV) {
169 if (!is_array($llV)) {
170 $this->LOCAL_LANG[$k][$llK] = $llV;
171 // For labels coming from the TypoScript (database) the charset is assumed to be "forceCharset" and if that is not set, assumed to be that of the individual system languages
172 $this->LOCAL_LANG_charset[$k][$llK] = $GLOBALS['TYPO3_CONF_VARS']['BE']['forceCharset'] ? $GLOBALS['TYPO3_CONF_VARS']['BE']['forceCharset'] : $GLOBALS['TSFE']->csConvObj->charSetArray[$k];
173 }
174 }
175 }
176 }
177 }
178 }
179
180 /**
181 * Returns the localized label of the LOCAL_LANG key, $key
182 * Notice that for debugging purposes prefixes for the output values can be set with the internal vars ->LLtestPrefixAlt and ->LLtestPrefix
183 *
184 * @param string The key from the LOCAL_LANG array for which to return the value.
185 * @param string Alternative string to return IF no value is found set for the key, neither for the local language nor the default.
186 * @param boolean If true, the output label is passed through htmlspecialchars()
187 * @return string The value from LOCAL_LANG.
188 */
189 function translate($key, $default = '', $hsc=FALSE) {
190 // The "from" charset of csConv() is only set for strings from TypoScript via _LOCAL_LANG
191 if (isset($this->LOCAL_LANG[$this->languageKey][$key])) {
192 $translation = $GLOBALS['TSFE']->csConv($this->LOCAL_LANG[$this->languageKey][$key], $this->LOCAL_LANG_charset[$this->languageKey][$key]);
193 } elseif ($this->alternativeLanguageKey && isset($this->LOCAL_LANG[$this->alternativeLanguageKey][$key])) {
194 $translation = $GLOBALS['TSFE']->csConv($this->LOCAL_LANG[$this->alternativeLanguageKey][$key], $this->LOCAL_LANG_charset[$this->alternativeLanguageKey][$key]);
195 } elseif (isset($this->LOCAL_LANG['default'][$key])) {
196 $translation = $this->LOCAL_LANG['default'][$key]; // No charset conversion because default is english and thereby ASCII
197 } else {
198 $translation = $default;
199 }
200
201 return $hsc ? htmlspecialchars($translation) : $translation;
202 }
203
204 }
205
206 ?>