|
1
|
|
|
<?php |
|
2
|
|
|
namespace Fab\Vidi\Service; |
|
3
|
|
|
|
|
4
|
|
|
/* |
|
5
|
|
|
* This file is part of the Fab/Vidi project under GPLv2 or later. |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please read the |
|
8
|
|
|
* LICENSE.md file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
use Fab\Vidi\Utility\BackendUtility; |
|
12
|
|
|
use TYPO3\CMS\Core\Resource\File; |
|
13
|
|
|
use TYPO3\CMS\Core\Resource\ResourceFactory; |
|
14
|
|
|
use TYPO3\CMS\Core\SingletonInterface; |
|
15
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
|
16
|
|
|
use Fab\Vidi\Domain\Model\Content; |
|
17
|
|
|
use Fab\Vidi\Tca\Tca; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* File References service. |
|
21
|
|
|
* Find a bunch of file references given by the property name. |
|
22
|
|
|
*/ |
|
23
|
|
|
class FileReferenceService implements SingletonInterface |
|
24
|
|
|
{ |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var array |
|
28
|
|
|
*/ |
|
29
|
|
|
static protected $instances = []; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Returns a class instance |
|
33
|
|
|
* |
|
34
|
|
|
* @return \Fab\Vidi\Service\FileReferenceService|object |
|
35
|
|
|
*/ |
|
36
|
|
|
static public function getInstance() |
|
37
|
|
|
{ |
|
38
|
|
|
return GeneralUtility::makeInstance(\Fab\Vidi\Service\FileReferenceService::class); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param Content $object |
|
43
|
|
|
* @param string $propertyName |
|
44
|
|
|
* @return File[] |
|
45
|
|
|
*/ |
|
46
|
|
|
public function findReferencedBy($propertyName, Content $object) |
|
47
|
|
|
{ |
|
48
|
|
|
|
|
49
|
|
|
if (!isset(self::$instances[$object->getUid()][$propertyName])) { |
|
50
|
|
|
|
|
51
|
|
|
// Initialize instances value |
|
52
|
|
|
if (!isset(self::$instances[$object->getUid()])) { |
|
53
|
|
|
self::$instances[$object->getUid()] = []; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
$fieldName = GeneralUtility::camelCaseToLowerCaseUnderscored($propertyName); |
|
57
|
|
|
$field = Tca::table($object->getDataType())->field($fieldName); |
|
58
|
|
|
if ($field->getForeignTable() === 'sys_file_reference') { |
|
59
|
|
|
$files = $this->findByFileReference($propertyName, $object); |
|
60
|
|
|
self::$instances[$object->getUid()][$propertyName] = $files; |
|
61
|
|
|
} else { |
|
62
|
|
|
// @todo the standard way of handling file references is by "sys_file_reference". Let see if there is other use cases... |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
return self::$instances[$object->getUid()][$propertyName]; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Fetch the files given an object assuming |
|
71
|
|
|
* |
|
72
|
|
|
* @param $propertyName |
|
73
|
|
|
* @param Content $object |
|
74
|
|
|
* @return File[] |
|
75
|
|
|
*/ |
|
76
|
|
|
protected function findByFileReference($propertyName, Content $object) |
|
77
|
|
|
{ |
|
78
|
|
|
|
|
79
|
|
|
$fileField = 'uid_local'; |
|
80
|
|
|
$tableName = 'sys_file_reference'; |
|
81
|
|
|
|
|
82
|
|
|
$rows = $this->getDataService()->getRecords( |
|
|
|
|
|
|
83
|
|
|
$tableName, |
|
84
|
|
|
[ |
|
85
|
|
|
'tablenames' => $object->getDataType(), |
|
86
|
|
|
'fieldname'=> GeneralUtility::camelCaseToLowerCaseUnderscored($propertyName), |
|
87
|
|
|
'uid_foreign'=> $object->getUid(), |
|
88
|
|
|
] |
|
89
|
|
|
); |
|
90
|
|
|
|
|
91
|
|
|
|
|
92
|
|
|
// Build array of Files |
|
93
|
|
|
$files = []; |
|
94
|
|
|
foreach ($rows as $row) { |
|
95
|
|
|
$files[] = ResourceFactory::getInstance()->getFileObject($row[$fileField]); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
return $files; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @return DataService |
|
103
|
|
|
*/ |
|
104
|
|
|
protected function getDataService(): string |
|
105
|
|
|
{ |
|
106
|
|
|
return GeneralUtility::makeInstance(DataService::class); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
} |
|
110
|
|
|
|
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.