<?php

namespace SGalinski\SgMail\Updates;

/***************************************************************
 *  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 SGalinski\SgMail\Service\BackendService;
use TYPO3\CMS\Core\Database\DatabaseConnection;
use TYPO3\CMS\Install\Updates\AbstractUpdate;

/**
 * Migrate template db entries to the correct root pages
 */
class UpdatePidToSiteRoot extends AbstractUpdate {
	/**
	 * @var string
	 */
	protected $title = 'Find all templates & queue entries with site root and assign the correct site root id as pid. Also check if the pids are actually site roots and update them accordingly';

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

	/**
	 * @var array
	 */
	protected $tables = [
		'tx_sgmail_domain_model_mail', 'tx_sgmail_domain_model_template'
	];

	/**
	 * Checks whether updates are required.
	 *
	 * @param string &$description The description for the update
	 * @return bool Whether an update is required (TRUE) or not (FALSE)
	 */
	public function checkForUpdate(&$description) {
		$description = 'Move site root ids to pid & update pids to their correspondent site root ids';
		/** @var DatabaseConnection $databaseConnection */
		$databaseConnection = $GLOBALS['TYPO3_DB'];

		// check if site_root columns actually exist
		$hasColumn = [];
		foreach ($this->tables as $table) {
			$hasColumn[$table] = $this->siteRootColumnExists($table);
		}

		// are there site root columns differing from pids?
		$where = 'site_root_id <> pid';
		foreach ($this->tables as $table) {
			if (!$hasColumn[$table]) {
				continue;
			}
			$result = $databaseConnection->exec_SELECTquery('*', $table, $where);
			if ($result->num_rows > 0) {
				return TRUE;
				break;
			}
		}

		// are the pids not belonging to site root pages ?
		foreach ($this->tables as $table) {
			$result = $databaseConnection->exec_SELECTquery('pid', $table, '')->fetch_all();
			foreach ($result as $row) {
				$siteRootId = BackendService::getSiteRoot($row[0]);
				if ($siteRootId !== $row[0]) {
					return TRUE;
				}
			}
		}

		return !(!FALSE || $this->isWizardDone());
	}

	/**
	 * Performs the according updates.
	 *
	 * @param array &$dbQueries Queries done in this update
	 * @param mixed &$customMessages Custom messages
	 * @return bool Whether everything went smoothly or not
	 */
	public function performUpdate(array &$dbQueries, &$customMessages) {
		/** @var DatabaseConnection $databaseConnection */
		$databaseConnection = $GLOBALS['TYPO3_DB'];
		$dbQueries = [];

		// set pids to siteroot
		foreach ($this->tables as $table) {
			if (!$this->siteRootColumnExists($table)) {
				continue;
			}

			$result = $databaseConnection->exec_SELECTquery('uid, site_root_id', $table, '')->fetch_all();
			$dbQueries[] = $databaseConnection->debug_lastBuiltQuery;

			/** @var array $result */
			foreach ($result as $row) {
				$where = 'uid = ' . $row[0];
				$databaseConnection->exec_UPDATEquery($table, $where, ['pid' => $row[1]]);
				$dbQueries[] = $databaseConnection->debug_lastBuiltQuery;
			}
		}

		// check if pid is a site root, if not update it to the nearest one
		foreach ($this->tables as $table) {
			$result = $databaseConnection->exec_SELECTquery('uid, pid', $table, '')->fetch_all();
			$dbQueries[] = $databaseConnection->debug_lastBuiltQuery;

			/** @var array $result */
			foreach ($result as $row) {
				$siteRootId = BackendService::getSiteRoot($row[1]);
				if ($siteRootId === 0) {
					$siteRootId = 1;
				}
				if ($siteRootId !== (int) $row[1]) {
					$where = 'uid = ' . $row[0];
					$databaseConnection->exec_UPDATEquery($table, $where, ['pid' => $siteRootId]);
					$dbQueries[] = $databaseConnection->debug_lastBuiltQuery;
				}
			}
		}

		$this->markWizardAsDone();
		return TRUE;
	}

	/**
	 * check if site_root columns actually exist
	 *
	 * @param $table
	 * @return bool
	 */
	private function siteRootColumnExists($table) {
		/** @var DatabaseConnection $databaseConnection */
		$databaseConnection = $GLOBALS['TYPO3_DB'];
		$result = $databaseConnection->admin_get_fields($table);

		foreach ($result as $column) {
			if ($column['Field'] === 'site_root_id') {
				return TRUE;
			}
		}

		return FALSE;
	}
}