/
BaseController.php
483 lines (422 loc) · 14 KB
/
BaseController.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
<?php
/**
* This file is part of FacturaScripts
* Copyright (C) 2017-2024 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\Lib\ExtendedController;
use Exception;
use FacturaScripts\Core\Base\Controller;
use FacturaScripts\Core\Base\ControllerPermissions;
use FacturaScripts\Core\Base\DataBase\DataBaseWhere;
use FacturaScripts\Core\Lib\Widget\VisualItem;
use FacturaScripts\Core\Model\Base\ModelClass;
use FacturaScripts\Core\Tools;
use FacturaScripts\Dinamic\Lib\ExportManager;
use FacturaScripts\Dinamic\Model\CodeModel;
use FacturaScripts\Dinamic\Model\User;
use Symfony\Component\HttpFoundation\Response;
/**
* Description of BaseController
*
* @author Carlos García Gómez <carlos@facturascripts.com>
*/
abstract class BaseController extends Controller
{
const MODEL_NAMESPACE = '\\FacturaScripts\\Dinamic\\Model\\';
/**
* Indicates the active view.
*
* @var string
*/
public $active;
/**
* Model to use with select and autocomplete filters.
*
* @var CodeModel
*/
public $codeModel;
/**
* Indicates current view, when drawing.
*
* @var string
*/
private $current;
/**
* Object to export data.
*
* @var ExportManager
*/
public $exportManager;
/**
* List of views displayed by the controller.
*
* @var BaseView[]
*/
public $views = [];
/**
* Inserts the views or tabs to display.
*/
abstract protected function createViews();
/**
* Loads the data to display.
*
* @param string $viewName
* @param BaseView $view
*/
abstract protected function loadData($viewName, $view);
/**
* Initializes all the objects and properties.
*
* @param string $className
* @param string $uri
*/
public function __construct(string $className, string $uri = '')
{
parent::__construct($className, $uri);
$activeTabGet = $this->request->query->get('activetab', '');
$this->active = $this->request->request->get('activetab', $activeTabGet);
$this->codeModel = new CodeModel();
$this->exportManager = new ExportManager();
}
/**
* Adds a new button to the tab.
*
* @param string $viewName
* @param array $btnArray
* @return BaseView
*/
public function addButton(string $viewName, array $btnArray): BaseView
{
if (!array_key_exists($viewName, $this->views)) {
throw new Exception('View not found: ' . $viewName);
}
$rowType = isset($btnArray['row']) ? 'footer' : 'actions';
$row = $this->views[$viewName]->getRow($rowType);
if ($row) {
$row->addButton($btnArray);
}
return $this->tab($viewName);
}
/**
* @param string $viewName
* @param BaseView $view
* @return BaseView
*/
public function addCustomView(string $viewName, BaseView $view): BaseView
{
if ($viewName !== $view->getViewName()) {
throw new Exception('$viewName must be equals to $view->name');
}
$view->loadPageOptions($this->user);
$this->views[$viewName] = $view;
if (empty($this->active)) {
$this->active = $viewName;
}
return $view;
}
public function getCurrentView(): BaseView
{
return $this->tab($this->current);
}
/**
* Returns the name assigned to the main view
*
* @return string
*/
public function getMainViewName(): string
{
foreach (array_keys($this->views) as $key) {
return $key;
}
return '';
}
/**
* Returns the configuration value for the indicated view.
*
* @param string $viewName
* @param string $property
* @return mixed
*/
public function getSettings(string $viewName, string $property)
{
return $this->tab($viewName)->settings[$property] ?? null;
}
/**
* Return the value for a field in the model of the view.
*
* @param string $viewName
* @param string $fieldName
* @return mixed
*/
public function getViewModelValue(string $viewName, string $fieldName)
{
return $this->tab($viewName)->model->{$fieldName} ?? null;
}
public function listView(string $viewName): ListView
{
if (isset($this->views[$viewName]) && $this->views[$viewName] instanceof ListView) {
return $this->views[$viewName];
}
throw new Exception('ListView not found: ' . $viewName);
}
/**
* 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);
VisualItem::setToken($this->multiRequestProtection->newToken());
// Create the views to display
$this->createViews();
$this->pipe('createViews');
}
public function setCurrentView(string $viewName): void
{
$this->current = $viewName;
}
/**
* Set value for setting of a view
*
* @param string $viewName
* @param string $property
* @param mixed $value
* @return BaseView
*/
public function setSettings(string $viewName, string $property, $value): BaseView
{
return $this->tab($viewName)->setSettings($property, $value);
}
public function tab(string $viewName): BaseView
{
if (isset($this->views[$viewName])) {
return $this->views[$viewName];
}
throw new Exception('View not found: ' . $viewName);
}
/**
* Run the autocomplete action.
* Returns a JSON string for the searched values.
*
* @return array
*/
protected function autocompleteAction(): array
{
$data = $this->requestGet(['field', 'fieldcode', 'fieldfilter', 'fieldtitle', 'formname', 'source', 'strict', 'term']);
if ($data['source'] == '') {
return $this->getAutocompleteValues($data['formname'], $data['field']);
}
$where = [];
foreach (DataBaseWhere::applyOperation($data['fieldfilter'] ?? '') as $field => $operation) {
$value = $this->request->get($field);
$where[] = new DataBaseWhere($field, $value, '=', $operation);
}
$results = [];
foreach ($this->codeModel->search($data['source'], $data['fieldcode'], $data['fieldtitle'], $data['term'], $where) as $value) {
$results[] = ['key' => Tools::fixHtml($value->code), 'value' => Tools::fixHtml($value->description)];
}
if (empty($results) && '0' == $data['strict']) {
$results[] = ['key' => $data['term'], 'value' => $data['term']];
} elseif (empty($results)) {
$results[] = ['key' => null, 'value' => Tools::lang()->trans('no-data')];
}
return $results;
}
/**
* Returns true if the active user has permission to view the information
* of the active record in the informed model.
*
* @param ModelClass $model
*
* @return bool
*/
protected function checkOwnerData($model): bool
{
if (false === $this->permissions->onlyOwnerData || empty($model->primaryColumnValue())) {
return true;
}
// si el modelo tiene nick, comprobamos nick
if (property_exists($model, 'nick')) {
if (null === $model->nick || $model->nick === $this->user->nick) {
return true;
}
if (property_exists($model, 'codagente') && $this->user->codagente) {
return $model->codagente === $this->user->codagente;
}
return false;
}
// si el modelo tiene agente, comprobamos agente
if (property_exists($model, 'codagente')) {
return $model->codagente === $this->user->codagente;
}
// si no hay nada en que apoyarse, permitimos
return true;
}
/**
* Run the datalist action.
* Returns a JSON string for the searched values.
*
* @return array
*/
protected function datalistAction(): array
{
$data = $this->requestGet(['field', 'fieldcode', 'fieldfilter', 'fieldtitle', 'formname', 'source', 'term']);
$where = [];
foreach (DataBaseWhere::applyOperation($data['fieldfilter'] ?? '') as $field => $operation) {
$where[] = new DataBaseWhere($field, $data['term'], '=', $operation);
}
$results = [];
foreach ($this->codeModel->all($data['source'], $data['fieldcode'], $data['fieldtitle'], false, $where) as $value) {
$results[] = ['key' => Tools::fixHtml($value->code), 'value' => Tools::fixHtml($value->description)];
}
return $results;
}
/**
* Action to delete data.
*
* @return bool
*/
protected function deleteAction()
{
// check user permissions
if (false === $this->permissions->allowDelete || false === $this->views[$this->active]->settings['btnDelete']) {
Tools::log()->warning('not-allowed-delete');
return false;
} elseif (false === $this->validateFormToken()) {
return false;
}
$model = $this->views[$this->active]->model;
$codes = $this->request->request->get('code', '');
if (empty($codes)) {
Tools::log()->warning('no-selected-item');
return false;
}
if (is_array($codes)) {
$this->dataBase->beginTransaction();
// deleting multiples rows
$numDeletes = 0;
foreach ($codes as $cod) {
if ($model->loadFromCode($cod) && $model->delete()) {
++$numDeletes;
continue;
}
// error?
$this->dataBase->rollback();
break;
}
$model->clear();
$this->dataBase->commit();
if ($numDeletes > 0) {
Tools::log()->notice('record-deleted-correctly');
return true;
}
} elseif ($model->loadFromCode($codes) && $model->delete()) {
// deleting a single row
Tools::log()->notice('record-deleted-correctly');
$model->clear();
return true;
}
Tools::log()->warning('record-deleted-error');
$model->clear();
return false;
}
protected function exportAction()
{
if (false === $this->views[$this->active]->settings['btnPrint'] ||
false === $this->permissions->allowExport) {
Tools::log()->warning('no-print-permission');
return;
}
$this->setTemplate(false);
$this->exportManager->newDoc(
$this->request->get('option', ''),
$this->title,
(int)$this->request->request->get('idformat', ''),
$this->request->request->get('langcode', '')
);
foreach ($this->views as $selectedView) {
if (false === $selectedView->settings['active']) {
continue;
}
$codes = $this->request->request->get('code');
if (false === $selectedView->export($this->exportManager, $codes)) {
break;
}
}
$this->exportManager->show($this->response);
}
/**
* Return values from Widget Values for autocomplete action
*
* @param string $viewName
* @param string $fieldName
*
* @return array
*/
protected function getAutocompleteValues(string $viewName, string $fieldName): array
{
$result = [];
$column = $this->views[$viewName]->columnForField($fieldName);
if (!empty($column)) {
foreach ($column->widget->values as $value) {
$result[] = ['key' => Tools::lang()->trans($value['title']), 'value' => $value['value']];
}
}
return $result;
}
/**
* Return array with parameters values
*
* @param array $keys
*
* @return array
*/
protected function requestGet(array $keys): array
{
$result = [];
foreach ($keys as $key) {
$result[$key] = $this->request->get($key);
}
return $result;
}
/**
* Run the select action.
* Returns a JSON string for the searched values.
*
* @return array
*/
protected function selectAction(): array
{
$required = (bool)$this->request->get('required', false);
$data = $this->requestGet(['field', 'fieldcode', 'fieldfilter', 'fieldtitle', 'formname', 'source', 'term']);
$return = $this->pipe('selectAction', $data, $required);
if ($return) {
return $return;
}
$where = [];
foreach (DataBaseWhere::applyOperation($data['fieldfilter'] ?? '') as $field => $operation) {
$where[] = new DataBaseWhere($field, $data['term'], '=', $operation);
}
$results = [];
foreach ($this->codeModel->all($data['source'], $data['fieldcode'], $data['fieldtitle'], !$required, $where) as $value) {
$results[] = ['key' => Tools::fixHtml($value->code), 'value' => Tools::fixHtml($value->description)];
}
return $results;
}
}