Proyectos de Subversion LeadersLinked - Backend

Rev

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