Proyectos de Subversion LeadersLinked - Backend

Rev

| Ultima modificación | Ver Log |

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