<?php

namespace SGalinski\SgNews\Domain\Repository;

/***************************************************************
 *  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\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use SGalinski\SgNews\Domain\Model\FileReference;
use TYPO3\CMS\Core\Resource\File;

/**
 * FileReference Repository
 */
class FileReferenceRepository extends AbstractRepository {

	/**
	 * Method creates a file reference entry in the database. This step is necessary because the
	 * extbase can not handle the default TCA for FAL records. Without this method the FAL records will miss
	 * the uid_foreign, field name and table names.
	 *
	 * @param File $file
	 * @param int $uid
	 * @param int $pid
	 * @param string $tablename
	 * @param string $fieldname
	 * @return FileReference
	 * @throws \Exception
	 */
	public function addFileReferenceFromFile(File $file, $uid, $pid, $tablename, $fieldname) {
		$fileReferenceId = $this->addFileReferenceFromFileId(
			$file->getUid(),
			['uid' => $uid, 'pid' => $pid],
			$tablename,
			$fieldname
		);

		$fileReference = NULL;
		if ($fileReferenceId > 0) {
			/** @var FileReference $fileReference */
			$fileReference = $this->findByUid($fileReferenceId);
		}

		return $fileReference;
	}

	/**
	 * Method creates a file reference entry in the database. This step is necessary because the
	 * extbase can not handle the default TCA for FAL records. Without this method the FAL records will miss
	 * the uid_foreign, field name and table names.
	 *
	 * @param $fileId
	 * @param array $reference
	 * @param string $tablename
	 * @param string $fieldname
	 * @return int
	 * @throws \Exception
	 */
	public function addFileReferenceFromFileId($fileId, array $reference, $tablename, $fieldname) {
		$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('sys_file_reference');
		$success = $queryBuilder->insert('sys_file_reference')
			->values([
				'crdate' => $GLOBALS['EXEC_TIME'],
				'tstamp' => $GLOBALS['EXEC_TIME'],
				'pid' => (int) $reference['pid'],
				'table_local' => 'sys_file',
				'uid_local' => $fileId,
				'uid_foreign' => (int) $reference['uid'],
				'tablenames' => $tablename,
				'fieldname' => $fieldname
			]);

		if (!$success) {
			throw new \Exception('An error occurred while adding a file reference record.', 1452590219);
		}

		return (int) $queryBuilder->getConnection()->lastInsertId();
	}
}