Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 16766 | Rev 16769 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
16248 efrain 1
<?php
2
 
3
declare(strict_types=1);
4
 
5
namespace LeadersLinked\Controller;
6
 
7
use Laminas\Db\Adapter\AdapterInterface;
16768 efrain 8
 
16248 efrain 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\KnowledgeAreaCategoryMapper;
15
use LeadersLinked\Model\KnowledgeAreaCategory;
16
use LeadersLinked\Mapper\UserMapper;
17
use LeadersLinked\Mapper\KnowledgeAreaCategoryUserMapper;
18
use LeadersLinked\Form\KnowledgeArea\KnowledgeAreaCategoryUserDataForm;
19
use LeadersLinked\Form\KnowledgeArea\KnowledgeAreaCategoryUserForm;
20
use LeadersLinked\Mapper\QueryMapper;
21
use Laminas\Paginator\Adapter\DbSelect;
22
use Laminas\Paginator\Paginator;
23
use LeadersLinked\Model\KnowledgeAreaCategoryUser;
24
 
25
 
26
class KnowledgeAreaCategoryUserController extends AbstractActionController {
27
 
28
    /**
29
     *
30
     * @var AdapterInterface
31
     */
32
    private $adapter;
16768 efrain 33
 
16248 efrain 34
    /**
35
     *
36
     * @var  LoggerInterface
37
     */
38
    private $logger;
16768 efrain 39
 
16248 efrain 40
    /**
41
     *
42
     * @var array
43
     */
44
    private $config;
16768 efrain 45
 
16248 efrain 46
    /**
47
     *
48
     * @param AdapterInterface $adapter
49
     * @param LoggerInterface $logger
50
     * @param array $config
51
     */
16768 efrain 52
    public function __construct($adapter, $logger, $config)
53
    {
16248 efrain 54
        $this->adapter = $adapter;
55
        $this->logger = $logger;
56
        $this->config = $config;
57
    }
58
 
59
    public function indexAction() {
60
        $request = $this->getRequest();
61
        $currentUserPlugin = $this->plugin('currentUserPlugin');
62
        $currentCompany = $currentUserPlugin->getCompany();
63
        $currentUser = $currentUserPlugin->getUser();
64
 
65
 
66
        $request = $this->getRequest();
67
        if ($request->isGet()) {
68
 
69
            $headers = $request->getHeaders();
70
 
71
            $isJson = false;
72
            if ($headers->has('Accept')) {
73
                $accept = $headers->get('Accept');
74
 
75
                $prioritized = $accept->getPrioritized();
76
 
77
                foreach ($prioritized as $key => $value) {
78
                    $raw = trim($value->getRaw());
79
 
80
                    if (!$isJson) {
81
                        $isJson = strpos($raw, 'json');
82
                    }
83
                }
84
            }
85
 
86
            //$isJson = true;
87
            if ($isJson) {
88
 
16766 efrain 89
                $category_uuid = Functions::sanitizeFilterString($this->params()->fromQuery('category_id'));
90
 
16248 efrain 91
                if(!$category_uuid) {
92
                    return new JsonModel([
93
                        'success' => true,
94
                        'data' => [
95
                            'total' => 0,
96
                            'items' => [],
97
                            'link_add' => '',
98
                            'link_upload' => '',
99
                        ]
100
                    ]);
101
 
102
                }
103
 
104
 
105
                $knowledgeAreaCategoryMapper = KnowledgeAreaCategoryMapper::getInstance($this->adapter);
106
                $knowledgeAreaCategory = $knowledgeAreaCategoryMapper->fetchOneByUuid($category_uuid);
107
 
108
                if(!$knowledgeAreaCategory) {
109
                    return new JsonModel([
110
                        'success' => false,
111
                        'data' => 'ERROR_KNOWLEDGE_AREA_CATEGORY_NOT_FOUND'
112
                    ]);
113
                }
114
 
115
                if($knowledgeAreaCategory->company_id != $currentCompany->id) {
116
                    return new JsonModel([
117
                        'success' => false,
118
                        'data' => 'ERROR_KNOWLEDGE_AREA_CATEGORY_IS_OTHER_COMPANY'
119
                    ]);
120
                }
121
 
122
 
123
 
124
 
125
                $search = $this->params()->fromQuery('search', []);
16766 efrain 126
                $search = empty($search['value']) ? '' :  Functions::sanitizeFilterString($search['value']);
16248 efrain 127
 
128
                $start = intval($this->params()->fromQuery('start', 0), 10);
129
                $records_x_page = intval($this->params()->fromQuery('length', 10), 10);
130
                $page =  intval($start / $records_x_page);
131
                $page++;
132
 
133
                $order = $this->params()->fromQuery('order', []);
134
                $order_field = empty($order[0]['column']) ? 99 : intval($order[0]['column'], 10);
16766 efrain 135
                $order_direction = empty($order[0]['dir']) ? 'ASC' : Functions::sanitizeFilterString(filter_var($order[0]['dir']));
16248 efrain 136
 
137
                $fields = ['first_name', 'last_name', 'email'];
138
                $order_field = isset($fields[$order_field]) ? $fields[$order_field] : 'first_name';
139
 
140
                if (!in_array($order_direction, ['ASC', 'DESC'])) {
141
                    $order_direction = 'ASC';
142
                }
143
 
144
 
145
 
146
                $acl = $this->getEvent()->getViewModel()->getVariable('acl');
147
                $allowAdd = $acl->isAllowed($currentUser->usertype_id, 'knowledge-area/categories/users/add');
148
                $allowEdit = $acl->isAllowed($currentUser->usertype_id, 'knowledge-area/categories/users/edit');
149
                $allowDelete = $acl->isAllowed($currentUser->usertype_id, 'knowledge-area/categories/users/delete');
150
                $allowUpload = $acl->isAllowed($currentUser->usertype_id, 'knowledge-area/categories/users/upload');
151
 
152
 
153
                $items = [];
154
 
155
                $queryMapper = QueryMapper::getInstance($this->adapter);
156
                $select = $queryMapper->getSql()->select();
157
                $select->columns(['role']);
158
                $select->from(['cu' => KnowledgeAreaCategoryUserMapper::_TABLE]);
159
                $select->join(['u' => UserMapper::_TABLE], 'cu.user_id = u.id',  ['uuid', 'first_name', 'last_name', 'email']);
160
                $select->where->equalTo('cu.category_id', $knowledgeAreaCategory->id);
161
 
162
 
163
                if($search) {
164
                    $select->where->nest()->like('first_name', '%' . $search . '%')
165
                    ->or->like('last_name', '%' . $search . '%')
166
                    ->or->like('email', '%' . $search . '%')->unnest();
167
                }
168
 
169
                $select->order($order_field . ' ' . $order_direction);
170
 
171
                //echo $select->getSqlString($this->adapter->platform); exit;
172
 
173
 
174
                $paginatorAdapter = new DbSelect($select, $this->adapter);
175
                $paginator = new Paginator($paginatorAdapter);
176
                $paginator->setItemCountPerPage($records_x_page);
177
                $paginator->setCurrentPageNumber($page);
178
 
179
                $records = $paginator->getCurrentItems();
180
 
181
                foreach ($records as $record) {
182
 
183
                    switch($record['role'])
184
                    {
185
 
186
                        case KnowledgeAreaCategoryUser::ROLE_ADMINISTRATOR :
187
                            $role = 'LABEL_ADMINISTRATOR';
188
                            break;
189
 
190
                        case KnowledgeAreaCategoryUser::ROLE_EDITOR :
191
                            $role = 'LABEL_EDITOR';
192
                            break;
193
 
194
                        case KnowledgeAreaCategoryUser::ROLE_USER :
195
                            $role = 'LABEL_USER';
196
                            break;
197
 
198
                        default :
199
                            $role = 'LABEL_UNKNOWN';
200
                            break;
201
                    }
202
 
203
 
204
 
205
 
206
 
207
 
208
                    $item = [
209
                        'first_name' => $record['first_name'],
210
                        'last_name' => $record['first_name'],
211
                        'email' => $record['email'],
212
                        'role' => $role,
213
                        'actions' => [
214
                            'link_edit' => $allowEdit ? $this->url()->fromRoute('knowledge-area/categories/users/edit', ['id' => $knowledgeAreaCategory->uuid, 'user_id' => $record['uuid'] ]) : '',
215
                            'link_delete' => $allowDelete ? $this->url()->fromRoute('knowledge-area/categories/users/delete', ['id' => $knowledgeAreaCategory->uuid, 'user_id' => $record['uuid'] ]) : '',
216
                        ]
217
                    ];
218
 
219
                    array_push($items, $item);
220
                }
221
 
222
 
223
                if($knowledgeAreaCategory->privacy == KnowledgeAreaCategory::PRIVACY_COMPANY) {
224
                    $roles = [
225
                        KnowledgeAreaCategoryUser::ROLE_USER => 'LABEL_USER',
226
                        KnowledgeAreaCategoryUser::ROLE_EDITOR => 'LABEL_EDITOR',
227
                        KnowledgeAreaCategoryUser::ROLE_ADMINISTRATOR => 'LABEL_ADMINISTRATOR',
228
                    ];
229
                } else {
230
                    $roles = [
231
                        KnowledgeAreaCategoryUser::ROLE_EDITOR => 'LABEL_EDITOR',
232
                        KnowledgeAreaCategoryUser::ROLE_ADMINISTRATOR => 'LABEL_ADMINISTRATOR',
233
                    ];
234
                }
235
 
236
 
237
 
238
                return new JsonModel([
239
                    'success' => true,
240
                    'data' => [
241
                        'total' => $paginator->getTotalItemCount(),
242
                        'items' => $items,
243
                        'link_add' => $allowAdd ? $this->url()->fromRoute('knowledge-area/categories/users/add', ['id' => $knowledgeAreaCategory->uuid ] ) : '',
244
                        'link_upload' => $allowUpload ? $this->url()->fromRoute('knowledge-area/categories/users/upload', ['id' => $knowledgeAreaCategory->uuid ] ) : '',
245
                        'roles' => $roles,
246
                    ]
247
                ]);
248
            } else {
249
 
250
                $form = new  KnowledgeAreaCategoryUserForm($this->adapter, $currentCompany->id, KnowledgeAreaCategory::PRIVACY_COMPANY);
251
                $formFilter = new KnowledgeAreaCategoryUserDataForm($this->adapter, $currentCompany->id);
252
 
253
 
254
                $this->layout()->setTemplate('layout/layout-backend');
255
                $viewModel = new ViewModel();
256
                $viewModel->setTemplate('leaders-linked/knowledge-area-category-users/index.phtml');
257
                $viewModel->setVariables([
258
                    'form' =>  $form,
259
                    'formFilter' => $formFilter,
260
                ]);
261
                return $viewModel;
262
            }
263
        } else {
264
            return new JsonModel([
265
                'success' => false,
266
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
267
            ]);
268
        }
269
 
270
    }
271
 
272
    public function addAction()
273
    {
274
        $currentUserPlugin  = $this->plugin('currentUserPlugin');
275
        $currentCompany     = $currentUserPlugin->getCompany();
276
        $currentUser        = $currentUserPlugin->getUser();
277
        $request            = $this->getRequest();
278
 
279
        if($request->isPost()) {
280
 
16766 efrain 281
            $category_uuid = Functions::sanitizeFilterString($this->params()->fromRoute('id'));
16248 efrain 282
 
283
            $knowledgeAreaCategoryMapper = KnowledgeAreaCategoryMapper::getInstance($this->adapter);
284
            $knowledgeAreaCategory = $knowledgeAreaCategoryMapper->fetchOneByUuid($category_uuid);
285
 
286
            if(!$knowledgeAreaCategory) {
287
                return new JsonModel([
288
                    'success' => false,
289
                    'data' => 'ERROR_KNOWLEDGE_AREA_CATEGORY_NOT_FOUND'
290
                ]);
291
            }
292
 
293
            if($knowledgeAreaCategory->company_id != $currentCompany->id) {
294
                return new JsonModel([
295
                    'success' => false,
296
                    'data' => 'ERROR_KNOWLEDGE_AREA_CATEGORY_IS_OTHER_COMPANY'
297
                ]);
298
            }
299
 
300
            $dataPost = $request->getPost()->toArray();
301
 
302
            $form = new  KnowledgeAreaCategoryUserForm($this->adapter, $currentCompany->id, $knowledgeAreaCategory->privacy);
303
            $form->setData($dataPost);
304
 
305
            if($form->isValid()) {
306
 
307
                $dataPost = (array) $form->getData();
308
 
309
 
310
                $userMapper = UserMapper::getInstance($this->adapter);
311
                $user = $userMapper->fetchOneByUuid($dataPost['user_id']);
312
 
313
                $knowledgeAreaCategoryUserMapper = KnowledgeAreaCategoryUserMapper::getInstance($this->adapter);
314
                $knowledgeAreaCategoryUser = $knowledgeAreaCategoryUserMapper->fetchOneByCategoryIdAndUserId($knowledgeAreaCategory->id, $user->id);
315
 
316
                if($knowledgeAreaCategoryUser) {
317
                    return new JsonModel([
318
                        'success' => false,
319
                        'data' => 'ERROR_KNOWLEDGE_AREA_CATEGORY_USER_ALREADY_FOUND'
320
                    ]);
321
                }
322
 
323
 
324
                $knowledgeAreaCategoryUser =  new KnowledgeAreaCategoryUser();
325
                $knowledgeAreaCategoryUser->category_id = $knowledgeAreaCategory->id;
326
                $knowledgeAreaCategoryUser->user_id = $user->id;
327
                $knowledgeAreaCategoryUser->role = $dataPost['role'];
328
                $knowledgeAreaCategoryUserMapper = KnowledgeAreaCategoryUserMapper::getInstance($this->adapter);
329
 
330
 
331
                if($knowledgeAreaCategoryUserMapper->insert($knowledgeAreaCategoryUser)) {
332
 
333
 
334
                    $this->logger->info('Se agrego el usuario ' . $user->first_name . ' ' . $user->last_name . ' (' . $user->email . ') la categoria ' . $knowledgeAreaCategory->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
335
 
336
                    $data = [
337
                        'success'   => true,
338
                        'data'   => 'LABEL_RECORD_ADDED'
339
                    ];
340
                } else {
341
                    $data = [
342
                        'success'   => false,
343
                        'data'      => $knowledgeAreaCategoryUserMapper->getError()
344
                    ];
345
 
346
                }
347
 
348
                return new JsonModel($data);
349
 
350
            } else {
351
                $messages = [];
352
                $form_messages = (array) $form->getMessages();
353
                foreach ($form_messages as $fieldname => $field_messages) {
354
 
355
                    $messages[$fieldname] = array_values($field_messages);
356
                }
357
 
358
                return new JsonModel([
359
                    'success' => false,
360
                    'data' => $messages
361
                ]);
362
 
363
            }
364
 
365
        } else {
366
            $data = [
367
                'success' => false,
368
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
369
            ];
370
 
371
            return new JsonModel($data);
372
        }
373
 
374
        return new JsonModel($data);
375
 
376
 
377
    }
378
 
379
    public function editAction()
380
    {
381
        $request = $this->getRequest();
382
        $currentUserPlugin = $this->plugin('currentUserPlugin');
383
        $currentCompany = $currentUserPlugin->getCompany();
384
        $currentUser = $currentUserPlugin->getUser();
385
 
386
        $request = $this->getRequest();
387
        $uuid = $this->params()->fromRoute('id');
388
        $user_uuid = $this->params()->fromRoute('user_id');
389
 
390
        $knowledgeAreaCategoryMapper = KnowledgeAreaCategoryMapper::getInstance($this->adapter);
391
        $knowledgeAreaCategory = $knowledgeAreaCategoryMapper->fetchOneByUuid($uuid);
392
 
393
        if(!$knowledgeAreaCategory) {
394
            return new JsonModel([
395
                'success' => false,
396
                'data' => 'ERROR_KNOWLEDGE_AREA_CATEGORY_NOT_FOUND'
397
            ]);
398
        }
399
 
400
        if($knowledgeAreaCategory->company_id != $currentCompany->id) {
401
            return new JsonModel([
402
                'success' => false,
403
                'data' => 'ERROR_KNOWLEDGE_AREA_CATEGORY_IS_OTHER_COMPANY'
404
            ]);
405
        }
406
 
407
        $userMapper = UserMapper::getInstance($this->adapter);
408
        $user = $userMapper->fetchOneByUuid($user_uuid);
409
 
410
        if(!$user) {
411
            return new JsonModel([
412
                'success' => false,
413
                'data' => 'ERROR_USER_NOT_FOUND'
414
            ]);
415
 
416
        }
417
 
418
        $knowledgeAreaCategoryUserMapper = KnowledgeAreaCategoryUserMapper::getInstance($this->adapter);
419
        $knowledgeAreaCategoryUser = $knowledgeAreaCategoryUserMapper->fetchOneByCategoryIdAndUserId($knowledgeAreaCategory->id, $user->id);
420
 
421
        if(!$knowledgeAreaCategoryUser) {
422
            return new JsonModel([
423
                'success' => false,
424
                'data' => 'ERROR_KNOWLEDGE_AREA_CATEGORY_USER_NOT_FOUND'
425
            ]);
426
        }
427
 
428
 
429
 
430
        if ($request->isPost()) {
431
            $dataPost = $request->getPost()->toArray();
432
 
433
            $form = new  KnowledgeAreaCategoryUserForm($this->adapter, $currentCompany->id, $knowledgeAreaCategory->privacy);
434
            $form->setData($dataPost);
435
 
436
 
437
 
438
            if ($form->isValid()) {
439
                $dataPost = (array) $form->getData();
440
 
441
                $knowledgeAreaCategoryUser->role = $dataPost['role'];
442
 
443
 
444
                if($knowledgeAreaCategoryUserMapper->update($knowledgeAreaCategoryUser)) {
445
 
446
                  $this->logger->info('Se actualizo el usuario ' . $user->first_name . ' ' . $user->last_name . ' (' . $user->email . ') la categoria ' . $knowledgeAreaCategory->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
447
 
448
                   $data = [
449
                       'success'   => true,
450
                       'data'   => 'LABEL_RECORD_UPDATED'
451
                   ];
452
               } else {
453
                   $data = [
454
                       'success'   => false,
455
                       'data'      => $knowledgeAreaCategoryUserMapper->getError()
456
                   ];
457
 
458
               }
459
 
460
                return new JsonModel($data);
461
            } else {
462
                $messages = [];
463
                $form_messages = (array) $form->getMessages();
464
                foreach ($form_messages as $fieldname => $field_messages) {
465
                    $messages[$fieldname] = array_values($field_messages);
466
                }
467
 
468
                return new JsonModel([
469
                    'success' => false,
470
                    'data' => $messages
471
                ]);
472
            }
473
        } else if ($request->isGet()) {
474
 
475
 
476
            $data = [
477
                'success' => true,
478
                'data' => [
479
                    'user_id' => $user->uuid,
480
                    'role' => $knowledgeAreaCategoryUser->role,
481
                ]
482
            ];
483
 
484
            return new JsonModel($data);
485
        }
486
 
487
 
488
        $data = [
489
            'success' => false,
490
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
491
        ];
492
 
493
        return new JsonModel($data);
494
 
495
    }
496
 
497
    public function deleteAction()
498
    {
499
        $request = $this->getRequest();
500
        $currentUserPlugin = $this->plugin('currentUserPlugin');
501
        $currentCompany = $currentUserPlugin->getCompany();
502
        $currentUser = $currentUserPlugin->getUser();
503
 
504
        $request = $this->getRequest();
505
        $uuid = $this->params()->fromRoute('id');
506
        $user_id = $this->params()->fromRoute('user_id');
507
 
508
 
509
        $knowledgeAreaCategoryMapper = KnowledgeAreaCategoryMapper::getInstance($this->adapter);
510
        $knowledgeAreaCategory = $knowledgeAreaCategoryMapper->fetchOneByUuid($uuid);
511
 
512
        if(!$knowledgeAreaCategory) {
513
            return new JsonModel([
514
                'success' => false,
515
                'data' => 'ERROR_KNOWLEDGE_AREA_CATEGORY_NOT_FOUND'
516
            ]);
517
        }
518
 
519
        if($knowledgeAreaCategory->company_id != $currentCompany->id) {
520
            return new JsonModel([
521
                'success' => false,
522
                'data' => 'ERROR_KNOWLEDGE_AREA_CATEGORY_IS_OTHER_COMPANY'
523
            ]);
524
        }
525
 
526
        $userMapper = UserMapper::getInstance($this->adapter);
527
        $user = $userMapper->fetchOneByUuid($user_id);
528
 
529
        if(!$user) {
530
            return new JsonModel([
531
                'success' => false,
532
                'data' => 'ERROR_USER_NOT_FOUND'
533
            ]);
534
 
535
        }
536
 
537
        $knowledgeAreaCategoryUserMapper = KnowledgeAreaCategoryUserMapper::getInstance($this->adapter);
538
        $knowledgeAreaCategoryUser = $knowledgeAreaCategoryUserMapper->fetchOneByCategoryIdAndUserId($knowledgeAreaCategory->id, $user->id);
539
 
540
        if(!$knowledgeAreaCategoryUser) {
541
            return new JsonModel([
542
                'success' => false,
543
                'data' => 'ERROR_KNOWLEDGE_AREA_CATEGORY_USER_NOT_FOUND'
544
            ]);
545
        }
546
 
547
 
548
 
549
        if ($request->isPost()) {
550
 
551
            if ($knowledgeAreaCategoryUserMapper->deleteOneByCategoryIdAndUserId($knowledgeAreaCategory->id, $user->id)) {
552
            $this->logger->info('Se borro el usuario ' . $user->first_name . ' ' . $user->last_name . ' (' . $user->email . ') la categoria ' . $knowledgeAreaCategory->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
553
 
554
                $data = [
555
                    'success' => true,
556
                    'data' => 'LABEL_RECORD_DELETED'
557
                ];
558
            } else {
559
 
560
                $data = [
561
                    'success' => false,
562
                    'data' => $knowledgeAreaCategoryUserMapper->getError()
563
                ];
564
 
565
 
566
            }
567
        } else {
568
            $data = [
569
                'success' => false,
570
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
571
            ];
572
 
573
 
574
        }
575
 
576
 
577
        return new JsonModel($data);
578
 
579
    }
580
}