| 17007 |
efrain |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
declare(strict_types=1);
|
|
|
4 |
|
|
|
5 |
namespace LeadersLinked\Controller;
|
|
|
6 |
|
|
|
7 |
use Laminas\Db\Adapter\AdapterInterface;
|
|
|
8 |
|
|
|
9 |
use Laminas\Mvc\Controller\AbstractActionController;
|
|
|
10 |
use Laminas\Log\LoggerInterface;
|
|
|
11 |
use Laminas\View\Model\ViewModel;
|
|
|
12 |
use Laminas\View\Model\JsonModel;
|
|
|
13 |
use LeadersLinked\Library\Functions;
|
|
|
14 |
use LeadersLinked\Mapper\HabitUserMapper;
|
|
|
15 |
use LeadersLinked\Model\HabitUser;
|
|
|
16 |
use LeadersLinked\Mapper\UserMapper;
|
| 17008 |
efrain |
17 |
|
| 17007 |
efrain |
18 |
use LeadersLinked\Mapper\QueryMapper;
|
|
|
19 |
use Laminas\Paginator\Adapter\DbSelect;
|
|
|
20 |
use Laminas\Paginator\Paginator;
|
| 17008 |
efrain |
21 |
use LeadersLinked\Form\Habit\HabitUserForm;
|
|
|
22 |
use LeadersLinked\Form\Habit\HabitUserUploadForm;
|
| 17007 |
efrain |
23 |
|
|
|
24 |
class HabitUserController extends AbstractActionController {
|
|
|
25 |
|
|
|
26 |
/**
|
|
|
27 |
*
|
|
|
28 |
* @var \Laminas\Db\Adapter\AdapterInterface
|
|
|
29 |
*/
|
|
|
30 |
private $adapter;
|
|
|
31 |
|
|
|
32 |
/**
|
|
|
33 |
*
|
|
|
34 |
* @var \LeadersLinked\Cache\CacheInterface
|
|
|
35 |
*/
|
|
|
36 |
private $cache;
|
|
|
37 |
|
|
|
38 |
|
|
|
39 |
/**
|
|
|
40 |
*
|
|
|
41 |
* @var \Laminas\Log\LoggerInterface
|
|
|
42 |
*/
|
|
|
43 |
private $logger;
|
|
|
44 |
|
|
|
45 |
/**
|
|
|
46 |
*
|
|
|
47 |
* @var array
|
|
|
48 |
*/
|
|
|
49 |
private $config;
|
|
|
50 |
|
|
|
51 |
|
|
|
52 |
/**
|
|
|
53 |
*
|
|
|
54 |
* @var \Laminas\Mvc\I18n\Translator
|
|
|
55 |
*/
|
|
|
56 |
private $translator;
|
|
|
57 |
|
|
|
58 |
|
|
|
59 |
/**
|
|
|
60 |
*
|
|
|
61 |
* @param \Laminas\Db\Adapter\AdapterInterface $adapter
|
|
|
62 |
* @param \LeadersLinked\Cache\CacheInterface $cache
|
|
|
63 |
* @param \Laminas\Log\LoggerInterface LoggerInterface $logger
|
|
|
64 |
* @param array $config
|
|
|
65 |
* @param \Laminas\Mvc\I18n\Translator $translator
|
|
|
66 |
*/
|
|
|
67 |
public function __construct($adapter, $cache, $logger, $config, $translator)
|
|
|
68 |
{
|
|
|
69 |
$this->adapter = $adapter;
|
|
|
70 |
$this->cache = $cache;
|
|
|
71 |
$this->logger = $logger;
|
|
|
72 |
$this->config = $config;
|
|
|
73 |
$this->translator = $translator;
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
public function indexAction() {
|
|
|
77 |
$request = $this->getRequest();
|
|
|
78 |
$currentUserPlugin = $this->plugin('currentUserPlugin');
|
|
|
79 |
$currentCompany = $currentUserPlugin->getCompany();
|
|
|
80 |
$currentUser = $currentUserPlugin->getUser();
|
|
|
81 |
|
|
|
82 |
|
|
|
83 |
$request = $this->getRequest();
|
|
|
84 |
if ($request->isGet()) {
|
|
|
85 |
|
|
|
86 |
$headers = $request->getHeaders();
|
|
|
87 |
|
|
|
88 |
$isJson = false;
|
|
|
89 |
if ($headers->has('Accept')) {
|
|
|
90 |
$accept = $headers->get('Accept');
|
|
|
91 |
|
|
|
92 |
$prioritized = $accept->getPrioritized();
|
|
|
93 |
|
|
|
94 |
foreach ($prioritized as $key => $value) {
|
|
|
95 |
$raw = trim($value->getRaw());
|
|
|
96 |
|
|
|
97 |
if (!$isJson) {
|
|
|
98 |
$isJson = strpos($raw, 'json');
|
|
|
99 |
}
|
|
|
100 |
}
|
|
|
101 |
}
|
|
|
102 |
|
|
|
103 |
//$isJson = true;
|
|
|
104 |
if ($isJson) {
|
|
|
105 |
|
|
|
106 |
|
|
|
107 |
$search = $this->params()->fromQuery('search', []);
|
|
|
108 |
$search = empty($search['value']) ? '' : Functions::sanitizeFilterString($search['value']);
|
|
|
109 |
|
|
|
110 |
$start = intval($this->params()->fromQuery('start', 0), 10);
|
|
|
111 |
$records_x_page = intval($this->params()->fromQuery('length', 10), 10);
|
|
|
112 |
$page = intval($start / $records_x_page);
|
|
|
113 |
$page++;
|
|
|
114 |
|
|
|
115 |
$order = $this->params()->fromQuery('order', []);
|
|
|
116 |
$order_field = empty($order[0]['column']) ? 99 : intval($order[0]['column'], 10);
|
|
|
117 |
$order_direction = empty($order[0]['dir']) ? 'ASC' : Functions::sanitizeFilterString(filter_var($order[0]['dir']));
|
|
|
118 |
|
|
|
119 |
$fields = ['first_name', 'last_name', 'email'];
|
|
|
120 |
$order_field = isset($fields[$order_field]) ? $fields[$order_field] : 'first_name';
|
|
|
121 |
|
|
|
122 |
if (!in_array($order_direction, ['ASC', 'DESC'])) {
|
|
|
123 |
$order_direction = 'ASC';
|
|
|
124 |
}
|
|
|
125 |
|
|
|
126 |
|
|
|
127 |
|
|
|
128 |
$acl = $this->getEvent()->getViewModel()->getVariable('acl');
|
|
|
129 |
$allowAdd = $acl->isAllowed($currentUser->usertype_id, 'habits/users/add');
|
| 17008 |
efrain |
130 |
// $allowEdit = $acl->isAllowed($currentUser->usertype_id, 'habits/users/edit');
|
| 17007 |
efrain |
131 |
$allowDelete = $acl->isAllowed($currentUser->usertype_id, 'habits/users/delete');
|
| 17008 |
efrain |
132 |
//$allowUpload = $acl->isAllowed($currentUser->usertype_id, 'habits/users/upload');
|
| 17007 |
efrain |
133 |
|
|
|
134 |
|
|
|
135 |
$items = [];
|
|
|
136 |
|
|
|
137 |
$queryMapper = QueryMapper::getInstance($this->adapter);
|
|
|
138 |
$select = $queryMapper->getSql()->select();
|
|
|
139 |
$select->columns(['role']);
|
|
|
140 |
$select->from(['cu' => HabitUserMapper::_TABLE]);
|
|
|
141 |
$select->join(['u' => UserMapper::_TABLE], 'cu.user_id = u.id', ['uuid', 'first_name', 'last_name', 'email']);
|
| 17008 |
efrain |
142 |
|
|
|
143 |
|
| 17007 |
efrain |
144 |
if($search) {
|
|
|
145 |
$select->where->nest()->like('first_name', '%' . $search . '%')
|
|
|
146 |
->or->like('last_name', '%' . $search . '%')
|
|
|
147 |
->or->like('email', '%' . $search . '%')->unnest();
|
|
|
148 |
}
|
|
|
149 |
|
|
|
150 |
$select->order($order_field . ' ' . $order_direction);
|
|
|
151 |
|
|
|
152 |
//echo $select->getSqlString($this->adapter->platform); exit;
|
|
|
153 |
|
|
|
154 |
|
|
|
155 |
$paginatorAdapter = new DbSelect($select, $this->adapter);
|
|
|
156 |
$paginator = new Paginator($paginatorAdapter);
|
|
|
157 |
$paginator->setItemCountPerPage($records_x_page);
|
|
|
158 |
$paginator->setCurrentPageNumber($page);
|
|
|
159 |
|
|
|
160 |
$records = $paginator->getCurrentItems();
|
|
|
161 |
|
|
|
162 |
foreach ($records as $record)
|
|
|
163 |
{
|
|
|
164 |
$item = [
|
|
|
165 |
'first_name' => $record['first_name'],
|
|
|
166 |
'last_name' => $record['last_name'],
|
|
|
167 |
'email' => $record['email'],
|
|
|
168 |
'actions' => [
|
| 17008 |
efrain |
169 |
//'link_edit' => $allowEdit ? $this->url()->fromRoute('habits/users/edit', ['id' => $record['uuid'] ]) : '',
|
| 17007 |
efrain |
170 |
'link_delete' => $allowDelete ? $this->url()->fromRoute('habits/users/delete', ['id' => $record['uuid'] ]) : '',
|
|
|
171 |
]
|
|
|
172 |
];
|
|
|
173 |
|
|
|
174 |
array_push($items, $item);
|
|
|
175 |
}
|
|
|
176 |
|
|
|
177 |
|
|
|
178 |
|
|
|
179 |
|
|
|
180 |
return new JsonModel([
|
|
|
181 |
'success' => true,
|
|
|
182 |
'data' => [
|
|
|
183 |
'total' => $paginator->getTotalItemCount(),
|
|
|
184 |
'items' => $items,
|
|
|
185 |
]
|
|
|
186 |
]);
|
|
|
187 |
} else {
|
|
|
188 |
|
|
|
189 |
|
| 17008 |
efrain |
190 |
$form = new HabitUserForm($this->adapter, $currentCompany->id);
|
| 17007 |
efrain |
191 |
|
| 17008 |
efrain |
192 |
|
| 17007 |
efrain |
193 |
$this->layout()->setTemplate('layout/layout-backend');
|
|
|
194 |
$viewModel = new ViewModel();
|
| 17008 |
efrain |
195 |
$viewModel->setTemplate('leaders-linked/habits/users.phtml');
|
| 17007 |
efrain |
196 |
$viewModel->setVariables([
|
| 17008 |
efrain |
197 |
'form' => $form,
|
|
|
198 |
|
| 17007 |
efrain |
199 |
]);
|
|
|
200 |
return $viewModel;
|
|
|
201 |
}
|
|
|
202 |
} else {
|
|
|
203 |
return new JsonModel([
|
|
|
204 |
'success' => false,
|
|
|
205 |
'data' => 'ERROR_METHOD_NOT_ALLOWED'
|
|
|
206 |
]);
|
|
|
207 |
}
|
|
|
208 |
|
|
|
209 |
}
|
|
|
210 |
|
|
|
211 |
|
|
|
212 |
|
|
|
213 |
public function addAction()
|
|
|
214 |
{
|
|
|
215 |
$currentUserPlugin = $this->plugin('currentUserPlugin');
|
|
|
216 |
$currentCompany = $currentUserPlugin->getCompany();
|
|
|
217 |
$currentUser = $currentUserPlugin->getUser();
|
|
|
218 |
$request = $this->getRequest();
|
|
|
219 |
|
|
|
220 |
if($request->isPost()) {
|
|
|
221 |
|
|
|
222 |
|
|
|
223 |
$dataPost = $request->getPost()->toArray();
|
|
|
224 |
|
|
|
225 |
$form = new HabitUserForm($this->adapter, $currentCompany->id);
|
|
|
226 |
$form->setData($dataPost);
|
|
|
227 |
|
|
|
228 |
if($form->isValid()) {
|
|
|
229 |
|
|
|
230 |
$dataPost = (array) $form->getData();
|
|
|
231 |
|
|
|
232 |
|
|
|
233 |
$userMapper = UserMapper::getInstance($this->adapter);
|
|
|
234 |
$user = $userMapper->fetchOneByUuid($dataPost['user_id']);
|
|
|
235 |
|
|
|
236 |
$habitUserMapper = HabitUserMapper::getInstance($this->adapter);
|
| 17008 |
efrain |
237 |
$habitUser = $habitUserMapper->fetchOneByCompanyIdAndUserId($currentCompany->id, $user->id);
|
| 17007 |
efrain |
238 |
|
|
|
239 |
if($habitUser) {
|
|
|
240 |
return new JsonModel([
|
|
|
241 |
'success' => false,
|
| 17008 |
efrain |
242 |
'data' => 'ERROR_HABITS_USER_NOT_FOUND'
|
| 17007 |
efrain |
243 |
]);
|
|
|
244 |
}
|
|
|
245 |
|
| 17008 |
efrain |
246 |
|
| 17007 |
efrain |
247 |
|
| 17008 |
efrain |
248 |
|
| 17007 |
efrain |
249 |
$habitUser = new HabitUser();
|
| 17008 |
efrain |
250 |
$habitUser->network_id = $currentCompany->network_id;
|
|
|
251 |
$habitUser->company_id = $currentCompany->id;
|
|
|
252 |
$habitUser->user_id = $user->id;
|
|
|
253 |
$habitUser->access = HabitUser::ACCESS_UNLIMITED;
|
|
|
254 |
$habitUserMapper = HabitUserMapper::getInstance($this->adapter);
|
| 17007 |
efrain |
255 |
|
|
|
256 |
|
|
|
257 |
if($habitUserMapper->insert($habitUser)) {
|
|
|
258 |
|
|
|
259 |
|
| 17008 |
efrain |
260 |
$this->logger->info('Se agrego el usuario ' . $user->first_name . ' ' . $user->last_name . ' (' . $user->email . ') ' , ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
|
| 17007 |
efrain |
261 |
|
|
|
262 |
$data = [
|
|
|
263 |
'success' => true,
|
|
|
264 |
'data' => 'LABEL_RECORD_ADDED'
|
|
|
265 |
];
|
|
|
266 |
} else {
|
|
|
267 |
$data = [
|
|
|
268 |
'success' => false,
|
|
|
269 |
'data' => $habitUserMapper->getError()
|
|
|
270 |
];
|
|
|
271 |
|
|
|
272 |
}
|
|
|
273 |
|
|
|
274 |
return new JsonModel($data);
|
|
|
275 |
|
|
|
276 |
} else {
|
|
|
277 |
$messages = [];
|
|
|
278 |
$form_messages = (array) $form->getMessages();
|
|
|
279 |
foreach ($form_messages as $fieldname => $field_messages) {
|
|
|
280 |
|
|
|
281 |
$messages[$fieldname] = array_values($field_messages);
|
|
|
282 |
}
|
|
|
283 |
|
|
|
284 |
return new JsonModel([
|
|
|
285 |
'success' => false,
|
|
|
286 |
'data' => $messages
|
|
|
287 |
]);
|
|
|
288 |
|
|
|
289 |
}
|
|
|
290 |
|
|
|
291 |
} else {
|
|
|
292 |
$data = [
|
|
|
293 |
'success' => false,
|
|
|
294 |
'data' => 'ERROR_METHOD_NOT_ALLOWED'
|
|
|
295 |
];
|
|
|
296 |
|
|
|
297 |
return new JsonModel($data);
|
|
|
298 |
}
|
|
|
299 |
|
|
|
300 |
return new JsonModel($data);
|
|
|
301 |
|
|
|
302 |
|
|
|
303 |
}
|
|
|
304 |
|
|
|
305 |
public function editAction()
|
|
|
306 |
{
|
|
|
307 |
$request = $this->getRequest();
|
|
|
308 |
$currentUserPlugin = $this->plugin('currentUserPlugin');
|
|
|
309 |
$currentCompany = $currentUserPlugin->getCompany();
|
|
|
310 |
$currentUser = $currentUserPlugin->getUser();
|
|
|
311 |
|
|
|
312 |
$request = $this->getRequest();
|
|
|
313 |
$uuid = $this->params()->fromRoute('id');
|
|
|
314 |
|
|
|
315 |
|
|
|
316 |
$userMapper = UserMapper::getInstance($this->adapter);
|
|
|
317 |
$user = $userMapper->fetchOneByUuid($uuid);
|
|
|
318 |
|
|
|
319 |
if(!$user) {
|
|
|
320 |
return new JsonModel([
|
|
|
321 |
'success' => false,
|
|
|
322 |
'data' => 'ERROR_USER_NOT_FOUND'
|
|
|
323 |
]);
|
|
|
324 |
|
|
|
325 |
}
|
|
|
326 |
|
|
|
327 |
$habitUserMapper = HabitUserMapper::getInstance($this->adapter);
|
|
|
328 |
$habitUser = $habitUserMapper->fetchOneByCompanyIdAndUserId($currentCompany->id, $user->id);
|
|
|
329 |
|
|
|
330 |
if(!$habitUser) {
|
|
|
331 |
return new JsonModel([
|
|
|
332 |
'success' => false,
|
| 17008 |
efrain |
333 |
'data' => 'ERROR_HABITS_USER_NOT_FOUND'
|
| 17007 |
efrain |
334 |
]);
|
|
|
335 |
}
|
|
|
336 |
|
|
|
337 |
|
|
|
338 |
|
|
|
339 |
if ($request->isPost()) {
|
|
|
340 |
$dataPost = $request->getPost()->toArray();
|
|
|
341 |
|
|
|
342 |
$form = new HabitUserForm($this->adapter, $currentCompany->id);
|
|
|
343 |
$form->setData($dataPost);
|
|
|
344 |
|
|
|
345 |
|
|
|
346 |
|
|
|
347 |
if ($form->isValid()) {
|
|
|
348 |
$dataPost = (array) $form->getData();
|
|
|
349 |
|
| 17008 |
efrain |
350 |
|
| 17007 |
efrain |
351 |
|
|
|
352 |
if($habitUserMapper->update($habitUser)) {
|
|
|
353 |
|
| 17008 |
efrain |
354 |
$this->logger->info('Se actualizo el usuario ' . $user->first_name . ' ' . $user->last_name . ' (' . $user->email . ') ', ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
|
| 17007 |
efrain |
355 |
|
|
|
356 |
$data = [
|
|
|
357 |
'success' => true,
|
|
|
358 |
'data' => 'LABEL_RECORD_UPDATED'
|
|
|
359 |
];
|
|
|
360 |
} else {
|
|
|
361 |
$data = [
|
|
|
362 |
'success' => false,
|
|
|
363 |
'data' => $habitUserMapper->getError()
|
|
|
364 |
];
|
|
|
365 |
|
|
|
366 |
}
|
|
|
367 |
|
|
|
368 |
return new JsonModel($data);
|
|
|
369 |
} else {
|
|
|
370 |
$messages = [];
|
|
|
371 |
$form_messages = (array) $form->getMessages();
|
|
|
372 |
foreach ($form_messages as $fieldname => $field_messages) {
|
|
|
373 |
$messages[$fieldname] = array_values($field_messages);
|
|
|
374 |
}
|
|
|
375 |
|
|
|
376 |
return new JsonModel([
|
|
|
377 |
'success' => false,
|
|
|
378 |
'data' => $messages
|
|
|
379 |
]);
|
|
|
380 |
}
|
|
|
381 |
} else if ($request->isGet()) {
|
|
|
382 |
|
|
|
383 |
|
|
|
384 |
$data = [
|
|
|
385 |
'success' => true,
|
|
|
386 |
'data' => [
|
|
|
387 |
'user_id' => $user->uuid,
|
|
|
388 |
'role' => $habitUser->role,
|
|
|
389 |
]
|
|
|
390 |
];
|
|
|
391 |
|
|
|
392 |
return new JsonModel($data);
|
|
|
393 |
}
|
|
|
394 |
|
|
|
395 |
|
|
|
396 |
$data = [
|
|
|
397 |
'success' => false,
|
|
|
398 |
'data' => 'ERROR_METHOD_NOT_ALLOWED'
|
|
|
399 |
];
|
|
|
400 |
|
|
|
401 |
return new JsonModel($data);
|
|
|
402 |
|
|
|
403 |
}
|
|
|
404 |
|
|
|
405 |
public function deleteAction()
|
|
|
406 |
{
|
|
|
407 |
$request = $this->getRequest();
|
|
|
408 |
$currentUserPlugin = $this->plugin('currentUserPlugin');
|
|
|
409 |
$currentCompany = $currentUserPlugin->getCompany();
|
|
|
410 |
$currentUser = $currentUserPlugin->getUser();
|
|
|
411 |
|
|
|
412 |
$request = $this->getRequest();
|
|
|
413 |
$uuid = $this->params()->fromRoute('id');
|
|
|
414 |
$user_id = $this->params()->fromRoute('user_id');
|
|
|
415 |
|
|
|
416 |
|
|
|
417 |
$userMapper = UserMapper::getInstance($this->adapter);
|
| 17008 |
efrain |
418 |
$user = $userMapper->fetchOneByUuid($uuid);
|
| 17007 |
efrain |
419 |
|
|
|
420 |
if(!$user) {
|
|
|
421 |
return new JsonModel([
|
|
|
422 |
'success' => false,
|
|
|
423 |
'data' => 'ERROR_USER_NOT_FOUND'
|
|
|
424 |
]);
|
|
|
425 |
|
|
|
426 |
}
|
|
|
427 |
|
|
|
428 |
$habitUserMapper = HabitUserMapper::getInstance($this->adapter);
|
| 17008 |
efrain |
429 |
$habitUser = $habitUserMapper->fetchOneByCompanyIdAndUserId($currentCompany->id, $user->id);
|
| 17007 |
efrain |
430 |
|
|
|
431 |
if(!$habitUser) {
|
|
|
432 |
return new JsonModel([
|
|
|
433 |
'success' => false,
|
| 17008 |
efrain |
434 |
'data' => 'ERROR_HABITS_USER_NOT_FOUND'
|
| 17007 |
efrain |
435 |
]);
|
|
|
436 |
}
|
|
|
437 |
|
| 17008 |
efrain |
438 |
|
| 17007 |
efrain |
439 |
|
| 17008 |
efrain |
440 |
|
| 17007 |
efrain |
441 |
|
|
|
442 |
if ($request->isPost()) {
|
|
|
443 |
|
| 17008 |
efrain |
444 |
if ($habitUserMapper->delete($habitUser)) {
|
|
|
445 |
$this->logger->info('Se borro el usuario ' . $user->first_name . ' ' . $user->last_name . ' (' . $user->email . ') ', ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
|
| 17007 |
efrain |
446 |
|
|
|
447 |
$data = [
|
|
|
448 |
'success' => true,
|
|
|
449 |
'data' => 'LABEL_RECORD_DELETED'
|
|
|
450 |
];
|
|
|
451 |
} else {
|
|
|
452 |
|
|
|
453 |
$data = [
|
|
|
454 |
'success' => false,
|
|
|
455 |
'data' => $habitUserMapper->getError()
|
|
|
456 |
];
|
|
|
457 |
|
|
|
458 |
|
|
|
459 |
}
|
|
|
460 |
} else {
|
|
|
461 |
$data = [
|
|
|
462 |
'success' => false,
|
|
|
463 |
'data' => 'ERROR_METHOD_NOT_ALLOWED'
|
|
|
464 |
];
|
|
|
465 |
|
|
|
466 |
|
|
|
467 |
}
|
|
|
468 |
|
|
|
469 |
|
|
|
470 |
return new JsonModel($data);
|
|
|
471 |
|
|
|
472 |
}
|
|
|
473 |
|
|
|
474 |
|
|
|
475 |
}
|