-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCompactViewController.php
More file actions
156 lines (135 loc) · 4.24 KB
/
CompactViewController.php
File metadata and controls
156 lines (135 loc) · 4.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
namespace BeechIt\Bynder\Controller;
/*
* This source file is proprietary property of Beech.it
* Date: 20-2-18
* All code (c) Beech.it all rights reserved
*/
use BeechIt\Bynder\Service\BynderService;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
use TYPO3\CMS\Core\Resource\Index\Indexer;
use TYPO3\CMS\Core\Resource\ResourceStorage;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Fluid\View\StandaloneView;
/**
* Class CompactViewController
*/
class CompactViewController
{
/**
* Fluid Standalone View
*
* @var StandaloneView
*/
protected $view;
/**
* TemplateRootPath
*
* @var string[]
*/
protected $templateRootPaths = ['EXT:bynder/Resources/Private/Templates/CompactView'];
/**
* PartialRootPath
*
* @var string[]
*/
protected $partialRootPaths = ['EXT:bynder/Resources/Private/Partials/CompactView'];
/**
* LayoutRootPath
*
* @var string[]
*/
protected $layoutRootPaths = ['EXT:bynder/Resources/Private/Layouts/CompactView'];
/**
* @var BynderService
*/
protected $bynderService;
/**
* CompactViewController constructor.
*/
public function __construct()
{
$this->bynderService = GeneralUtility::makeInstance(BynderService::class);
$this->view = GeneralUtility::makeInstance(StandaloneView::class);
$this->view->setPartialRootPaths($this->partialRootPaths);
$this->view->setTemplateRootPaths($this->templateRootPaths);
$this->view->setLayoutRootPaths($this->layoutRootPaths);
}
/**
* Action: Display compact view
*
* @param ServerRequestInterface $request
* @param ResponseInterface $response
* @return ResponseInterface
*/
public function indexAction(ServerRequestInterface $request, ResponseInterface $response)
{
$this->view->setTemplate('Index');
$this->view->assignMultiple([
'language' => $this->getBackendUserAuthentication()->uc['lang'] ?: ($this->getBackendUserAuthentication()->user['lang'] ?: 'en_EN'),
'apiBaseUrl' => $this->bynderService->getApiBaseUrl(),
'element' => $request->getQueryParams()['element'],
'assetTypes' => $request->getQueryParams()['assetTypes']
]);
$response->getBody()->write($this->view->render());
return $response;
}
/**
* Action: Retrieve file from storage
*
* @param ServerRequestInterface $request
* @param ResponseInterface $response
* @return ResponseInterface
*/
public function getFilesAction(ServerRequestInterface $request, ResponseInterface $response)
{
$files = [];
$error = '';
$fileStorage = $this->getBynderStorage();
foreach ($request->getParsedBody()['files'] ?? [] as $fileIdentifier) {
$file = $fileStorage->getFile($fileIdentifier);
if ($file) {
// (Re)Fetch metadata
$this->getIndexer($file->getStorage())->extractMetaData($file);
$files[] = $file->getUid();
}
}
if ($files === []) {
$error = 'No files given/found';
}
$response->getBody()->write(json_encode(['files' => $files, 'error' => $error]));
return $response;
}
/**
* @return ResourceStorage
*/
protected function getBynderStorage(): ResourceStorage
{
/** @var ResourceStorage $fileStorage */
foreach ($this->getBackendUserAuthentication()->getFileStorages() as $fileStorage) {
if ($fileStorage->getDriverType() === 'bynder') {
return $fileStorage;
}
}
throw new \InvalidArgumentException('Missing Bynder file storage');
}
/**
* @return BackendUserAuthentication
*/
protected function getBackendUserAuthentication()
{
return $GLOBALS['BE_USER'];
}
/**
* Gets the Indexer.
*
* @param ResourceStorage $storage
* @return Indexer
*/
protected function getIndexer(ResourceStorage $storage)
{
return GeneralUtility::makeInstance(Indexer::class, $storage);
}
}