1 | <?php |
||
17 | class Role extends Base |
||
18 | { |
||
19 | |||
20 | /** |
||
21 | * Role model instance |
||
22 | * @var \gplcart\core\models\UserRole $role |
||
23 | */ |
||
24 | protected $role; |
||
25 | |||
26 | /** |
||
27 | * @param UserRoleModel $role |
||
28 | */ |
||
29 | public function __construct(UserRoleModel $role) |
||
30 | { |
||
31 | parent::__construct(); |
||
32 | |||
33 | $this->role = $role; |
||
34 | } |
||
35 | |||
36 | /** |
||
37 | * Callback for "role-perm-add" command |
||
38 | */ |
||
39 | public function cmdPermAddRole() |
||
40 | { |
||
41 | list($role_id, $existing, $submitted) = $this->getPermissionsRole(); |
||
42 | |||
43 | $data = array('permissions' => array_unique(array_merge($existing, $submitted))); |
||
44 | |||
45 | $this->setSubmitted(null, $data); |
||
46 | $this->setSubmitted('update', $role_id); |
||
47 | $this->validateComponent('user_role'); |
||
48 | $this->updateRole($role_id); |
||
49 | $this->output(); |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * Callback for "role-perm-delete" command |
||
54 | */ |
||
55 | public function cmdPermDeleteRole() |
||
56 | { |
||
57 | list($role_id, $existing, $submitted) = $this->getPermissionsRole(); |
||
58 | |||
59 | $data = array( |
||
60 | 'permissions' => array_unique(array_diff($existing, $submitted)) |
||
61 | ); |
||
62 | |||
63 | $this->setSubmitted(null, $data); |
||
64 | $this->setSubmitted('update', $role_id); |
||
65 | $this->validateComponent('user_role'); |
||
66 | $this->updateRole($role_id); |
||
67 | $this->output(); |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * Callback for "role-get" command |
||
72 | */ |
||
73 | public function cmdGetRole() |
||
74 | { |
||
75 | $result = $this->getListRole(); |
||
76 | $this->outputFormat($result); |
||
77 | $this->outputFormatTableRole($result); |
||
78 | $this->output(); |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * Callback for "role-delete" command |
||
83 | */ |
||
84 | public function cmdDeleteRole() |
||
85 | { |
||
86 | $id = $this->getParam(0); |
||
87 | $all = $this->getParam('all'); |
||
88 | |||
89 | if (!isset($id) && empty($all)) { |
||
90 | $this->errorExit($this->text('Invalid command')); |
||
91 | } |
||
92 | |||
93 | $result = false; |
||
94 | |||
95 | if (isset($id)) { |
||
96 | |||
97 | if (!is_numeric($id)) { |
||
98 | $this->errorExit($this->text('Invalid ID')); |
||
99 | } |
||
100 | |||
101 | $result = $this->role->delete($id); |
||
102 | |||
103 | } else if ($all) { |
||
104 | $deleted = $count = 0; |
||
105 | foreach ($this->role->getList() as $item) { |
||
106 | $count++; |
||
107 | $deleted += (int) $this->role->delete($item['role_id']); |
||
108 | } |
||
109 | |||
110 | $result = ($count == $deleted); |
||
111 | } |
||
112 | |||
113 | if (!$result) { |
||
114 | $this->errorExit($this->text('An error occurred')); |
||
115 | } |
||
116 | |||
117 | $this->output(); |
||
118 | } |
||
119 | |||
120 | /** |
||
121 | * Callback for "role-add" command |
||
122 | */ |
||
123 | public function cmdAddRole() |
||
124 | { |
||
125 | if ($this->getParam()) { |
||
126 | $this->submitAddRole(); |
||
127 | } else { |
||
128 | $this->wizardAddRole(); |
||
129 | } |
||
130 | |||
131 | $this->output(); |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * Callback for "role-update" command |
||
136 | */ |
||
137 | public function cmdUpdateRole() |
||
138 | { |
||
139 | $params = $this->getParam(); |
||
140 | |||
141 | if (empty($params[0]) || count($params) < 2) { |
||
142 | $this->errorExit($this->text('Invalid command')); |
||
143 | } |
||
144 | |||
145 | if (!is_numeric($params[0])) { |
||
146 | $this->errorExit($this->text('Invalid ID')); |
||
147 | } |
||
148 | |||
149 | $this->setSubmitted(null, $this->getParam()); |
||
150 | $this->setSubmittedList('permissions'); |
||
151 | $this->setSubmitted('update', $params[0]); |
||
152 | $this->validateComponent('user_role'); |
||
153 | $this->updateRole($params[0]); |
||
154 | $this->output(); |
||
155 | } |
||
156 | |||
157 | /** |
||
158 | * Returns an array of user roles |
||
159 | * @return array |
||
160 | */ |
||
161 | protected function getListRole() |
||
162 | { |
||
163 | $id = $this->getParam(0); |
||
164 | |||
165 | if (!isset($id)) { |
||
166 | return $this->role->getList(array('limit' => $this->getLimit())); |
||
167 | } |
||
168 | |||
169 | if (empty($id) || !is_numeric($id)) { |
||
170 | $this->errorExit($this->text('Invalid ID')); |
||
171 | } |
||
172 | |||
173 | $result = $this->role->get($id); |
||
174 | |||
175 | if (empty($result)) { |
||
176 | $this->errorExit($this->text('Invalid ID')); |
||
177 | } |
||
178 | |||
179 | return array($result); |
||
180 | } |
||
181 | |||
182 | /** |
||
183 | * Output table format |
||
184 | * @param array $items |
||
185 | */ |
||
186 | protected function outputFormatTableRole(array $items) |
||
187 | { |
||
188 | $header = array( |
||
189 | $this->text('ID'), |
||
190 | $this->text('Name'), |
||
191 | $this->text('Redirect'), |
||
192 | $this->text('Permissions'), |
||
193 | $this->text('Enabled') |
||
194 | ); |
||
195 | |||
196 | $rows = array(); |
||
197 | |||
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() |
||
238 | |||
239 | /** |
||
240 | * Updates a user role |
||
241 | * @param string $role_id |
||
242 | */ |
||
243 | protected function updateRole($role_id) |
||
249 | |||
250 | /** |
||
251 | * Add a new user role at once |
||
252 | */ |
||
253 | protected function submitAddRole() |
||
260 | |||
261 | /** |
||
262 | * Add a new user role |
||
263 | */ |
||
264 | protected function addRole() |
||
274 | |||
275 | /** |
||
276 | * Add a new user role step by step |
||
277 | */ |
||
278 | protected function wizardAddRole() |
||
288 | |||
289 | } |
||
290 |