Proyectos de Subversion LeadersLinked - Backend

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
7811 nelberth 1
<?php
2
declare(strict_types=1);
3
 
4
namespace LeadersLinked\Controller;
5
 
6
use Laminas\Authentication\AuthenticationService;
7
use Laminas\Authentication\Result as AuthResult;
8
use Laminas\Db\Adapter\AdapterInterface;
16768 efrain 9
 
7811 nelberth 10
use Laminas\Mvc\Controller\AbstractActionController;
11
use Laminas\Mvc\I18n\Translator;
12
use Laminas\Log\LoggerInterface;
13
use Laminas\View\Model\ViewModel;
14
use Laminas\View\Model\JsonModel;
7818 nelberth 15
use LeadersLinked\Model\HighPerformanceTeamsGroupsMembers;
7811 nelberth 16
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
7818 nelberth 17
use LeadersLinked\Form\HighPerformanceTeamsGroupsMembersForm;
10313 nelberth 18
 
19
use LeadersLinked\Form\HighPerformanceTeamsGroupsMembersTypeForm;
7811 nelberth 20
use LeadersLinked\Library\Functions;
21
use LeadersLinked\Mapper\UserMapper;
7818 nelberth 22
use LeadersLinked\Mapper\HighPerformanceTeamsGroupsMembersMapper;
7811 nelberth 23
use LeadersLinked\Mapper\CompanyMapper;
24
use LeadersLinked\Mapper\CompanyUserMapper;
25
 
11688 nelberth 26
use LeadersLinked\Model\ChatGroupUser;
27
 
11689 nelberth 28
use LeadersLinked\Mapper\ConnectionMapper;
11688 nelberth 29
use LeadersLinked\Model\ChatGroup;
30
use LeadersLinked\Mapper\ChatGroupMapper;
31
use LeadersLinked\Mapper\ChatGroupUserMapper;
32
 
33
use LeadersLinked\Mapper\ChatGroupUserMessageMapper;
34
use LeadersLinked\Mapper\ChatGroupMessageMapper;
7827 nelberth 35
use LeadersLinked\Mapper\HighPerformanceTeamsGroupsMapper;
7811 nelberth 36
 
