Skip to content
Snippets Groups Projects
TranslationLinksViewHelper.php 5.23 KiB
<?php

namespace SGalinski\SgNews\ViewHelpers\Backend;

/***************************************************************
 *  Copyright notice
 *
 *  (c) sgalinski Internet Services (https://www.sgalinski.de)
 *
 *  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
 *  the Free Software Foundation; either version 3 of the License, or
 *  (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
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  This copyright notice MUST APPEAR in all copies of the script!
 ***************************************************************/

use SGalinski\SgNews\Service\LicensingService;
use SGalinski\SgNews\Utility\BackendNewsUtility;
use SGalinski\SgNews\ViewHelpers\AbstractViewHelper;
use TYPO3\CMS\Backend\Routing\UriBuilder;
use TYPO3\CMS\Backend\Utility\BackendUtility;
use TYPO3\CMS\Core\Imaging\Icon;
use TYPO3\CMS\Core\Imaging\IconFactory;
use TYPO3\CMS\Core\Type\Icon\IconState;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\VersionNumberUtility;
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;

/**
 * Class EditOnClickViewHelper
 **/
class TranslationLinksViewHelper extends AbstractViewHelper {

	/**
	 * Register the ViewHelp0er arguments
	 */
	public function initializeArguments() {
		parent::initializeArguments();
		$this->registerArgument('uid', 'int', 'The uid of the record', TRUE);
		$this->registerArgument('table', 'string', 'The table of the record', TRUE);
		$this->registerArgument('pageUid', 'int', 'The uid of the page', FALSE, 0);
	}

	/**
	 * Renders the translation links for the specified record
	 *
	 * @return string
	 * @throws \InvalidArgumentException
	 */
	public function render(): string {
		$uid = $this->arguments['uid'];
		$table = $this->arguments['table'];
		$pageUid = $this->arguments['pageUid'];
		$out = '';
		if (!LicensingService::checkKey()) {
			return $out;
		}
		$table = trim($table);
		if ($table !== 'pages' && !isset($GLOBALS['TCA'][$table]['ctrl']['languageField'])) {
			return $out;
		}

		$uid = (int) $uid;
		$pageUid = (int) $pageUid;
		$row = BackendUtility::getRecord($table, $uid);
		if ($row) {
			$iconFactory = GeneralUtility::makeInstance(IconFactory::class);
			$lanuageField = $GLOBALS['TCA'][$table]['ctrl']['languageField'];
			$currentLanguageUid = $table === 'pages' ? 0 : $row[$lanuageField];
			if ($currentLanguageUid === -1) {
				$languages = [
					-1 => [
						'title' => LocalizationUtility::translate('backend.language.allLanguages', 'SgNews'),
						'flag' => 'flags-multiple'
					]
				];
			} else {
				$languages = BackendNewsUtility::getAvailableLanguages($pageUid);
			}

			$editLabel = LocalizationUtility::translate('backend.action.edit', 'SgNews');
			$newLabel = LocalizationUtility::translate('backend.action.new', 'SgNews');
			$translationParameters = '&cmd[' . $table . '][' . $row['uid'] . '][localize]=%s';
			if (version_compare(VersionNumberUtility::getCurrentTypo3Version(), '9.0.0', '<')) {
				$returnUrl = BackendUtility::getModuleUrl('web_SgNewsNews', ['id' => GeneralUtility::_GP('id')]);
			} else {
				$returnUrl = GeneralUtility::makeInstance(UriBuilder::class)
					->buildUriFromRoute('web_SgNewsNews', ['id' => GeneralUtility::_GP('id')]);
			}

			foreach ($languages as $languageUid => $language) {
				$translatedUid = 0;
				if ((int) $languageUid <= 0) {
					$translationTable = $table;
					$translatedUid = $uid;
				} else {
					$translationTable = $table;
					$translatedRows = BackendUtility::getRecordLocalization($table, $uid, $languageUid);
					if ($translatedRows) {
						$translatedUid = (int) $translatedRows[0]['uid'];
					}
				}

				if ($translatedUid) {
					if (version_compare(VersionNumberUtility::getCurrentTypo3Version(), '9.0.0', '<')) {
						$link = BackendUtility::getModuleUrl('record_edit', [
							'edit' => [
								$translationTable => [
									$translatedUid => 'edit'
								]
							],
							'returnUrl' => $returnUrl
						]);
					} else {
						$link = GeneralUtility::makeInstance(UriBuilder::class)
							->buildUriFromRoute('record_edit', [
								'edit' => [
									$translationTable => [
										$translatedUid => 'edit'
									]
								],
								'returnUrl' => (string) $returnUrl
							]);
					}

					$out .= ' <a href="' . $link . '" title="'. $language['title'] . ' [' . $editLabel . ']" >'
						. $iconFactory->getIcon($language['flag'], Icon::SIZE_SMALL)->render()
						. '</a>';
				} else {
					$out .= ' <a href="'
						. BackendUtility::getLinkToDataHandlerAction(
							sprintf($translationParameters, $languageUid), $returnUrl
						)
						. '" title="' . $language['title'] . ' [' . $newLabel . ']" >'
						. $iconFactory->getIcon(
							$language['flag'],
							Icon::SIZE_SMALL,
							'overlay-new',
							IconState::cast(IconState::STATE_DISABLED)
						)->render()
						. '</a>';
				}
			}
		}

		return $out;
	}
}