[BUGFIX] Clean up category references when a file is deleted 24/27124/5
authorFabien Udriot <fabien.udriot@ecodev.ch>
Wed, 29 Jan 2014 12:28:16 +0000 (16:28 +0400)
committerAndreas Wolf <andreas.wolf@typo3.org>
Thu, 30 Jan 2014 11:41:36 +0000 (12:41 +0100)
File are categorizable by default as of TYPO3 6.2
When a BE User deletes a file, associated categories
must be removed as well.

Change-Id: I82175bbd207e68590c20de8fd536bab0de001fe3
Releases: 6.2
Resolves: #55430
Reviewed-on: https://review.typo3.org/27124
Reviewed-by: Anja Leichsenring
Tested-by: Anja Leichsenring
Reviewed-by: Andreas Wolf
Tested-by: Andreas Wolf
typo3/sysext/core/Classes/Resource/Processing/FileDeletionAspect.php

index 1af9713..c95fd20 100644 (file)
@@ -93,21 +93,41 @@ class FileDeletionAspect {
                        // remove all references
                        $this->getDatabase()->exec_DELETEquery(
                                'sys_file_reference',
-                               'uid_local=' . intval($fileObject->getUid()) . ' AND table_local = \'sys_file\''
+                               'uid_local=' . (int)$fileObject->getUid() . ' AND table_local = \'sys_file\''
                        );
+
+                       $this->cleanupCategoryReferences($fileObject);
                        $this->cleanupProcessedFiles($fileObject);
                } elseif ($fileObject instanceof ProcessedFile) {
-                       $this->getDatabase()->exec_DELETEquery('sys_file_processedfile', 'uid=' . intval($fileObject->getUid()));
+                       $this->getDatabase()->exec_DELETEquery('sys_file_processedfile', 'uid=' . (int)$fileObject->getUid());
                }
        }
 
        /**
+        * Remove all category references of the deleted file.
+        *
+        * @param File $fileObject
+        * @return void
+        */
+       protected function cleanupCategoryReferences(File $fileObject) {
+
+               // Retrieve the file metadata uid which is different from the file uid.
+               $metadataProperties = $fileObject->_getMetaData();
+
+               $metadDataUid = isset($metadataProperties['_ORIG_uid']) ? $metadataProperties['_ORIG_uid'] : $metadataProperties['uid'];
+               $this->getDatabase()->exec_DELETEquery(
+                       'sys_category_record_mm',
+                       'uid_foreign=' . (int)$metadDataUid . ' AND tablenames = \'sys_file_metadata\''
+               );
+       }
+
+       /**
         * Remove all processed files that belong to the given File object
         *
         * @param FileInterface $fileObject
         * @return void
         */
-       public function cleanupProcessedFiles(FileInterface $fileObject) {
+       protected function cleanupProcessedFiles(FileInterface $fileObject) {
 
                // only delete processed files of File objects
                if (!$fileObject instanceof File) {