Skip to content
Snippets Groups Projects
PluginRenderer.php 6.78 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\Imaging\Icon;
use TYPO3\CMS\Core\Imaging\IconFactory;
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 {
	/**
	 * Return an array of pages or categories, with their respective ids
	 *
	 * @param $recodIds
	 * @return array $records
	 */
	public function buildRecordArray($table, $recodIds) {
		$backendUtilityObject = GeneralUtility::makeInstance(BackendUtility::class);
		$recordsArray = explode(',', $recodIds);
		$records = [];

		foreach ($recordsArray as $i => $recordId) {
			$records[] = $backendUtilityObject->getRecord(
					$table, $recordId, 'title'
				)['title'] . ' ' . '(' . $recordId . ')';
		}
		return $records;
	}

	/**
	 * 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']
			);
			$labelSubheader = BackendUtility::getItemLabel(
				'tt_content', 'subheader'
			);

			// Instantiate 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'] . '>';
				}
			}

			// Subheader
			if ($row['subheader']) {
				$headerContent .= '<div>' . '<strong>' . $translationServiceObject->translate(
						$labelSubheader
					) . '</strong>' . ':&nbsp;' . $row['subheader'] . '</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'];

			if ($xmlFilePath !== '') {
				$xmlContent = GeneralUtility::getFileAbsFileName(
					str_replace(
						'FILE:EXT:', 'EXT:', $xmlFilePath
					)
				);

				// Get contents of the FlexForm configuration file
				$xmlString = file_get_contents($xmlContent);
				$configurationArray = 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>';

				// Loop through plugin options and render rows
				foreach ($pluginOptions['data']['sDEF']['lDEF'] as $key => $setting) {

					// if plugin option exists within the Flexform configuration file
					if (array_key_exists($key, $configurationArray['sheets']['sDEF']['ROOT']['el'])) {

						// Display label
						$labelId = $configurationArray['sheets']['sDEF']['ROOT']['el'][$key]['TCEforms']['label'];
						$itemContent .= '<row>' . '<td><strong>' . $translationServiceObject->translate(
								trim($labelId)
							) . '</strong></td>';

						// Display value
						// Show different icon for the 'Show API Result' field, depending on checkbox value
						if ($key === 'settings.showApiResult') {
							if ($setting['vDEF'] === '0') {
								$itemContent .= '<td>' . GeneralUtility::makeInstance(IconFactory::class)->getIcon(
										'actions-close', Icon::SIZE_SMALL
									)
										->render('inline') . '</td>' . '</tr>';
							} else {
								$itemContent .= '<td>' . GeneralUtility::makeInstance(IconFactory::class)->getIcon(
										'actions-check', Icon::SIZE_SMALL
									)
										->render('inline') . '</td>' . '</tr>';
							}
							// Remaining values
							$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';
					// Render 'Record Storage Page' row
					$itemContent .= '<row>' . '<td><strong>' . $translationServiceObject->translate(
							$labelRecordStorage
						) . '</strong></td>';
					$itemContent .= '<td>' . implode(
							', ', $this->buildRecordArray('pages', $row['pages'])
						) . '</td>' . '</tr>';