Skip to content
Snippets Groups Projects
TcaProvider.php 3.85 KiB
Newer Older
Stefan Galinski's avatar
Stefan Galinski committed
<?php

namespace SGalinski\SgNews\TCA;

/***************************************************************
 *  Copyright notice
 *
 *  (c) sgalinski Internet Services (https://www.sgalinski.de)
Stefan Galinski's avatar
Stefan Galinski committed
 *  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 2 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\Form\FormEngine;
use TYPO3\CMS\Core\Database\DatabaseConnection;
use TYPO3\CMS\Core\DataHandling\DataHandler;
use TYPO3\CMS\Core\SingletonInterface;
use TYPO3\CMS\Core\Utility\GeneralUtility;

/**
 * This class contains methods for usage within the TCA forms.
 */
class TcaProvider implements SingletonInterface {

	/**
	 * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
	 */
	protected $objectManager;

	/**
	 * Constructor
	 */
	public function __construct() {
		$this->objectManager = GeneralUtility::makeInstance('TYPO3\CMS\Extbase\Object\ObjectManager');
	}

	/**
	 * Hook method for calculating the title for articles and copying the subtitle into the nav_title field
	 *
	 * @param DataHandler $tceMain
	 * @return void
	 */
	public function processDatamap_afterAllOperations(DataHandler $tceMain) {
		/** @var DatabaseConnection $database */
		$database = $GLOBALS['TYPO3_DB'];

		foreach ($tceMain->datamap as $table => $tablesData) {
			if (!in_array($table, ['pages', 'pages_language_overlay'])) {
				continue;
			}

			foreach ($tablesData as $identity => $tableData) {
				if (strpos($identity, 'NEW') !== FALSE) {
					$identity = $tceMain->substNEWwithIDs[$identity];
				}

				$translationRow = [];
				$pagesIdentity = (int) $identity;
				if ($table === 'pages_language_overlay') {
					$translationRow = $database->exec_SELECTgetSingleRow(
						'pid, subtitle, t3ver_oid', 'pages_language_overlay', 'uid = ' . (int) $identity
Stefan Galinski's avatar
Stefan Galinski committed
					);

					$workspaceOriginalId = (int) $translationRow['t3ver_oid'];
					if ($workspaceOriginalId > 0) {
						$translationRow = $database->exec_SELECTgetSingleRow(
							'pid, subtitle, t3ver_oid', 'pages_language_overlay', 'uid = ' . $workspaceOriginalId
						);
					}

Stefan Galinski's avatar
Stefan Galinski committed
					$pagesIdentity = (int) $translationRow['pid'];
				}

				$row = $database->exec_SELECTgetSingleRow(
					'doktype, subtitle, lastUpdated, tx_sgnews_highlighted, t3ver_oid', 'pages', 'uid = ' .
						$pagesIdentity
Stefan Galinski's avatar
Stefan Galinski committed
				);

				$workspaceOriginalId = (int) $row['t3ver_oid'];
				if ($workspaceOriginalId > 0) {
					$row = $database->exec_SELECTgetSingleRow(
						'doktype, subtitle, lastUpdated, tx_sgnews_highlighted, t3ver_oid', 'pages', 'uid = ' .
						$workspaceOriginalId
					);
				}

Stefan Galinski's avatar
Stefan Galinski committed
				// only articles
				$row['doktype'] = (int) $row['doktype'];
				if (!is_array($row) || !count($row) || !in_array($row['doktype'], [116])) {
					continue;
				}

				$prefix = '';
				if ($row['tx_sgnews_highlighted'] >= 1) {
					$prefix = 'Top - ';
				}

				$subtitle = $row['subtitle'];
				if ($table === 'pages_language_overlay') {
					$subtitle = $translationRow['subtitle'];
				}
				$calculatedTitle = $prefix . strftime('%Y-%m-%d', $row['lastUpdated']) . ' - ' . $subtitle;

				$updateFields = ['nav_title' => $subtitle, 'title' => trim($calculatedTitle)];
				$database->exec_UPDATEquery($table, 'uid = ' . $identity, $updateFields);
			}
		}
	}
}

?>