/
external_file_provider.php
176 lines (139 loc) · 5.73 KB
/
external_file_provider.php
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php /** @noinspection PhpInconsistentReturnPointsInspection */
namespace Concrete\Controller\SinglePage\Dashboard\System\Files;
use Concrete\Core\File\ExternalFileProvider\ExternalFileProviderFactory;
use Concrete\Core\File\ExternalFileProvider\Type\Type;
use Concrete\Core\Entity\File\ExternalFileProvider\Type\Type as TypeEntity;
use Concrete\Core\Http\ResponseFactory;
use Concrete\Core\Page\Controller\DashboardPageController;
use Concrete\Core\Entity\File\ExternalFileProvider\ExternalFileProvider as ExternalFileProviderEntity;
use Concrete\Core\Support\Facade\Url;
use Concrete\Core\Utility\Service\Validation\Strings;
class ExternalFileProvider extends DashboardPageController
{
/** @var ResponseFactory */
protected $responseFactory;
public function on_start()
{
parent::on_start();
$this->responseFactory = $this->app->make(ResponseFactory::class);
}
public function view()
{
$this->set('externalFileProviders', $this->app->make(ExternalFileProviderFactory::class)->fetchList());
$types = [];
foreach (Type::getList() as $type) {
$types[$type->getID()] = $type->getName();
}
$this->set('types', $types);
}
public function select_type()
{
$efpTypeID = $this->request('efpTypeID');
$type = Type::getByID($efpTypeID);
$this->set('type', $type);
}
public function external_file_provider_added()
{
$this->set('message', t('External file provider added.'));
$this->view();
}
public function external_file_provider_deleted()
{
$this->set('message', t('External file provider removed.'));
$this->view();
}
public function external_file_provider_updated()
{
$this->set('message', t('External file provider saved.'));
$this->view();
}
public function edit($efpID = false)
{
$externalFileProvider = $efpID ? $this->app->make(ExternalFileProviderFactory::class)->fetchByID($efpID) : null;
if ($externalFileProvider !== null) {
/* @var ExternalFileProviderEntity $externalFileProvider */
$this->set('externalFileProvider', $externalFileProvider);
$this->set('type', $externalFileProvider->getTypeObject());
} else {
return $this->responseFactory->redirect(Url::to('/dashboard/system/files/external_file_provider'));
}
}
/**
* @return ExternalFileProviderEntity|null
*/
protected function validateStorageRequest()
{
/** @var Strings $val */
$val = $this->app->make(Strings::class);
$type = Type::getByID($this->request->get('efpTypeID'));
if (!$type instanceof TypeEntity) {
$this->error->add(t('Invalid type object.'));
} else {
$e = $type->getConfigurationObject()->validateRequest($this->request);
if (is_object($e)) {
$this->error->add($e);
}
}
if (!$val->notempty($this->request->request->get('efpName'))) {
$this->error->add(t('Your external file provider must have a name.'));
}
return $type;
}
public function update()
{
$type = $this->validateStorageRequest();
$post = $this->request->request;
$efpID = $post->get('efpID');
$efp = $efpID ? $this->app->make(ExternalFileProviderFactory::class)->fetchByID($efpID) : null;
/* @var ExternalFileProviderEntity|null $efp */
if (!$this->token->validate('update')) {
$this->error->add($this->token->getErrorMessage());
}
if ($efp === null) {
$this->error->add(t('Invalid external file provider object.'));
}
if (!$this->error->has()) {
$configuration = $type->getConfigurationObject();
$configuration->loadFromRequest($this->request);
$efp->setName($post->get('efpName'));
$efp->setConfigurationObject($configuration);
$efp->save();
return $this->responseFactory->redirect(Url::to('/dashboard/system/files/external_file_provider', 'external_file_provider_updated'));
}
$this->edit($efpID);
}
public function delete()
{
if (!$this->token->validate('delete')) {
$this->error->add($this->token->getErrorMessage());
}
$efpID = $this->request->request->get('efpID');
$efp = $efpID ? $this->app->make(ExternalFileProviderFactory::class)->fetchByID($efpID) : null;
/* @var ExternalFileProviderEntity|null $efp */
if ($efp === null) {
$this->error->add(t('Invalid external file provider object.'));
}
if (!$this->error->has()) {
$efp->delete();
return $this->responseFactory->redirect(Url::to('/dashboard/system/files/external_file_provider', 'external_file_provider_deleted'));
}
$this->edit($efpID);
}
public function add()
{
$type = $this->validateStorageRequest();
if (!$this->token->validate('add')) {
$this->error->add($this->token->getErrorMessage());
}
if (!$this->error->has()) {
$configuration = $type->getConfigurationObject();
$configuration->loadFromRequest($this->request);
$factory = $this->app->make(ExternalFileProviderFactory::class);
/* @var ExternalFileProviderFactory $factory */
$externalFileProvider = $factory->create($configuration, $this->request->request->get('efpName'));
$factory->persist($externalFileProvider);
return $this->responseFactory->redirect(Url::to('/dashboard/system/files/external_file_provider', 'external_file_provider_added'));
}
$this->set('type', $type);
}
}