Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 16821 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 www 1
<?php
2
declare(strict_types=1);
3
 
4
namespace LeadersLinked\Controller;
5
 
6
 
7
use Laminas\Db\Adapter\AdapterInterface;
16768 efrain 8
 
1 www 9
use Laminas\Mvc\Controller\AbstractActionController;
10
use Laminas\Log\LoggerInterface;
11
use Laminas\View\Model\ViewModel;
12
use Laminas\View\Model\JsonModel;
13
use LeadersLinked\Library\Functions;
14
use LeadersLinked\Mapper\CompanyMapper;
15
use LeadersLinked\Model\Company;
16
use LeadersLinked\Mapper\ServiceMapper;
17
use LeadersLinked\Mapper\CompanyServiceMapper;
18
use LeadersLinked\Mapper\RoleMapper;
19
use LeadersLinked\Mapper\CompanyRoleMapper;
20
use LeadersLinked\Mapper\CompanyUserMapper;
21
use LeadersLinked\Mapper\UserMapper;
22
use LeadersLinked\Model\CompanyService;
23
use LeadersLinked\Mapper\CompanyUserRoleMapper;
24
use LeadersLinked\Model\Role;
25
use LeadersLinked\Model\CompanyRole;
26
use LeadersLinked\Model\CompanyUserRole;
27
use LeadersLinked\Model\CompanyUser;
28
use Google\Service\Directory\UserMakeAdmin;
15338 efrain 29
use LeadersLinked\Mapper\NetworkMapper;
15540 efrain 30
use LeadersLinked\Model\Service;
1 www 31
 
