FileService.php 10.1 KB
Newer Older
Damjan's avatar
Damjan committed
1 2
<?php

3 4
namespace SGalinski\Lfeditor\Service;

Damjan's avatar
Damjan committed
5 6 7
/***************************************************************
 *  Copyright notice
 *
Stefan Galinski's avatar
Stefan Galinski committed
8
 *  (c) sgalinski Internet Services (https://www.sgalinski.de)
9
 *
Damjan's avatar
Damjan committed
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
Damjan's avatar
Damjan committed
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
Damjan's avatar
Damjan committed
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 32 33
use SGalinski\Lfeditor\Exceptions\LFException;
use SGalinski\Lfeditor\Utility\SgLib;
use SGalinski\Lfeditor\Utility\Typo3Lib;
use TYPO3\CMS\Core\Utility\GeneralUtility;

Damjan's avatar
Damjan committed
34 35 36
/**
 * include some general functions only usable for the 'lfeditor' module
 */
37
abstract class FileService extends AbstractService {
Damjan's avatar
Damjan committed
38 39 40
	/**
	 * @var array
	 */
Damjan's avatar
Damjan committed
41
	protected $localLang = array();
Damjan's avatar
Damjan committed
42 43

	/**
44 45 46
	 * Absolute address (origin) of language file
	 * which contains translation for language given as a key of the array.
	 *
Damjan's avatar
Damjan committed
47 48
	 * @var array
	 */
Damjan's avatar
Damjan committed
49
	protected $originLang = array();
Damjan's avatar
Damjan committed
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78

	/**
	 * @var string
	 */
	protected $absPath;

	/**
	 * @var string
	 */
	protected $relFile;

	/**
	 * @var string
	 */
	protected $absFile;

	/**
	 * @var string
	 */
	protected $fileType;

	/**
	 * @var string
	 */
	protected $workspace;

	/**
	 * @var array
	 */
79
	protected $meta = array();
Damjan's avatar
Damjan committed
80 81

	/**
82
	 * @param $editedLanguages
Damjan's avatar
Damjan committed
83 84
	 * @return mixed
	 */
85
	abstract protected function prepareFileContents($editedLanguages);
Damjan's avatar
Damjan committed
86 87 88 89

	/**
	 * @return mixed
	 */
90
	abstract public function readFile();
Damjan's avatar
Damjan committed
91 92 93 94 95 96

	/**
	 * sets some variables
	 *
	 * @param string $file filename or relative path from second param to the language file
	 * @param string $path absolute path to the extension or language file
Fabian Galinski's avatar
Fabian Galinski committed
97 98
	 * @param string $metaFile absolute path to the meta file (includes filename) DO NOT REMOVE! It's required in some
	 * implementations.
Damjan's avatar
Damjan committed
99 100
	 * @return void
	 */
101
	public function init($file, $path, $metaFile) {
102 103 104
		$this->setAbsPath($path);
		$this->setRelFile($file);
		$this->setAbsFile($this->absPath . '/' . $this->relFile);
Damjan's avatar
Damjan committed
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
	}

	/**
	 * sets information
	 *
	 * structure:
	 * $infos["absPath"] = absolute path to an extension or file
	 * $infos["relFile"] = relative path with filename from "absPath"
	 * $infos["workspace"] = workspace (base or xll)
	 * $infos["fileType"] = file type (php or xml)
	 * $infos["localLang"] = language data
	 * $infos["originLang"] = origin language array
	 * $infos["meta"] = meta data
	 *
	 * @param array $informations
	 * @return void
	 */
	public function setVar($informations) {
		// path and file information
		if (!empty($informations['absPath'])) {
125
			$this->absPath = Typo3Lib::fixFilePath($informations['absPath'] . '/');
Damjan's avatar
Damjan committed
126 127
		}
		if (!empty($informations['relFile'])) {
128
			$this->relFile = Typo3Lib::fixFilePath($informations['relFile']);
Damjan's avatar
Damjan committed
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 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
		}
		$this->absFile = $this->absPath . $this->relFile;

		// file type and workspace
		if (!empty($informations['workspace'])) {
			$this->workspace = $informations['workspace'];
		}
		if (!empty($informations['fileType'])) {
			$this->fileType = $informations['fileType'];
		}

		// data arrays
		if (!count($this->localLang) && is_array($informations['localLang'])) {
			$this->localLang = $informations['localLang'];
		}
		if (!count($this->originLang) && is_array($informations['originLang'])) {
			$this->originLang = $informations['originLang'];
		}
		if (!count($this->meta) && is_array($informations['meta'])) {
			$this->meta = $informations['meta'];
		}
	}

