|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright (C) 2022-2023 Rafael San José Tovar <[email protected]> |
|
4
|
|
|
* |
|
5
|
|
|
* This program is free software; you can redistribute it and/or modify |
|
6
|
|
|
* it under the terms of the GNU General Public License as published by |
|
7
|
|
|
* the Free Software Foundation; either version 3 of the License, or |
|
8
|
|
|
* (at your option) any later version. |
|
9
|
|
|
* |
|
10
|
|
|
* This program is distributed in the hope that it will be useful, |
|
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13
|
|
|
* GNU General Public License for more details. |
|
14
|
|
|
* |
|
15
|
|
|
* You should have received a copy of the GNU General Public License |
|
16
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
namespace Alxarafe\Controllers; |
|
20
|
|
|
|
|
21
|
|
|
use Alxarafe\Core\Base\Controller; |
|
22
|
|
|
use Alxarafe\Core\Base\View; |
|
23
|
|
|
use Alxarafe\Core\Helpers\FormatUtils; |
|
|
|
|
|
|
24
|
|
|
use Alxarafe\Core\Helpers\Utils\FileSystemUtils; |
|
|
|
|
|
|
25
|
|
|
use Alxarafe\Core\Models\Module; |
|
|
|
|
|
|
26
|
|
|
use Alxarafe\Core\Providers\FlashMessages; |
|
|
|
|
|
|
27
|
|
|
use Alxarafe\Core\Providers\ModuleManager; |
|
|
|
|
|
|
28
|
|
|
use Alxarafe\Views\ModulesView; |
|
29
|
|
|
use Symfony\Component\Finder\Finder; |
|
30
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Class Init |
|
34
|
|
|
* |
|
35
|
|
|
* |
|
36
|
|
|
* |
|
37
|
|
|
* @author Rafael San José Tovar <[email protected]> |
|
38
|
|
|
* @version 08 2021 |
|
39
|
|
|
* |
|
40
|
|
|
* @package Modules\Main |
|
41
|
|
|
*/ |
|
42
|
|
|
class Modules extends Controller |
|
43
|
|
|
{ |
|
44
|
|
|
/** |
|
45
|
|
|
* Modules folder were are stored. |
|
46
|
|
|
* |
|
47
|
|
|
* @var string |
|
48
|
|
|
*/ |
|
49
|
|
|
private $modulesFolder; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @var array |
|
53
|
|
|
*/ |
|
54
|
|
|
private $modulesList = []; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Modules constructor. |
|
58
|
|
|
*/ |
|
59
|
|
|
public function __construct() |
|
60
|
|
|
{ |
|
61
|
|
|
parent::__construct(new Module()); |
|
|
|
|
|
|
62
|
|
|
$this->modulesFolder = basePath('src' . constant('DIRECTORY_SEPARATOR') . 'Modules'); |
|
|
|
|
|
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* The start point of the controller. |
|
67
|
|
|
* |
|
68
|
|
|
* @return Response |
|
69
|
|
|
*/ |
|
70
|
|
|
public function indexMethod(): Response |
|
71
|
|
|
{ |
|
72
|
|
|
$this->modulesList = $this->getAvailableModules(); |
|
73
|
|
|
$this->updateModulesData(); |
|
74
|
|
|
|
|
75
|
|
|
return parent::indexMethod(); |
|
|
|
|
|
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Returns a list of availabe modules. |
|
80
|
|
|
* |
|
81
|
|
|
* @return array |
|
82
|
|
|
*/ |
|
83
|
|
|
private function getAvailableModules(): array |
|
84
|
|
|
{ |
|
85
|
|
|
$modules = Finder::create() |
|
86
|
|
|
->directories() |
|
87
|
|
|
->depth(0) |
|
88
|
|
|
->in($this->modulesFolder) |
|
89
|
|
|
->sortByName(); |
|
90
|
|
|
$modulesList = []; |
|
91
|
|
|
foreach ($modules as $module) { |
|
92
|
|
|
$modulesList[$module->getFileName()] = str_replace(basePath(DIRECTORY_SEPARATOR), '', $module->getPathName()); |
|
|
|
|
|
|
93
|
|
|
} |
|
94
|
|
|
return $modulesList; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Updated all modules to database. |
|
99
|
|
|
*/ |
|
100
|
|
|
private function updateModulesData(): void |
|
101
|
|
|
{ |
|
102
|
|
|
foreach ($this->modulesList as $name => $path) { |
|
103
|
|
|
$module = new Module(); |
|
104
|
|
|
if (!$module->getBy('name', $name)) { |
|
105
|
|
|
$module->name = $name; |
|
106
|
|
|
$module->path = $path; |
|
107
|
|
|
$module->updated_date = FormatUtils::getFormatted(FormatUtils::getFormatDateTime()); |
|
108
|
|
|
$module->save(); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Default create method for new registers. |
|
115
|
|
|
* |
|
116
|
|
|
* @return Response |
|
117
|
|
|
*/ |
|
118
|
|
|
public function createMethod(): Response |
|
119
|
|
|
{ |
|
120
|
|
|
// TODO: Implement createMethod() method. |
|
121
|
|
|
// Require allow to submit a file |
|
122
|
|
|
return parent::createMethod(); |
|
|
|
|
|
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Default read method for new registers. |
|
127
|
|
|
* |
|
128
|
|
|
* @return Response |
|
129
|
|
|
*/ |
|
130
|
|
|
public function readMethod(): Response |
|
131
|
|
|
{ |
|
132
|
|
|
// TODO: Implement readMethod() method. |
|
133
|
|
|
// The data can be showed from table. |
|
134
|
|
|
return parent::readMethod(); |
|
|
|
|
|
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Default update method for update an individual register. |
|
139
|
|
|
* |
|
140
|
|
|
* @return Response |
|
141
|
|
|
*/ |
|
142
|
|
|
public function updateMethod(): Response |
|
143
|
|
|
{ |
|
144
|
|
|
// TODO: Implement updateMethod() method. |
|
145
|
|
|
// The data must be updated from each module. |
|
146
|
|
|
return parent::updateMethod(); |
|
|
|
|
|
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Default delete method for delete an individual register. |
|
151
|
|
|
* |
|
152
|
|
|
* @return Response |
|
153
|
|
|
*/ |
|
154
|
|
|
public function deleteMethod(): Response |
|
155
|
|
|
{ |
|
156
|
|
|
if (!$this->canAccess || !$this->canDelete) { |
|
|
|
|
|
|
157
|
|
|
$this->renderer->setTemplate('master/noaccess'); |
|
|
|
|
|
|
158
|
|
|
return $this->sendResponseTemplate(); |
|
|
|
|
|
|
159
|
|
|
} |
|
160
|
|
|
$id = $this->request->query->get($this->model->getIdField()); |
|
|
|
|
|
|
161
|
|
|
$this->model = new Module(); |
|
|
|
|
|
|
162
|
|
|
if ($this->model->load($id)) { |
|
163
|
|
|
$this->model->enabled = 0; |
|
164
|
|
|
if ($this->model->save()) { |
|
165
|
|
|
FlashMessages::getInstance()::setSuccess( |
|
166
|
|
|
$this->translator->trans('module-disabled', ['%moduleName%' => $this->model->{$this->model->getNameField()}]) |
|
|
|
|
|
|
167
|
|
|
); |
|
168
|
|
|
} |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
FileSystemUtils::rrmdir(basePath($this->model->path)); |
|
|
|
|
|
|
172
|
|
|
return parent::deleteMethod(); |
|
|
|
|
|
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Default enable method for enable an individual register. |
|
177
|
|
|
* |
|
178
|
|
|
* @return Response |
|
179
|
|
|
*/ |
|
180
|
|
|
public function enableMethod(): Response |
|
181
|
|
|
{ |
|
182
|
|
|
if (!$this->canAccess || !$this->canUpdate) { |
|
|
|
|
|
|
183
|
|
|
$this->renderer->setTemplate('master/noaccess'); |
|
|
|
|
|
|
184
|
|
|
return $this->sendResponseTemplate(); |
|
185
|
|
|
} |
|
186
|
|
|
$id = $this->request->query->get($this->model->getIdField()); |
|
|
|
|
|
|
187
|
|
|
$this->model = new Module(); |
|
|
|
|
|
|
188
|
|
|
if ($this->model->load($id)) { |
|
189
|
|
|
$modelName = $this->model->{$this->model->getNameField()}; |
|
190
|
|
|
ModuleManager::getInstance()::enableModule($modelName); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
return $this->redirect(baseUrl('index.php?' . constant('CALL_CONTROLLER') . '=' . $this->shortName)); |
|
|
|
|
|
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* Default disable method for disable an individual register. |
|
198
|
|
|
* |
|
199
|
|
|
* @return Response |
|
200
|
|
|
*/ |
|
201
|
|
|
public function disableMethod(): Response |
|
202
|
|
|
{ |
|
203
|
|
|
if (!$this->canAccess || !$this->canUpdate) { |
|
|
|
|
|
|
204
|
|
|
$this->renderer->setTemplate('master/noaccess'); |
|
|
|
|
|
|
205
|
|
|
return $this->sendResponseTemplate(); |
|
206
|
|
|
} |
|
207
|
|
|
$id = $this->request->query->get($this->model->getIdField()); |
|
|
|
|
|
|
208
|
|
|
$this->model = new Module(); |
|
|
|
|
|
|
209
|
|
|
if ($this->model->load($id)) { |
|
210
|
|
|
$modelName = $this->model->{$this->model->getNameField()}; |
|
211
|
|
|
ModuleManager::getInstance()::disableModule($modelName); |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
return $this->redirect(baseUrl('index.php?' . constant('CALL_CONTROLLER') . '=' . $this->shortName)); |
|
|
|
|
|
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
/**. |
|
218
|
|
|
* Returns the page details. |
|
219
|
|
|
* |
|
220
|
|
|
* @return array |
|
221
|
|
|
*/ |
|
222
|
|
|
public function pageDetails(): array |
|
223
|
|
|
{ |
|
224
|
|
|
$details = [ |
|
225
|
|
|
'title' => 'modules-title', |
|
226
|
|
|
'icon' => '<i class="fas fa-cogs"></i>', |
|
227
|
|
|
'description' => 'modules-description', |
|
228
|
|
|
'menu' => 'admin', |
|
229
|
|
|
]; |
|
230
|
|
|
return $details; |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
/** |
|
234
|
|
|
* Returns a list of actions buttons. By default returns Read/Update/Delete actions. |
|
235
|
|
|
* If some needs to be replace, replace it on final class. |
|
236
|
|
|
* |
|
237
|
|
|
* @param string $id |
|
238
|
|
|
* |
|
239
|
|
|
* @return array |
|
240
|
|
|
*/ |
|
241
|
|
|
public function getActionButtons(string $id = ''): array |
|
242
|
|
|
{ |
|
243
|
|
|
$actionButtons = []; |
|
244
|
|
|
$actionButtons['enable'] = [ |
|
245
|
|
|
'class' => 'btn btn-success btn-sm', |
|
246
|
|
|
'type' => 'button', |
|
247
|
|
|
'link' => $this->url . '&' . constant('METHOD_CONTROLLER') . '=enable&id=' . $id, |
|
|
|
|
|
|
248
|
|
|
'icon' => '<i class="far fa-check-square"></i>', |
|
249
|
|
|
'text' => $this->translator->trans('enable'), |
|
|
|
|
|
|
250
|
|
|
]; |
|
251
|
|
|
$actionButtons['disable'] = [ |
|
252
|
|
|
'class' => 'btn btn-warning btn-sm', |
|
253
|
|
|
'type' => 'button', |
|
254
|
|
|
'link' => $this->url . '&' . constant('METHOD_CONTROLLER') . '=disable&id=' . $id, |
|
255
|
|
|
'icon' => '<i class="far fa-square"></i>', |
|
256
|
|
|
'text' => $this->translator->trans('disable'), |
|
257
|
|
|
]; |
|
258
|
|
|
|
|
259
|
|
|
$actionButtons = array_merge($actionButtons, parent::getActionButtons($id)); |
|
|
|
|
|
|
260
|
|
|
unset($actionButtons['read'], $actionButtons['update']); |
|
261
|
|
|
|
|
262
|
|
|
return $actionButtons; |
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
|
|
public function setTemplate(): string |
|
266
|
|
|
{ |
|
267
|
|
|
return 'modules'; |
|
268
|
|
|
} |
|
269
|
|
|
} |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths