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