/
ListController.php
427 lines (382 loc) · 14.3 KB
/
ListController.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
<?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\Lib\ExtendedController;
use FacturaScripts\Core\Base\ControllerPermissions;
use FacturaScripts\Core\Base\DataBase\DataBaseWhere;
use FacturaScripts\Core\Model\Base\ModelClass;
use FacturaScripts\Dinamic\Model\User;
use Symfony\Component\HttpFoundation\Response;
/**
* Controller that lists the data in table mode
*
* @author Carlos García Gómez <carlos@facturascripts.com>
* @author Jose Antonio Cuello Principal <yopli2000@gmail.com>
* @author Cristo M. Estévez Hernández <cristom.estevez@gmail.com>
*/
abstract class ListController extends BaseController
{
/**
* Initializes all the objects and properties.
*
* @param string $className
* @param string $uri
*/
public function __construct(string $className, string $uri = '')
{
parent::__construct($className, $uri);
$this->setTemplate('Master/ListController');
}
/**
* 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);
// Get action to execute
$action = $this->request->request->get('action', $this->request->query->get('action', ''));
// Execute actions before loading data
if (false === $this->execPreviousAction($action) || false === $this->pipe('execPreviousAction', $action)) {
return;
}
// Load filter saved and data for every view
foreach ($this->views as $viewName => $view) {
if ($this->active == $viewName) {
$view->processFormData($this->request, 'load');
} else {
$view->processFormData($this->request, 'preload');
}
$this->loadData($viewName, $view);
$this->pipe('loadData', $viewName, $view);
}
// Execute actions after loading data
$this->execAfterAction($action);
$this->pipe('execAfterAction', $action);
}
/**
* Add an autocomplete type filter to the ListView.
*
* @param string $viewName
* @param string $key (Filter identifier)
* @param string $label (Human reader description)
* @param string $field (Field of the model to apply filter)
* @param string $table (Table to search)
* @param string $fieldcode (Primary column of the table to search and match)
* @param string $fieldtitle (Column to show name or description)
* @param array $where (Extra where conditions)
*/
protected function addFilterAutocomplete(string $viewName, string $key, string $label, string $field, string $table, string $fieldcode = '', string $fieldtitle = '', array $where = [])
{
$this->views[$viewName]->addFilterAutocomplete($key, $label, $field, $table, $fieldcode, $fieldtitle, $where);
}
/**
* Adds a boolean condition type filter to the ListView.
*
* @param string $viewName
* @param string $key (Filter identifier)
* @param string $label (Human reader description)
* @param string $field (Field of the model to apply filter)
* @param string $operation (operation to perform with match value)
* @param mixed $matchValue (Value to match)
* @param DataBaseWhere[] $default (where to apply when filter is empty)
*/
protected function addFilterCheckbox(string $viewName, string $key, string $label = '', string $field = '', string $operation = '=', $matchValue = true, array $default = [])
{
$this->views[$viewName]->addFilterCheckbox($key, $label, $field, $operation, $matchValue, $default);
}
/**
* Adds a date type filter to the ListView.
*
* @param string $viewName
* @param string $key (Filter identifier)
* @param string $label (Human reader description)
* @param string $field (Field of the table to apply filter)
* @param string $operation (Operation to perform)
*/
protected function addFilterDatePicker(string $viewName, string $key, string $label = '', string $field = '', string $operation = '>=')
{
$this->views[$viewName]->addFilterDatePicker($key, $label, $field, $operation);
}
/**
* Adds a numeric type filter to the ListView.
*
* @param string $viewName
* @param string $key (Filter identifier)
* @param string $label (Human reader description)
* @param string $field (Field of the table to apply filter)
* @param string $operation (Operation to perform)
*/
protected function addFilterNumber(string $viewName, string $key, string $label = '', string $field = '', string $operation = '>=')
{
$this->views[$viewName]->addFilterNumber($key, $label, $field, $operation);
}
/**
* Adds a period type filter to the ListView.
* (period + start date + end date)
*
* @param string $viewName
* @param string $key (Filter identifier)
* @param string $label (Human reader description)
* @param string $field (Field of the table to apply filter)
*/
protected function addFilterPeriod(string $viewName, string $key, string $label, string $field)
{
$this->views[$viewName]->addFilterPeriod($key, $label, $field);
}
/**
* Add a select type filter to a ListView.
*
* @param string $viewName
* @param string $key (Filter identifier)
* @param string $label (Human reader description)
* @param string $field (Field of the table to apply filter)
* @param array $values (Values to show)
*/
protected function addFilterSelect(string $viewName, string $key, string $label, string $field, array $values = [])
{
$this->views[$viewName]->addFilterSelect($key, $label, $field, $values);
}
/**
* Add a select where type filter to a ListView.
*
* @param string $viewName
* @param string $key (Filter identifier)
* @param array $values (Values to show)
*
* Example of values:
* [
* ['label' => 'Only active', 'where' => [ new DataBaseWhere('suspended', 'FALSE') ]]
* ['label' => 'Only suspended', 'where' => [ new DataBaseWhere('suspended', 'TRUE') ]]
* ['label' => 'All records', 'where' => []],
* ]
*/
protected function addFilterSelectWhere(string $viewName, string $key, array $values)
{
$this->views[$viewName]->addFilterSelectWhere($key, $values);
}
/**
* Adds an order field to the ListView.
*
* @param string $viewName
* @param array $fields
* @param string $label
* @param int $default (0 = None, 1 = ASC, 2 = DESC)
*/
protected function addOrderBy(string $viewName, array $fields, string $label = '', int $default = 0)
{
$orderLabel = empty($label) ? $fields[0] : $label;
$this->views[$viewName]->addOrderBy($fields, $orderLabel, $default);
}
/**
* Adds a list of fields to the search in the ListView.
* To use integer columns, use CAST(columnName AS CHAR(50)).
*
* @param string $viewName
* @param array $fields
*/
protected function addSearchFields(string $viewName, array $fields)
{
$this->views[$viewName]->addSearchFields($fields);
}
/**
* Creates and adds a ListView to the controller.
*
* @param string $viewName
* @param string $modelName
* @param string $viewTitle
* @param string $icon
*/
protected function addView(string $viewName, string $modelName, string $viewTitle = '', string $icon = 'fas fa-search')
{
$title = empty($viewTitle) ? $this->title : $viewTitle;
$view = new ListView($viewName, $title, self::MODEL_NAMESPACE . $modelName, $icon);
$this->addCustomView($viewName, $view);
$this->setSettings($viewName, 'btnPrint', true);
$this->setSettings($viewName, 'card', false);
$this->setSettings($viewName, 'megasearch', true);
}
/**
* Removes the selected page filter.
*/
protected function deleteFilterAction()
{
$idfilter = $this->request->request->get('loadfilter', 0);
if ($this->views[$this->active]->deletePageFilter($idfilter)) {
$this->toolBox()->i18nLog()->notice('record-deleted-correctly');
$this->request->request->remove('loadfilter');
return;
}
$this->toolBox()->i18nLog()->warning('record-deleted-error');
}
/**
* Runs the controller actions after data read.
*
* @param string $action
*/
protected function execAfterAction($action)
{
switch ($action) {
case 'export':
$this->exportAction();
break;
case 'megasearch':
$this->megaSearchAction();
break;
}
}
/**
* Runs the actions that alter the data before reading it.
*
* @param string $action
*
* @return bool
*/
protected function execPreviousAction($action)
{
switch ($action) {
case 'autocomplete':
$this->setTemplate(false);
$results = $this->autocompleteAction();
$this->response->setContent(json_encode($results));
return false;
case 'delete':
$this->deleteAction();
break;
case 'delete-filter':
$this->deleteFilterAction();
break;
case 'save-filter':
$this->saveFilterAction();
break;
}
return true;
}
protected function exportAction()
{
if (false === $this->views[$this->active]->settings['btnPrint']) {
$this->toolBox()->i18nLog()->warning('no-print-permission');
return;
}
$this->setTemplate(false);
$codes = $this->request->request->get('code');
$option = $this->request->get('option', '');
$this->exportManager->newDoc($option);
$this->views[$this->active]->export($this->exportManager, $codes);
$this->exportManager->show($this->response);
}
/**
* Returns the where filter to apply to obtain the data created by the active user.
*
* @param ModelClass $model
*
* @return DataBaseWhere[]
*/
protected function getOwnerFilter($model): array
{
$where = [];
if (property_exists($model, 'nick')) {
// DatabaseWhere applies parentheses grouping the ORs
// result: (`nick` = 'username' OR `nick` IS NULL OR `codagente` = 'agent') AND [... user filters]
$where[] = new DataBaseWhere('nick', $this->user->nick);
$where[] = new DataBaseWhere('nick', null, 'IS', 'OR');
if (property_exists($model, 'codagente') && $this->user->codagente) {
$where[] = new DataBaseWhere('codagente', $this->user->codagente, '=', 'OR');
}
return $where;
}
if (property_exists($model, 'codagente')) {
$where[] = new DataBaseWhere('codagente', $this->user->codagente);
}
return $where;
}
/**
* @param string $viewName
* @param BaseView $view
*/
protected function loadData($viewName, $view)
{
$where = $this->permissions->onlyOwnerData ? $this->getOwnerFilter($view->model) : [];
$view->loadData('', $where);
}
/**
* Returns a JSON response to MegaSearch.
*/
protected function megaSearchAction()
{
$this->setTemplate(false);
$json = [];
// we search in all listviews
foreach ($this->views as $viewName => $listView) {
if (false === $this->getSettings($viewName, 'megasearch') || empty($listView->searchFields)) {
continue;
}
$json[$viewName] = [
'title' => $listView->title,
'icon' => $listView->icon,
'columns' => $this->megaSearchColumns($listView),
'results' => []
];
$fields = implode('|', $listView->searchFields);
$where = [new DataBaseWhere($fields, $this->request->get('query', ''), 'LIKE')];
$listView->loadData(false, $where);
foreach ($listView->cursor as $model) {
$item = ['url' => $model->url()];
foreach ($listView->getColumns() as $col) {
if (false === $col->hidden()) {
$item[$col->widget->fieldname] = $col->widget->plainText($model);
}
}
$json[$viewName]['results'][] = $item;
}
}
$this->response->setContent(json_encode($json));
}
/**
* Returns columns title for megaSearchAction function.
*
* @param ListView $view
*
* @return array
*/
private function megaSearchColumns($view): array
{
$result = [];
foreach ($view->getColumns() as $col) {
if (false === $col->hidden()) {
$result[] = $this->toolBox()->i18n()->trans($col->title);
}
}
return $result;
}
/**
* Saves filter values for active view and user.
*/
protected function saveFilterAction()
{
$idFilter = $this->views[$this->active]->savePageFilter($this->request, $this->user);
if (!empty($idFilter)) {
$this->toolBox()->i18nLog()->notice('record-updated-correctly');
// load filters in request
$this->request->request->set('loadfilter', $idFilter);
}
}
}