Skip to content
Snippets Groups Projects
PluginRenderer.php 6.62 KiB
Newer Older
<?php

namespace SGalinski\SgYoutube\Hooks;

/***************************************************************
 *  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 TYPO3\CMS\Backend\Utility\BackendUtility;
use TYPO3\CMS\Backend\View\PageLayoutView;
use TYPO3\CMS\Backend\View\PageLayoutViewDrawItemHookInterface;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Form\Service\TranslationService;

/**
 * Class PluginRenderer
 * Renders back-end preview for the SG YouTube Videos plugin
 *
 * @package SGalinski\SgYoutube\Hooks
 */
class PluginRenderer implements PageLayoutViewDrawItemHookInterface {
	/**
	 * Preprocesses the preview rendering of a content element of type "sg_youtube"
	 *
	 * @param PageLayoutView $parentObject Calling parent object
	 * @param bool $drawItem Whether to draw the item using the default functionality
	 * @param string $headerContent Header content
	 * @param string $itemContent Item content
	 * @param array $row Record row of tt_content
	 * @return void
	 */
	public function preProcess(PageLayoutView &$parentObject, &$drawItem, &$headerContent, &$itemContent, array &$row) {
		if ($row['list_type'] === 'sgyoutube_youtube') {

			// Content element labels
			$labelPluginName = BackendUtility::getLabelFromItemListMerged(
				$row['pid'], 'tt_content', 'list_type', $row['list_type']
			);
			$labelHeader = BackendUtility::getItemLabel(
				'tt_content', 'header'
			);
			$labelHeaderLink = BackendUtility::getItemLabel(
				'tt_content', 'header_link'
			);
			$labelSubheader = BackendUtility::getItemLabel(
				'tt_content', 'subheader'
			);
			$labelContentLink = BackendUtility::getItemLabel(
				'tt_content', 'tx_sgcontentlink_contentlink'
			);

			// Make instance of TranslationService class
			$translationServiceObject = GeneralUtility::makeInstance(TranslationService::class);

			// Begin rendering of content element fields
			// Plugin name
			$headerContent = '<div style="padding: 0 4px 10px 0;border-bottom: 1px solid #CACACA;margin-bottom: 6px; font-weight:700;">' . $labelPluginName . '</div>';

			// Header
			if ($row['header']) {
				if ($row['header_layout'] === '10') {
					$headerContent .= '<p>' . $row['header'] . '</p>';
				} elseif ($row['header_layout'] === '100') {
					$headerContent .= '<div style="display:none;">' . $row['header'] . '</div>';
				} else {
					$headerContent .= '<h' . $row['header_layout'] . '>' . $row['header'] . '</h' . $row['header_layout'] . '>';
				}
			}

			//Header link
			if ($row['header_link']) {
				$headerContent .= '<div><strong>' . $translationServiceObject->translate(
						$labelHeaderLink
					) . '&nbsp;(' . $translationServiceObject->translate(
						$labelHeader
					) . ')</strong>:&nbsp;' . $row['header_link'] . '</div>';
			}

			// Subheader
			if ($row['subheader']) {
				$headerContent .= '<div>' . '<strong>' . $translationServiceObject->translate(
						$labelSubheader
					) . '</strong>' . ':&nbsp;' . $row['subheader'] . '</div>';
			}
			// Content link
			if ($row['tx_sgcontentlink_contentlink']) {
				$headerContent .= '<div>' . '<strong>' . $translationServiceObject->translate(
						$labelContentLink
					) . '</strong>' . ':&nbsp;' . $row['tx_sgcontentlink_contentlink'] . '</div>';
			}

			// Set plugin options array
			$pluginOptions = GeneralUtility::xml2array($row['pi_flexform'], 'T3:');

			// Get the FlexForm configuration file
			$xmlFilePath = $GLOBALS['TCA']['tt_content']['columns']['pi_flexform']['config']['ds']['sgyoutube_youtube,list'];
			$xmlContent = GeneralUtility::getFileAbsFileName(
				str_replace(
					'FILE:EXT:', 'EXT:', $xmlFilePath
				)
			);

			// Get contents of the FlexForm configuration file
			$xmlString = file_get_contents($xmlContent);
			$arrayJobs = GeneralUtility::xml2array($xmlString);

			// Begin rendering of table
			$itemContent .= '<br>' . '<table class="table table-striped">';

			// Table header
			$labelSettings = 'LLL:EXT:sg_youtube/Resources/Private/Language/locallang.xlf:settings';
			$labelvalue = 'LLL:EXT:sg_youtube/Resources/Private/Language/locallang.xlf:values';
			$itemContent .= '<row>' . '<th style="width: 50%">' . $translationServiceObject->translate(
					$labelSettings
				) . '</th>';
			$itemContent .= '<th style="width: 50%">' . $translationServiceObject->translate(
					$labelvalue
				) . '</th>' . '</tr>';

			// Table content rows
			// Loop through plugin options
			foreach ($pluginOptions['data']['sDEF']['lDEF'] as $key => $setting) {
				// if plugin option exists within the Flexform configuration file
				if (array_key_exists($key, $arrayJobs['sheets']['sDEF']['ROOT']['el'])) {
					// Display label
					$labelId = $arrayJobs['sheets']['sDEF']['ROOT']['el'][$key]['TCEforms']['label'];
					$itemContent .= '<row>' . '<td><strong>' . $translationServiceObject->translate(
							trim($labelId)
						) . '</strong></td>';
					// Display value
					$itemContent .= '<td>' . $setting['vDEF'] . '</td>' . '</tr>';
				}
			}

			// Record Storage
			if ($row['pages'] !== '') {
				// Record Storage Page label
				$labelRecordStorage = 'LLL:EXT:frontend/Resources/Private/Language/locallang_ttc.xlf:pages.ALT.list_formlabel';

				// Get storage page(s)
				$backendUtilityObject = GeneralUtility::makeInstance(BackendUtility::class);
				$storagePageIds = explode(',', $row['pages']);
				$storagePages = [];

				foreach ($storagePageIds as $i => $storagePageId) {
					$storagePages[] = $backendUtilityObject->getRecord(
							'pages', $storagePageId, 'title'
						)['title'] . ' ' . '(' . $storagePageId . ')';
				}

				// Render 'Record Storage Page' row
				$itemContent .= '<row>' . '<td><strong>' . $translationServiceObject->translate(
						$labelRecordStorage
					) . '</strong></td>';
				$itemContent .= '<td>' . implode(', ', $storagePages) . '</td>' . '</tr>';
			}

			// End rendering of table
			$itemContent .= '</table>';
			$drawItem = FALSE;
		}
	}
}