Proyectos de Subversion LeadersLinked - Backend

Rev

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

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