7827 nelberth 37
class HighPerformanceTeamsGroupsMembersController extends AbstractActionController
7811 nelberth 38
{
39
    /**
40
     *
16769 efrain 41
     * @var \Laminas\Db\Adapter\AdapterInterface
7811 nelberth 42
     */
43
    private $adapter;
44
 
45
    /**
46
     *
16769 efrain 47
     * @var \LeadersLinked\Cache\CacheInterface
7811 nelberth 48
     */
16769 efrain 49
    private $cache;
50
 
51
 
52
    /**
53
     *
54
     * @var \Laminas\Log\LoggerInterface
55
     */
7811 nelberth 56
    private $logger;
57
 
58
    /**
59
     *
60
     * @var array
61
     */
62
    private $config;
63
 
16769 efrain 64
 
7811 nelberth 65
    /**
66
     *
16769 efrain 67
     * @var \Laminas\Mvc\I18n\Translator
68
     */
69
    private $translator;
70
 
71
 
72
    /**
73
     *
74
     * @param \Laminas\Db\Adapter\AdapterInterface $adapter
75
     * @param \LeadersLinked\Cache\CacheInterface $cache
76
     * @param \Laminas\Log\LoggerInterface LoggerInterface $logger
7811 nelberth 77
     * @param array $config
16769 efrain 78
     * @param \Laminas\Mvc\I18n\Translator $translator
7811 nelberth 79
     */
16769 efrain 80
    public function __construct($adapter, $cache, $logger, $config, $translator)
7811 nelberth 81
    {
16769 efrain 82
        $this->adapter      = $adapter;
83
        $this->cache        = $cache;
84
        $this->logger       = $logger;
85
        $this->config       = $config;
86
        $this->translator   = $translator;
7811 nelberth 87
    }
88
 
89
 
90
 
91
 
92
    public function indexAction()
93
    {
94
 
7829 nelberth 95
 
7827 nelberth 96
 
7811 nelberth 97
        $currentUserPlugin = $this->plugin('currentUserPlugin');
98
        $currentUser = $currentUserPlugin->getUser();
99
        $currentCompany = $currentUserPlugin->getCompany();
11083 nelberth 100
        $group_uuid= $this->params()->fromRoute('group_id');
7811 nelberth 101
 
102
        $request = $this->getRequest();
8053 nelberth 103
        $acl = $this->getEvent()->getViewModel()->getVariable('acl');
104
        $allowInvite = $acl->isAllowed($currentUser->usertype_id,'high-performance-teams/groups/view/members/invite');
10288 nelberth 105
 
106
        $allowDelete = $acl->isAllowed($currentUser->usertype_id,'high-performance-teams/groups/view/members/delete');
10290 nelberth 107
 
10287 nelberth 108
        $allowEdit = $acl->isAllowed($currentUser->usertype_id,'high-performance-teams/groups/view/members/edit');
10286 nelberth 109
 
11169 nelberth 110
        $highPerformanceTeamsGroupsMapper = HighPerformanceTeamsGroupsMapper::getInstance($this->adapter);
111
        $highPerformanceTeamsGroups = $highPerformanceTeamsGroupsMapper->fetchOneByUuid($group_uuid);
112
 
113
        if (!$highPerformanceTeamsGroups) {
114
            $data = [
115
                        'success' => false,
116
                        'data' => 'ERROR_RECORD_NOT_FOUND'
117
                    ];
10287 nelberth 118
 
11169 nelberth 119
            return new JsonModel($data);
120
        }
11180 nelberth 121
 
122
        $highPerformanceTeamsGroupsMembersMapper = HighPerformanceTeamsGroupsMembersMapper::getInstance($this->adapter);
123
        $highPerformanceTeamsGroupsMembers = $highPerformanceTeamsGroupsMembersMapper->fetchAllInnerJoinUser($highPerformanceTeamsGroups->id,UserMapper::_TABLE);
124
        if(!$highPerformanceTeamsGroupsMembers){
125
            return new JsonModel([
126
                'success' => false,
127
                'data' => 'ERROR_RECORD_NOT_FOUND'
128
            ]);
129
        }
130
 
11169 nelberth 131
 
132
        $member = $highPerformanceTeamsGroupsMembersMapper->fetchOneByGroupIdAndUserId($highPerformanceTeamsGroups->id,$currentUser->id);
133
            if(!$member){
134
                return new JsonModel([
135
                    'success' => false,
136
                    'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS'
137
                ]);
138
            }
139
 
140
        if(!($member->status == HighPerformanceTeamsGroupsMembers::STATUS_ACCEPTED || $member->status == HighPerformanceTeamsGroupsMembers::STATUS_ADDED_BY_ADMIN) ){
141
                    return new JsonModel([
142
                        'success' => false,
143
                        'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS'
144
                    ]);
145
                }
146
 
147
            if($member->type==HighPerformanceTeamsGroupsMembers::TYPE_MODERATOR||$member->type==HighPerformanceTeamsGroupsMembers::TYPE_USER){
148
                    $allowDelete=false;
149
                    $allowEdit=false;
150
                }
151
 
7811 nelberth 152
        if($request->isGet()) {
153
 
154
 
155
            $headers  = $request->getHeaders();
156
 
157
            $isJson = false;
158
            if($headers->has('Accept')) {
159
                $accept = $headers->get('Accept');
160
 
161
                $prioritized = $accept->getPrioritized();
162
 
163
                foreach($prioritized as $key => $value) {
164
                    $raw = trim($value->getRaw());
165
 
166
                    if(!$isJson) {
167
                        $isJson = strpos($raw, 'json');
168
                    }
169
 
170
                }
171
            }
172
 
11169 nelberth 173
 
7811 nelberth 174
            if($isJson) {
175
 
11169 nelberth 176
 
10360 nelberth 177
 
10359 nelberth 178
                $items=[];
10358 nelberth 179
                foreach($highPerformanceTeamsGroupsMembers as $record)
7891 nelberth 180
                {
11897 nelberth 181
 
182
                    $userType = $highPerformanceTeamsGroupsMembersMapper->fetchOneByGroupIdAndUserId($highPerformanceTeamsGroups->id,$record->id);
183
 
184
 
185
                    if($userType->status==HighPerformanceTeamsGroupsMembers::STATUS_ACCEPTED){
186
                        $contentAllowDelete2=$allowDelete;
10320 nelberth 187
                        $allowDelete=true;
188
                    }
10321 nelberth 189
 
190
                    if($userType->type==HighPerformanceTeamsGroupsMembers::TYPE_CREATOR){
10323 nelberth 191
                        $contentAllowDelete=$allowDelete;
192
                        $contentAllowEdit=$allowEdit;
10321 nelberth 193
                        $allowDelete=false;
194
                        $allowEdit=false;
195
                    }
7891 nelberth 196
                    $item = [
197
                        'first_name' => $record->first_name,
198
                        'last_name' => $record->last_name,
7910 nelberth 199
                        'image'=>  $this->url()->fromRoute('storage', ['type' => 'user', 'code' => $record->uuid, 'filename' => $record->image]),
11083 nelberth 200
                        'link_delete' => $allowDelete ? $this->url()->fromRoute('high-performance-teams/groups/view/members/delete', ['group_id' => $highPerformanceTeamsGroups->uuid,'user_id'=>$record->uuid]) : '',
201
                        'link_edit' => $allowEdit ? $this->url()->fromRoute('high-performance-teams/groups/view/members/edit', ['group_id' => $highPerformanceTeamsGroups->uuid,'user_id'=>$record->uuid]) : '',
7891 nelberth 202
 
8053 nelberth 203
 
7868 nelberth 204
                    ];
7891 nelberth 205
 
10323 nelberth 206
                    if($userType->type==HighPerformanceTeamsGroupsMembers::TYPE_CREATOR){
207
                        $allowDelete= $contentAllowDelete;
208
                        $allowEdit=$contentAllowEdit;
209
                    }
11897 nelberth 210
                    if($userType->status==HighPerformanceTeamsGroupsMembers::STATUS_ACCEPTED){
10339 nelberth 211
                        $allowDelete=$contentAllowDelete2;
212
                    }
7891 nelberth 213
                    array_push($items, $item);
214
 
7868 nelberth 215
                }
7891 nelberth 216
 
217
 
7811 nelberth 218
 
219
                return new JsonModel([
220
                    'success' => true,
221
                    'data' => [
7818 nelberth 222
                        'items' => $items
7811 nelberth 223
                    ]
224
                ]);
225
 
226
            }
7948 nelberth 227
        }else if($request->isPost()){
7963 nelberth 228
 
7948 nelberth 229
            $headers  = $request->getHeaders();
230
 
231
            $isJson = false;
232
            if($headers->has('Accept')) {
233
                $accept = $headers->get('Accept');
234
 
235
                $prioritized = $accept->getPrioritized();
236
 
237
                foreach($prioritized as $key => $value) {
238
                    $raw = trim($value->getRaw());
239
 
240
                    if(!$isJson) {
241
                        $isJson = strpos($raw, 'json');
242
                    }
243
 
244
                }
245
            }
246
 
247
            if($isJson) {
8053 nelberth 248
 
7973 nelberth 249
                $dataPost = $request->getPost()->toArray();
250
                $search = $dataPost['search'];
16778 efrain 251
                $search = empty($search['value']) ? '' : Functions::sanitizeFilterString($search['value']);
7997 nelberth 252
 
12125 nelberth 253
 
12117 nelberth 254
 
12125 nelberth 255
                $userMapper = UserMapper::getInstance($this->adapter);
256
                $records = $userMapper->fetchAllSuggestForInvitationByHptgId($highPerformanceTeamsGroups->id,$highPerformanceTeamsGroups->company_id,$search);
257
                $items=[];
7948 nelberth 258
                foreach($records as $record)
259
                {
12125 nelberth 260
 
261
                    $item = [
262
                        'first_name' => $record->first_name,
263
                        'last_name' => $record->last_name,
264
                        'image'=>  $this->url()->fromRoute('storage', ['type' => 'user', 'code' => $record->uuid, 'filename' => $record->image]),
265
                        'email'=> $record->email,
266
                        'link_invite'=> $allowInvite ? $this->url()->fromRoute('high-performance-teams/groups/view/members/invite', ['group_id' => $highPerformanceTeamsGroups->uuid,'user_id'=>$record->uuid]) : '',
267
 
268
                    ];
7948 nelberth 269
 
12125 nelberth 270
                    array_push($items, $item);
271
 
7948 nelberth 272
                }
273
 
274
 
275
 
276
                return new JsonModel([
277
                    'success' => true,
278
                    'data' => [
279
                        'items' => $items
280
                    ]
281
                ]);
282
 
283
            }
7811 nelberth 284
        } else {
285
            return new JsonModel([
286
                'success' => false,
287
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
288
            ]);
289
        }
290
    }
291
 
8012 nelberth 292
    public function inviteAction()
7811 nelberth 293
    {
8020 nelberth 294
 
8017 nelberth 295
 
296
 
8020 nelberth 297
        $currentUserPlugin = $this->plugin('currentUserPlugin');
7811 nelberth 298
        $currentUser = $currentUserPlugin->getUser();
299
        $currentCompany = $currentUserPlugin->getCompany();
8028 nelberth 300
 
11097 nelberth 301
        $user_uuid = $this->params()->fromRoute('user_id');
302
        $group_uuid = $this->params()->fromRoute('group_id');
8029 nelberth 303
 
7811 nelberth 304
        $request = $this->getRequest();
305
        if($request->isPost()) {
8031 nelberth 306
 
8047 nelberth 307
 
8023 nelberth 308
                $userMapper = UserMapper::getInstance($this->adapter);
309
                $user = $userMapper->fetchOneByUuid($user_uuid);
310
 
8040 nelberth 311
                if(!$user) {
312
                    return new JsonModel([
313
                        'success' => false,
314
                        'data' => 'ERROR_RECORD_NOT_FOUND'
315
                    ]);
316
                }
317
 
8023 nelberth 318
                $companyUserMapper = CompanyUserMapper::getInstance($this->adapter);
319
                $companyUser = $companyUserMapper->fetchOneByCompanyIdAndUserId($currentCompany->id,$user->id);
8048 nelberth 320
 
321
                $highPerformanceTeamsGroupsMapper = HighPerformanceTeamsGroupsMapper::getInstance($this->adapter);
322
                $highPerformanceTeamsGroups = $highPerformanceTeamsGroupsMapper->fetchOneByUuid($group_uuid);
8049 nelberth 323
 
8050 nelberth 324
                if(!$highPerformanceTeamsGroups) {
8040 nelberth 325
                    return new JsonModel([
326
                        'success' => false,
327
                        'data' => 'ERROR_RECORD_NOT_FOUND'
328
                    ]);
329
                }
8050 nelberth 330
 
10324 nelberth 331
                $highPerformanceTeamsGroupsMembersMapper = HighPerformanceTeamsGroupsMembersMapper::getInstance($this->adapter);
10325 nelberth 332
 
10324 nelberth 333
                $member = $highPerformanceTeamsGroupsMembersMapper->fetchOneByGroupIdAndUserId($highPerformanceTeamsGroups->id,$currentUser->id);
11170 nelberth 334
                if(!$member){
335
                    return new JsonModel([
336
                        'success' => false,
337
                        'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS'
338
                    ]);
339
                }
10324 nelberth 340
                if($member->type==HighPerformanceTeamsGroupsMembers::TYPE_MODERATOR||$member->type==HighPerformanceTeamsGroupsMembers::TYPE_USER){
341
                    return new JsonModel([
342
                        'success' => false,
343
                        'data' => 'ERROR_METHOD_NOT_ALLOWED'
344
                    ]);
345
                }
346
 
8044 nelberth 347
                $highPerformanceTeamsGroupsMember = new HighPerformanceTeamsGroupsMembers();
348
                $highPerformanceTeamsGroupsMember->group_id  = $highPerformanceTeamsGroups->id;
349
                $highPerformanceTeamsGroupsMember->user_id = $user->id;
350
                $highPerformanceTeamsGroupsMember->type = HighPerformanceTeamsGroupsMembers::TYPE_USER;
8053 nelberth 351
 
8038 nelberth 352
                if($companyUser){
8044 nelberth 353
                    $highPerformanceTeamsGroupsMember->status = HighPerformanceTeamsGroupsMembers::STATUS_ADDED_BY_ADMIN;
8061 nelberth 354
                    $data = [
355
                        'success'   => true,
356
                        'data'   => 'LABEL_RECORD_ADDED'
357
                    ];
8023 nelberth 358
                }else{
11778 nelberth 359
                    $connectionMapper = ConnectionMapper::getInstance($this->adapter);
360
                    $connection = $connectionMapper->fetchOneByUserId1AndUserId2($currentUser->id, $user->id);
361
                    if(!$connection) {
362
                        return new JsonModel([
363
                            'success' => false,
364
                            'data' => 'ERROR_THIS_USER_IS_NOT_A_CONNECTION'
365
                        ]);
366
                    }
8044 nelberth 367
                    $highPerformanceTeamsGroupsMember->status = HighPerformanceTeamsGroupsMembers::STATUS_INVITED;
8061 nelberth 368
                    $data = [
369
                        'success'   => true,
370
                        'data'   => 'LABEL_USER_COMPANY_HAS_BEEN_REQUESTED'
371
                    ];
8023 nelberth 372
                }
10324 nelberth 373
                $highPerformanceTeamsGroupsMembersMapper->insert($highPerformanceTeamsGroupsMember);
8045 nelberth 374
 
10324 nelberth 375
               if($highPerformanceTeamsGroupsMembersMapper) {
11687 nelberth 376
                    if($companyUser){
377
 
378
                        $chatGroupMapper = ChatGroupMapper::getInstance($this->adapter);
379
                        $chatGroup = $chatGroupMapper->fetchOneHptg($highPerformanceTeamsGroups->id);
380
                        if(!$chatGroup) {
381
                            return new JsonModel([
382
                                'success' => false,
383
                                'data' => 'ERROR_CHAT_GROUP_NOT_FOUND'
384
                            ]);
385
                        }
386
 
387
                        $chatGroupUserMapper = ChatGroupUserMapper::getInstance($this->adapter);
11774 nelberth 388
                        $chatGroupOwner = $chatGroupUserMapper->fetchOwnerByGroupId($chatGroup->id);
11687 nelberth 389
 
390
                        $userMapper = UserMapper::getInstance($this->adapter);
391
                        $user = $userMapper->fetchOneByUuid($user_uuid);
392
 
393
                        if(!$user) {
394
                            return new JsonModel([
395
                                'success' => false,
396
                                'data' => 'ERROR_USER_NOT_FOUND'
397
                            ]);
398
                        }
399
 
400
                        if($chatGroupOwner->user_id == $user->id) {
401
                            return new JsonModel([
402
                                'success' => false,
403
                                'data' => 'ERROR_CHAT_I_CAN_NOT_ADD_HIMSELF'
404
                            ]);
405
                        }
406
 
407
 
408
                        $chatGroupUser = $chatGroupUserMapper->fetchOneByGroupIdAndUserId($chatGroup->id, $user->id);
409
                        if($chatGroupUser) {
410
                            return new JsonModel([
411
                                'success' => false,
412
                                'data' => 'ERROR_THIS_USER_ALREADY_EXISTS_IN_THIS_GROUP'
413
                            ]);
414
                        }
415
 
416
 
417
                        $chatGroupUser = new ChatGroupUser();
418
                        $chatGroupUser->group_id    = $chatGroup->id;
419
                        $chatGroupUser->user_id     = $user->id;
420
                        $chatGroupUser->owner       = ChatGroupUser::OWNER_NO;
421
 
422
                        $result = $chatGroupUserMapper->insert($chatGroupUser);
423
                        if(!$result) {
424
                            return new JsonModel([
425
                                'success' => false,
426
                                'data' => $chatGroupUserMapper->getError()
427
                            ]);
428
                        }
11688 nelberth 429
 
11687 nelberth 430
 
431
                    }
432
 
8044 nelberth 433
                    $this->logger->info('Se agrego el miembro de grupo  con el correo: ' . $user->email, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
7811 nelberth 434
 
11687 nelberth 435
                    $data = [
436
                        'success'   => true,
437
                        'data'   => 'LABEL_RECORD_ADDED'
438
                    ];
7811 nelberth 439
                } else {
440
                    $data = [
441
                        'success'   => false,
7818 nelberth 442
                        'data'      => $highPerformanceTeamsGroupsMembersMapper->getError()
7811 nelberth 443
                    ];
444
 
445
                }
446
 
447
                return new JsonModel($data);
8044 nelberth 448
 
8030 nelberth 449
 
7811 nelberth 450
 
451
        } else {
452
            $data = [
453
                'success' => false,
454
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
455
            ];
456
 
457
            return new JsonModel($data);
458
        }
459
 
460
        return new JsonModel($data);
461
 
462
 
463
    }
464
 
465
 
466
 
467
     public function editAction(){
468
 
469
        $currentUserPlugin = $this->plugin('currentUserPlugin');
470
        $currentUser = $currentUserPlugin->getUser();
471
        $currentCompany = $currentUserPlugin->getCompany();
472
        $request = $this->getRequest();
10283 nelberth 473
 
11097 nelberth 474
        $group_uuid = $this->params()->fromRoute('group_id');
475
        $user_uuid = $this->params()->fromRoute('user_id');
10293 nelberth 476
 
7811 nelberth 477
 
10283 nelberth 478
        if(!$group_uuid) {
7811 nelberth 479
            $data = [
480
                'success'   => false,
481
                'data'   => 'ERROR_INVALID_PARAMETER'
482
            ];
483
 
484
            return new JsonModel($data);
485
        }
486
 
10283 nelberth 487
        if(!$user_uuid) {
488
            $data = [
489
                'success'   => false,
490
                'data'   => 'ERROR_INVALID_PARAMETER'
491
            ];
492
 
493
            return new JsonModel($data);
494
        }
10296 nelberth 495
 
10299 nelberth 496
        $highPerformanceTeamsGroupsMapper = HighPerformanceTeamsGroupsMapper::getInstance($this->adapter);
10297 nelberth 497
 
10328 nelberth 498
        $highPerformanceTeamsGroups = $highPerformanceTeamsGroupsMapper->fetchOneByUuid($group_uuid);
10301 nelberth 499
 
10328 nelberth 500
        if (!$highPerformanceTeamsGroups) {
7811 nelberth 501
            $data = [
502
                'success' => false,
503
                'data' => 'ERROR_RECORD_NOT_FOUND'
504
            ];
505
 
506
            return new JsonModel($data);
507
        }
10328 nelberth 508
 
509
        $highPerformanceTeamsGroupsMembersMapper = HighPerformanceTeamsGroupsMembersMapper::getInstance($this->adapter);
510
        $member = $highPerformanceTeamsGroupsMembersMapper->fetchOneByGroupIdAndUserId($highPerformanceTeamsGroups->id,$currentUser->id);
11170 nelberth 511
        if(!$member){
512
            return new JsonModel([
513
                'success' => false,
514
                'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS'
515
            ]);
516
        }
10328 nelberth 517
        if($member->type==HighPerformanceTeamsGroupsMembers::TYPE_MODERATOR||$member->type==HighPerformanceTeamsGroupsMembers::TYPE_USER){
518
            return new JsonModel([
519
                'success' => false,
520
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
521
            ]);
522
        }
7811 nelberth 523
 
10283 nelberth 524
        $userMapper = UserMapper::getInstance($this->adapter);
525
        $user = $userMapper->fetchOneByUuid($user_uuid);
10302 nelberth 526
 
10328 nelberth 527
 
528
        $highPerformanceTeamsGroupsMembers=$highPerformanceTeamsGroupsMembersMapper->fetchOneByGroupIdAndUserId($highPerformanceTeamsGroups->id,$user->id);
529
        if($highPerformanceTeamsGroupsMembers->type==HighPerformanceTeamsGroupsMembers::TYPE_CREATOR){
530
            return new JsonModel([
531
                'success' => false,
532
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
533
            ]);
534
        }
10298 nelberth 535
 
7811 nelberth 536
        if($request->isPost()) {
10330 nelberth 537
 
538
            $form = new  HighPerformanceTeamsGroupsMembersTypeForm($this->adapter, $currentCompany->id);
539
            $dataPost = $request->getPost()->toArray();
540
            $form->setData($dataPost);
10331 nelberth 541
 
7811 nelberth 542
            if($form->isValid()) {
543
                $dataPost = (array) $form->getData();
10332 nelberth 544
 
545
                $hydrator = new ObjectPropertyHydrator();
546
                $hydrator->hydrate($dataPost, $highPerformanceTeamsGroupsMembers);
10333 nelberth 547
 
548
                $result = $highPerformanceTeamsGroupsMembersMapper->update($highPerformanceTeamsGroupsMembers);
10334 nelberth 549
 
7811 nelberth 550
                if($result) {
11755 nelberth 551
 
10337 nelberth 552
                    $this->logger->info('Se actualizo el miembro ' .$user->first_name.' en el grupo '. $highPerformanceTeamsGroups->title, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
553
 
7811 nelberth 554
                    $data = [
555
                        'success' => true,
556
                        'data' => 'LABEL_RECORD_UPDATED'
557
                    ];
558
                } else {
559
                    $data = [
560
                        'success'   => false,
7818 nelberth 561
                        'data'      => $highPerformanceTeamsGroupsMembersMapper->getError()
7811 nelberth 562
                    ];
563
                }
564
 
565
                return new JsonModel($data);
566
 
567
            } else {
568
                $messages = [];
569
                $form_messages = (array) $form->getMessages();
570
                foreach($form_messages  as $fieldname => $field_messages)
571
                {
572
                    $messages[$fieldname] = array_values($field_messages);
573
                }
574
 
575
                return new JsonModel([
576
                    'success'   => false,
577
                    'data'   => $messages
578
                ]);
579
            }
580
        }else if ($request->isGet()) {
581
            $hydrator = new ObjectPropertyHydrator();
582
 
583
            $data = [
584
                'success' => true,
10298 nelberth 585
                'data' => $highPerformanceTeamsGroupsMembers->type
7811 nelberth 586
            ];
587
 
588
            return new JsonModel($data);
589
        } else {
590
            $data = [
591
                'success' => false,
592
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
593
            ];
594
 
595
            return new JsonModel($data);
596
        }
597
 
598
        return new JsonModel($data);
599
 
600
    }
601
 
602
 
603
 
604
 
605
    public function deleteAction(){
8053 nelberth 606
 
607
 
608
 
7811 nelberth 609
        $currentUserPlugin = $this->plugin('currentUserPlugin');
610
        $currentCompany = $currentUserPlugin->getCompany();
611
        $currentUser = $currentUserPlugin->getUser();
612
 
613
        $request = $this->getRequest();
11083 nelberth 614
        $group_uuid = $this->params()->fromRoute('group_id');
615
        $user_uuid = $this->params()->fromRoute('user_id');
8057 nelberth 616
        if (!$group_uuid) {
617
            $data = [
618
                'success' => false,
619
                'data' => 'ERROR_INVALID_PARAMETER'
620
            ];
621
 
622
            return new JsonModel($data);
623
        }
624
 
625
        if (!$user_uuid) {
626
            $data = [
627
                'success' => false,
628
                'data' => 'ERROR_INVALID_PARAMETER'
629
            ];
630
 
631
            return new JsonModel($data);
632
        }
10328 nelberth 633
 
8057 nelberth 634
 
635
        if ($request->isPost()) {
8058 nelberth 636
 
8055 nelberth 637
            $userMapper = UserMapper::getInstance($this->adapter);
638
            $user = $userMapper->fetchOneByUuid($user_uuid);
7811 nelberth 639
 
8055 nelberth 640
            if (!$user) {
641
                $data = [
642
                    'success' => false,
643
                    'data' => 'ERROR_RECORD_NOT_FOUND'
644
                ];
7811 nelberth 645
 
8055 nelberth 646
                return new JsonModel($data);
647
            }
8059 nelberth 648
 
8055 nelberth 649
            $highPerformanceTeamsGroupsMapper = HighPerformanceTeamsGroupsMapper::getInstance($this->adapter);
650
            $highPerformanceTeamsGroups = $highPerformanceTeamsGroupsMapper->fetchOneByUuid($group_uuid);
651
 
8059 nelberth 652
            if (!$highPerformanceTeamsGroups) {
8055 nelberth 653
                $data = [
654
                    'success' => false,
655
                    'data' => 'ERROR_RECORD_NOT_FOUND'
656
                ];
657
 
658
                return new JsonModel($data);
659
            }
10328 nelberth 660
 
661
            $highPerformanceTeamsGroupsMembersMapper = HighPerformanceTeamsGroupsMembersMapper::getInstance($this->adapter);
662
            $highPerformanceTeamsGroupsMembers=$highPerformanceTeamsGroupsMembersMapper->fetchOneByGroupIdAndUserId($highPerformanceTeamsGroups->id,$user->id);
10341 nelberth 663
            if($highPerformanceTeamsGroupsMembers->user_id!=$currentUser->id){
664
 
10338 nelberth 665
                $member = $highPerformanceTeamsGroupsMembersMapper->fetchOneByGroupIdAndUserId($highPerformanceTeamsGroups->id,$currentUser->id);
11170 nelberth 666
                if(!$member){
667
                    return new JsonModel([
668
                        'success' => false,
669
                        'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS'
670
                    ]);
671
                }
10338 nelberth 672
                if($member->type==HighPerformanceTeamsGroupsMembers::TYPE_MODERATOR||$member->type==HighPerformanceTeamsGroupsMembers::TYPE_USER){
673
                    return new JsonModel([
674
                        'success' => false,
11890 nelberth 675
                        'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS'
10338 nelberth 676
                    ]);
677
                }
678
 
679
 
680
                if($highPerformanceTeamsGroupsMembers->type==HighPerformanceTeamsGroupsMembers::TYPE_CREATOR){
681
                    return new JsonModel([
682
                        'success' => false,
11891 nelberth 683
                        'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS'
10338 nelberth 684
                    ]);
685
                }
11887 nelberth 686
            }else{
11888 nelberth 687
                if($highPerformanceTeamsGroupsMembers->status != HighPerformanceTeamsGroupsMembers::STATUS_ACCEPTED){
11887 nelberth 688
                    return new JsonModel([
689
                        'success' => false,
11890 nelberth 690
                        'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS'
11887 nelberth 691
                    ]);
692
                }
10328 nelberth 693
            }
11697 nelberth 694
 
11887 nelberth 695
 
11691 nelberth 696
            $chatGroupMapper = ChatGroupMapper::getInstance($this->adapter);
11694 nelberth 697
            $chatGroup = $chatGroupMapper->fetchOneHptg($highPerformanceTeamsGroups->id);
11691 nelberth 698
            if(!$chatGroup) {
699
                return new JsonModel([
700
                    'success' => false,
11704 nelberth 701
                    'data' => 'ERROR_CHAT_GROUP_NOT_FOUND'
11691 nelberth 702
                ]);
703
            }
10344 nelberth 704
 
11691 nelberth 705
            $chatGroupUserMapper = ChatGroupUserMapper::getInstance($this->adapter);
706
            $chatGroupOwner = $chatGroupUserMapper->fetchOwnerByGroupId($chatGroup->id);
707
 
11718 nelberth 708
 
11691 nelberth 709
            $userMapper = UserMapper::getInstance($this->adapter);
710
            $user = $userMapper->fetchOneByUuid($user_uuid);
711
 
712
            if(!$user) {
713
                return new JsonModel([
714
                    'success' => false,
11704 nelberth 715
                    'data' => 'ERROR_USER_NOT_FOUND'
11691 nelberth 716
                ]);
717
            }
718
 
719
            if($chatGroupOwner->user_id == $user->id) {
720
                return new JsonModel([
721
                    'success' => false,
11704 nelberth 722
                    'data' => 'ERROR_CHAT_I_CAN_NOT_REMOVE_MYSELF'
11691 nelberth 723
                ]);
724
            }
725
 
726
 
8056 nelberth 727
 
11690 nelberth 728
 
11691 nelberth 729
            $chatGroupUser = $chatGroupUserMapper->fetchOneByGroupIdAndUserId($chatGroup->id, $user->id);
730
            if(!$chatGroupUser) {
731
                return new JsonModel([
732
                    'success' => false,
11704 nelberth 733
                    'data' => 'ERROR_CHAT_GROUP_YOU_NOT_MEMBER'
11691 nelberth 734
                ]);
735
            }
736
 
11690 nelberth 737
 
11691 nelberth 738
            $response = $chatGroupUserMapper->deleteByGroupIdAndUserId($chatGroup->id, $user->id);
11695 nelberth 739
            if(!$response) {
11691 nelberth 740
                return new JsonModel([
741
                    'success' => false,
11701 nelberth 742
                    'data' => $chatGroupMapper->getError()
11691 nelberth 743
                ]);
11695 nelberth 744
            }
11691 nelberth 745
 
11703 nelberth 746
            $result = $highPerformanceTeamsGroupsMembersMapper->delete($highPerformanceTeamsGroupsMembers->id);
11691 nelberth 747
            if ($result) {
11690 nelberth 748
 
10345 nelberth 749
                $this->logger->info('Se borro el grupo el miembro con el email de  ' . $user->email, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
10353 nelberth 750
                if($highPerformanceTeamsGroupsMembers->user_id==$currentUser->id){
10352 nelberth 751
                    return new JsonModel([
752
                        'success' => true,
753
                        'data' => 'LABEL_RECORD_DELETED',
754
                        'reload'=>true
755
                    ]);
756
                }
757
 
10350 nelberth 758
 
7811 nelberth 759
                $data = [
760
                    'success' => true,
10352 nelberth 761
                    'data' => 'LABEL_RECORD_DELETED',
762
                    'reload'=>false
7811 nelberth 763
                ];
764
            } else {
765
 
766
                $data = [
767
                    'success' => false,
7818 nelberth 768
                    'data' => $highPerformanceTeamsGroupsMembersMapper->getError()
7811 nelberth 769
                ];
770
 
771
                return new JsonModel($data);
772
            }
773
        } else {
774
            $data = [
775
                'success' => false,
776
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
777
            ];
778
 
779
            return new JsonModel($data);
780
        }
781
 
782
        return new JsonModel($data);
783
    }
784
 
785
 
786
 
787
 
788
}