|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Hoogi91\Spreadsheets\Hooks; |
| 4 | + |
| 5 | +use TYPO3\CMS\Backend\Utility\BackendUtility; |
| 6 | +use TYPO3\CMS\Core\DataHandling\DataHandler; |
| 7 | +use TYPO3\CMS\Core\Resource\FileReference; |
| 8 | +use TYPO3\CMS\Core\Resource\FileRepository; |
| 9 | +use TYPO3\CMS\Core\Utility\GeneralUtility; |
| 10 | + |
| 11 | +/** |
| 12 | + * Class DataHandlerHook |
| 13 | + * @package Hoogi91\Spreadsheets\Hooks |
| 14 | + */ |
| 15 | +class DataHandlerHook |
| 16 | +{ |
| 17 | + private static $records = []; |
| 18 | + |
| 19 | + /** |
| 20 | + * Post hook to set default spreadsheet selection for newly created items |
| 21 | + * |
| 22 | + * @param string|mixed $status Status which should be "new" to activate this hook |
| 23 | + * @param string|mixed $table Table which should be "tt_content" to activate this hook |
| 24 | + * @param int|string|mixed $id Temporary ID used to search for real new uid |
| 25 | + * @param array $fieldArray Field array that has been saved to database |
| 26 | + * @param DataHandler $dataHandler Data handler instance |
| 27 | + * |
| 28 | + * @return void |
| 29 | + */ |
| 30 | + public function processDatamap_afterDatabaseOperations( // @codingStandardsIgnoreLine |
| 31 | + $status, |
| 32 | + $table, |
| 33 | + $id, |
| 34 | + array $fieldArray, |
| 35 | + DataHandler $dataHandler |
| 36 | + ): void { |
| 37 | + // skip processing for unknown uid, wrong table, status or not updated assets |
| 38 | + $uid = $dataHandler->substNEWwithIDs[$id] ?? (is_int($id) ? $id : null); |
| 39 | + if ($uid === null |
| 40 | + || $table !== 'tt_content' |
| 41 | + || !array_key_exists('tx_spreadsheets_assets', $fieldArray) |
| 42 | + || !in_array($status, ['new', 'update'], true)) { |
| 43 | + return; |
| 44 | + } |
| 45 | + |
| 46 | + // skip if not spreadsheet table or bodytext is already filled |
| 47 | + $CType = $fieldArray['CType'] ?? $this->getBackendRecordField($uid, 'CType'); |
| 48 | + if ($CType !== 'spreadsheets_table') { |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + // truncate bodytext after update if assets have been removed |
| 53 | + if ($fieldArray['tx_spreadsheets_assets'] === 0) { |
| 54 | + if ($status === 'update') { |
| 55 | + $dataHandler->updateDB('tt_content', $uid, ['bodytext' => '']); |
| 56 | + } |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + /** @var FileRepository $fileRepository */ |
| 61 | + $fileRepository = GeneralUtility::makeInstance(FileRepository::class); |
| 62 | + /** @var FileReference[] $relations */ |
| 63 | + $relations = $fileRepository->findByRelation('tt_content', 'tx_spreadsheets_assets', $uid); |
| 64 | + if (empty($relations)) { |
| 65 | + return; |
| 66 | + } |
| 67 | + |
| 68 | + // update bodytext to default file selection |
| 69 | + if (empty($this->getBackendRecordField($uid, 'bodytext')) === true) { |
| 70 | + $dataHandler->updateDB('tt_content', $uid, ['bodytext' => 'spreadsheet://' . $relations[0]->getUid()]); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Get backend record field but load entry once |
| 76 | + * |
| 77 | + * @param int $uid UID of tt_content record |
| 78 | + * @param string $field Field to extract |
| 79 | + * |
| 80 | + * @return mixed|null |
| 81 | + */ |
| 82 | + private function getBackendRecordField(int $uid, string $field) |
| 83 | + { |
| 84 | + if (!isset(self::$records[$uid])) { |
| 85 | + self::$records[$uid] = BackendUtility::getRecord('tt_content', $uid); // @codeCoverageIgnore |
| 86 | + } |
| 87 | + return self::$records[$uid][$field] ?? null; |
| 88 | + } |
| 89 | +} |
0 commit comments