Passed
Push — devel-3.0 ( 4622e9...e92637 )
by Rubén
03:18
created

EventlogGrid::getGrid()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 1
dl 0
loc 17
rs 9.9666
c 0
b 0
f 0
1
<?php
2
/**
3
 * sysPass
4
 *
5
 * @author    nuxsmin
6
 * @link      https://syspass.org
7
 * @copyright 2012-2018, Rubén Domínguez nuxsmin@$syspass.org
8
 *
9
 * This file is part of sysPass.
10
 *
11
 * sysPass is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU General Public License as published by
13
 * the Free Software Foundation, either version 3 of the License, or
14
 * (at your option) any later version.
15
 *
16
 * sysPass is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU General Public License
22
 *  along with sysPass.  If not, see <http://www.gnu.org/licenses/>.
23
 */
24
25
namespace SP\Modules\Web\Controllers\Helpers\Grid;
26
27
28
use SP\Core\Acl\Acl;
29
use SP\Core\Acl\ActionsInterface;
30
use SP\Html\DataGrid\DataGrid;
31
use SP\Html\DataGrid\DataGridAction;
32
use SP\Html\DataGrid\DataGridActionSearch;
33
use SP\Html\DataGrid\DataGridActionType;
34
use SP\Html\DataGrid\DataGridData;
35
use SP\Html\DataGrid\DataGridHeader;
36
use SP\Html\DataGrid\DataGridInterface;
37
use SP\Html\DataGrid\DataGridTab;
38
use SP\Storage\Database\QueryResult;
39
40
/**
41
 * Class EventlogGrid
42
 *
43
 * @package SP\Modules\Web\Controllers\Helpers\Grid
44
 */
