3647f9b98127e4bc4ce98f13824a4578681b3570
[TYPO3CMS/Extensions/static_info_tables.git] / Classes / Controller / ManagerController.php
1 <?php
2 namespace SJBR\StaticInfoTables\Controller;
3 use \SJBR\StaticInfoTables\Domain\Model\Country;
4 use \SJBR\StaticInfoTables\Domain\Model\CountryZone;
5 use \SJBR\StaticInfoTables\Domain\Model\Language;
6 use \TYPO3\CMS\Core\Utility\VersionNumberUtility;
7 /***************************************************************
8 * Copyright notice
9 *
10 * (c) 2013-2014 Stanislas Rolland <typo3@sjbr.ca>
11 * All rights reserved
12 *
13 * This script is part of the TYPO3 project. The TYPO3 project is
14 * free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * The GNU General Public License can be found at
20 * http://www.gnu.org/copyleft/gpl.html.
21 * A copy is found in the textfile GPL.txt and important notices to the license
22 * from the author is found in LICENSE.txt distributed with these scripts.
23 *
24 *
25 * This script is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
29 *
30 * This copyright notice MUST APPEAR in all copies of the script!
31 ***************************************************************/
32 /**
33 * Static Info Tables Manager controller
34 *
35 * @author Stanislas Rolland <typo3@sjbr.ca>
36 */
37 class ManagerController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionController {
38
39 /**
40 * @var string Name of the extension this controller belongs to
41 */
42 protected $extensionName = 'StaticInfoTables';
43
44 /**
45 * @var \SJBR\StaticInfoTables\Domain\Repository\CountryRepository
46 */
47 protected $countryRepository;
48
49 /**
50 * Dependency injection of the Country Repository
51 *
52 * @param \SJBR\StaticInfoTables\Domain\Repository\CountryRepository $countryRepository
53 * @return void
54 */
55 public function injectCountryRepository(\SJBR\StaticInfoTables\Domain\Repository\CountryRepository $countryRepository) {
56 $this->countryRepository = $countryRepository;
57 }
58
59 /**
60 * @var \SJBR\StaticInfoTables\Domain\Repository\CountryZoneRepository
61 */
62 protected $countryZoneRepository;
63
64 /**
65 * Dependency injection of the Country Zone Repository
66 *
67 * @param \SJBR\StaticInfoTables\Domain\Repository\CountryZoneRepository $countryZoneRepository
68 * @return void
69 */
70 public function injectCountryZoneRepository(\SJBR\StaticInfoTables\Domain\Repository\CountryZoneRepository $countryZoneRepository) {
71 $this->countryZoneRepository = $countryZoneRepository;
72 }
73
74 /**
75 * @var \SJBR\StaticInfoTables\Domain\Repository\CurrencyRepository
76 */
77 protected $currencyRepository;
78
79 /**
80 * Dependency injection of the Currency Repository
81 *
82 * @param \SJBR\StaticInfoTables\Domain\Repository\CurrencyRepository $currencyRepository
83 * @return void
84 */
85 public function injectCurrencyRepository(\SJBR\StaticInfoTables\Domain\Repository\CurrencyRepository $currencyRepository) {
86 $this->currencyRepository = $currencyRepository;
87 }
88
89 /**
90 * @var \SJBR\StaticInfoTables\Domain\Repository\LanguageRepository
91 */
92 protected $languageRepository;
93
94 /**
95 * Dependency injection of the Language Repository
96 *
97 * @param \SJBR\StaticInfoTables\Domain\Repository\LanguageRepository $languageRepository
98 * @return void
99 */
100 public function injectLanguageRepository(\SJBR\StaticInfoTables\Domain\Repository\LanguageRepository $languageRepository) {
101 $this->languageRepository = $languageRepository;
102 }
103
104 /**
105 * @var \SJBR\StaticInfoTables\Domain\Repository\TerritoryRepository
106 */
107 protected $territoryRepository;
108
109 /**
110 * Dependency injection of the Territory Repository
111 *
112 * @param \SJBR\StaticInfoTables\Domain\Repository\TerritoryRepository $territoryRepository
113 * @return void
114 */
115 public function injectTerritoryRepository(\SJBR\StaticInfoTables\Domain\Repository\TerritoryRepository $territoryRepository) {
116 $this->territoryRepository = $territoryRepository;
117 }
118
119 /**
120 * Creates a Message object and adds it to the FlashMessageQueue.
121 *
122 * @param string $messageBody The message
123 * @param string $messageTitle Optional message title
124 * @param integer $severity Optional severity, must be one of \TYPO3\CMS\Core\Messaging\FlashMessage constants
125 * @param boolean $storeInSession Optional, defines whether the message should be stored in the session (default) or not
126 * @return void
127 * @throws \InvalidArgumentException if the message body is no string
128 * @see \TYPO3\CMS\Core\Messaging\FlashMessage
129 * @api
130 */
131 public function addFlashMessage($messageBody, $messageTitle = '', $severity = \TYPO3\CMS\Core\Messaging\AbstractMessage::OK, $storeInSession = TRUE) {
132 if (VersionNumberUtility::convertVersionNumberToInteger(VersionNumberUtility::getNumericTypo3Version()) >= 6002000) {
133 parent::addFlashMessage($messageBody, $messageTitle, $severity, $storeInSession);
134 } else {
135 $this->flashMessageContainer->add($messageTitle, $messageBody, $severity);
136 }
137 }
138
139 /**
140 * Display general information
141 *
142 * @return string An HTML display of data overview
143 */
144 public function informationAction() {
145 $this->view->assign('actions',
146 array(
147 array(
148 'code' => 'newLanguagePack',
149 'title' => 'createLanguagePackTitle',
150 'description' => 'createLanguagePackDescription'
151 ),
152 array(
153 'code' => 'testForm',
154 'title' => 'testFormTitle',
155 'description' => 'testFormDescription'
156 ),
157 array(
158 'code' => 'sqlDumpNonLocalizedData',
159 'title' => 'sqlDumpNonLocalizedDataTitle',
160 'description' => 'sqlDumpNonLocalizedDataDescription'
161 )
162 )
163 );
164 }
165
166 /**
167 * Display the language pack creation form
168 *
169 * @param \SJBR\StaticInfoTables\Domain\Model\LanguagePack $languagePack
170 * @return string An HTML form for creating a language pack
171 */
172 public function newLanguagePackAction(\SJBR\StaticInfoTables\Domain\Model\LanguagePack $languagePack = NULL) {
173 if (!is_object($languagePack)) {
174 $languagePack = $this->objectManager->create('SJBR\\StaticInfoTables\\Domain\\Model\\LanguagePack');
175 }
176 $languagePack->setVersion($GLOBALS['TYPO3_CONF_VARS']['EXTCONF'][\TYPO3\CMS\Core\Utility\GeneralUtility::camelCaseToLowerCaseUnderscored($this->extensionName)]['version']);
177 $languagePack->setAuthor($GLOBALS['BE_USER']->user['realName']);
178 $languagePack->setAuthorEmail($GLOBALS['BE_USER']->user['email']);
179 $localeUtility = $this->objectManager->get('SJBR\\StaticInfoTables\\Utility\\LocaleUtility');
180 $this->view->assign('locales', $localeUtility->getLocales());
181 $this->view->assign('languagePack', $languagePack);
182 }
183
184 /**
185 * Creation/update a language pack for the Static Info Tables
186 *
187 * @param \SJBR\StaticInfoTables\Domain\Model\LanguagePack $languagePack
188 * @return string An HTML display of data overview
189 */
190 public function createLanguagePackAction(\SJBR\StaticInfoTables\Domain\Model\LanguagePack $languagePack) {
191 // Add the localization columns
192 $locale = $languagePack->getLocale();
193 // Get the English name of the locale
194 $localeUtility = $this->objectManager->get('SJBR\\StaticInfoTables\\Utility\\LocaleUtility');
195 $language = $localeUtility->getLanguageFromLocale($locale);
196 $languagePack->setLanguage($language);
197 $languagePack->setTypo3VersionRange($GLOBALS['TYPO3_CONF_VARS']['EXTCONF'][\TYPO3\CMS\Core\Utility\GeneralUtility::camelCaseToLowerCaseUnderscored($this->extensionName)]['constraints']['depends']['typo3']);
198 // If version is not set, use the version of the base extension
199 if (!$languagePack->getVersion()) {
200 $languagePack->setVersion($GLOBALS['TYPO3_CONF_VARS']['EXTCONF'][\TYPO3\CMS\Core\Utility\GeneralUtility::camelCaseToLowerCaseUnderscored($this->extensionName)]['version']);
201 }
202 $this->countryRepository->addLocalizationColumns($locale);
203 $this->countryZoneRepository->addLocalizationColumns($locale);
204 $this->currencyRepository->addLocalizationColumns($locale);
205 $this->languageRepository->addLocalizationColumns($locale);
206 $this->territoryRepository->addLocalizationColumns($locale);
207 // Store the Language Pack
208 $languagePackRepository = $this->objectManager->get('SJBR\\StaticInfoTables\\Domain\\Repository\\LanguagePackRepository');
209 $messages = $languagePackRepository->writeLanguagePack($languagePack);
210 if (count($messages)) {
211 foreach ($messages as $message) {
212 $this->addFlashMessage($message, '', \TYPO3\CMS\Core\Messaging\AbstractMessage::OK);
213 }
214 }
215 $this->forward('information');
216 }
217
218 /**
219 * Display a test form
220 *
221 * @param Country $country
222 * @param CountryZone $countryZone
223 * @param Language $language
224 * @return string An HTML form
225 */
226 public function testFormAction(Country $country = NULL, CountryZone $countryZone = NULL, Language $language = NULL) {
227 if (is_object($country) && (is_object($countryZone) || !$this->countryZoneRepository->findByCountry($country)->count())) {
228 $this->forward('testFormResult', 'Manager', $this->extensionName, array('country' => $country, 'countryZone' => $countryZone, 'language' => $language));
229 }
230 $countries = $this->countryRepository->findAllOrderedBy('nameLocalized');
231 if (is_object($country)) {
232 $countryZones = $this->countryZoneRepository->findByCountry($country);
233 $selectedCountry = $country->getUid();
234 $this->view->assign('selectedCountry', $country->getUid());
235 } else {
236 $countryZones = array();
237 }
238 $languages = $this->languageRepository->findAllNonConstructedNonSacred()->toArray();
239 if (is_object($language)) {
240 $this->view->assign('selectedLanguage', $language->getUid());
241 }
242 $this->view->assign('countries', $countries);
243 $this->view->assign('countryZones', $countryZones);
244 if (is_object($countryZone)) {
245 $this->view->assign('selectedCountryZone', $countryZone->getUid());
246 }
247 $this->view->assign('languages', $languages);
248 }
249
250 /**
251 * Display the test form result
252 *
253 * @param Country $country
254 * @param CountryZone $countryZone
255 * @param Language $language
256 * @return string HTML code presenting the localized data
257 */
258 public function testFormResultAction(Country $country = NULL, CountryZone $countryZone = NULL, Language $language = NULL) {
259 $this->view->assign('country', $country);
260 $currencies = $this->currencyRepository->findByCountry($country);
261 if ($currencies->count()) {
262 $this->view->assign('currency', $currencies[0]);
263 }
264 if (is_object($countryZone)) {
265 $this->view->assign('countryZone', $countryZone);
266 }
267 $this->view->assign('language', $language);
268 $territories = $this->territoryRepository->findByCountry($country);
269 if ($territories->count()) {
270 $this->view->assign('territory', $territories[0]);
271 }
272 }
273
274 /**
275 * Creation/update a language pack for the Static Info Tables
276 *
277 * @return string An HTML display of data overview
278 */
279 public function sqlDumpNonLocalizedDataAction() {
280 // Create a SQL dump of non-localized data
281 $dumpContent = array();
282 $dumpContent[] = $this->countryRepository->sqlDumpNonLocalizedData();
283 $dumpContent[] = $this->countryZoneRepository->sqlDumpNonLocalizedData();
284 $dumpContent[] = $this->currencyRepository->sqlDumpNonLocalizedData();
285 $dumpContent[] = $this->languageRepository->sqlDumpNonLocalizedData();
286 $dumpContent[] = $this->territoryRepository->sqlDumpNonLocalizedData();
287 // Write the SQL dump file
288 $extensionKey = \TYPO3\CMS\Core\Utility\GeneralUtility::camelCaseToLowerCaseUnderscored($this->extensionName);
289 $extensionPath = \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath($extensionKey);
290 $filename = 'export-ext_tables_static+adt.sql';
291 \TYPO3\CMS\Core\Utility\GeneralUtility::writeFile($extensionPath . $filename, implode(LF, $dumpContent));
292 $message = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('sqlDumpCreated', $this->extensionName) . ' ' . $extensionPath . $filename;
293 $this->addFlashMessage($message, '', \TYPO3\CMS\Core\Messaging\AbstractMessage::OK);
294 $this->forward('information');
295 }
296
297 /**
298 * Get the typo3-supported locale options for the language pack creation
299 *
300 * @return array An array of language objects
301 */
302 protected function getLocales() {
303 $localeArray = array();
304 $locales = $this->objectManager->get('TYPO3\\CMS\\Core\\Localization\\Locales');
305 $languages = $locales->getLanguages();
306 foreach ($languages as $locale => $language) {
307 // No language pack for English
308 if ($locale != 'default') {
309 $languageObject = $this->objectManager->create('SJBR\\StaticInfoTables\\Domain\\Model\\Language');
310 $languageObject->setCollatingLocale($locale);
311 $localizedLanguage = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('lang_' . $locale, 'Lang');
312 $label = ($localizedLanguage ? $localizedLanguage : $language) . ' (' . $locale . ')';
313 $languageObject->setNameEn($label);
314 $localeArray[$label] = $languageObject;
315 }
316 }
317 ksort($localeArray);
318 return $localeArray;
319 }
320
321 /**
322 * Get language name from locale
323 *
324 * @param string $locale
325 * @return string Language name
326 */
327 protected function getLanguageFromLocale($locale) {
328 $locales = $this->objectManager->get('TYPO3\\CMS\\Core\\Localization\\Locales');
329 $languages = $locales->getLanguages();
330 $language = $languages[$locale];
331 return $language . ' (' . $locale . ')';
332 }
333
334 }
335 class_alias('SJBR\StaticInfoTables\Controller\ManagerController', 'Tx_StaticInfoTables_Controller_ManagerController');
336 ?>