	/**
	 * returns requested information
	 *
	 * @param $info string
	 * @return string
	 */
	public function getVar($info) {
		$value = '';
		if ($info == 'relFile') {
			$value = $this->relFile;
		} elseif ($info == 'absPath') {
			$value = $this->absPath;
		} elseif ($info == 'absFile') {
			$value = $this->absFile;
		} elseif ($info == 'fileType') {
			$value = $this->fileType;
		} elseif ($info == 'workspace') {
			$value = $this->workspace;
		}

		return $value;
	}

	/**
	 * returns language data
	 *
	 * @param string $langKey valid language key
	 * @return array language data
	 */
	public function getLocalLangData($langKey = '') {
		if (empty($langKey)) {
			return $this->localLang;
		} elseif (is_array($this->localLang[$langKey])) {
			return $this->localLang[$langKey];
		} else {
Damjan's avatar
Damjan committed
187
			return array();
Damjan's avatar
Damjan committed
188 189 190 191 192 193 194 195 196 197 198 199 200
		}
	}

	/**
	 * deletes or sets constants in local language data
	 *
	 * @param string $constant constant name (if empty an index number will be used)
	 * @param string $value new value (if empty the constant will be deleted)
	 * @param string $langKey language shortcut
	 * @param boolean $forceDel set to true, if you want delete default values too
	 * @return void
	 */
	public function setLocalLangData($constant, $value, $langKey, $forceDel = FALSE) {
201
		if (!empty($value) || (($langKey === 'default' && !$forceDel))) {
Damjan's avatar
Damjan committed
202 203
			$this->localLang[$langKey][$constant] = $value;
		} elseif (isset($this->localLang[$langKey][$constant])) {
204 205 206 207 208 209 210
			if ($this->session->getDataByKey('editingMode') === 'override' &&
				isset($this->localLang[$langKey][$constant])
			) {
				$this->localLang[$langKey][$constant] = "";
			} else {
				unset($this->localLang[$langKey][$constant]);
			}
Damjan's avatar
Damjan committed
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
		}
	}

	/**
	 * returns origin
	 *
	 * @param string $langKey valid language key
	 * @return mixed an origin or full array
	 */
	public function getOriginLangData($langKey = '') {
		if (empty($langKey)) {
			return $this->originLang;
		} else {
			return $this->originLang[$langKey];
		}
	}

	/**
	 * sets new origin of a given language
	 *
	 * @param string $origin new origin
	 * @param string $langKey language shortcut
	 * @return void
	 */
	public function setOriginLangData($origin, $langKey) {
		if (!empty($origin)) {
			$this->originLang[$langKey] = $origin;
		}
	}

	/**
	 * returns meta data
	 *
	 * @param string $metaIndex special meta index
	 * @return mixed meta data
	 */
	public function getMetaData($metaIndex = '') {
		if (!empty($metaIndex)) {
			return $this->meta[$metaIndex];
		} else {
			return $this->meta;
		}
	}

	/**
	 * deletes or sets constants in meta data
	 *
	 * @param string $metaIndex
	 * @param string $value new value (if empty the meta index will be deleted)
	 * @return void
	 */
	public function setMetaData($metaIndex, $value) {
		if (!empty($value)) {
			$this->meta[$metaIndex] = $value;
		} elseif (isset($this->meta[$metaIndex])) {
			unset($this->meta[$metaIndex]);
		}
	}