45
final class EventlogGrid extends GridBase
46
{
47
    /**
48
     * @var QueryResult
49
     */
50
    private $queryResult;
51
52
    /**
53
     * @param QueryResult $queryResult
54
     *
55
     * @return DataGridTab
56
     */
57
    public function getGrid(QueryResult $queryResult): DataGridInterface
58
    {
59
        $this->queryResult = $queryResult;
60
61
        $grid = $this->getGridLayout();
62
63
        $searchAction = $this->getSearchAction();
64
65
        $grid->setDataActions($this->getSearchAction());
66
        $grid->setPager($this->getPager($searchAction)->setOnClickFunction('eventlog/nav'));
67
68
        $grid->setDataActions($this->getRefrestAction());
69
        $grid->setDataActions($this->getClearAction());
70
71
        $grid->setTime(round(getElapsedTime($this->queryTimeStart), 5));
0 ignored issues
show
Bug introduced by
round(getElapsedTime($this->queryTimeStart), 5) of type double is incompatible with the type integer expected by parameter $time of SP\Html\DataGrid\DataGridBase::setTime(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

71
        $grid->setTime(/** @scrutinizer ignore-type */ round(getElapsedTime($this->queryTimeStart), 5));
Loading history...
72
73
        return $grid;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $grid returns the type SP\Html\DataGrid\DataGrid which is incompatible with the documented return type SP\Html\DataGrid\DataGridTab.
Loading history...
74
    }
75
76
    /**
77
     * @return DataGridInterface
78
     */
79
    protected function getGridLayout(): DataGridInterface
80
    {
81
        // Grid
82
        $dataGrid = new DataGrid($this->view->getTheme());
83
        $dataGrid->setId('tblEventLog');
84
        $dataGrid->setDataTableTemplate('datagrid-table-simple', 'grid');
85
        $dataGrid->setDataRowTemplate('datagrid-rows', $this->view->getBase());
86
        $dataGrid->setDataPagerTemplate('datagrid-nav-full', 'grid');
87
        $dataGrid->setHeader($this->getHeader());
88
        $dataGrid->setData($this->getData());
89
        $dataGrid->setTitle(__('Registro de Eventos'));
90
91
        return $dataGrid;
92
    }
93
94
    /**
95
     * @return DataGridHeader
96
     */
97
    protected function getHeader(): DataGridHeader
98
    {
99
        // Grid Header
100
        $gridHeader = new DataGridHeader();
101
        $gridHeader->addHeader(__('ID'));
102
        $gridHeader->addHeader(__('Fecha / Hora'));
103
        $gridHeader->addHeader(__('Nivel'));
104
        $gridHeader->addHeader(__('Evento'));
105
        $gridHeader->addHeader(__('Login'));
106
        $gridHeader->addHeader(__('IP'));
107
        $gridHeader->addHeader(__('Descripción'));
108
109
        return $gridHeader;
110
    }
111
112
    /**
113
     * @return DataGridData
114
     */
115
    protected function getData(): DataGridData
116
    {
117
        // Grid Data
118
        $isDemoMode = $this->configData->isDemoEnabled();
119
120
        $gridData = new DataGridData();
121
        $gridData->setDataRowSourceId('id');
122
        $gridData->addDataRowSource('id');
123
        $gridData->addDataRowSource('date');
124
        $gridData->addDataRowSource('level');
125
        $gridData->addDataRowSource('action');
126
        $gridData->addDataRowSource('login');
127
        $gridData->addDataRowSource('ipAddress', false,
128
            function ($value) use ($isDemoMode) {
129
                return $isDemoMode ? preg_replace('#\d+#', '*', $value) : $value;
130
            });
131
        $gridData->addDataRowSource('description', false,
132
            function ($value) use ($isDemoMode) {
133
                if ($isDemoMode) {
134
                    $value = preg_replace('/\\d+\\.\\d+\\.\\d+\\.\\d+/', "*.*.*.*", $value);
135
                }
136
137
                $text = str_replace(';;', PHP_EOL, $value);
138
139
                if (preg_match('/^SQL.*/m', $text)) {
140
                    $text = preg_replace([
141
                        '/([a-zA-Z_]+),/m',
142
                        '/(UPDATE|DELETE|TRUNCATE|INSERT|SELECT|WHERE|LEFT|ORDER|LIMIT|FROM)/m'],
143
                        ['\\1,<br>', '<br>\\1'],
144
                        $text);
145
                }
146
147
//                if (strlen($text) >= 100) {
148
//                    $text = wordwrap($text, 100, PHP_EOL, true);
149
//                }
150
151
                return str_replace(PHP_EOL, '<br>', $text);
152
            });
153
        $gridData->setData($this->queryResult);
154
155
        return $gridData;
156
    }
157
158
    /**
159
     * @return DataGridActionSearch
160
     */
161
    private function getSearchAction()
162
    {
163
        // Grid Actions
164
        $gridActionSearch = new DataGridActionSearch();
165
        $gridActionSearch->setId(ActionsInterface::EVENTLOG_SEARCH);
166
        $gridActionSearch->setType(DataGridActionType::SEARCH_ITEM);
167
        $gridActionSearch->setName('frmSearchEvent');
168
        $gridActionSearch->setTitle(__('Buscar Evento'));
169
        $gridActionSearch->setOnSubmitFunction('eventlog/search');
170
        $gridActionSearch->addData('action-route', Acl::getActionRoute(ActionsInterface::EVENTLOG_SEARCH));
171
172
        return $gridActionSearch;
173
    }
174
175
    /**
176
     * @return DataGridAction
177
     */
178
    private function getRefrestAction()
179
    {
180
        $gridAction = new DataGridAction();
181
        $gridAction->setId(ActionsInterface::EVENTLOG_SEARCH);
182
        $gridAction->setType(DataGridActionType::MENUBAR_ITEM);
183
        $gridAction->setName(__('Refrescar'));
184
        $gridAction->setTitle(__('Refrescar'));
185
        $gridAction->setIcon($this->icons->getIconRefresh());
186
        $gridAction->setOnClickFunction('eventlog/search');
187
        $gridAction->addData('action-route', Acl::getActionRoute(ActionsInterface::EVENTLOG_SEARCH));
188
        $gridAction->addData('target', '#data-table-tblEventLog');
189
190
        return $gridAction;
191
    }
192
193
    /**
194
     * @return DataGridAction
195
     */
196
    private function getClearAction()
197
    {
198
        $gridAction = new DataGridAction();
199
        $gridAction->setId(ActionsInterface::EVENTLOG_CLEAR);
200
        $gridAction->setType(DataGridActionType::MENUBAR_ITEM);
201
        $gridAction->setName(__('Vaciar registro de eventos'));
202
        $gridAction->setTitle(__('Vaciar registro de eventos'));
203
        $gridAction->setIcon($this->icons->getIconClear());
204
        $gridAction->setOnClickFunction('eventlog/clear');
205
        $gridAction->addData('action-route', Acl::getActionRoute(ActionsInterface::EVENTLOG_CLEAR));
206
        $gridAction->addData('nextaction', Acl::getActionRoute(ActionsInterface::EVENTLOG));
207
208
        return $gridAction;
209
    }
210
}