/
EditPageOption.php
299 lines (264 loc) · 9.53 KB
/
EditPageOption.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
<?php
/**
* This file is part of FacturaScripts
* Copyright (C) 2017-2022 Carlos Garcia Gomez <carlos@facturascripts.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace FacturaScripts\Core\Controller;
use FacturaScripts\Core\Base\Controller;
use FacturaScripts\Core\Base\ControllerPermissions;
use FacturaScripts\Core\Base\DataBase\DataBaseWhere;
use FacturaScripts\Core\Lib\Widget\VisualItemLoadEngine;
use FacturaScripts\Dinamic\Model\CodeModel;
use FacturaScripts\Dinamic\Model\PageOption;
use FacturaScripts\Dinamic\Model\User;
use Symfony\Component\HttpFoundation\Response;
/**
* Edit option for any page.
*
* @author Carlos García Gómez <carlos@facturascripts.com>
* @author Jose Antonio Cuello <yopli2000@gmail.com>
* @author Fco. Antonio Moreno Pérez <famphuelva@gmail.com>
*/
class EditPageOption extends Controller
{
/**
* Contains the url to go back.
*
* @var string
*/
public $backPage;
/**
* @var array
*/
public $columns = [];
/**
* @var array
*/
public $modals = [];
/**
* Details of the view configuration
*
* @var PageOption
*/
public $model;
/**
* @var array
*/
public $rows = [];
/**
* Selected user, for which the controller columns are created or modified
*
* @var string
*/
public $selectedUser;
/**
* Selected view, for which columns are created or modified
*
* @var string
*/
public $selectedViewName;
public function getPageData(): array
{
$data = parent::getPageData();
$data['menu'] = 'admin';
$data['showonmenu'] = false;
$data['title'] = 'options';
$data['icon'] = 'fas fa-wrench';
return $data;
}
/**
* Get the list of users, excluding the user admin
*
* @return array
*/
public function getUserList(): array
{
$result = [];
$users = CodeModel::all(User::tableName(), 'nick', 'nick', false);
foreach ($users as $codeModel) {
if ($codeModel->code != 'admin') {
$result[$codeModel->code] = $codeModel->description;
}
}
return $result;
}
/**
* Runs the controller's private logic.
*
* @param Response $response
* @param User $user
* @param ControllerPermissions $permissions
*/
public function privateCore(&$response, $user, $permissions)
{
parent::privateCore($response, $user, $permissions);
$this->model = new PageOption();
$this->loadSelectedViewName();
$this->backPage = $this->request->get('url') ?: $this->selectedViewName;
$this->selectedUser = $this->user->admin ? $this->request->get('nick') : $this->user->nick;
$this->loadPageOptions();
$action = $this->request->get('action', '');
switch ($action) {
case 'delete':
$this->deleteAction();
break;
case 'save':
$this->saveAction();
break;
}
}
/**
* Delete configuration for view
*/
protected function deleteAction()
{
if (false === $this->permissions->allowDelete) {
$this->toolBox()->i18nLog()->warning('not-allowed-delete');
return;
}
if ($this->model->delete()) {
$this->toolBox()->i18nLog()->notice('record-deleted-correctly');
$this->loadPageOptions();
return;
}
$this->toolBox()->i18nLog()->warning('default-not-deletable');
}
/**
* Load the display options to edit.
* If it does not find them in the database,
* it loads the default options of the xml view.
*/
protected function loadPageOptions()
{
if ($this->selectedUser && false === $this->loadPageOptionsForUser()) {
VisualItemLoadEngine::installXML($this->selectedViewName, $this->model);
}
if (empty($this->selectedUser) && false === $this->loadPageOptionsForAll()) {
VisualItemLoadEngine::installXML($this->selectedViewName, $this->model);
}
VisualItemLoadEngine::loadArray($this->columns, $this->modals, $this->rows, $this->model);
}
protected function loadSelectedViewName()
{
$code = $this->request->get('code', '');
if (false === strpos($code, '-')) {
$this->selectedViewName = $code;
return;
}
$parts = explode('-', $code);
$this->selectedViewName = empty($parts) ? $code : $parts[0];
}
/**
* Save new configuration for view
*/
protected function saveAction()
{
if (false === $this->permissions->allowUpdate) {
$this->toolBox()->i18nLog()->warning('not-allowed-modify');
return;
}
foreach ($this->model->columns as $key1 => $group) {
if ($group['tag'] === 'column') {
$name = $group['name'];
$this->setColumnOption($this->model->columns[$key1], $name, 'title', false, false);
$this->setColumnOption($this->model->columns[$key1], $name, 'display', false, false);
$this->setColumnOption($this->model->columns[$key1], $name, 'level', false, true);
$this->setColumnOption($this->model->columns[$key1], $name, 'readonly', true, true);
$this->setColumnOption($this->model->columns[$key1], $name, 'decimal', true, true);
$this->setColumnOption($this->model->columns[$key1], $name, 'numcolumns', false, true);
$this->setColumnOption($this->model->columns[$key1], $name, 'order', false, true);
continue;
}
foreach ($group['children'] as $key2 => $col) {
$name = $col['name'];
$this->setColumnOption($this->model->columns[$key1]['children'][$key2], $name, 'title', false, false);
$this->setColumnOption($this->model->columns[$key1]['children'][$key2], $name, 'display', false, false);
$this->setColumnOption($this->model->columns[$key1]['children'][$key2], $name, 'level', false, true);
$this->setColumnOption($this->model->columns[$key1]['children'][$key2], $name, 'readonly', true, true);
$this->setColumnOption($this->model->columns[$key1]['children'][$key2], $name, 'decimal', true, true);
$this->setColumnOption($this->model->columns[$key1]['children'][$key2], $name, 'numcolumns', false, true);
$this->setColumnOption($this->model->columns[$key1]['children'][$key2], $name, 'order', false, true);
}
}
if ($this->model->save()) {
$this->toolBox()->i18nLog()->notice('record-updated-correctly');
$this->loadPageOptions();
return;
}
$this->toolBox()->i18nLog()->error('record-save-error');
}
/**
* Loads the general display options for all users,
* and indicates if they exist or not.
*
* @return bool
*/
private function loadPageOptionsForAll(): bool
{
$where = [
new DataBaseWhere('name', $this->selectedViewName),
new DataBaseWhere('nick', null, 'IS'),
];
return $this->model->loadFromCode('', $where);
}
/**
* Loads the display options specific to the user.
* If they do not exist, look for the display options common to all users.
* In either case, it indicates whether it has found a configuration.
*
* @return bool
*/
private function loadPageOptionsForUser(): bool
{
$where = [
new DataBaseWhere('name', $this->selectedViewName),
new DataBaseWhere('nick', $this->selectedUser),
];
if ($this->model->loadFromCode('', $where)) {
// Existen opciones para el usuario.
return true;
}
if (false === $this->loadPageOptionsForAll()) {
// No existe opciones generales. Asignamos las opciones por defecto de la vista xml al usuario.
$this->model->nick = $this->selectedUser;
return false;
}
// No existe opciones para el usuario. Clonamos las generales.
$this->model->id = null;
$this->model->nick = $this->selectedUser;
return true;
}
/**
* @param array $column
* @param string $name
* @param string $key
* @param bool $isWidget
* @param bool $allowEmpty
*/
private function setColumnOption(&$column, string $name, string $key, bool $isWidget, bool $allowEmpty)
{
$newValue = self::toolBox()::utils()::noHtml($this->request->request->get($name . '-' . $key));
if ($isWidget) {
if (!empty($newValue) || $allowEmpty) {
$column['children'][0][$key] = $newValue;
}
return;
}
if (!empty($newValue) || $allowEmpty) {
$column[$key] = $newValue;
}
}
}