Completed
Push — master ( b951a8...853dbc )
by Iurii
01:25
created

Role::outputFormatTableRole()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 23
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 9.0856
c 0
b 0
f 0
cc 3
eloc 16
nc 3
nop 1
1
<?php
2
3
/**
4
 * @package CLI
5
 * @author Iurii Makukh <[email protected]>
6
 * @copyright Copyright (c) 2018, Iurii Makukh <[email protected]>
7
 * @license https://www.gnu.org/licenses/gpl-3.0.en.html GPL-3.0+
8
 */
9
10
namespace gplcart\modules\cli\controllers;
11
12
use gplcart\core\models\UserRole as UserRoleModel;
13
use gplcart\modules\cli\controllers\Base;
14
15
/**
16
 * Handles commands related to user roles
17
 */
18
class Role extends Base
19
{
20
21
    /**
22
     * Role model instance
23
     * @var \gplcart\core\models\UserRole $role
24
     */
25
    protected $role;
26
27
    /**
28
     * @param UserRoleModel $role
29
     */
30
    public function __construct(UserRoleModel $role)
31
    {
32
        parent::__construct();
33
34
        $this->role = $role;
35
    }
36
37
    /**
38
     * Callback for "role-perm-add" command
39
     */
40
    public function cmdPermAddRole()
41
    {
42
        list($role_id, $existing, $submitted) = $this->getPermissionsRole();
43
44
        $data = array('permissions' => array_unique(array_merge($existing, $submitted)));
45
46
        $this->setSubmitted(null, $data);
47
        $this->setSubmitted('update', $role_id);
48
        $this->validateComponent('user_role');
49
        $this->updateRole($role_id);
50
        $this->output();
51
    }
52
53
    /**
54
     * Callback for "role-perm-delete" command
55
     */
56
    public function cmdPermDeleteRole()
57
    {
58
        list($role_id, $existing, $submitted) = $this->getPermissionsRole();
59
60
        $data = array(
61
            'permissions' => array_unique(array_diff($existing, $submitted))
62
        );
63
64
        $this->setSubmitted(null, $data);
65
        $this->setSubmitted('update', $role_id);
66
        $this->validateComponent('user_role');
67
        $this->updateRole($role_id);
68
        $this->output();
69
    }
70
71
    /**
72
     * Callback for "role-get" command
73
     */
74
    public function cmdGetRole()
75
    {
76
        $result = $this->getListRole();
77
        $this->outputFormat($result);
78
        $this->outputFormatTableRole($result);
79
        $this->output();
80
    }
81
82
    /**
83
     * Callback for "role-delete" command
84
     */
85
    public function cmdDeleteRole()
86
    {
87
        $id = $this->getParam(0);
88
        $all = $this->getParam('all');
89
90
        if (empty($id) && empty($all)) {
91
            $this->errorExit($this->text('Invalid command'));
92
        }
93
94
        $result = false;
95
96
        if (!empty($id)) {
97
98
            if (!is_numeric($id)) {
99
                $this->errorExit($this->text('Invalid ID'));
100
            }
101
102
            $result = $this->role->delete($id);
103
104
        } else if ($all) {
105
            $deleted = $count = 0;
106
            foreach ($this->role->getList() as $item) {
107
                $count++;
108
                $deleted += (int) $this->role->delete($item['role_id']);
109
            }
110
111
            $result = ($count == $deleted);
112
        }
113
114
        if (!$result) {
115
            $this->errorExit($this->text('An error occurred'));
116
        }
117
118
        $this->output();
119
    }
120
121
    /**
122
     * Callback for "role-add" command
123
     */
124
    public function cmdAddRole()
125
    {
126
        if ($this->getParam()) {
127
            $this->submitAddRole();
128
        } else {
129
            $this->wizardAddRole();
130
        }
131
132
        $this->output();
133
    }
134
135
    /**
136
     * Callback for "role-update" command
137
     */
138
    public function cmdUpdateRole()
139
    {
140
        $params = $this->getParam();
141
142
        if (empty($params[0]) || count($params) < 2) {
143
            $this->errorExit($this->text('Invalid command'));
144
        }
145
146
        if (!is_numeric($params[0])) {
147
            $this->errorExit($this->text('Invalid ID'));
148
        }
149
150
        $this->setSubmitted(null, $this->getParam());
151
        $this->setSubmittedList('permissions');
152
        $this->setSubmitted('update', $params[0]);
153
        $this->validateComponent('user_role');
154
        $this->updateRole($params[0]);
155
        $this->output();
156
    }
157
158
    /**
159
     * Returns an array of user roles
160
     * @return array
161
     */
162
    protected function getListRole()
163
    {
164
        $id = $this->getParam(0);
165
166
        if (!isset($id)) {
167
            return $this->role->getList(array('limit' => $this->getLimit()));
168
        }
169
170
        if (empty($id) || !is_numeric($id)) {
171
            $this->errorExit($this->text('Invalid ID'));
172
        }
173
174
        $result = $this->role->get($id);
175
176
        if (empty($result)) {
177
            $this->errorExit($this->text('Invalid ID'));
178
        }
179
180
        return array($result);
181
    }
182
183
    /**
184
     * Output table format
185
     * @param array $items
186
     */
187
    protected function outputFormatTableRole(array $items)
188
    {
189
        $header = array(
190
            $this->text('ID'),
191
            $this->text('Name'),
192
            $this->text('Redirect'),
193
            $this->text('Permissions'),
194
            $this->text('Enabled')
195
        );
196
197
        $rows = array();
198
        foreach ($items as $item) {
199
            $rows[] = array(
200
                $item['role_id'],
201
                $item['name'],
202
                $item['redirect'],
203
                count($item['permissions']),
204
                empty($item['status']) ? $this->text('No') : $this->text('Yes')
205
            );
206
        }
207
208
        $this->outputFormatTable($rows, $header);
209
    }
210
211
    /**
212
     * Returns an array containing parsed submitted data, such as:
213
     * role ID, the role permissions, submitted permissions
214
     * @return array
215
     */
216
    protected function getPermissionsRole()
217
    {
218
        $arguments = $this->getArguments();
219
220
        if (count($arguments) < 2) {
221
            $this->errorExit($this->text('Invalid command'));
222
        }
223
224
        $role_id = array_shift($arguments);
225
226
        if (!is_numeric($role_id)) {
227
            $this->errorExit($this->text('Invalid ID'));
228
        }
229
230
        $role = $this->role->get($role_id);
231
232
        if (!isset($role['permissions'])) {
233
            $this->errorExit($this->text('Invalid ID'));
234
        }
235
236
        return array($role_id, $role['permissions'], $arguments);
237
    }
238
239
    /**
240
     * Updates a user role
241
     * @param string $role_id
242
     */
243
    protected function updateRole($role_id)
244
    {
245
        if (!$this->isError() && !$this->role->update($role_id, $this->getSubmitted())) {
246
            $this->errorExit($this->text('Role has not been updated'));
247
        }
248
    }
249
250
    /**
251
     * Add a new user role at once
252
     */
253
    protected function submitAddRole()
254
    {
255
        $this->setSubmitted(null, $this->getParam());
256
        $this->setSubmittedList('permissions');
257
        $this->validateComponent('user_role');
258
        $this->addRole();
259
    }
260
261
    /**
262
     * Add a new user role
263
     */
264
    protected function addRole()
265
    {
266
        if (!$this->isError()) {
267
            $id = $this->role->add($this->getSubmitted());
268
            if (empty($id)) {
269
                $this->errorExit($this->text('Role has not been added'));
270
            }
271
            $this->line($id);
272
        }
273
    }
274
275
    /**
276
     * Add a new user role step by step
277
     */
278
    protected function wizardAddRole()
279
    {
280
        $this->validatePrompt('name', $this->text('Name'), 'user_role');
281
        $this->validatePromptList('permissions', $this->text('Permissions'), 'user_role');
282
        $this->validatePrompt('redirect', $this->text('Redirect'), 'user_role', '');
283
        $this->validatePrompt('status', $this->text('Status'), 'user_role', 0);
284
        $this->setSubmittedList('permissions');
285
        $this->validateComponent('user_role');
286
        $this->addRole();
287
    }
288
289
}
290