1
|
|
|
<?php |
2
|
|
|
namespace Fab\Media\Grid; |
3
|
|
|
|
4
|
|
|
/* |
5
|
|
|
* This file is part of the Fab/Media 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\Grid\ColumnRendererAbstract; |
12
|
|
|
use Fab\Vidi\Service\DataService; |
13
|
|
|
use TYPO3\CMS\Backend\Template\Components\Buttons\LinkButton; |
14
|
|
|
use Fab\Vidi\Utility\BackendUtility; |
15
|
|
|
use TYPO3\CMS\Core\Imaging\Icon; |
16
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
17
|
|
|
use Fab\Vidi\Tca\Tca; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class rendering usage of an asset in the grid. |
21
|
|
|
*/ |
22
|
|
|
class UsageRenderer extends ColumnRendererAbstract |
23
|
|
|
{ |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Render usage of an asset in the grid. |
27
|
|
|
* |
28
|
|
|
* @return string |
29
|
|
|
*/ |
30
|
|
|
public function render() |
31
|
|
|
{ |
32
|
|
|
$file = $this->getFileConverter()->convert($this->object); |
33
|
|
|
|
34
|
|
|
$result = ''; |
35
|
|
|
|
36
|
|
|
// Add number of references on the top! |
37
|
|
|
if ($this->object['number_of_references'] > 1) { |
38
|
|
|
$result .= sprintf( |
39
|
|
|
'<div><strong>%s (%s)</strong></div>', |
40
|
|
|
$this->getLanguageService()->sL('LLL:EXT:media/Resources/Private/Language/locallang.xlf:references'), |
41
|
|
|
$this->object['number_of_references'] |
42
|
|
|
); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
// Render File usage |
46
|
|
|
$fileReferences = $this->getFileReferenceService()->findFileReferences($file); |
47
|
|
View Code Duplication |
if (!empty($fileReferences)) { |
|
|
|
|
48
|
|
|
|
49
|
|
|
// Finalize file references assembling. |
50
|
|
|
$result .= sprintf( |
51
|
|
|
$this->getWrappingTemplate(), |
52
|
|
|
$this->getLanguageService()->sL('LLL:EXT:media/Resources/Private/Language/locallang.xlf:file_reference'), |
53
|
|
|
$this->assembleOutput($fileReferences, array('referenceIdentifier' => 'uid_foreign', 'tableName' => 'tablenames')) |
54
|
|
|
); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
// Render link usage in RTE |
58
|
|
|
$linkSoftReferences = $this->getFileReferenceService()->findSoftLinkReferences($file); |
59
|
|
View Code Duplication |
if (!empty($linkSoftReferences)) { |
|
|
|
|
60
|
|
|
|
61
|
|
|
// Finalize link references assembling. |
62
|
|
|
$result .= sprintf( |
63
|
|
|
$this->getWrappingTemplate(), |
64
|
|
|
$this->getLanguageService()->sL('LLL:EXT:media/Resources/Private/Language/locallang.xlf:link_references_in_rte'), |
65
|
|
|
$this->assembleOutput($linkSoftReferences, array('referenceIdentifier' => 'recuid', 'tableName' => 'tablename')) |
66
|
|
|
); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
// Render image usage in RTE |
70
|
|
|
$imageSoftReferences = $this->getFileReferenceService()->findSoftImageReferences($file); |
71
|
|
View Code Duplication |
if (!empty($imageSoftReferences)) { |
|
|
|
|
72
|
|
|
|
73
|
|
|
// Finalize image references assembling. |
74
|
|
|
$result .= sprintf( |
75
|
|
|
$this->getWrappingTemplate(), |
76
|
|
|
$this->getLanguageService()->sL('LLL:EXT:media/Resources/Private/Language/locallang.xlf:image_references_in_rte'), |
77
|
|
|
$this->assembleOutput($imageSoftReferences, array('referenceIdentifier' => 'recuid', 'tableName' => 'tablename')) |
78
|
|
|
); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return $result; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Assemble output reference. |
86
|
|
|
* |
87
|
|
|
* @param array $references |
88
|
|
|
* @param array $mapping |
89
|
|
|
* @return string |
90
|
|
|
*/ |
91
|
|
|
protected function assembleOutput(array $references, array $mapping) |
92
|
|
|
{ |
93
|
|
|
|
94
|
|
|
$result = ''; |
95
|
|
|
foreach ($references as $reference) { |
96
|
|
|
$button = $this->makeLinkButton() |
97
|
|
|
->setHref($this->getEditUri($reference, $mapping)) |
98
|
|
|
->setClasses('btn-edit-reference') |
99
|
|
|
->setIcon($this->getIconFactory()->getIcon('actions-document-open', Icon::SIZE_SMALL)) |
100
|
|
|
->render(); |
101
|
|
|
|
102
|
|
|
$tableName = $reference[$mapping['tableName']]; |
103
|
|
|
$identifier = (int)$reference[$mapping['referenceIdentifier']]; |
104
|
|
|
|
105
|
|
|
$result .= sprintf( |
106
|
|
|
'<li title="">%s %s</li>', |
107
|
|
|
$button, |
108
|
|
|
$this->computeTitle($tableName, $identifier) |
109
|
|
|
); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return $result; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param string $tableName |
117
|
|
|
* @param int $identifier |
118
|
|
|
* @return string |
119
|
|
|
*/ |
120
|
|
|
protected function computeTitle($tableName, $identifier) |
121
|
|
|
{ |
122
|
|
|
$title = ''; |
123
|
|
|
if (!empty($GLOBALS['TCA'][$tableName])) { |
124
|
|
|
$title = $this->getRecordTitle($tableName, $identifier); |
125
|
|
|
if (!$title) { |
126
|
|
|
$title = Tca::table($tableName)->getTitle(); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
return $title; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @return object|LinkButton |
134
|
|
|
*/ |
135
|
|
|
protected function makeLinkButton() |
136
|
|
|
{ |
137
|
|
|
return GeneralUtility::makeInstance(LinkButton::class); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @param array $reference |
142
|
|
|
* @param array $mapping |
143
|
|
|
* @return string |
144
|
|
|
*/ |
145
|
|
|
protected function getEditUri(array $reference, array $mapping) |
146
|
|
|
{ |
147
|
|
|
|
148
|
|
|
$parameterName = sprintf('edit[%s][%s]', $reference[$mapping['tableName']], $reference[$mapping['referenceIdentifier']]); |
149
|
|
|
$uri = BackendUtility::getModuleUrl( |
150
|
|
|
'record_edit', |
151
|
|
|
array( |
152
|
|
|
$parameterName => 'edit', |
153
|
|
|
'returnUrl' => $this->getModuleUrl() |
154
|
|
|
) |
155
|
|
|
); |
156
|
|
|
return $uri; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @return string |
161
|
|
|
*/ |
162
|
|
|
protected function getModuleUrl() |
163
|
|
|
{ |
164
|
|
|
|
165
|
|
|
$additionalParameters = []; |
166
|
|
|
if (GeneralUtility::_GP('id')) { |
167
|
|
|
$additionalParameters = array( |
168
|
|
|
'id' => urldecode(GeneralUtility::_GP('id')), |
169
|
|
|
); |
170
|
|
|
} |
171
|
|
|
return BackendUtility::getModuleUrl(GeneralUtility::_GP('route'), $additionalParameters); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Return the title given a table name and an identifier. |
176
|
|
|
* |
177
|
|
|
* @param string $tableName |
178
|
|
|
* @param string $identifier |
179
|
|
|
* @return string |
180
|
|
|
*/ |
181
|
|
|
protected function getRecordTitle($tableName, $identifier) |
182
|
|
|
{ |
183
|
|
|
|
184
|
|
|
$result = ''; |
185
|
|
|
if ($tableName && (int)$identifier > 0) { |
186
|
|
|
|
187
|
|
|
$labelField = Tca::table($tableName)->getLabelField(); |
188
|
|
|
|
189
|
|
|
// Get the title of the record. |
190
|
|
|
$record = $this->getDataService() |
191
|
|
|
->getRecord($tableName, ['uid' => $identifier,]); |
192
|
|
|
|
193
|
|
|
if (!empty($record[$labelField])) { |
194
|
|
|
$result = $record[$labelField]; |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
return $result; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @return object|DataService |
203
|
|
|
*/ |
204
|
|
|
protected function getDataService(): DataService |
205
|
|
|
{ |
206
|
|
|
return GeneralUtility::makeInstance(DataService::class); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Return the wrapping HTML template. |
211
|
|
|
* |
212
|
|
|
* @return string |
213
|
|
|
*/ |
214
|
|
|
protected function getWrappingTemplate() |
215
|
|
|
{ |
216
|
|
|
return '<div style="text-decoration: underline; margin-top: 10px; margin-bottom: 10px">%s</div><ul class="usage-list">%s</ul>'; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* @return \Fab\Media\Resource\FileReferenceService|object |
221
|
|
|
*/ |
222
|
|
|
protected function getFileReferenceService() |
223
|
|
|
{ |
224
|
|
|
return GeneralUtility::makeInstance(\Fab\Media\Resource\FileReferenceService::class); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* @return \Fab\Media\TypeConverter\ContentToFileConverter|object |
229
|
|
|
*/ |
230
|
|
|
protected function getFileConverter() |
231
|
|
|
{ |
232
|
|
|
return GeneralUtility::makeInstance(\Fab\Media\TypeConverter\ContentToFileConverter::class); |
233
|
|
|
} |
234
|
|
|
} |
235
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.