Skip to content
Snippets Groups Projects
TcaProvider.php 3.68 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\Core\Database\ConnectionPool;
Stefan Galinski's avatar
Stefan Galinski committed
use TYPO3\CMS\Core\DataHandling\DataHandler;
use TYPO3\CMS\Core\SingletonInterface;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Object\ObjectManager;
Stefan Galinski's avatar
Stefan Galinski committed

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

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

	/**
	 * Constructor
	 *
	 * @throws \InvalidArgumentException
Stefan Galinski's avatar
Stefan Galinski committed
	 */
	public function __construct() {
		$this->objectManager = GeneralUtility::makeInstance(ObjectManager::class);
Stefan Galinski's avatar
Stefan Galinski committed
	}

	/**
	 * 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 array $tablesData */
Stefan Galinski's avatar
Stefan Galinski committed
		foreach ($tceMain->datamap as $table => $tablesData) {
			if ($table !== 'pages') {
Stefan Galinski's avatar
Stefan Galinski committed
				continue;
			}

			/** @var string $identity */
Stefan Galinski's avatar
Stefan Galinski committed
			foreach ($tablesData as $identity => $tableData) {
				if (strpos($identity, 'NEW') !== FALSE) {
					$identity = $tceMain->substNEWwithIDs[$identity];
				}

				$translationRow = [];
				$pagesIdentity = (int) $identity;

				$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('pages');
				$queryBuilder->getRestrictions()->removeAll();
				$row = $queryBuilder->select('doktype', 'subtitle', 'lastUpdated', 'tx_sgnews_highlighted', 't3ver_oid')
					->from('pages')
					->where(
						$queryBuilder->expr()->eq('uid', $queryBuilder->createNamedParameter($pagesIdentity, \PDO::PARAM_INT))
					)
					->execute()->fetch();
Stefan Galinski's avatar
Stefan Galinski committed

				$workspaceOriginalId = (int) $row['t3ver_oid'];
				if ($workspaceOriginalId > 0) {
					$row = $queryBuilder->where(
							$queryBuilder->expr()->eq('uid', $queryBuilder->createNamedParameter($workspaceOriginalId, \PDO::PARAM_INT))
						)->execute()->fetch();
Stefan Galinski's avatar
Stefan Galinski committed
				// only articles
				$row['doktype'] = (int) $row['doktype'];
				if (!is_array($row) || !count($row) || (int) $row['doktype'] !== 116) {
Stefan Galinski's avatar
Stefan Galinski committed
					continue;
				}

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

				$subtitle = $row['subtitle'];
				$calculatedTitle = $prefix . strftime('%Y-%m-%d', $row['lastUpdated']) . ' - ' . $subtitle;

				$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($table);
				$queryBuilder->update($table)
					->where(
						$queryBuilder->expr()->eq('uid', $queryBuilder->createNamedParameter($identity, \PDO::PARAM_INT))
					)
					->set('nav_title', $subtitle, TRUE)
					->set('title', trim($calculatedTitle), TRUE)
					->execute();