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

CategoryGrid::getGridLayout()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 0
dl 0
loc 12
rs 10
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\DataGridAction;
31
use SP\Html\DataGrid\DataGridActionSearch;
32
use SP\Html\DataGrid\DataGridActionType;
33
use SP\Html\DataGrid\DataGridData;
34
use SP\Html\DataGrid\DataGridHeader;
35
use SP\Html\DataGrid\DataGridInterface;
36
use SP\Html\DataGrid\DataGridTab;
37
use SP\Storage\Database\QueryResult;
38
39
/**
40
 * Class CategoryGrid
41
 *
42
 * @package SP\Modules\Web\Controllers\Helpers\Grid
43
 */
44
final class CategoryGrid 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($this->getSearchAction());
65
        $grid->setPager($this->getPager($searchAction));
66
        $grid->setDataActions($this->getCreateAction());
67
        $grid->setDataActions($this->getEditAction());
68
69
        $deleteAction = $this->getDeleteAction();
70
71
        $grid->setDataActions($deleteAction);
72
        $grid->setDataActions($deleteAction, true);
73
74
        $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

74
        $grid->setTime(/** @scrutinizer ignore-type */ round(getElapsedTime($this->queryTimeStart), 5));
Loading history...
75
76
        return $grid;
77
    }
78
79
    /**
80
     * @return DataGridInterface
81
     */
82
    protected function getGridLayout(): DataGridInterface
83
    {
84
        // Grid
85
        $gridTab = new DataGridTab($this->view->getTheme());
86
        $gridTab->setId('tblCategories');
87
        $gridTab->setDataRowTemplate('datagrid-rows', 'grid');
88
        $gridTab->setDataPagerTemplate('datagrid-nav-full', 'grid');
89
        $gridTab->setHeader($this->getHeader());
90
        $gridTab->setData($this->getData());
91
        $gridTab->setTitle(__('Categorías'));
92
93
        return $gridTab;
94
    }
95
96
    /**
97
     * @return DataGridHeader
98
     */
99
    protected function getHeader(): DataGridHeader
100
    {
101
        // Grid Header
102
        $gridHeader = new DataGridHeader();
103
        $gridHeader->addHeader(__('Nombre'));
104
        $gridHeader->addHeader(__('Descripción'));
105
106
        return $gridHeader;
107
    }
108
109
    /**
110
     * @return DataGridData
111
     */
112
    protected function getData(): DataGridData
113
    {
114
        // Grid Data
115
        $gridData = new DataGridData();
116
        $gridData->setDataRowSourceId('id');
117
        $gridData->addDataRowSource('name');
118
        $gridData->addDataRowSource('description');
119
        $gridData->setData($this->queryResult);
120
121
        return $gridData;
122
    }
123
124
    /**
125
     * @return DataGridActionSearch
126
     */
127
    private function getSearchAction()
128
    {
129
        // Grid Actions
130
        $gridActionSearch = new DataGridActionSearch();
131
        $gridActionSearch->setId(ActionsInterface::CATEGORY_SEARCH);
132
        $gridActionSearch->setType(DataGridActionType::SEARCH_ITEM);
133
        $gridActionSearch->setName('frmSearchCategory');
134
        $gridActionSearch->setTitle(__('Buscar Categoría'));
135
        $gridActionSearch->setOnSubmitFunction('appMgmt/search');
136
        $gridActionSearch->addData('action-route', Acl::getActionRoute(ActionsInterface::CATEGORY_SEARCH));
137
138
        return $gridActionSearch;
139
    }
140
141
    /**
142
     * @return DataGridAction
143
     */
144
    private function getCreateAction()
145
    {
146
        $gridAction = new DataGridAction();
147
        $gridAction->setId(ActionsInterface::CATEGORY_CREATE);
148
        $gridAction->setType(DataGridActionType::MENUBAR_ITEM);
149
        $gridAction->setName(__('Nueva Categoría'));
150
        $gridAction->setTitle(__('Nueva Categoría'));
151
        $gridAction->setIcon($this->icons->getIconAdd());
152
        $gridAction->setSkip(true);
153
        $gridAction->setOnClickFunction('appMgmt/show');
154
        $gridAction->addData('action-route', Acl::getActionRoute(ActionsInterface::CATEGORY_CREATE));
155
156
        return $gridAction;
157
    }
158
159
    /**
160
     * @return DataGridAction
161
     */
162
    private function getEditAction()
163
    {
164
        $gridAction = new DataGridAction();
165
        $gridAction->setId(ActionsInterface::CATEGORY_EDIT);
166
        $gridAction->setType(DataGridActionType::EDIT_ITEM);
167
        $gridAction->setName(__('Editar Categoría'));
168
        $gridAction->setTitle(__('Editar Categoría'));
169
        $gridAction->setIcon($this->icons->getIconEdit());
170
        $gridAction->setOnClickFunction('appMgmt/show');
171
        $gridAction->addData('action-route', Acl::getActionRoute(ActionsInterface::CATEGORY_EDIT));
172
173
        return $gridAction;
174
    }
175
176
    /**
177
     * @return DataGridAction
178
     */
179
    private function getDeleteAction()
180
    {
181
        $gridAction = new DataGridAction();
182
        $gridAction->setId(ActionsInterface::CATEGORY_DELETE);
183
        $gridAction->setType(DataGridActionType::DELETE_ITEM);
184
        $gridAction->setName(__('Eliminar Categoría'));
185
        $gridAction->setTitle(__('Eliminar Categoría'));
186
        $gridAction->setIcon($this->icons->getIconDelete());
187
        $gridAction->setOnClickFunction('appMgmt/delete');
188
        $gridAction->addData('action-route', Acl::getActionRoute(ActionsInterface::CATEGORY_DELETE));
189
190
        return $gridAction;
191
    }
192
}