FileBaseService.php 5.32 KB
Newer Older
1
2
<?php

3
4
namespace SGalinski\Lfeditor\Service;

5
6
7
/***************************************************************
 *  Copyright notice
 *
8
9
 *  (c) sgalinski Internet Services (http://www.sgalinski.de)
 *
10
11
12
13
14
 *  All rights reserved
 *
 *  This script is part of the TYPO3 project. The TYPO3 project is
 *  free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
15
 *  the Free Software Foundation; either version 3 of the License, or
16
17
18
19
20
21
22
 *  (at your option) any later version.
 *
 *  The GNU General Public License can be found at
 *  http://www.gnu.org/copyleft/gpl.html.
 *
 *  This script is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
25
26
27
28
 *  GNU General Public License for more details.
 *
 *  This copyright notice MUST APPEAR in all copies of the script!
 ***************************************************************/

29
30
31
use SGalinski\Lfeditor\Exceptions\LFException;
use SGalinski\Lfeditor\Utility\SgLib;
use TYPO3\CMS\Core\Localization\Locales;
32
use TYPO3\CMS\Core\Utility\ArrayUtility;
33
use TYPO3\CMS\Core\Utility\GeneralUtility;
34
35
36
37

/**
 * base workspace class
 */
Damjan's avatar
Damjan committed
38
abstract class FileBaseService extends FileService {
39
40
41
42
43
44
45
46
47
48
49
50
	/**
	 * @param string $content
	 * @param string $lang
	 * @return mixed
	 */
	abstract protected function getLocalizedFile($content, $lang);

	/**
	 * @param string $filename
	 * @param string $langKey
	 * @return mixed
	 */
51
	abstract public function checkLocalizedFile($filename, $langKey);
52
53
54
55
56

	/**
	 * @param string $langKey
	 * @return mixed
	 */
57
	abstract public function nameLocalizedFile($langKey);
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74

	/**
	 * @param string $file
	 * @param string $langKey
	 * @return mixed
	 */
	abstract protected function readLLFile($file, $langKey);

	/**
	 * extended init
	 *
	 * @throws LFException
	 * @param string $file name of the file (can be a path, if you need this (no check))
	 * @param string $path path to the file
	 * @return void
	 */
	public function init($file, $path) {
75
76
77
		if (class_exists('TYPO3\CMS\Core\Localization\Locales')) {
			/** @var $locales Locales */
			$locales = GeneralUtility::makeInstance('TYPO3\CMS\Core\Localization\Locales');
78
79
			$availableLanguages = implode('|', $locales->getLocales());
		} else {
Damjan's avatar
Damjan committed
80
			$availableLanguages = TYPO3_languages;
81
82
83
84
85
86
87
		}

		// localization files should not be edited
		if ($this->checkLocalizedFile(basename($file), $availableLanguages)) {
			throw new LFException('failure.langfile.notSupported');
		}

88
		$this->setWorkspace('base');
89
		parent::init($file, $path);
90
91

		$this->prepareLanguageFilesForFirstUsage();
92
93
94
95
96
97
98
99
100
101
	}

	/**
	 * reads the absolute language file with all localized sub files
	 *
	 * @throws LFException
	 * @return void
	 */
	public function readFile() {
		// read absolute file
102
		$localLang = $this->readLLFile($this->absFile, 'default');
103
		// loop all languages
104
		$languages = SgLib::getSystemLanguages();
Damjan's avatar
Damjan committed
105
		$originLang = array();
106
107
		foreach ($languages as $lang) {
			$originLang[$lang] = $this->absFile;
108
			if ((is_array($localLang[$lang]) && count($localLang[$lang])) || $lang === 'default') {
109
110
111
112
113
114
115
116
117
118
				if (is_array($localLang[$lang]) && count($localLang[$lang])) {
					ksort($localLang[$lang]);
				}
				continue;
			}

			// get localized file
			$lFile = $this->getLocalizedFile($localLang[$lang], $lang);
			if ($this->checkLocalizedFile(basename($lFile), $lang)) {
				$originLang[$lang] = $lFile;
Damjan's avatar
Damjan committed
119
				$localLang[$lang] = array();
120
121
122
123
124
125
126
127
128
129
130
131
132

				if (!is_file($lFile)) {
					continue;
				}

				// read the content
				try {
					$llang = $this->readLLFile($lFile, $lang);
				} catch (LFException $e) {
					throw $e;
				}

				// merge arrays and save origin of current language
133
				ArrayUtility::mergeRecursiveWithOverrule($localLang, $llang);
134
135
136
137
			}
		}

		// check
138
		if (!is_array($localLang)) {
139
140
141
142
143
144
145
			throw new LFException('failure.search.noFileContent');
		}

		// copy all to object variables, if everything was ok
		$this->localLang = $localLang;
		$this->originLang = $originLang;
	}
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194

	/**
	 * Prepares this language file and it's translations for the first usage.
	 *
	 * @throws LFException
	 * @return void
	 */
	protected function prepareLanguageFilesForFirstUsage() {
		$adaptedContent = $this->checkFileForFirstImport($this->absFile);
		if ($adaptedContent) {
			$files = [];
			$files[$this->absFile] = $adaptedContent;

			$languages = SgLib::getSystemLanguages();
			foreach ($languages as $lang) {
				if ($lang === 'default') {
					continue;
				}

				$file = dirname($this->absFile) . '/' . $this->nameLocalizedFile($lang);
				if (file_exists($file) && $this->checkLocalizedFile(basename($file), $lang)) {
					$adaptedContent = $this->checkFileForFirstImport($file);
					if ($adaptedContent) {
						$files[$file] = $adaptedContent;
					}
				}
			}

			$this->writeFilesWithContent($files);
		}
	}

	/**
	 * Checks if the given file is generated by this extension, if not the data will be prepared and returned.
	 *
	 * @param string $file
	 * @return bool|string
	 */
	protected function checkFileForFirstImport($file) {
		$fileContent = file_get_contents($file);
		if (strpos($fileContent, '<generator>LFEditor</generator>') === FALSE) {
			$fileContent = str_replace('<![CDATA[', '&lt;![CDATA[', $fileContent);
			$fileContent = str_replace(']]>', ']]&gt;', $fileContent);
		} else {
			return FALSE;
		}

		return $fileContent;
	}
195
196
197
}

?>