Passed
Push — devel-3.0 ( ca620b...2581df )
by Rubén
03:02
created

TrackGrid::getData()   A

Complexity

Conditions 5
Paths 1

Size

Total Lines 37
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 21
nc 1
nop 0
dl 0
loc 37
rs 9.2728
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
use SP\Core\Acl\Acl;
28
use SP\Core\Acl\ActionsInterface;
29
use SP\Html\DataGrid\DataGridAction;
30
use SP\Html\DataGrid\DataGridActionSearch;
31
use SP\Html\DataGrid\DataGridActionType;
32
use SP\Html\DataGrid\DataGridData;
33
use SP\Html\DataGrid\DataGridHeader;
34
use SP\Html\DataGrid\DataGridInterface;
35
use SP\Html\DataGrid\DataGridTab;
36
use SP\Http\Address;
37
use SP\Storage\Database\QueryResult;
38
39
/**
40
 * Class TrackGrid
41
 *
42
 * @package SP\Modules\Web\Controllers\Helpers\Grid
43
 */
44
final class TrackGrid extends GridBase
45
{
46
    /**
47
     * @var QueryResult
48
     */
49
    private $queryResult;
50
51
    /**
52
     * @param QueryResult $queryResult
53
     *
54
     * @return DataGridInterface
55
     */
56
    public function getGrid(QueryResult $queryResult): DataGridInterface
57
    {
58
        $this->queryResult = $queryResult;
59
60
        $grid = $this->getGridLayout();
61
62
        $searchAction = $this->getSearchAction();
63
64
        $grid->setDataActions($searchAction);
65
        $grid->setPager($this->getPager($searchAction));
66
67
        $grid->setDataActions($this->getRefrestAction());
68
        $grid->setDataActions($this->getClearAction());
69
        $grid->setDataActions($this->getUnlockAction());
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;
74
    }
75
76
    /**
77
     * @return DataGridInterface
78
     */
79
    protected function getGridLayout(): DataGridInterface
80
    {
81
        // Grid
82
        $gridTab = new DataGridTab($this->view->getTheme());
83
        $gridTab->setId('tblTracks');
84
        $gridTab->setDataRowTemplate('datagrid-rows', 'grid');
85
        $gridTab->setDataPagerTemplate('datagrid-nav-full', 'grid');
86
        $gridTab->setHeader($this->getHeader());
87
        $gridTab->setData($this->getData());
88
        $gridTab->setTitle(__('Tracks'));
89
90
        return $gridTab;
91
    }
92
93
    /**
94
     * @return DataGridHeader
95
     */
96
    protected function getHeader(): DataGridHeader
97
    {
98
        // Grid Header
99
        $gridHeader = new DataGridHeader();
100
        $gridHeader->addHeader(__('Fecha'));
101
        $gridHeader->addHeader(__('Fecha Desbloqueo'));
102
        $gridHeader->addHeader(__('Origen'));
103
        $gridHeader->addHeader('IPv4');
104
        $gridHeader->addHeader('IPv6');
105
        $gridHeader->addHeader(__('Usuario'));
106
107
        return $gridHeader;
108
    }
109
110
    /**
111
     * @return DataGridData
112
     */
113
    protected function getData(): DataGridData
114
    {
115
        $demo = $this->configData->isDemoEnabled();
116
117
118
        // Grid Data
119
        $gridData = new DataGridData();
120
        $gridData->setDataRowSourceId('id');
121
        $gridData->addDataRowSource('dateTime');
122
        $gridData->addDataRowSource('dateTimeUnlock');
123
        $gridData->addDataRowSource('source', null, null, false);
124
        $gridData->addDataRowSource('ipv4', null, function ($value) use ($demo) {
125
            if ($value !== null) {
126
                if ($demo) {
127
                    return '*.*.*.*';
128
                }
129
130
                return Address::fromBinary($value);
131
            }
132
133
            return '&nbsp;';
134
        });
135
        $gridData->addDataRowSource('ipv6', null, function ($value) use ($demo) {
136
            if ($value !== null) {
137
                if ($demo) {
138
                    return '*.*.*.*';
139
                }
140
141
                return Address::fromBinary($value);
142
            }
143
144
            return '&nbsp;';
145
        });
146
        $gridData->addDataRowSource('userId');
147
        $gridData->setData($this->queryResult);
148
149
        return $gridData;
150
    }
151
152
    /**
153
     * @return DataGridActionSearch
154
     */
155
    private function getSearchAction()
156
    {
157
        // Grid Actions
158
        $gridActionSearch = new DataGridActionSearch();
159
        $gridActionSearch->setId(ActionsInterface::TRACK_SEARCH);
160
        $gridActionSearch->setType(DataGridActionType::SEARCH_ITEM);
161
        $gridActionSearch->setName('frmSearchTrack');
162
        $gridActionSearch->setTitle(__('Buscar Track'));
163
        $gridActionSearch->setOnSubmitFunction('appMgmt/search');
164
        $gridActionSearch->addData('action-route', Acl::getActionRoute(ActionsInterface::TRACK_SEARCH));
165
166
        return $gridActionSearch;
167
    }
168
169
    /**
170
     * @return DataGridAction
171
     */
172
    private function getRefrestAction()
173
    {
174
        $gridAction = new DataGridAction();
175
        $gridAction->setId(ActionsInterface::TRACK_SEARCH);
176
        $gridAction->setType(DataGridActionType::MENUBAR_ITEM);
177
        $gridAction->setSkip(true);
178
        $gridAction->setName(__('Refrescar'));
179
        $gridAction->setTitle(__('Refrescar'));
180
        $gridAction->setIcon($this->icons->getIconRefresh());
181
        $gridAction->setOnClickFunction('appMgmt/search');
182
        $gridAction->addData('action-route', Acl::getActionRoute(ActionsInterface::TRACK_SEARCH));
183
184
        return $gridAction;
185
    }
186
187
    /**
188
     * @return DataGridAction
189
     */
190
    private function getClearAction()
191
    {
192
        $gridAction = new DataGridAction();
193
        $gridAction->setId(ActionsInterface::TRACK_CLEAR);
194
        $gridAction->setType(DataGridActionType::MENUBAR_ITEM);
195
        $gridAction->setSkip(true);
196
        $gridAction->setName(Acl::getActionInfo(ActionsInterface::TRACK_CLEAR));
197
        $gridAction->setTitle(Acl::getActionInfo(ActionsInterface::TRACK_CLEAR));
198
        $gridAction->setIcon($this->icons->getIconClear());
199
        $gridAction->setOnClickFunction('track/clear');
200
        $gridAction->addData('action-route', Acl::getActionRoute(ActionsInterface::TRACK_CLEAR));
201
202
        return $gridAction;
203
    }
204
205
    /**
206
     * @return DataGridAction
207
     */
208
    private function getUnlockAction()
209
    {
210
        $gridAction = new DataGridAction();
211
        $gridAction->setId(ActionsInterface::TRACK_UNLOCK);
212
        $gridAction->setType(DataGridActionType::EDIT_ITEM);
213
        $gridAction->setName(Acl::getActionInfo(ActionsInterface::TRACK_UNLOCK));
214
        $gridAction->setTitle(Acl::getActionInfo(ActionsInterface::TRACK_UNLOCK));
215
        $gridAction->setIcon($this->icons->getIconCheck());
216
        $gridAction->setOnClickFunction('track/unlock');
217
        $gridAction->addData('action-route', Acl::getActionRoute(ActionsInterface::TRACK_UNLOCK));
218
        $gridAction->setFilterRowSource('tracked', 0);
219
220
        return $gridAction;
221
    }
222
}