‪TYPO3CMS  9.5
DeleteUploadsFinisher.php
Go to the documentation of this file.
1 <?php
2 declare(strict_types = 1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
21 
30 {
31 
36  protected function ‪executeInternal()
37  {
38  $formRuntime = $this->finisherContext->getFormRuntime();
39 
40  $uploadFolders = [];
41  $elements = $formRuntime->getFormDefinition()->getRenderablesRecursively();
42  foreach ($elements as $element) {
43  if (!$element instanceof ‪FileUpload) {
44  continue;
45  }
46  $file = $formRuntime[$element->getIdentifier()];
47  if (!$file) {
48  continue;
49  }
50 
51  if ($file instanceof ‪FileReference) {
52  $file = $file->getOriginalResource();
53  }
54 
55  $folder = $file->getParentFolder();
56  $uploadFolders[$folder->getCombinedIdentifier()] = $folder;
57 
58  $file->getStorage()->deleteFile($file->getOriginalFile());
59  }
60 
61  $this->‪deleteEmptyUploadFolders($uploadFolders);
62  }
63 
70  protected function ‪deleteEmptyUploadFolders(array $folders): void
71  {
72  foreach ($folders as $folder) {
73  if ($this->‪isEmptyFolder($folder)) {
74  $folder->delete();
75  }
76  }
77  }
78 
83  protected function ‪isEmptyFolder(‪Folder $folder): bool
84  {
85  return $folder->‪getFileCount() === 0
86  && $folder->‪getStorage()->‪countFoldersInFolder($folder) === 0;
87  }
88 }
‪TYPO3\CMS\Form\Domain\Finishers\DeleteUploadsFinisher\isEmptyFolder
‪bool isEmptyFolder(Folder $folder)
Definition: DeleteUploadsFinisher.php:83
‪TYPO3\CMS\Form\Domain\Finishers\DeleteUploadsFinisher
Definition: DeleteUploadsFinisher.php:30
‪TYPO3\CMS\Core\Resource\Folder\getFileCount
‪int getFileCount(array $filterMethods=[], $recursive=false)
Definition: Folder.php:258
‪TYPO3\CMS\Form\Domain\Finishers\DeleteUploadsFinisher\executeInternal
‪executeInternal()
Definition: DeleteUploadsFinisher.php:36
‪TYPO3\CMS\Form\Domain\Finishers
Definition: AbstractFinisher.php:3
‪TYPO3\CMS\Core\Resource\Folder\getStorage
‪ResourceStorage getStorage()
Definition: Folder.php:146
‪TYPO3\CMS\Form\Domain\Finishers\DeleteUploadsFinisher\deleteEmptyUploadFolders
‪deleteEmptyUploadFolders(array $folders)
Definition: DeleteUploadsFinisher.php:70
‪TYPO3\CMS\Form\Domain\Finishers\AbstractFinisher
Definition: AbstractFinisher.php:35
‪TYPO3\CMS\Core\Resource\Folder
Definition: Folder.php:34
‪TYPO3\CMS\Extbase\Domain\Model\FileReference
Definition: FileReference.php:25
‪TYPO3\CMS\Core\Resource\ResourceStorage\countFoldersInFolder
‪int countFoldersInFolder(Folder $folder, $useFilters=true, $recursive=false)
Definition: ResourceStorage.php:2364
‪TYPO3\CMS\Form\Domain\Model\FormElements\FileUpload
Definition: FileUpload.php:24