Skip to content
Snippets Groups Projects
BackendController.php 10.2 KiB
Newer Older

/***************************************************************
 *  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!
 ***************************************************************/

Paul Ilea's avatar
Paul Ilea committed
use SGalinski\SgNews\Service\LicensingService;
use SGalinski\SgNews\Utility\BackendNewsUtility;
Paul Ilea's avatar
Paul Ilea committed
use TYPO3\CMS\Backend\Clipboard\Clipboard;
use TYPO3\CMS\Backend\Template\Components\ButtonBar;
use TYPO3\CMS\Backend\Template\Components\DocHeaderComponent;
use TYPO3\CMS\Backend\Utility\BackendUtility;
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
Paul Ilea's avatar
Paul Ilea committed
use TYPO3\CMS\Core\Imaging\Icon;
use TYPO3\CMS\Core\Imaging\IconFactory;
use TYPO3\CMS\Core\Page\PageRenderer;
use TYPO3\CMS\Core\Utility\GeneralUtility;
Paul Ilea's avatar
Paul Ilea committed
use TYPO3\CMS\Core\Utility\VersionNumberUtility;
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
Paul Ilea's avatar
Paul Ilea committed
use TYPO3\CMS\Extbase\Mvc\View\ViewInterface;
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
class BackendController extends ActionController {
Paul Ilea's avatar
Paul Ilea committed
	 * The uid of the current page
Paul Ilea's avatar
Paul Ilea committed
	 * @var int
Paul Ilea's avatar
Paul Ilea committed
	private $pageUid = 0;
Paul Ilea's avatar
Paul Ilea committed
	 * The info fields of the current page
	 *
	 * @var array
Paul Ilea's avatar
Paul Ilea committed
	private $pageInfo = [];
Paul Ilea's avatar
Paul Ilea committed
	 * The uid of the current root page
	 *
	 * @var int
Paul Ilea's avatar
Paul Ilea committed
	private $rootPageUid = 0;
Paul Ilea's avatar
Paul Ilea committed
	 * Command array on the form [tablename][uid][command] = value.
	 * This array may get additional data set internally based on clipboard commands send in CB var!
	 *
	 * @var array
Paul Ilea's avatar
Paul Ilea committed
	private $command;
Paul Ilea's avatar
Paul Ilea committed
	 * Clipboard command array. May trigger changes in "cmd"
	 *
	 * @var array
	 */
	private $clipboardCommandArray;

	/**
	 * Currently selected language
	 *
	 * @var int
	 */
	public $language;

	/**
	 * DocHeaderComponent
	 *
	 * @var DocHeaderComponent
	 */
	private $docHeaderComponent;

	/**
	 * Initialize action for all actions
	 *
	 * @throws \InvalidArgumentException
Paul Ilea's avatar
Paul Ilea committed
	 */
	public function initializeAction() {
		$this->command = GeneralUtility::_GP('cmd');
		$this->clipboardCommandArray = GeneralUtility::_GP('CB');
		$this->initClipboard();
	}

