Commit 79ad2b42 authored by Dainis Abols's avatar Dainis Abols
Browse files

Folder fix

parent 17d06f5b
......@@ -3,8 +3,14 @@
namespace Lu\LuNas\Hooks;
use Lu\LuApi\DataSources\SolrSynchronize;
use Lu\LuNas\Domain\Model\Document;
use Lu\LuNas\Domain\Session\BackendSessionHandler;
use Lu\LuNas\Helpers\DataHelper;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\DataHandling\DataHandler;
use TYPO3\CMS\Core\Resource\FileRepository;
use TYPO3\CMS\Core\Resource\ResourceFactory;
use TYPO3\CMS\Core\Utility\GeneralUtility;
/**
* Class TCEmainHook. Sync SOLR data on save, update, and delete events
......@@ -30,6 +36,16 @@ class TCEmainHook
public function processDatamap_afterDatabaseOperations($status, $table, $id, $fieldArray, &$reference)
{
if ($table === 'tx_lunas_domain_model_document') {
$recordUid = $status == 'new' ? $reference->substNEWwithIDs[$id] : $id;
// If file fields have changed move files if necessary
if (!empty($fieldArray['filename_lv']) && $fieldArray['filename_lv']) {
$this->moveFile($recordUid, 'filename_lv');
}
if (!empty($fieldArray['filename_en']) && $fieldArray['filename_en']) {
$this->moveFile($recordUid, 'filename_en');
}
$this->syncAndStore();
}
}
......@@ -59,4 +75,53 @@ class TCEmainHook
$result = $dataHelper->APIDecode($jsonData);
$backendSession->store('lu_nas_syn_result', $result);
}
}
\ No newline at end of file
/**
* Moves file to NAS folder
*
* @param $recordUid
* @param $field
*/
private function moveFile($recordUid, $field)
{
$documentTable = GeneralUtility::makeInstance(Document::class)->getTableName();
$fileRepository = GeneralUtility::makeInstance(FileRepository::class);
$storageRepository = GeneralUtility::makeInstance(ResourceFactory::class);
$storage = $storageRepository->getStorageObject(1);
$nasFolder = $storage->getFolder('/lu_nas/');
$fileObj = $fileRepository->findByRelation($documentTable, $field, $recordUid)[0];
if (!empty($fileObj)) {
$filePath = $fileObj->getOriginalFile()->getIdentifier();
$file = $storageRepository->getFileObjectFromCombinedIdentifier('1:'.$filePath);
if (!preg_match('/^\/lu_nas\//', $filePath)) {
$file->moveTo($nasFolder);
$this->deleteOtherReferences($file->getUid());
}
}
}
/**
* Soft delete all references outside NAS document
*
* @param $fileId
*/
private function deleteOtherReferences($fileId)
{
$connectionPool = GeneralUtility::makeInstance(ConnectionPool::class);
$queryBuilder = $connectionPool->getQueryBuilderForTable('sys_file_reference');
$queryBuilder->getRestrictions()->removeAll();
$queryBuilder
->update('sys_file_reference')
->where(
$queryBuilder->expr()->eq('uid_local', $fileId)
)
->andWhere(
$queryBuilder->expr()->neq('tablenames', $queryBuilder->createNamedParameter('tx_lunas_domain_model_document'))
)
->set('deleted', 1)
->execute();
}
}
{
"name": "luitd/lu-nas",
"version": "4.2.7",
"version": "4.2.8",
"description": "Latvijas Universitātes Normatīvo Aktu sistēmas TYPO3 spraudnis",
"type": "typo3-cms-extension",
"keywords": [
......
......@@ -9,7 +9,7 @@ $EM_CONF[$_EXTKEY] = [
'author_email' => 'dainis.abols@lu.lv',
'state' => 'stable',
'clearCacheOnLoad' => true,
'version' => '4.2.7',
'version' => '4.2.8',
'constraints' => [
'depends' => [
'typo3' => '10.4.0-10.9.99',
......
......@@ -4,7 +4,7 @@ TYPO3 Extension for Regulations (NAS)
## Version
4.2.7
4.2.8
## Dependecies
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment