Proyectos de Subversion LeadersLinked - Backend

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
16971 efrain 1
<?php
2
declare(strict_types=1);
3
 
4
namespace LeadersLinked\Controller;
5
 
6
use Laminas\Mvc\Controller\AbstractActionController;
7
use Laminas\View\Model\ViewModel;
8
use Laminas\View\Model\JsonModel;
9
 
10
use LeadersLinked\Library\Functions;
11
use LeadersLinked\Form\HighPerformanceTeam\HighPerformanceTeamForm;
12
use LeadersLinked\Mapper\HighPerformanceTeamMapper;
13
use LeadersLinked\Mapper\HighPerformanceTeamMemberMapper;
14
use LeadersLinked\Model\HighPerformanceTeamMember;
15
 
16
 
17
 
18
 
19
 
20
class HighPerformanceTeamController extends AbstractActionController
21
{
22
    /**
23
     *
24
     * @var \Laminas\Db\Adapter\AdapterInterface
25
     */
26
    private $adapter;
27
 
28
    /**
29
     *
30
     * @var \LeadersLinked\Cache\CacheInterface
31
     */
32
    private $cache;
33
 
34
 
35
    /**
36
     *
37
     * @var \Laminas\Log\LoggerInterface
38
     */
39
    private $logger;
40
 
41
    /**
42
     *
43
     * @var array
44
     */
45
    private $config;
46
 
47
 
48
    /**
49
     *
50
     * @var \Laminas\Mvc\I18n\Translator
51
     */
52
    private $translator;
53
 
54
 
55
    /**
56
     *
57
     * @param \Laminas\Db\Adapter\AdapterInterface $adapter
58
     * @param \LeadersLinked\Cache\CacheInterface $cache
59
     * @param \Laminas\Log\LoggerInterface LoggerInterface $logger
60
     * @param array $config
61
     * @param \Laminas\Mvc\I18n\Translator $translator
62
     */
63
    public function __construct($adapter, $cache, $logger, $config, $translator)
64
    {
65
        $this->adapter      = $adapter;
66
        $this->cache        = $cache;
67
        $this->logger       = $logger;
68
        $this->config       = $config;
69
        $this->translator   = $translator;
70
    }
71
 
72
 
73
 
74
 
75
    public function indexAction()
76
    {
77
 
78
 
79
        $currentUserPlugin = $this->plugin('currentUserPlugin');
80
        $currentUser = $currentUserPlugin->getUser();
81
        $currentCompany = $currentUserPlugin->getCompany();
82
 
83
 
84
        $request = $this->getRequest();
85
        if($request->isGet()) {
86
 
87
 
88
            $headers  = $request->getHeaders();
89
 
90
            $isJson = false;
91
            if($headers->has('Accept')) {
92
                $accept = $headers->get('Accept');
93
 
94
                $prioritized = $accept->getPrioritized();
95
 
96
                foreach($prioritized as $key => $value) {
97
                    $raw = trim($value->getRaw());
98
 
99
                    if(!$isJson) {
100
                        $isJson = strpos($raw, 'json');
101
                    }
102
 
103
                }
104
            }
105
 
106
            if($isJson) {
107
                $ids = [0];
108
                $highPerformanceTeamMemberMapper = HighPerformanceTeamMemberMapper::getInstance($this->adapter);
109
                $records = $highPerformanceTeamMemberMapper->fetchAllByUserId($currentUser->id);
110
                foreach($records as $record)
111
                {
112
                    if(in_array($record->status, [ HighPerformanceTeamMember::STATUS_ADDED_BY_ADMIN, HighPerformanceTeamMember::STATUS_ACCEPTED ])) {
113
                        array_push($ids, $record->id);
114
 
115
                    }
116
 
117
                }
118
 
119
                $records = $highPerformanceTeamMemberMapper->fetchAllMyGroupsByUserId($currentUser->id);
120
                foreach($records as $record)
121
                {
122
                    if(!in_array($$ids, $record->id)) {
123
                        array_push($ids, $record->id);
124
 
125
                    }
126
 
127
                }
128
 
129
 
130
                $data = [];
131
                $highPerformanceTeamMapper = HighPerformanceTeamMapper::getInstance($this->adapter);
132
                $records = $highPerformanceTeamMapper->fetchAllActiveByCompanyIdAndIds($currentCompany->id, $ids);
133
 
134
 
135
                foreach($records as $record)
136
                {
137
                    $data[ $record->uuid ] = $record->title;
138
                }
139
 
140
 
141
                return new JsonModel([
142
                    'success' => true,
143
                    'data' => $data,
144
                ]);
145
 
146
 
147
 
148
 
149
 
150
 
151
            } else  {
152
 
153
 
154
 
155
 
156
 
157
                $formTeam = new HighPerformanceTeamForm();
158
                $this->layout()->setTemplate('layout/layout-backend');
159
                $viewModel = new ViewModel();
160
                $viewModel->setTemplate('leaders-linked/high-performance-teams/index.phtml');
161
                $viewModel->setVariables([
162
                    'formTeam' => $formTeam,
163
                ]);
164
                return $viewModel ;
165
            }
166
        } else {
167
            return new JsonModel([
168
                'success' => false,
169
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
170
            ]);
171
        }
172
    }
173
 
174
    /*
175
 
176
    public function addAction()
177
    {
178
      $currentUserPlugin = $this->plugin('currentUserPlugin');
179
        $currentUser = $currentUserPlugin->getUser();
180
        $currentCompany = $currentUserPlugin->getCompany();
181
 
182
        $request = $this->getRequest();
183
        if($request->isPost()) {
184
            $form = new  HighPerformanceTeamsGroupsForm($this->adapter, $currentCompany->id);
185
            $dataPost = $request->getPost()->toArray();
186
 
187
            $form->setData($dataPost);
188
 
189
            if($form->isValid()) {
190
                $dataPost = (array) $form->getData();
191
                $dataPost['status'] =  isset($dataPost['status']) ? $dataPost['status'] : HighPerformanceTeamsGroups::STATUS_INACTIVE;
192
                $dataPost['company_id']=$currentCompany->id;
193
 
194
                $hydrator = new ObjectPropertyHydrator();
195
                $highPerformanceTeamsGroups = new HighPerformanceTeamsGroups();
196
                $hydrator->hydrate($dataPost, $highPerformanceTeamsGroups);
197
 
198
                $highPerformanceTeamsGroupsMapper = HighPerformanceTeamsGroupsMapper::getInstance($this->adapter);
199
 
200
                $result = $highPerformanceTeamsGroupsMapper->insert($highPerformanceTeamsGroups);
201
 
202
                if($result) {
203
 
204
                        $highPerformanceTeamsGroupsMember = new HighPerformanceTeamsGroupsMembers();
205
                        $highPerformanceTeamsGroupsMember->group_id  = $highPerformanceTeamsGroups->id;
206
 
207
                        $highPerformanceTeamsGroupsMember->user_id = $currentUser->id;
208
                        $highPerformanceTeamsGroupsMember->status = HighPerformanceTeamsGroupsMembers::STATUS_ADDED_BY_ADMIN;
209
                        $highPerformanceTeamsGroupsMember->type = HighPerformanceTeamsGroupsMembers::TYPE_CREATOR;
210
                        $highPerformanceTeamsGroupsMemberMapper = HighPerformanceTeamsGroupsMembersMapper::getInstance($this->adapter);
211
 
212
                        $highPerformanceTeamsGroupsMemberMapper->insert($highPerformanceTeamsGroupsMember);
213
 
214
                        $chatGroup = new ChatGroup();
215
                        $chatGroup->name = $highPerformanceTeamsGroups->title;
216
                        $chatGroup->high_performance_team_group_id =$highPerformanceTeamsGroups->id;
217
                        $chatGroupMapper = ChatGroupMapper::getInstance($this->adapter);
218
                        $result = $chatGroupMapper->insert($chatGroup);
219
                        if ($result) {
220
                            $chatGroup = $chatGroupMapper->fetchOne($chatGroup->id);
221
                            $chatGroupUser = new ChatGroupUser();
222
                            $chatGroupUser->group_id = $chatGroup->id;
223
                            $chatGroupUser->user_id = $currentUser->id;
224
                            $chatGroupUser->owner = ChatGroupUser::OWNER_YES;
225
                            $chatGroupUserMapper = ChatGroupUserMapper::getInstance($this->adapter);
226
                            $result = $chatGroupUserMapper->insert($chatGroupUser);
227
 
228
                                    if($result)  {
229
                                        $fullpath_chat = $this->config['leaderslinked.fullpath.chat'];
230
                                        $dirpath = $fullpath_chat . $chatGroup->uuid;
231
                                        if (! file_exists($dirpath)) {
232
                                            mkdir($dirpath, 0777, true);
233
                                            chmod($dirpath, 0777);
234
                                        }
235
 
236
 
237
                                    } else {
238
                                        $response = [
239
                                            'success' => false,
240
                                            'data' => $chatGroupUserMapper->getError(),
241
                                        ];
242
                                        return new JsonModel($response);
243
                                    }
244
 
245
                        } else {
246
                                    $response = [
247
                                        'success' => false,
248
                                        'data' => $chatGroupMapper->getError(),
249
                                    ];
250
                                    return new JsonModel($response);
251
                        }
252
 
253
 
254
 
255
 
256
 
257
 
258
                    $this->logger->info('Se agrego el grupo de alto rendimiento ' . $highPerformanceTeamsGroups->title, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
259
 
260
                    $data = [
261
                        'success'   => true,
262
                        'data'   => 'LABEL_RECORD_ADDED'
263
                    ];
264
                } else {
265
                    $data = [
266
                        'success'   => false,
267
                        'data'      => $highPerformanceTeamsGroupsMapper->getError()
268
                    ];
269
 
270
                }
271
 
272
                return new JsonModel($data);
273
 
274
            } else {
275
                $messages = [];
276
                $form_messages = (array) $form->getMessages();
277
                foreach($form_messages  as $fieldname => $field_messages)
278
                {
279
 
280
                    $messages[$fieldname] = array_values($field_messages);
281
                }
282
 
283
                return new JsonModel([
284
                    'success'   => false,
285
                    'data'   => $messages
286
                ]);
287
            }
288
 
289
        }else if ($request->isGet()) {
290
            $companyUserMapper = CompanyUserMapper::getInstance($this->adapter);
291
            $userMapper = UserMapper::getInstance($this->adapter);
292
            $datosCompanyUser = $companyUserMapper->fetchAllByCompanyId($currentCompany->id);
293
 
294
            $users=[];
295
 
296
            foreach($datosCompanyUser as $record){
297
                $datosUser = $userMapper->fetchOne($record->user_id);
298
 
299
                    $user=[
300
                        'uuid'=>$datosUser->uuid,
301
                        'email'=>$datosUser->email,
302
                        'first_name'=>$datosUser->first_name,
303
                        'last_name'=>$datosUser->last_name,
304
 
305
                    ];
306
                    array_push($users, $user);
307
 
308
            }
309
 
310
            return new JsonModel([
311
                'success' => true,
312
                'data' => $users
313
 
314
            ]);
315
        } else {
316
            $data = [
317
                'success' => false,
318
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
319
            ];
320
 
321
            return new JsonModel($data);
322
        }
323
 
324
        return new JsonModel($data);
325
 
326
 
327
    }
328
 
329
 
330
 
331
     public function editAction(){
332
 
333
        $currentUserPlugin = $this->plugin('currentUserPlugin');
334
        $currentUser = $currentUserPlugin->getUser();
335
        $currentCompany = $currentUserPlugin->getCompany();
336
        $request = $this->getRequest();
337
        $uuid = $this->params()->fromRoute('group_id');
338
 
339
 
340
        if(!$uuid) {
341
            $data = [
342
                'success'   => false,
343
                'data'   => 'ERROR_INVALID_PARAMETER'
344
            ];
345
 
346
            return new JsonModel($data);
347
        }
348
 
349
        $highPerformanceTeamsGroupsMapper = HighPerformanceTeamsGroupsMapper::getInstance($this->adapter);
350
        $highPerformanceTeamsGroups = $highPerformanceTeamsGroupsMapper->fetchOneByUuid($uuid);
351
 
352
        if (!$highPerformanceTeamsGroups) {
353
            $data = [
354
                'success' => false,
355
                'data' => 'ERROR_RECORD_NOT_FOUND'
356
            ];
357
 
358
            return new JsonModel($data);
359
        }
360
 
361
        $highPerformanceTeamsGroupsMembersMapper = HighPerformanceTeamsGroupsMembersMapper::getInstance($this->adapter);
362
        $highPerformanceTeamsGroupsMember = $highPerformanceTeamsGroupsMembersMapper->fetchOneByGroupIdAndUserId($highPerformanceTeamsGroups->id, $currentUser->id);
363
        if(!$highPerformanceTeamsGroupsMember) {
364
            return new JsonModel([
365
                'success' => false,
366
                'data' => 'ERROR_RECORD_NOT_FOUND'
367
            ]);
368
        }
369
 
370
        if(!($highPerformanceTeamsGroupsMember->status == HighPerformanceTeamsGroupsMembers::STATUS_ACCEPTED || $highPerformanceTeamsGroupsMember->status == HighPerformanceTeamsGroupsMembers::STATUS_ADDED_BY_ADMIN) ){
371
            return new JsonModel([
372
                'success' => false,
373
                'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS'
374
            ]);
375
        }
376
 
377
        if($highPerformanceTeamsGroupsMember->type==HighPerformanceTeamsGroupsMembers::TYPE_MODERATOR||$highPerformanceTeamsGroupsMember->type==HighPerformanceTeamsGroupsMembers::TYPE_USER){
378
            return new JsonModel([
379
                'success' => false,
380
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
381
            ]);
382
        }
383
        if($request->isPost()) {
384
            $form = new  HighPerformanceTeamsGroupsForm($this->adapter, $currentCompany->id);
385
            $dataPost = $request->getPost()->toArray();
386
            $dataPost['status'] =  isset($dataPost['status']) ? $dataPost['status'] : HighPerformanceTeamsGroups::STATUS_INACTIVE;
387
            $form->setData($dataPost);
388
 
389
            if($form->isValid()) {
390
                $dataPost = (array) $form->getData();
391
 
392
                $hydrator = new ObjectPropertyHydrator();
393
                $hydrator->hydrate($dataPost, $highPerformanceTeamsGroups);
394
                $result = $highPerformanceTeamsGroupsMapper->update($highPerformanceTeamsGroups);
395
 
396
                if($result) {
397
                    $chatGroup = new ChatGroup();
398
                    $chatGroup->name = $highPerformanceTeamsGroups->title;
399
                    $chatGroup->high_performance_team_group_id=$highPerformanceTeamsGroups->id;
400
                    $chatGroupMapper = ChatGroupMapper::getInstance($this->adapter);
401
                    $result = $chatGroupMapper->update($chatGroup);
402
 
403
 
404
                    $this->logger->info('Se actualizo el grupo de alto rendimiento ' . $highPerformanceTeamsGroups->title, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
405
 
406
                    $data = [
407
                        'success' => true,
408
                        'data' => 'LABEL_RECORD_UPDATED'
409
                    ];
410
                } else {
411
                    $data = [
412
                        'success'   => false,
413
                        'data'      => $highPerformanceTeamsGroupsMapper->getError()
414
                    ];
415
                }
416
 
417
                return new JsonModel($data);
418
 
419
            } else {
420
                $messages = [];
421
                $form_messages = (array) $form->getMessages();
422
                foreach($form_messages  as $fieldname => $field_messages)
423
                {
424
                    $messages[$fieldname] = array_values($field_messages);
425
                }
426
 
427
                return new JsonModel([
428
                    'success'   => false,
429
                    'data'   => $messages
430
                ]);
431
            }
432
        }else if ($request->isGet()) {
433
            $hydrator = new ObjectPropertyHydrator();
434
 
435
            $data = [
436
                'success' => true,
437
                'data' => $hydrator->extract($highPerformanceTeamsGroups)
438
            ];
439
 
440
            return new JsonModel($data);
441
        } else {
442
            $data = [
443
                'success' => false,
444
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
445
            ];
446
 
447
            return new JsonModel($data);
448
        }
449
 
450
        return new JsonModel($data);
451
 
452
    }
453
 
454
 
455
 
456
 
457
    public function deleteAction(){
458
        $currentUserPlugin = $this->plugin('currentUserPlugin');
459
        $currentCompany = $currentUserPlugin->getCompany();
460
        $currentUser = $currentUserPlugin->getUser();
461
 
462
        $request = $this->getRequest();
463
        $uuid = $this->params()->fromRoute('group_id');
464
 
465
 
466
        if (!$uuid) {
467
            $data = [
468
                'success' => false,
469
                'data' => 'ERROR_INVALID_PARAMETER'
470
            ];
471
 
472
            return new JsonModel($data);
473
        }
474
 
475
 
476
 
477
        $highPerformanceTeamsGroupsMapper = HighPerformanceTeamsGroupsMapper::getInstance($this->adapter);
478
        $highPerformanceTeamsGroups = $highPerformanceTeamsGroupsMapper->fetchOneByUuid($uuid);
479
 
480
        if (!$highPerformanceTeamsGroups) {
481
            $data = [
482
                'success' => false,
483
                'data' => 'ERROR_RECORD_NOT_FOUND'
484
            ];
485
 
486
            return new JsonModel($data);
487
        }
488
 
489
        $highPerformanceTeamsGroupsMembersMapper = HighPerformanceTeamsGroupsMembersMapper::getInstance($this->adapter);
490
        $highPerformanceTeamsGroupsMember = $highPerformanceTeamsGroupsMembersMapper->fetchOneByGroupIdAndUserId($highPerformanceTeamsGroups->id, $currentUser->id);
491
        if(!$highPerformanceTeamsGroupsMember) {
492
            return new JsonModel([
493
                'success' => false,
494
                'data' => 'ERROR_RECORD_NOT_FOUND'
495
            ]);
496
        }
497
        if(!($highPerformanceTeamsGroupsMember->status == HighPerformanceTeamsGroupsMembers::STATUS_ACCEPTED || $highPerformanceTeamsGroupsMember->status == HighPerformanceTeamsGroupsMembers::STATUS_ADDED_BY_ADMIN) ){
498
            return new JsonModel([
499
                'success' => false,
500
                'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS'
501
            ]);
502
        }
503
 
504
        if($highPerformanceTeamsGroupsMember->type==HighPerformanceTeamsGroupsMembers::TYPE_ADMINISTRATOR||$highPerformanceTeamsGroupsMember->type==HighPerformanceTeamsGroupsMembers::TYPE_MODERATOR||$highPerformanceTeamsGroupsMember->type==HighPerformanceTeamsGroupsMembers::TYPE_USER){
505
            return new JsonModel([
506
                'success' => false,
507
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
508
            ]);
509
        }
510
 
511
        if ($request->isPost()) {
512
 
513
 
514
            $chatGroupMapper = ChatGroupMapper::getInstance($this->adapter);
515
            $chatGroup = $chatGroupMapper->fetchOneHptg($highPerformanceTeamsGroups->id);
516
 
517
            if(!$chatGroup) {
518
                return new JsonModel([
519
                    'success' => false,
520
                    'data' => 'ERROR_GROUP_NOT_FOUND'
521
                ]);
522
            }
523
 
524
            $chatGroupUserMapper = ChatGroupUserMapper::getInstance($this->adapter);
525
            $chatGroupOwner = $chatGroupUserMapper->fetchOwnerByGroupId($chatGroup->id);
526
 
527
            if($chatGroupOwner->user_id != $currentUser->id) {
528
                return new JsonModel([
529
                    'success' => false,
530
                    'data' => 'ERROR_CHAT_GROUP_YOU_ARE_NOT_OWNER'
531
                ]);
532
            }
533
 
534
 
535
            $chatGroupUserMessageMapper = ChatGroupUserMessageMapper::getInstance($this->adapter);
536
            $result = $chatGroupUserMessageMapper->deleteAllByGroupId($chatGroup->id);
537
            if(!$result) {
538
                return new JsonModel([
539
                    'success' => false,
540
                    'data' => $chatGroupUserMessageMapper->getError()
541
                ]);
542
            }
543
 
544
            $chatGroupMessageMapper = ChatGroupMessageMapper::getInstance($this->adapter);
545
            $result = $chatGroupMessageMapper->deleteAllByGroupId($chatGroup->id);
546
            if(!$result) {
547
                return new JsonModel([
548
                    'success' => false,
549
                    'data' => $chatGroupMessageMapper->getError()
550
                ]);
551
            }
552
 
553
            $result = $chatGroupUserMapper->deleteAllByGroupId($chatGroup->id);
554
            if(!$result) {
555
                return new JsonModel([
556
                    'success' => false,
557
                    'data' => $chatGroupUserMapper->getError()
558
                ]);
559
            }
560
 
561
            $chatGroupMapper->deleteByGroupId($chatGroup->id);
562
            if(!$result) {
563
                return new JsonModel([
564
                    'success' => false,
565
                    'data' => $chatGroupMapper->getError()
566
                ]);
567
            }
568
 
569
 
570
            $fullpath_chat = $this->config['leaderslinked.fullpath.chat'];
571
            $dirpath = $fullpath_chat . $chatGroup->uuid;
572
 
573
            Functions::rmDirRecursive($dirpath);
574
 
575
 
576
 
577
            $result = $highPerformanceTeamsGroupsMapper->delete($highPerformanceTeamsGroups->id);
578
            if ($result) {
579
 
580
 
581
                    $this->logger->info('Se borro el grupo de alto rendimiento  ' . $highPerformanceTeamsGroups->title, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
582
 
583
                    $data = [
584
                        'success' => true,
585
                        'data' => 'LABEL_RECORD_DELETED'
586
                    ];
587
            } else {
588
 
589
                $data = [
590
                    'success' => false,
591
                    'data' => $highPerformanceTeamsGroupsMapper->getError()
592
                ];
593
 
594
                return new JsonModel($data);
595
            }
596
        } else {
597
            $data = [
598
                'success' => false,
599
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
600
            ];
601
 
602
            return new JsonModel($data);
603
        }
604
 
605
        return new JsonModel($data);
606
    } */
607
 
608
 
609
 
610
 
611
}