32
class CompanyController extends AbstractActionController
33
{
34
    /**
35
     *
16769 efrain 36
     * @var \Laminas\Db\Adapter\AdapterInterface
1 www 37
     */
38
    private $adapter;
39
 
40
    /**
41
     *
16769 efrain 42
     * @var \LeadersLinked\Cache\CacheInterface
1 www 43
     */
16769 efrain 44
    private $cache;
45
 
46
 
47
    /**
48
     *
49
     * @var \Laminas\Log\LoggerInterface
50
     */
1 www 51
    private $logger;
16768 efrain 52
 
1 www 53
    /**
54
     *
55
     * @var array
56
     */
57
    private $config;
58
 
16769 efrain 59
 
1 www 60
    /**
61
     *
16769 efrain 62
     * @var \Laminas\Mvc\I18n\Translator
63
     */
64
    private $translator;
65
 
66
 
67
    /**
68
     *
69
     * @param \Laminas\Db\Adapter\AdapterInterface $adapter
70
     * @param \LeadersLinked\Cache\CacheInterface $cache
71
     * @param \Laminas\Log\LoggerInterface LoggerInterface $logger
1 www 72
     * @param array $config
16769 efrain 73
     * @param \Laminas\Mvc\I18n\Translator $translator
1 www 74
     */
16769 efrain 75
    public function __construct($adapter, $cache, $logger, $config, $translator)
1 www 76
    {
16769 efrain 77
        $this->adapter      = $adapter;
78
        $this->cache        = $cache;
79
        $this->logger       = $logger;
80
        $this->config       = $config;
81
        $this->translator   = $translator;
1 www 82
    }
83
 
84
    public function indexAction()
85
    {
86
        $request = $this->getRequest();
87
        $currentUserPlugin = $this->plugin('currentUserPlugin');
88
        $currentUser    = $currentUserPlugin->getUser();
89
 
15351 efrain 90
 
91
        $currentNetworkPlugin = $this->plugin('currentNetworkPlugin');
92
        $network = $currentNetworkPlugin->getNetwork();
93
 
1 www 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
 
16299 efrain 115
            //$isJson = true;
1 www 116
            if($isJson) {
117
 
118
                $acl = $this->getEvent()->getViewModel()->getVariable('acl');
119
                $allowServices = $acl->isAllowed($currentUser->usertype_id, 'companies/services');
120
                $allowRoles = $acl->isAllowed($currentUser->usertype_id, 'companies/roles');
121
                $allowEdit = $acl->isAllowed($currentUser->usertype_id, 'companies/edit');
122
 
123
 
124
                $serviceMapper = ServiceMapper::getInstance($this->adapter);
125
                $records = $serviceMapper->fetchAll();
126
 
127
                $services = [];
128
                foreach($records as $record)
129
                {
130
                    $services[ $record->id ] = $record->name;
131
                }
132
 
133
 
134
                $roleMapper = RoleMapper::getInstance($this->adapter);
135
                $records = $roleMapper->fetchAll();
136
 
137
                $roles = [];
138
                foreach($records as $record)
139
                {
140
                    $roles[ $record->id ] = $record->name;
141
                }
15338 efrain 142
 
143
                $networkMapper = NetworkMapper::getInstance($this->adapter);
144
                $records = $networkMapper->fetchAll();
145
 
146
                $networks = [];
147
                foreach($records as $record)
148
                {
16299 efrain 149
                    $networks[ $record->id ] = $record;
15338 efrain 150
                }
16299 efrain 151
 
15338 efrain 152
 
1 www 153
 
154
 
155
                $search = $this->params()->fromQuery('search', []);
16766 efrain 156
                $search = empty($search['value']) ? '' :  Functions::sanitizeFilterString($search['value']);
1 www 157
 
158
                $page               = intval($this->params()->fromQuery('start', 1), 10);
159
                $records_x_page     = intval($this->params()->fromQuery('length', 10), 10);
160
                $order =  $this->params()->fromQuery('order', []);
161
                $order_field        = empty($order[0]['column']) ? 99 :  intval($order[0]['column'], 10);
16766 efrain 162
                $order_direction    = empty($order[0]['dir']) ? 'ASC' : strtoupper(Functions::sanitizeFilterString($order[0]['dir']));
1 www 163
 
164
                $fields =  ['name'];
165
                $order_field = isset($fields[$order_field]) ? $fields[$order_field] : 'name';
166
 
167
                if(!in_array($order_direction, ['ASC', 'DESC'])) {
168
                    $order_direction = 'ASC';
169
                }
170
 
16766 efrain 171
                $status = Functions::sanitizeFilterString($this->params()->fromQuery('status'));
1 www 172
                if(!in_array($status, [
173
                    Company::STATUS_ACTIVE,
174
                    Company::STATUS_DELETED,
175
                    Company::STATUS_INACTIVE,
176
                    Company::STATUS_PENDING,
177
                    Company::STATUS_REJECTED
178
                ])) {
179
                    $status = '';
180
                }
181
 
16766 efrain 182
                $network_id = Functions::sanitizeFilterString($this->params()->fromQuery('network_id'));
15338 efrain 183
 
184
                $network = $networkMapper->fetchOneByUuid($network_id);
185
                if($network) {
186
                    $network_id = $network->id;
187
                } else {
188
                    $network_id = 0;
189
                }
190
 
1 www 191
 
192
                $companyMapper = CompanyMapper::getInstance($this->adapter);
16767 efrain 193
                $paginator = $companyMapper->fetchAllDataTable($search, $status, $network_id, $page,  $records_x_page, $order_field, $order_direction);
1 www 194
 
195
                $companyServiceMapper = CompanyServiceMapper::getInstance($this->adapter);
196
                $companyRoleMapper = CompanyRoleMapper::getInstance($this->adapter);
197
                $companyUserMapper = CompanyUserMapper::getInstance($this->adapter);
198
                $userMapper = UserMapper::getInstance($this->adapter);
199
 
15351 efrain 200
 
1 www 201
 
202
                $items = [];
203
                $records = $paginator->getCurrentItems();
204
                foreach($records as $record)
205
                {
206
                    $security = [
207
                        'first_name' => '',
208
                        'last_name' => '',
209
                        'email' => '',
210
                    ];
211
 
212
                    $companyUser = $companyUserMapper->fetchCreatorByCompanyId($record->id);
213
                    if($companyUser) {
214
                        $user = $userMapper->fetchOne($companyUser->user_id);
215
                        if($user) {
216
                            $security['first_name'] = $user->first_name;
217
                            $security['last_name'] = $user->last_name;
218
                            $security['email'] = $user->email;
219
                        }
220
                    }
221
 
222
                    $companyServices = $companyServiceMapper->fetchAllByCompanyId($record->id);
223
                    $companyRoles = $companyRoleMapper->fetchAllByCompanyId($record->id);
224
 
225
 
226
                    $details['services'] = [] ;
227
 
228
                    foreach($companyServices as $companyService)
229
                    {
230
                        if($companyService->status == CompanyService::ACTIVE) {
231
 
232
 
233
                            array_push($details['services'], $services[$companyService->service_id]);
234
                        }
235
                    }
236
 
237
                    $details['roles'] = [] ;
238
 
239
                    foreach($companyRoles as $companyRole)
240
                    {
241
                        array_push($details['roles'], $roles[$companyRole->role_id]);
242
                    }
243
 
244
 
15358 efrain 245
 
1 www 246
 
15358 efrain 247
 
1 www 248
                    $details['internal'] = $record->internal == Company::INTERNAL_YES ? 'LABEL_YES' : 'LABEL_NO';
249
 
250
 
251
                    $link_profile = '';
252
                    switch($record->status)
253
                    {
254
                        case Company::STATUS_PENDING :
255
                            $details['status'] = 'LABEL_PENDING';
256
 
257
                            break;
258
 
259
                        case Company::STATUS_ACTIVE :
15358 efrain 260
                            $link_profile = 'https://'. $networks[$record->network_id]->main_hostname . '/company/view/' . $record->uuid;
1 www 261
                            $details['status'] = 'LABEL_ACTIVE';
262
                            break;
263
 
264
                        case Company::STATUS_INACTIVE :
265
                            $details['status'] = 'LABEL_INACTIVE';
266
                            break;
267
 
268
                        case Company::STATUS_REJECTED :
269
                            $details['status'] = 'LABEL_REJECTED';
270
                            break;
271
 
272
                        case Company::STATUS_DELETED :
273
                            $details['status'] = 'LABEL_DELETED';
274
                            break;
275
 
276
                        default :
277
                            $details['status'] = 'LABEL_UNKNOWN';
278
                            break;
279
                    }
280
 
281
 
282
                    $item = [
283
                        'id' => $record->id,
284
                        'name' => $record->name,
15358 efrain 285
                        'network' => $networks[$record->network_id]->name,
1 www 286
                        'link_profile' => $link_profile,
287
                        'details' => $details,
288
                        'security' => $security,
289
                        'actions' => [
290
                           'link_services' => $allowServices ? $this->url()->fromRoute('companies/services', ['id' => $record->uuid ]) : '',
291
                           'link_roles' => $allowRoles ? $this->url()->fromRoute('companies/roles', ['id' => $record->uuid ]) : '',
292
                           'link_edit' => $allowEdit ? $this->url()->fromRoute('companies/edit', ['id' => $record->uuid ]) : '',
293
                        ]
294
 
295
                    ];
296
 
297
                    array_push($items, $item);
298
                }
299
 
300
                return new JsonModel([
301
                    'success' => true,
302
                    'data' => [
303
                        'items' => $items,
304
                        'total' => $paginator->getTotalItemCount(),
305
                    ]
306
                ]);
307
            } else  {
308
 
15338 efrain 309
                $networkMapper = NetworkMapper::getInstance($this->adapter);
310
                $records = $networkMapper->fetchAll();
311
 
312
                $networks = [];
313
                foreach($records as $record)
314
                {
315
                    $networks[ $record->uuid ] = $record->name;
316
                }
317
 
318
 
319
 
1 www 320
                $this->layout()->setTemplate('layout/layout-backend');
321
                $viewModel = new ViewModel();
322
                $viewModel->setTemplate('leaders-linked/companies/index.phtml');
15338 efrain 323
                $viewModel->setVariables(['networks' => $networks ]);
1 www 324
                return $viewModel ;
325
            }
326
 
327
        } else {
328
            return new JsonModel([
329
                'success' => false,
330
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
331
            ]);
332
        }
333
    }
