Skip to content
Snippets Groups Projects
TranslationLinksViewHelper.php 4.50 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\Utility\BackendUtility;
use TYPO3\CMS\Core\Imaging\Icon;
use TYPO3\CMS\Core\Imaging\IconFactory;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;

/**
 * Class EditOnClickViewHelper
 **/
class TranslationLinksViewHelper extends AbstractViewHelper {
	/**
	 * Renders the translation links for the specified record
	 *
	 * @param int $pageUid
	 * @param string $table
	 * @param int $uid
	 * @return string
	 * @throws \InvalidArgumentException
	 */
	public function render($pageUid = 0, $table, $uid) {
		$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) {
			/** @var IconFactory $iconFactory */
			$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 ($table === 'pages') {
				$translationParameters = '&edit[pages_language_overlay][' . $row['uid'] . ']=new';
				$translationParameters .= '&overrideVals[pages_language_overlay][doktype]=' . $row['doktype'];
				$translationParameters .= '&overrideVals[pages_language_overlay][sys_language_uid]=%s';
			}
			foreach ($languages as $languageUid => $language) {
				$translatedUid = 0;
				if ((int) $languageUid <= 0) {
					$translationTable = $table;
					$translatedUid = $uid;
				} else {
					$translationTable = $table === 'pages' ? 'pages_language_overlay' : $table;
					$translatedRows = BackendUtility::getRecordLocalization($table, $uid, $languageUid);
					if (count($translatedRows)) {
						$translatedUid = (int) $translatedRows[0]['uid'];
					}
				}
				if ($translatedUid) {
					$onClick = BackendUtility::editOnClick(
						'&edit[' . $translationTable . '][' . $translatedUid . ']=edit', '', -1
					);
					$out .= ' <a href="#" onclick="' . $onClick . '" title="' . $language['title'] . ' [' . $editLabel . ']" >' .
						$iconFactory->getIcon($language['flag'], Icon::SIZE_SMALL)->render() .
						'</a>';
				} else {
					if ($table === 'pages') {
						$onClick = BackendUtility::editOnClick(
							sprintf($translationParameters, $languageUid), '', -1
						);
					} else {
						$onClick = 'window.location.href=' . BackendUtility::getLinkToDataHandlerAction(
								sprintf($translationParameters, $languageUid), -1
							) . ' return false;';
					}
					$out .= ' <a href="#" onclick="' . $onClick . '" title="' . $language['title'] . ' [' . $newLabel . ']" >' .
						$iconFactory->getIcon($language['flag'], Icon::SIZE_SMALL, 'overlay-new')->render() .
						'</a>';
				}
			}
		}
		return $out;
	}
}