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