334
 
335
    public function servicesAction()
336
    {
337
        $currentUserPlugin = $this->plugin('currentUserPlugin');
338
        $currentUser = $currentUserPlugin->getUser();
339
 
340
        $request = $this->getRequest();
341
        $uuid = $this->params()->fromRoute('id');
342
 
343
 
344
        if(!$uuid) {
345
            $data = [
346
                'success'   => false,
347
                'data'   => 'ERROR_INVALID_PARAMETER'
348
            ];
349
 
350
            return new JsonModel($data);
351
        }
352
 
353
        $companyMapper = CompanyMapper::getInstance($this->adapter);
354
        $company = $companyMapper->fetchOneByUuid($uuid);
355
        if(!$company) {
356
            $data = [
357
                'success'   => false,
358
                'data'   => 'ERROR_COMPANY_NOT_FOUND'
359
            ];
360
 
361
            return new JsonModel($data);
362
        }
363
 
364
        if($request->isPost()) {
365
 
366
            $companyServiceMapper = CompanyServiceMapper::getInstance($this->adapter);
367
 
368
            $serviceMapper = ServiceMapper::getInstance($this->adapter);
369
            $services = $serviceMapper->fetchAll();
370
 
371
            foreach($services as $service)
372
            {
16766 efrain 373
                $status     = Functions::sanitizeFilterString($this->params()->fromPost('status' . $service->id));
374
                $paid_from  = Functions::sanitizeFilterString($this->params()->fromPost('paid_from' . $service->id));
375
                $paid_to    = Functions::sanitizeFilterString($this->params()->fromPost('paid_to' . $service->id));
1 www 376
 
16932 efrain 377
 
378
                //echo '$service = ' . $service->id . ' status = ' . $status . ' from = ' . $paid_from . ' to = ' . $paid_to . "r\n";
379
 
1 www 380
                $ok = true;
16932 efrain 381
                if($status == CompanyService::ACTIVE) {
1 www 382
 
383
                        $dt_paid_from = \DateTime::createFromFormat('d/m/Y', $paid_from);
384
                        $dt_paid_to = \DateTime::createFromFormat('d/m/Y', $paid_to);
385
 
386
                        if(!$dt_paid_from || !$dt_paid_to) {
387
                            $ok = false;
388
                        }  else {
389
                            if($dt_paid_from->getTimestamp() > $dt_paid_to->getTimestamp()) {
390
                                $ok = false;
391
                            } else {
392
                                $dt_paid_from->setTime(0, 0, 0);
393
                                $paid_from = $dt_paid_from->format('Y-m-d');
394
 
395
 
396
                                $dt_paid_to->setTime(23, 59, 59);
397
                                $paid_to = $dt_paid_to->format('Y-m-d');
398
                            }
399
                        }
16932 efrain 400
                } else {
401
                    $paid_from = null;
402
                    $paid_to = null;
1 www 403
                }
404
 
405
                if(!$ok) {
406
                    return new JsonModel([
407
                        'success' => false,
408
                        'data' => 'ERROR_PARAMETERS_ARE_INVALID'
409
                    ]);
410
                }
411
 
412
 
413
                $companyService = $companyServiceMapper->fetchOneByCompanyIdAndServiceId($company->id, $service->id);
414
                if($companyService) {
415
 
416
                    $companyService->status = $status;
417
 
16932 efrain 418
                    if($paid_from) {
419
                        $companyService->paid_from = $paid_from;
420
                    }
421
                    if($paid_to) {
422
                        $companyService->paid_to = $paid_to;
423
                    }
424
 
1 www 425
                    if(!$companyServiceMapper->update($companyService)) {
426
                        return new JsonModel([
427
                            'success' => false,
428
                            'data' => $companyServiceMapper->getError()
429
                        ]);
430
                    }
431
 
432
 
433
                } else {
434
                    $companyService = new CompanyService();
435
                    $companyService->service_id = $service->id;
436
                    $companyService->company_id = $company->id;
437
                    $companyService->status = $status;
438
                    $companyService->paid_from = $paid_from;
439
                    $companyService->paid_to = $paid_to;
440
 
441
 
442
                    if(!$companyServiceMapper->insert($companyService)) {
443
                        return new JsonModel([
444
                            'success' => false,
445
                            'data' => $companyServiceMapper->getError()
446
                        ]);
447
                    }
448
                }
449
 
450
 
451
            }
452
 
453
            $this->logger->info('Se actualizo los servicios de la empresa ' . $company->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
454
 
455
            return new JsonModel([
456
                'success' => true,
457
                'data' => 'LABEL_RECORD_UPDATED'
458
            ]);
459
 
460
 
461
        } else if ($request->isGet()) {
15540 efrain 462
 
463
 
464
 
1 www 465
            $serviceMapper = ServiceMapper::getInstance($this->adapter);
466
            $records = $serviceMapper->fetchAll();
467
 
468
            $services = [];
469
            foreach($records as $record)
470
            {
15540 efrain 471
                if($company->default_for_network == Company::DEFAULT_FOR_NETWORK_NO) {
472
                    if($record->default_company_is_required == Service::DEFAULT_COMPANY_IS_REQUIRED_YES) {
473
                        continue;
474
                    }
475
                }
476
 
477
 
1 www 478
                $services[ $record->id ] = [
479
                    'id'        => $record->id,
480
                    'name'      => $record->name,
481
                    'status'    => '',
482
                    'paid_from' => '',
483
                    'paid_to'   => '',
484
                ];
485
            }
486
 
487
 
488
 
489
            $companyServiceMapper = CompanyServiceMapper::getInstance($this->adapter);
490
            $records = $companyServiceMapper->fetchAllByCompanyId($company->id);
491
 
492
 
493
            foreach($records as $record)
494
            {
495
                $paid_from = '';
496
                if($record->paid_from) {
497
                    $dt = \DateTime::createFromFormat('Y-m-d H:i:s', $record->paid_from);
498
                    if($dt) {
499
                        $paid_from = $dt->format('d/m/Y');
500
                    }
501
 
502
                }
503
 
504
                $paid_to = '';
505
                if($record->paid_to) {
506
                    $dt = \DateTime::createFromFormat('Y-m-d H:i:s', $record->paid_to);
507
                    if($dt) {
508
                        $paid_to = $dt->format('d/m/Y');
509
                    }
510
 
511
                }
512
 
513
 
514
                $services[ $record->service_id ] ['status']  = $record->status;
515
                $services[ $record->service_id ] ['paid_from']  = $paid_from;
516
                $services[ $record->service_id ] ['paid_to']  = $paid_to;
517
 
518
            }
519
 
520
            $data = [
521
                'success' => true,
522
                'data' => $services,
523
            ];
524
 
525
 
526
 
527
            return new JsonModel($data);
528
        } else {
529
            $data = [
530
                'success' => false,
531
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
532
            ];
533
 
534
            return new JsonModel($data);
535
        }
536
 
537
        return new JsonModel($data);
538
    }
539
 
540
 
541
    public function rolesAction()
542
    {
543
        $currentUserPlugin = $this->plugin('currentUserPlugin');
544
        $currentUser = $currentUserPlugin->getUser();
545
 
546
        $request = $this->getRequest();
547
        $uuid = $this->params()->fromRoute('id');
548
 
549
 
550
        if(!$uuid) {
551
            $data = [
552
                'success'   => false,
553
                'data'   => 'ERROR_INVALID_PARAMETER'
554
            ];
555
 
556
            return new JsonModel($data);
557
        }
558
 
559
        $companyMapper = CompanyMapper::getInstance($this->adapter);
560
        $company = $companyMapper->fetchOneByUuid($uuid);
561
        if(!$company) {
562
            $data = [
563
                'success'   => false,
564
                'data'   => 'ERROR_COMPANY_NOT_FOUND'
565
            ];
566
 
567
            return new JsonModel($data);
568
        }
569
 
570
        if($request->isPost()) {
571
 
572
            $companyRoleMapper = CompanyRoleMapper::getInstance($this->adapter);
573
            $companyUserMapper = CompanyUserMapper::getInstance($this->adapter);
574
            $companyUserRoleMapper = CompanyUserRoleMapper::getInstance($this->adapter);
575
 
576
            $roleMapper = RoleMapper::getInstance($this->adapter);
577
            $roles = $roleMapper->fetchAll();
578
 
579
 
580
            foreach($roles as $role)
581
            {
582
                $companyRole = $companyRoleMapper->fetchOneByCompanyIdAndRoleId($company->id, $role->id);
583
                $checked     = filter_var( $this->params()->fromPost('checked' . $role->id), FILTER_SANITIZE_NUMBER_INT);
584
 
585
                if($checked) {
586
 
587
                    if(!$companyRole) {
588
                        $companyRole = new CompanyRole();
589
                        $companyRole->company_id = $company->id;
590
                        $companyRole->role_id = $role->id;
591
 
592
                        if(!$companyRoleMapper->insert($companyRole)) {
593
                            return new JsonModel([
594
                                'success' => false,
595
                                'data' => $companyRoleMapper->getError()
596
                            ]);
597
                        }
598
 
599
                    }
600
 
601
                } else {
602
                    if($companyRole) {
603
                        $companyUserRoleMapper->deleteByCompanyIdAndRoleId($company->id, $role->id);
604
                        $companyRoleMapper->deleteByCompanyIdAndRoleId($company->id, $role->id);
605
                    }
606
 
607
                }
608
            }
609
 
610
            $this->logger->info('Se actualizo los roles de la empresa ' . $company->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
611
 
612
            return new JsonModel([
613
                'success' => true,
614
                'data' => 'LABEL_RECORD_UPDATED'
615
            ]);
616
 
617
 
618
        } else if ($request->isGet()) {
619
            $companyServiceMapper = CompanyServiceMapper::getInstance($this->adapter);
620
 
621
            $roleMapper = RoleMapper::getInstance($this->adapter);
622
            $records = $roleMapper->fetchAll();
623
 
624
 
625
 
626
            $roles = [];
627
            foreach($records as $record)
628
            {
629
                if($record->service_id) {
630
                    $companyService = $companyServiceMapper->fetchOneByCompanyIdAndServiceId($company->id, $record->service_id);
631
 
632
                    if(!$companyService || $companyService->status == CompanyService::INACTIVE) {
633
                        continue;
634
                    }
635
 
636
                }
637
 
638
 
639
 
640
                $roles[ $record->id ] = [
641
                   'id' => $record->id,
642
                   'name' => $record->name,
643
                   'fixed' => $record->creator == Role::CREATOR_YES ? true : false,
644
                   'checked' => $record->creator == Role::CREATOR_YES ? true : false,
645
                ];
646
            }
647
 
648
 
649
            $companyRoleMapper = CompanyRoleMapper::getInstance($this->adapter);
650
            $records = $companyRoleMapper->fetchAllByCompanyId($company->id);
651
 
652
            foreach($records as $record)
653
            {
654
                $roles[ $record->role_id ]['checked'] = true;
655
 
656
            }
657
 
658
            $data = [
659
                'success' => true,
660
                'data' => $roles,
661
            ];
662
 
663
 
664
 
665
            return new JsonModel($data);
666
        } else {
667
            $data = [
668
                'success' => false,
669
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
670
            ];
671
 
672
            return new JsonModel($data);
673
        }
674
 
675
        return new JsonModel($data);
676
    }
677
 
678
    public function editAction()
679
    {
680
        $currentUserPlugin = $this->plugin('currentUserPlugin');
681
        $currentUser = $currentUserPlugin->getUser();
682
 
683
        $request = $this->getRequest();
684
        $uuid = $this->params()->fromRoute('id');
685
 
686
 
687
        if(!$uuid) {
688
            $data = [
689
                'success'   => false,
690
                'data'   => 'ERROR_INVALID_PARAMETER'
691
            ];
692
 
693
            return new JsonModel($data);
694
        }
695
 
696
        $companyMapper = CompanyMapper::getInstance($this->adapter);
697
        $company = $companyMapper->fetchOneByUuid($uuid);
698
        if(!$company) {
699
            $data = [
700
                'success'   => false,
701
                'data'   => 'ERROR_COMPANY_NOT_FOUND'
702
            ];
703
 
704
            return new JsonModel($data);
705
        }
706
 
707
        if($request->isPost()) {
708
 
709
 
16766 efrain 710
            $status = Functions::sanitizeFilterString($this->params()->fromPost('status'));
711
            $user_uuid = Functions::sanitizeFilterString($this->params()->fromPost('user_uuid'));
1 www 712
 
713
 
714
            if(!in_array($status, [
715
                Company::STATUS_ACTIVE,
716
                Company::STATUS_INACTIVE,
717
                Company::STATUS_DELETED,
718
                Company::STATUS_PENDING,
719
                Company::STATUS_REJECTED,
720
 
721
            ])) {
722
                return new JsonModel([
723
                    'success'   => false,
724
                    'data'   => 'ERROR_PARAMETERS_ARE_INVALID'
725
                ]);
726
            }
727
 
728
            $userMapper = UserMapper::getInstance($this->adapter);
729
            $user = $userMapper->fetchOneByUuid($user_uuid);
730
 
731
            if(!$user) {
732
                return new JsonModel([
733
                    'success'   => false,
734
                    'data'   => 'ERROR_USER_NOT_FOUND'
735
                ]);
736
            }
737
 
738
            $companyUserMapper = CompanyUserMapper::getInstance($this->adapter);
739
            $companyUser = $companyUserMapper->fetchOneByCompanyIdAndUserId($company->id, $user->id);
740
 
741
            if(!$companyUser) {
742
                return new JsonModel([
743
                    'success'   => false,
744
                    'data'   => 'ERROR_COMPANY_USER_NOT_FOUND'
745
                ]);
746
            }
747
 
16821 efrain 748
 
1 www 749
 
16821 efrain 750
 
1 www 751
            if($companyUser->status != CompanyUser::STATUS_ACCEPTED && $companyUser->status != CompanyUser::STATUS_ADMIN_WILL_ADD) {
752
                return new JsonModel([
753
                    'success'   => false,
754
                    'data'   => 'ERROR_COMPANY_USER_IS_NOT_ACTIVE'
755
                ]);
756
            }
757
 
758
            $creator = $companyUserMapper->fetchCreatorByCompanyId($company->id);
759
            if($creator->user_id != $user->id) {
760
 
761
                $creator->creator = CompanyUser::CREATOR_NO;
762
                $creator->backend = CompanyUser::BACKEND_NO;
763
                $companyUserMapper->update($creator);
764
 
765
                $companyUser->creator = CompanyUser::CREATOR_YES;
766
                $companyUser->backend = CompanyUser::BACKEND_YES;
767
                $companyUserMapper->update($companyUser);
768
 
769
            }
770
 
771
            $company->status = $status;
772
            if($companyMapper->update($company)) {
773
 
774
                $this->logger->info('Se edito la empresa ' . $company->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
775
 
776
                return new JsonModel([
777
                    'success' => true,
778
                    'data' => 'LABEL_RECORD_UPDATED'
779
                ]);
780
            }  else {
781
                return new JsonModel([
782
                    'success' => false,
783
                    'data' => $companyMapper->getError(),
784
                ]);
785
            }
786
 
787
 
788
 
789
 
790
 
791
 
792
        } else if ($request->isGet()) {
793
            $userMapper = UserMapper::getInstance($this->adapter);
794
            $companyUserMapper = CompanyUserMapper::getInstance($this->adapter);
795
            $records = $companyUserMapper->fetchAllByCompanyId($company->id);
796
 
797
            $users = [] ;
798
            foreach($records as $record)
799
            {
800
                if($record->owner == CompanyUser::OWNER_YES) {
801
                    continue;
802
                }
803
 
804
 
805
                if($record->status != CompanyUser::STATUS_ACCEPTED && $record->status != CompanyUser::STATUS_ADMIN_WILL_ADD) {
806
                    continue;
807
                }
808
 
809
                $user = $userMapper->fetchOne($record->user_id);
810
 
811
                array_push($users, [
812
                    'uuid' => $user->uuid,
813
                    'name' => trim($user->first_name . ' ' . $user->last_name) . ' (' . $user->email . ')',
814
 
815
                ] );
816
 
817
 
818
 
819
 
820
 
821
 
822
            }
823
 
824
            usort($users, function($a, $b) {
825
                return $a['name'] <=> $b['name'];
826
            });
827
 
828
 
829
            $creator = $companyUserMapper->fetchCreatorByCompanyId($company->id);
830
            if($creator) {
831
                $userMapper = UserMapper::getInstance($this->adapter);
832
                $user = $userMapper->fetchOne($creator->user_id);
833
                if($user) {
834
                    $creator = $user->uuid;
835
                } else {
836
                    $creator = '';
837
                }
838
            } else {
839
                $creator = '';
840
            }
841
 
842
            $data = [
843
                'success' => true,
844
                'data' => [
845
                    'status' => $company->status,
846
                    'users' => $users,
847
                    'creator' => $creator,
848
                ],
849
            ];
850
 
851
 
852
 
853
            return new JsonModel($data);
854
        } else {
855
            $data = [
856
                'success' => false,
857
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
858
            ];
859
 
860
            return new JsonModel($data);
861
        }
862
 
863
        return new JsonModel($data);
864
    }
865
 
866
}
867