<?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\ViewHelpers\AbstractViewHelper;
use TYPO3\CMS\Backend\Clipboard\Clipboard;
use TYPO3\CMS\Backend\Utility\BackendUtility;
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
use TYPO3\CMS\Core\Imaging\Icon;
use TYPO3\CMS\Core\Imaging\IconFactory;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
use TYPO3\CMS\Recordlist\RecordList\DatabaseRecordList;

/**
 * Class ControlViewHelper
 **/
class ControlViewHelper extends AbstractViewHelper {
	/**
	 * Renders the control buttons for the specified record
	 *
	 * @param string $table
	 * @param mixed $row
	 * @param array $sortingData
	 * @param boolean $clipboard
	 * @return string
	 * @throws \InvalidArgumentException
	 * @throws \UnexpectedValueException
	 */
	public function render($table, $row, array $sortingData = [], $clipboard = FALSE) {
		if (!is_array($row)) {
			$row = BackendUtility::getRecord($table, $row->getUid());
		}
		/** @var DatabaseRecordList $databaseRecordList */
		$databaseRecordList = GeneralUtility::makeInstance(DatabaseRecordList::class);
		/** @var BackendUserAuthentication $backendUser */
		$backendUser = $GLOBALS['BE_USER'];
		$pageInfo = BackendUtility::readPageAccess($row['pid'], $backendUser->getPagePermsClause(1));
		$databaseRecordList->calcPerms = $GLOBALS['BE_USER']->calcPerms($pageInfo);
		$databaseRecordList->currentTable = $sortingData;
		$out = $databaseRecordList->makeControl($table, $row);
		if ($clipboard) {
			$databaseRecordList->MOD_SETTINGS['clipBoard'] = TRUE;
			$databaseRecordList->clipObj = GeneralUtility::makeInstance(Clipboard::class);
			$databaseRecordList->clipObj->initializeClipboard();
			$GLOBALS['SOBE'] = $databaseRecordList;
			$out .= $databaseRecordList->makeClip($table, $row);
		}
		if ($table === 'pages' && LicensingService::checkKey()) {
			$rootline = BackendUtility::BEgetRootLine($row['uid'], '', TRUE);
			ksort($rootline);
			$path = '/root';
			foreach ($rootline as $page) {
				$path .= '/p' . dechex($page['uid']);
			}

			/** @var IconFactory $iconFactory */
			$iconFactory = GeneralUtility::makeInstance(IconFactory::class);
			$out .= ' <div class="btn-group" role="group">
						<a href="#" onclick="return sgNewsGoToPageModule(' . $row['uid'] . ', \'' . $path . '\');" class="btn btn-default" title="' . LocalizationUtility::translate(
					'backend.button.editPageContent', 'SgNews'
				) . '">' .
				$iconFactory->getIcon('actions-document-open', Icon::SIZE_SMALL)->render() .
				'</a>
					</div>';
		}
		return $out;
	}
}