	/**
	 * writes language files
	 *
273
	 * @param array | NULL $editedLanguages
Damjan's avatar
Damjan committed
274 275 276
	 * @throws LFException raised if a file cant be written
	 * @return void
	 */
277
	public function writeFile($editedLanguages = NULL) {
Damjan's avatar
Damjan committed
278
		// get prepared language files
279
		$languageFiles = $this->prepareFileContents($editedLanguages);
280 281
		$this->writeFilesWithContent($languageFiles);
	}
Damjan's avatar
Damjan committed
282

283 284 285 286 287
	/**
	 * Writes the given files with the given content.
	 *
	 * Array structure:
	 * array (
288 289
	 *        '/var/www/file.xlf' => 'My content',
	 *        ...
290 291 292 293 294 295
	 * )
	 *
	 * @param array $files
	 * @throws LFException
	 * @return void
	 */
296
	public function writeFilesWithContent(array $files = array()) {
Damjan's avatar
Damjan committed
297
		// check write permissions of all files
298
		foreach ($files as $file => $content) {
299
			if (!SgLib::checkWritePerms($file)) {
Damjan's avatar
Damjan committed
300 301 302 303 304
				throw new LFException('failure.file.badPermissions');
			}
		}

		// write files
305
		foreach ($files as $file => $content) {
306
			if (!GeneralUtility::writeFile($file, $content)) {
Damjan's avatar
Damjan committed
307 308 309 310
				throw new LFException('failure.file.notWritten');
			}
		}
	}
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340

	/**
	 * Writes generator meta tag.
	 *
	 * @return void
	 */
	protected function addGeneratorString() {
		$this->meta['generator'] = 'LFEditor';
	}

	/**
	 * Returns $absFile.
	 *
	 * @return string
	 */
	public function getAbsFile() {
		return $this->absFile;
	}

	/**
	 * Sets $absFile.
	 *
	 * @param string $absFile
	 * @return void
	 */
	public function setAbsFile($absFile) {
		$this->absFile = $absFile;
	}

	/**
341
	 * Returns $absPath - absolute path to an extension or file.
342 343 344 345 346 347 348 349
	 *
	 * @return string
	 */
	public function getAbsPath() {
		return $this->absPath;
	}

	/**
350
	 * Sets $absPath - absolute path to an extension or file.
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
	 *
	 * @param string $absPath
	 * @return void
	 */
	public function setAbsPath($absPath) {
		$this->absPath = $absPath;
	}

	/**
	 * Returns $fileType
	 *
	 * @return string
	 */
	public function getFileType() {
		return $this->fileType;
	}

	/**
	 * Sets $fileType.
	 *
	 * @param string $fileType
	 * @return void
	 */
	public function setFileType($fileType) {
		$this->fileType = $fileType;
	}

	/**
	 * Returns $locallang
	 *
	 * @return array
	 */
	public function getLocalLang() {
		return $this->localLang;
	}

	/**
	 * Sets $locallang.
	 *
	 * @param array $localLang
	 */
392
	public function setLocalLang(array $localLang) {
393 394 395 396
		$this->localLang = $localLang;
	}

	/**
397 398
	 * Returns meta data.
	 *
399 400 401 402 403 404 405
	 * @return array
	 */
	public function getMeta() {
		return $this->meta;
	}

	/**
406 407
	 * Sets meta data.
	 *
408 409
	 * @param array $meta
	 */
410
	public function setMeta(array $meta) {
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
		$this->meta = $meta;
	}

	/**
	 * Returns $originLang.
	 *
	 * @return array
	 */
	public function getOriginLang() {
		return $this->originLang;
	}

	/**
	 * Sets $originLang.
	 *
	 * @param array $originLang
	 */
428
	public function setOriginLang(array $originLang) {
429 430 431 432
		$this->originLang = $originLang;
	}

	/**
433 434
	 * Returns relFile - relative path with filename from "absPath".
	 *
435 436 437 438 439 440 441
	 * @return string
	 */
	public function getRelFile() {
		return $this->relFile;
	}

	/**
442 443
	 * Sets relFile - relative path with filename from "absPath".
	 *
444 445 446 447 448 449 450
	 * @param string $relFile
	 */
	public function setRelFile($relFile) {
		$this->relFile = $relFile;
	}

	/**
451 452
	 * Returns workspace.
	 *
453 454 455 456 457 458 459
	 * @return string
	 */
	public function getWorkspace() {
		return $this->workspace;
	}

	/**
460 461
	 * Sets workspace.
	 *
462 463 464 465 466
	 * @param string $workspace
	 */
	public function setWorkspace($workspace) {
		$this->workspace = $workspace;
	}
Damjan's avatar
Damjan committed
467 468
}

Stefan Galinski's avatar
Stefan Galinski committed
469
?>