1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Platine Framework |
5
|
|
|
* |
6
|
|
|
* Platine Framework is a lightweight, high-performance, simple and elegant |
7
|
|
|
* PHP Web framework |
8
|
|
|
* |
9
|
|
|
* This content is released under the MIT License (MIT) |
10
|
|
|
* |
11
|
|
|
* Copyright (c) 2020 Platine Framework |
12
|
|
|
* |
13
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
14
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
15
|
|
|
* in the Software without restriction, including without limitation the rights |
16
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
17
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
18
|
|
|
* furnished to do so, subject to the following conditions: |
19
|
|
|
* |
20
|
|
|
* The above copyright notice and this permission notice shall be included in all |
21
|
|
|
* copies or substantial portions of the Software. |
22
|
|
|
* |
23
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
24
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
25
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
26
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
27
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
28
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
29
|
|
|
* SOFTWARE. |
30
|
|
|
*/ |
31
|
|
|
|
32
|
|
|
declare(strict_types=1); |
33
|
|
|
|
34
|
|
|
namespace Platine\Framework\Http\Action; |
35
|
|
|
|
36
|
|
|
use Platine\Framework\Config\AppDatabaseConfig; |
37
|
|
|
use Platine\Framework\Enum\YesNoStatus; |
38
|
|
|
use Platine\Framework\Helper\ActionHelper; |
39
|
|
|
use Platine\Http\ResponseInterface; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @class BaseConfigurationAction |
43
|
|
|
* @package Platine\Framework\Http\Action |
44
|
|
|
*/ |
45
|
|
|
abstract class BaseConfigurationAction extends BaseAction |
46
|
|
|
{ |
47
|
|
|
/** |
48
|
|
|
* {@inheritdoc} |
49
|
|
|
*/ |
50
|
|
|
public function __construct( |
51
|
|
|
ActionHelper $actionHelper, |
52
|
|
|
protected AppDatabaseConfig $dbConfig, |
53
|
|
|
) { |
54
|
|
|
parent::__construct($actionHelper); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritdoc} |
59
|
|
|
*/ |
60
|
|
|
public function respond(): ResponseInterface |
61
|
|
|
{ |
62
|
|
|
$this->setView($this->getViewName()); |
63
|
|
|
$param = $this->param; |
64
|
|
|
$request = $this->request; |
65
|
|
|
|
66
|
|
|
$paramName = $this->getParamName(); |
67
|
|
|
$validatorName = $this->getValidatorName(); |
68
|
|
|
|
69
|
|
|
if ($request->getMethod() === 'GET') { |
70
|
|
|
$configToParam = (new $paramName())->fromConfig($this->dbConfig); |
71
|
|
|
$this->addContext('param', $configToParam); |
72
|
|
|
|
73
|
|
|
return $this->viewResponse(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$formParam = new $paramName($param->posts()); |
77
|
|
|
$this->addContext('param', $formParam); |
78
|
|
|
|
79
|
|
|
$validator = new $validatorName($formParam, $this->lang); |
80
|
|
|
if ($validator->validate() === false) { |
81
|
|
|
$this->addContext('errors', $validator->getErrors()); |
82
|
|
|
|
83
|
|
|
return $this->viewResponse(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
// Save the configuration |
87
|
|
|
$this->save(); |
88
|
|
|
|
89
|
|
|
$this->flash->setSuccess($this->lang->tr('Configuration saved successfully')); |
90
|
|
|
|
91
|
|
|
return $this->redirect($this->getRouteName()); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Save the configuration |
96
|
|
|
* @return void |
97
|
|
|
*/ |
98
|
|
|
protected function save(): void |
99
|
|
|
{ |
100
|
|
|
$params = $this->getParamDefinitions(); |
101
|
|
|
$moduleName = $this->getModuleName(); |
102
|
|
|
//TODO get all configuration in order to know if need insert/update |
103
|
|
|
$this->dbConfig->get(sprintf('%s.', $moduleName)); |
104
|
|
|
|
105
|
|
|
foreach ($params as $name => $arr) { |
106
|
|
|
$postKey = $arr['key'] ?? $name; |
107
|
|
|
$value = $this->param->post($postKey); |
108
|
|
|
// if is array and empty |
109
|
|
|
if ($arr['type'] === 'array' && empty($value)) { |
110
|
|
|
$value = []; |
111
|
|
|
} elseif ($arr['type'] === 'callable' && is_callable($arr['callable'])) { |
112
|
|
|
$value = call_user_func_array($arr['callable'], [$this->dbConfig]); |
113
|
|
|
if ($value !== null) { |
114
|
|
|
$arr['type'] = gettype($value); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
if (is_array($value)) { |
119
|
|
|
$value = serialize($value); |
120
|
|
|
} |
121
|
|
|
$key = sprintf('%s.%s', $moduleName, $name); |
122
|
|
|
if ($this->dbConfig->has($key)) { |
123
|
|
|
$entity = $this->dbConfig->getLoader()->loadConfig([ |
124
|
|
|
'module' => $moduleName, |
125
|
|
|
'name' => $name, |
126
|
|
|
]); |
127
|
|
|
|
128
|
|
|
if ($entity !== null) { |
129
|
|
|
$entity->name = $name; |
130
|
|
|
$entity->env = null; |
131
|
|
|
$entity->module = $moduleName; |
132
|
|
|
$entity->value = $value; |
133
|
|
|
$entity->status = YesNoStatus::YES; |
134
|
|
|
$entity->type = $arr['type']; |
135
|
|
|
$entity->comment = $arr['comment']; |
136
|
|
|
|
137
|
|
|
$this->dbConfig->getLoader()->updateConfig($entity); |
138
|
|
|
} |
139
|
|
|
} else { |
140
|
|
|
$this->dbConfig->getLoader()->insertConfig([ |
141
|
|
|
'name' => $name, |
142
|
|
|
'env' => null, |
143
|
|
|
'module' => $moduleName, |
144
|
|
|
'value' => $value, |
145
|
|
|
'status' => YesNoStatus::YES, |
146
|
|
|
'type' => $arr['type'], |
147
|
|
|
'comment' => $arr['comment'], |
148
|
|
|
]); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Return the view name |
155
|
|
|
* @return string |
156
|
|
|
*/ |
157
|
|
|
protected function getViewName(): string |
158
|
|
|
{ |
159
|
|
|
return ''; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Return the route name |
164
|
|
|
* @return string |
165
|
|
|
*/ |
166
|
|
|
protected function getRouteName(): string |
167
|
|
|
{ |
168
|
|
|
return ''; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Return the form parameter class name |
173
|
|
|
* @return class-string<\Platine\Framework\Form\Param\BaseParam> |
|
|
|
|
174
|
|
|
*/ |
175
|
|
|
abstract protected function getParamName(): string; |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Return the validation class name |
179
|
|
|
* @return class-string<\Platine\Framework\Form\Validator\AbstractValidator> |
|
|
|
|
180
|
|
|
*/ |
181
|
|
|
abstract protected function getValidatorName(): string; |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Return the parameters definition |
185
|
|
|
* @return array<string, array<string, mixed>> |
186
|
|
|
*/ |
187
|
|
|
abstract protected function getParamDefinitions(): array; |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Return the configuration module name |
191
|
|
|
* @return string |
192
|
|
|
*/ |
193
|
|
|
abstract protected function getModuleName(): string; |
194
|
|
|
} |
195
|
|
|
|