	/**
	 * Initializes the view before invoking an action method.
	 *
	 * Override this method to solve assign variables common for all actions
	 * or prepare the view in another way before the action is called.
	 *
	 * @param ViewInterface $view The view to be initialized
	 *
	 * @return void
	 * @throws \UnexpectedValueException
Paul Ilea's avatar
Paul Ilea committed
	 * @throws \InvalidArgumentException
	 * @api
Paul Ilea's avatar
Paul Ilea committed
	protected function initializeView(ViewInterface $view) {
		$GLOBALS['LANG']->includeLLFile('EXT:core/Resources/Private/Language/locallang_mod_web_list.xlf');
		// create doc header component
Paul Ilea's avatar
Paul Ilea committed
		$this->pageUid = (int) GeneralUtility::_GP('id');
		/** @var BackendUserAuthentication $backendUser */
		$backendUser = $GLOBALS['BE_USER'];
Paul Ilea's avatar
Paul Ilea committed
		$this->pageInfo = BackendUtility::readPageAccess($this->pageUid, $backendUser->getPagePermsClause(1));
		if ($this->pageInfo) {
			$this->docHeaderComponent = GeneralUtility::makeInstance(DocHeaderComponent::class);
			if ($this->pageUid) {
				$this->rootPageUid = BackendNewsUtility::getRootUidByPageUid($this->pageUid);
			}

			/** @var BackendUserAuthentication $backendUser */
			$backendUser = $GLOBALS['BE_USER'];

			$menuSettings = GeneralUtility::_GP('SET') ?: [];
			foreach ($menuSettings as $key => $menuSetting) {
				$backendUser->pushModuleData('tools_beuser/index.php/web_SgNewsNews_' . $key, $menuSetting);
			}

			$currentLanguageInfo = NULL;
Paul Ilea's avatar
Paul Ilea committed

			if (!LicensingService::checkKey()) {
				$this->view->assign('showLicenseBanner', TRUE);
Paul Ilea's avatar
Paul Ilea committed
			} else {
				$this->language = $backendUser->getModuleData(
					'tools_beuser/index.php/web_SgNewsNews_language', 'ses'
				) ?: 0;
				$languageOptions = BackendNewsUtility::getAvailableLanguages($this->pageUid);
				$currentLanguageInfo = $languageOptions[$this->language] ?? NULL;
Paul Ilea's avatar
Paul Ilea committed
			}
			$pageRenderer = GeneralUtility::makeInstance(PageRenderer::class);
			$pageRenderer->addJsInlineCode('typo3_version', 'TYPO3.version='
				. VersionNumberUtility::convertVersionNumberToInteger(VersionNumberUtility::getCurrentTypo3Version())
				. ';');
			if (version_compare(VersionNumberUtility::getCurrentTypo3Version(), '9.0.0', '<')) {
				$pageRenderer->loadExtJS();
			}

			$this->docHeaderComponent->setMetaInformation($this->pageInfo);
			$this->makeButtons();
			$this->makeLanguageMenu();
			$this->view->assign('pageUid', $this->pageUid);
			$this->view->assign('rootPageUid', $this->rootPageUid);
Paul Ilea's avatar
Paul Ilea committed
			$this->view->assign('language', $this->language);
			$this->view->assign('languageInfo', $currentLanguageInfo);
			$this->view->assign('docHeader', $this->docHeaderComponent->docHeaderContent());
			$this->view->assign('typo3Version', VersionNumberUtility::convertVersionNumberToInteger(TYPO3_version));
Paul Ilea's avatar
Paul Ilea committed
		}
	}

	/**
	 * @param array $filters
	 * @throws \InvalidArgumentException
	 * @throws \UnexpectedValueException
	 * @throws \TYPO3\CMS\Extbase\Persistence\Exception\InvalidQueryException
	 */
	public function indexAction(array $filters = NULL) {
		$showNewsList = FALSE;
		if (
			($this->pageUid && $this->pageUid === $this->rootPageUid) ||
			(int) $this->pageInfo['doktype'] === BackendNewsUtility::CATEGORY_DOKTYPE
		) {
			/** @var BackendUserAuthentication $backendUser */
			$backendUser = $GLOBALS['BE_USER'];
			if ($filters === NULL) {
				$filters = $backendUser->getModuleData('tools_beuser/index.php/web_SgNewsNews_filters', 'ses') ?: [];
			} else {
				$backendUser->pushModuleData('tools_beuser/index.php/web_SgNewsNews_filters', $filters);
			}
			if ((int) $this->pageInfo['doktype'] !== BackendNewsUtility::CATEGORY_DOKTYPE) {
				$categories = BackendNewsUtility::getCategoriesForSiteRoot($this->rootPageUid);
				$this->view->assign('categories', $categories);
				$this->view->assign('showCategoryFilter', TRUE);
			} else {
				$filters['categories'] = [$this->pageUid];
			}
			$tags = BackendNewsUtility::getTagsForPage($this->pageUid, $this->language);
			$news = BackendNewsUtility::getNewsByFilters($this->rootPageUid, $filters, $this->language);

			$this->view->assign('tags', $tags);
			$this->view->assign('news', $news);
			$this->view->assign('filters', $filters);
			$showNewsList = TRUE;
		} else {
			$alternativePageOptions = BackendNewsUtility::getAlternativePageOptions();
			$this->view->assign('alternativePageOptions', $alternativePageOptions);
		}
		$this->view->assign('showNewsList', $showNewsList);
	}

	/**
	 * create buttons for the backend module header
	 *
	 * @return void
	 * @throws \InvalidArgumentException
	 * @throws \UnexpectedValueException
	 */
	private function makeButtons() {
		/** @var ButtonBar $buttonBar */
		$buttonBar = $this->docHeaderComponent->getButtonBar();

		/** @var IconFactory $iconFactory */
		$iconFactory = GeneralUtility::makeInstance(IconFactory::class);

		if (version_compare(VersionNumberUtility::getCurrentTypo3Version(), '9.0.0', '<')) {
			$locallangPath = 'LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:';
		} else {
			$locallangPath = 'LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:';
		}

Paul Ilea's avatar
Paul Ilea committed
		// Refresh
		$refreshButton = $buttonBar->makeLinkButton()
			->setHref(GeneralUtility::getIndpEnv('REQUEST_URI'))
			->setTitle(
				LocalizationUtility::translate(
					$locallangPath . 'labels.reload'
				)
			)
			->setIcon($iconFactory->getIcon('actions-edit-undo', Icon::SIZE_SMALL));
Paul Ilea's avatar
Paul Ilea committed
		$buttonBar->addButton($refreshButton, ButtonBar::BUTTON_POSITION_RIGHT);

		// shortcut button
		$shortcutButton = $buttonBar->makeShortcutButton()
			->setModuleName($this->request->getPluginName())
			->setGetVariables(
				[
					'id',
					'M'
				]
			)
			->setSetVariables([]);
		$buttonBar->addButton($shortcutButton, ButtonBar::BUTTON_POSITION_RIGHT);

		$this->docHeaderComponent->getButtonBar();
	}

	/**
	 * create buttons for the backend module header
	 *
	 * @return void
	 * @throws \InvalidArgumentException
	 */
	private function makeLanguageMenu() {
		if (!LicensingService::checkKey()) {
			return;
		}
		$languageMenu = $this->docHeaderComponent->getMenuRegistry()->makeMenu();
		$languageMenu->setIdentifier('languageMenu');
		$languages = BackendNewsUtility::getAvailableLanguages($this->pageUid);
		foreach ($languages as $key => $language) {
			$menuItem = $languageMenu
				->makeMenuItem()
				->setTitle($language['title'])
				->setHref(
					BackendUtility::getModuleUrl('web_SgNewsNews') . '&id=' . $this->pageUid . '&SET[language]=' . $key
				);
			if ((int) $this->language === (int) $key) {
Paul Ilea's avatar
Paul Ilea committed
				$menuItem->setActive(TRUE);
			}
			$languageMenu->addMenuItem($menuItem);
		}
		$this->docHeaderComponent->getMenuRegistry()->addMenu($languageMenu);
	}

	/**
	 * Clipboard pasting and deleting.
	 *
	 * @throws \InvalidArgumentException
	 */
	public function initClipboard() {
		if (\is_array($this->clipboardCommandArray)) {
Paul Ilea's avatar
Paul Ilea committed
			$clipObj = GeneralUtility::makeInstance(Clipboard::class);
			$clipObj->initializeClipboard();
			if ($this->clipboardCommandArray['paste']) {
				$clipObj->setCurrentPad($this->clipboardCommandArray['pad']);
				$this->command = $clipObj->makePasteCmdArray(
					$this->clipboardCommandArray['paste'],
					$this->command,
					$this->clipboardCommandArray['update'] ?? NULL
				);
			}

			if ($this->clipboardCommandArray['delete']) {
				$clipObj->setCurrentPad($this->clipboardCommandArray['pad']);
				$this->command = $clipObj->makeDeleteCmdArray($this->command);
			}

			if ($this->clipboardCommandArray['el']) {
				$this->clipboardCommandArray['setP'] = 'normal';
				$clipObj->setCmd($this->clipboardCommandArray);
				$clipObj->cleanCurrent();
				$clipObj->endClipboard();
			}
		}