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

User::getListUser()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 8.7972
c 0
b 0
f 0
cc 4
eloc 12
nc 5
nop 0
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\User as UserModel;
13
use gplcart\modules\cli\controllers\Base;
14
15
/**
16
 * Handles commands related to users
17
 */
18
class User extends Base
19
{
20
21
    /**
22
     * User model instance
23
     * @var \gplcart\core\models\User $user
24
     */
25
    protected $user;
26
27
    /**
28
     * @param UserModel $user
29
     */
30
    public function __construct(UserModel $user)
31
    {
32
        parent::__construct();
33
34
        $this->user = $user;
35
    }
36
37
    /**
38
     * Callback for "user-on" command
39
     */
40
    public function cmdOnUser()
41
    {
42
        $this->setStatusUser(true);
43
        $this->output();
44
    }
45
46
    /**
47
     * Callback for "user-off" command
48
     */
49
    public function cmdOffUser()
50
    {
51
        $this->setStatusUser(false);
52
        $this->output();
53
    }
54
55
    /**
56
     * Callback for "user-get" command
57
     */
58
    public function cmdGetUser()
59
    {
60
        $result = $this->getListUser();
61
        $this->outputFormat($result);
62
        $this->outputFormatTableUser($result);
63
        $this->output();
64
    }
65
66
    /**
67
     * Callback for "user-delete" command
68
     */
69
    public function cmdDeleteUser()
70
    {
71
        $id = $this->getParam(0);
72
73
        if (empty($id) || !is_numeric($id)) {
74
            $this->errorExit($this->text('Invalid ID'));
75
        }
76
77
        $options = null;
78
79
        if ($this->getParam('role')) {
80
            $options = array('role_id' => $id);
81
        } else if ($this->getParam('store')) {
82
            $options = array('store_id' => $id);
83
        }
84
85
        if (isset($options)) {
86
            $deleted = $count = 0;
87
            foreach ($this->user->getList($options) as $item) {
88
                $count++;
89
                $deleted += (int) $this->user->delete($item['user_id']);
90
            }
91
            $result = ($count == $deleted);
92
        } else {
93
            $result = $this->user->delete($id);
94
        }
95
96
        if (!$result) {
97
            $this->errorExit($this->text('An error occurred'));
98
        }
99
100
        $this->output();
101
    }
102
103
    /**
104
     * Callback for "user-add" command
105
     */
106
    public function cmdAddUser()
107
    {
108
        if ($this->getParam()) {
109
            $this->submitAddUser();
110
        } else {
111
            $this->wizardAddUser();
112
        }
113
114
        $this->output();
115
    }
116
117
    /**
118
     * Callback for "user-update" command
119
     */
120
    public function cmdUpdateUser()
121
    {
122
        $this->submitUpdateUser();
123
        $this->output();
124
    }
125
126
    /**
127
     * Returns an array of users
128
     * @return array
129
     */
130
    protected function getListUser()
131
    {
132
        $id = $this->getParam(0);
133
134
        if (isset($id)) {
135
136
            if (!is_numeric($id)) {
137
                $this->errorExit($this->text('Invalid ID'));
138
            }
139
140
            $result = $this->user->get($id);
141
142
            if (empty($result)) {
143
                $this->errorExit($this->text('Invalid ID'));
144
            }
145
146
            return array($result);
147
        }
148
149
        $list = $this->user->getList();
150
        $this->limitArray($list);
151
        return $list;
152
    }
153
154
    /**
155
     * Output table format
156
     * @param array $items
157
     */
158
    protected function outputFormatTableUser(array $items)
159
    {
160
        $header = array(
161
            $this->text('ID'),
162
            $this->text('Name'),
163
            $this->text('E-mail'),
164
            $this->text('Role'),
165
            $this->text('Store'),
166
            $this->text('Enabled'),
167
            $this->text('Created'),
168
        );
169
170
        $rows = array();
171
        foreach ($items as $item) {
172
            $rows[] = array(
173
                $item['user_id'],
174
                $item['name'],
175
                $item['email'],
176
                $item['role_id'],
177
                $item['store_id'],
178
                empty($item['status']) ? $this->text('No') : $this->text('Yes'),
179
                $this->date($item['created'])
180
            );
181
        }
182
183
        $this->outputFormatTable($rows, $header);
184
    }
185
186
    /**
187
     * Updates a user
188
     */
189
    protected function submitUpdateUser()
190
    {
191
        $params = $this->getParam();
192
193
        if (empty($params[0]) || count($params) < 2) {
194
            $this->errorExit($this->text('Invalid command'));
195
        }
196
197
        if (!is_numeric($params[0])) {
198
            $this->errorExit($this->text('Invalid ID'));
199
        }
200
201
        $this->setSubmitted(null, $this->getParam());
202
        $this->setSubmitted('update', $params[0]);
203
        $this->setSubmittedJson('data');
204
205
        $this->validateComponent('user', array('admin' => true));
206
        $this->updateUser($params[0]);
207
    }
208
209
    /**
210
     * Updates a user
211
     * @param string $user_id
212
     */
213
    protected function updateUser($user_id)
214
    {
215
        if (!$this->isError() && !$this->user->update($user_id, $this->getSubmitted())) {
216
            $this->errorExit($this->text('User has not been updated'));
217
        }
218
    }
219
220
    /**
221
     * Add a new user at once
222
     */
223
    protected function submitAddUser()
224
    {
225
        $this->setSubmitted(null, $this->getParam());
226
        $this->setSubmittedJson('data');
227
        $this->validateComponent('user');
228
        $this->addUser();
229
    }
230
231
    /**
232
     * Add a new user
233
     */
234
    protected function addUser()
235
    {
236
        if (!$this->isError()) {
237
            $id = $this->user->add($this->getSubmitted());
238
            if (empty($id)) {
239
                $this->errorExit($this->text('User has not been added'));
240
            }
241
            $this->line($id);
242
        }
243
    }
244
245
    /**
246
     * Add a new user step by step
247
     */
248
    protected function wizardAddUser()
249
    {
250
        // Required
251
        $this->validatePrompt('email', $this->text('E-mail'), 'user');
252
        $this->validatePrompt('password', $this->text('Password'), 'user');
253
        $this->validatePrompt('name', $this->text('Name'), 'user');
254
255
        // Optional
256
        $this->validatePrompt('role_id', $this->text('Role ID'), 'user', 0);
257
        $this->validatePrompt('store_id', $this->text('Store ID'), 'user', $this->config->get('store', 1));
258
        $this->validatePrompt('timezone', $this->text('Timezone'), 'user', $this->config->get('timezone', 'Europe/London'));
259
        $this->validatePrompt('status', $this->text('Status'), 'user', 0);
260
        $this->validatePrompt('data', $this->text('Data'), 'user');
261
262
        $this->setSubmittedJson('data');
263
        $this->validateComponent('user');
264
        $this->addUser();
265
    }
266
267
    /**
268
     * Sets a user status
269
     * @param $status
270
     */
271
    public function setStatusUser($status)
272
    {
273
        $options = $id = $result = null;
274
275
        if ($this->getParam('all')) {
276
            $options = array();
277
        } else {
278
279
            $id = $this->getParam(0);
280
281
            if (!is_numeric($id)) { // Allow 0 for roleless users
282
                $this->errorExit($this->text('Invalid ID'));
283
            }
284
285
            if ($this->getParam('role')) {
286
                $options = array('role_id' => $id);
287
            } else if ($this->getParam('store')) {
288
                $options = array('store_id' => $id);
289
            }
290
        }
291
292
        if (isset($options)) {
293
            $updated = $count = 0;
294
            foreach ($this->user->getList($options) as $item) {
295
                $count++;
296
                $updated += (int) $this->user->update($item['user_id'], array('status' => (bool) $status));
297
            }
298
            $result = ($count == $updated);
299
        } else if (isset($id)) {
300
            $result = $this->user->update($id, array('status' => (bool) $status));
301
        }
302
303
        if (empty($result)) {
304
            $this->errorExit($this->text('An error occurred'));
305
        }
306
    }
307
308
}
309