Proyectos de Subversion LeadersLinked - Services

Rev

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

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
/**
3
 *
4
 * Controlador: Mis Perfiles
5
 *
6
 */
7
declare(strict_types=1);
8
 
9
namespace LeadersLinked\Controller;
10
 
11
use Laminas\Db\Adapter\AdapterInterface;
12
 
13
use Laminas\Mvc\Controller\AbstractActionController;
14
use Laminas\Log\LoggerInterface;
15
use Laminas\View\Model\ViewModel;
16
use Laminas\View\Model\JsonModel;
17
use LeadersLinked\Mapper\CompanyLocationMapper;
18
use LeadersLinked\Mapper\CompanyFollowerMapper;
19
use LeadersLinked\Mapper\CompanyUserMapper;
20
use LeadersLinked\Mapper\CompanyMapper;
21
use LeadersLinked\Mapper\IndustryMapper;
22
use LeadersLinked\Mapper\CompanySizeMapper;
23
use LeadersLinked\Model\CompanyFollower;
24
use LeadersLinked\Mapper\UserBlockedMapper;
25
use LeadersLinked\Mapper\UserMapper;
26
use LeadersLinked\Model\Company;
27
use LeadersLinked\Model\CompanyUser;
28
use LeadersLinked\Mapper\QueryMapper;
29
use LeadersLinked\Mapper\NotificationMapper;
30
use LeadersLinked\Model\Network;
31
use LeadersLinked\Library\Functions;
32
use Laminas\Mvc\I18n\Translator;
33
use LeadersLinked\Cache\CacheInterface;
283 www 34
use LeadersLinked\Library\Storage;
1 efrain 35
 
36
class CompanyController extends AbstractActionController
37
{
38
    /**
39
     *
40
     * @var \Laminas\Db\Adapter\AdapterInterface
41
     */
42
    private $adapter;
43
 
44
    /**
45
     *
46
     * @var \LeadersLinked\Cache\CacheInterface
47
     */
48
    private $cache;
49
 
50
 
51
    /**
52
     *
53
     * @var \Laminas\Log\LoggerInterface
54
     */
55
    private $logger;
56
 
57
    /**
58
     *
59
     * @var array
60
     */
61
    private $config;
62
 
63
 
64
    /**
65
     *
66
     * @var \Laminas\Mvc\I18n\Translator
67
     */
68
    private $translator;
69
 
70
 
71
    /**
72
     *
73
     * @param \Laminas\Db\Adapter\AdapterInterface $adapter
74
     * @param \LeadersLinked\Cache\CacheInterface $cache
75
     * @param \Laminas\Log\LoggerInterface LoggerInterface $logger
76
     * @param array $config
77
     * @param \Laminas\Mvc\I18n\Translator $translator
78
     */
79
    public function __construct($adapter, $cache, $logger, $config, $translator)
80
    {
81
        $this->adapter      = $adapter;
82
        $this->cache        = $cache;
83
        $this->logger       = $logger;
84
        $this->config       = $config;
85
        $this->translator   = $translator;
86
    }
87
 
88
 
89
    public function indexAction()
90
    {
91
        return new JsonModel([
92
            'success' => false,
93
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
94
        ]);
95
    }
96
 
97
 
98
 
99
    public function viewAction()
100
    {
776 stevensc 101
        // Validaciones iniciales
102
        $validationResponse = $this->validateViewRequest();
103
        if ($validationResponse) {
104
            return $validationResponse;
105
        }
106
 
107
        $id = $this->params()->fromRoute('id');
108
        $currentUserPlugin = $this->plugin('currentUserPlugin');
109
        $currentUser = $currentUserPlugin->getUser();
110
 
111
        // Obtener y validar empresa
112
        $company = $this->getAndValidateCompany($id, $currentUser->network_id);
113
        if ($company instanceof JsonModel) {
114
            return $company; // Error response
115
        }
116
 
117
        // Construir datos de respuesta
118
        $data = $this->buildCompanyResponseData($company, $currentUser);
119
 
120
        return new JsonModel([
121
            'success' => true,
122
            'data' => $data
123
        ]);
124
    }
125
 
126
    /**
127
     * Valida la petición de visualización
128
     */
129
    private function validateViewRequest()
130
    {
1 efrain 131
        $request = $this->getRequest();
132
 
776 stevensc 133
        if (!$request->isGet()) {
770 stevensc 134
            return new JsonModel([
135
                'success' => false,
136
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
137
            ]);
138
        }
139
 
140
        $id = $this->params()->fromRoute('id');
776 stevensc 141
        if (!$id) {
770 stevensc 142
            return new JsonModel([
143
                'success' => false,
144
                'data' => 'ERROR_INVALID_PARAMETER'
145
            ]);
146
        }
147
 
776 stevensc 148
        return null; // No error
149
    }
150
 
151
    /**
152
     * Obtiene y valida la empresa
153
     */
154
    private function getAndValidateCompany($id, $networkId)
155
    {
770 stevensc 156
        $companyMapper = CompanyMapper::getInstance($this->adapter);
776 stevensc 157
        $company = $companyMapper->fetchOneByUuidAndNetworkId($id, $networkId);
770 stevensc 158
 
776 stevensc 159
        if (!$company) {
770 stevensc 160
            return new JsonModel([
161
                'success' => false,
162
                'data' => 'ERROR_COMPANY_NOT_FOUND'
163
            ]);
164
        }
165
 
776 stevensc 166
        if ($company->status != Company::STATUS_ACTIVE) {
770 stevensc 167
            return new JsonModel([
168
                'success' => false,
169
                'data' => 'ERROR_COMPANY_IS_NOT_ACTIVE'
170
            ]);
171
        }
172
 
776 stevensc 173
        return $company;
174
    }
175
 
176
    /**
177
     * Construye todos los datos de respuesta de la empresa
178
     */
179
    private function buildCompanyResponseData($company, $currentUser)
180
    {
181
        $locations = $this->getCompanyLocations($company->id);
182
        $industryAndSize = $this->getCompanyIndustryAndSize($company);
183
        $followerInfo = $this->getFollowerInfo($company->id, $currentUser->id);
184
        $ownerContactInfo = $this->getOwnerContactInfo($company->id, $currentUser->id);
185
 
186
        $storage = Storage::getInstance($this->config, $this->adapter);
187
 
188
        // Datos base de la empresa
189
        $data = [
190
            'link_follow' => '',
191
            'link_unfollow' => '',
192
            'link_request' => '',
193
            'link_accept' => '',
194
            'link_cancel' => '',
195
            'link_reject' => '',
196
            'link_leave' => '',
197
            'link_timeline' => $this->url()->fromRoute('feed/timeline', ['id' => $company->uuid, 'type' => 'company']),
198
            'total_followers' => $followerInfo['total_followers'],
199
            'company_name' => $company->name,
200
            'company_uuid' => $company->uuid,
201
            'name' => trim($company->name),
202
            'image' => $storage->getCompanyImage($company),
203
            'cover' => $storage->getCompanyCover($company),
204
            'overview' => $company->description,
205
            'website' => $company->website,
206
            'foundation_year' => $company->foundation_year,
207
            'facebook' => $company->facebook,
208
            'instagram' => $company->instagram,
209
            'twitter' => $company->twitter,
210
            'locations' => $locations,
211
            'industry' => $industryAndSize['industry'],
212
            'company_size' => $industryAndSize['company_size'],
213
            'is_follower' => $followerInfo['is_follower'],
214
            'link_inmail' => $ownerContactInfo['link_inmail'],
215
            'show_contact' => $ownerContactInfo['show_contact'],
216
        ];
217
 
218
        // Agregar enlaces de acciones según relación usuario-empresa
219
        $this->addUserCompanyActionLinks($data, $company, $currentUser);
220
        $this->addFollowActionLinks($data, $company, $currentUser);
221
 
222
        return $data;
223
    }
224
 
225
    /**
226
     * Obtiene las ubicaciones de la empresa
227
     */
228
    private function getCompanyLocations($companyId)
229
    {
770 stevensc 230
        $companyLocationMapper = CompanyLocationMapper::getInstance($this->adapter);
776 stevensc 231
        $records = $companyLocationMapper->fetchAllLocationByCompanyId($companyId);
770 stevensc 232
 
233
        $locations = [];
776 stevensc 234
        foreach ($records as $record) {
235
            $locations[] = [
236
                'formatted_address' => $record['formatted_address'],
770 stevensc 237
                'country' => $record['country'],
238
                'is_main' => $record['is_main'],
239
            ];
240
        }
241
 
776 stevensc 242
        return $locations;
243
    }
244
 
245
    /**
246
     * Obtiene información de industria y tamaño de la empresa
247
     */
248
    private function getCompanyIndustryAndSize($company)
249
    {
770 stevensc 250
        $industryMapper = IndustryMapper::getInstance($this->adapter);
251
        $industry = $industryMapper->fetchOne($company->industry_id);
252
 
253
        $companySizeMapper = CompanySizeMapper::getInstance($this->adapter);
254
        $companySize = $companySizeMapper->fetchOne($company->company_size_id);
255
 
776 stevensc 256
        return [
257
            'industry' => $industry->name,
258
            'company_size' => $companySize->name . ' (' . $companySize->minimum_no_of_employee . '-' . $companySize->maximum_no_of_employee . ')'
259
        ];
260
    }
261
 
262
    /**
263
     * Obtiene información de seguimiento de la empresa
264
     */
265
    private function getFollowerInfo($companyId, $userId)
266
    {
770 stevensc 267
        $companyFollowerMapper = CompanyFollowerMapper::getInstance($this->adapter);
776 stevensc 268
        $totalFollowers = $companyFollowerMapper->getCountFollowers($companyId);
269
        $follower = $companyFollowerMapper->fetchOneByCompanyIdAndUserId($companyId, $userId);
770 stevensc 270
 
776 stevensc 271
        return [
272
            'total_followers' => $totalFollowers,
273
            'is_follower' => $follower ? 1 : 0,
274
            'follower_record' => $follower
275
        ];
276
    }
277
 
278
    /**
279
     * Obtiene información de contacto del propietario de la empresa
280
     */
281
    private function getOwnerContactInfo($companyId, $currentUserId)
282
    {
283
        $companyUserMapper = CompanyUserMapper::getInstance($this->adapter);
284
        $companyUserOwner = $companyUserMapper->fetchOwnerByCompanyId($companyId);
770 stevensc 285
 
776 stevensc 286
        $linkInmail = '';
287
        $showContact = 1;
288
 
289
        if ($companyUserOwner) {
770 stevensc 290
            $userBlockedMapper = UserBlockedMapper::getInstance($this->adapter);
776 stevensc 291
            $userBlocked = $userBlockedMapper->fetchOneByUserIdAndBlockedId($currentUserId, $companyUserOwner->user_id);
292
 
293
            if (!$userBlocked) {
770 stevensc 294
                $userMapper = UserMapper::getInstance($this->adapter);
295
                $userOwner = $userMapper->fetchOne($companyUserOwner->user_id);
1 efrain 296
 
776 stevensc 297
                if ($userOwner) {
298
                    $linkInmail = $this->url()->fromRoute('inmail/user', ['id' => $userOwner->uuid]);
299
                }
300
            } else {
301
                $showContact = 0;
770 stevensc 302
            }
303
        }
1 efrain 304
 
776 stevensc 305
        return [
306
            'link_inmail' => $linkInmail,
307
            'show_contact' => $showContact,
308
            'owner_record' => $companyUserOwner
770 stevensc 309
        ];
776 stevensc 310
    }
311
 
312
    /**
313
     * Agrega enlaces de acciones relacionadas con la membresía de empresa
314
     */
315
    private function addUserCompanyActionLinks(&$data, $company, $currentUser)
316
    {
317
        $companyUserMapper = CompanyUserMapper::getInstance($this->adapter);
318
        $companyUser = $companyUserMapper->fetchOneByCompanyIdAndUserId($company->id, $currentUser->id);
770 stevensc 319
 
776 stevensc 320
        if ($companyUser) {
321
            switch ($companyUser->status) {
322
                case CompanyUser::STATUS_ADMIN_WILL_ADD:
323
                    $data['link_accept'] = $this->url()->fromRoute('company/accept', ['id' => $company->uuid]);
324
                    $data['link_reject'] = $this->url()->fromRoute('company/reject', ['id' => $company->uuid]);
325
                    break;
326
 
327
                case CompanyUser::STATUS_SENT:
328
                    $data['link_cancel'] = $this->url()->fromRoute('company/cancel', ['id' => $company->uuid]);
329
                    break;
330
 
331
                case CompanyUser::STATUS_ACCEPTED:
332
                    if ($companyUser->owner == CompanyUser::OWNER_NO && $companyUser->creator == CompanyUser::CREATOR_NO) {
333
                        $data['link_leave'] = $this->url()->fromRoute('company/leave', ['id' => $company->uuid]);
334
                    }
335
                    break;
336
 
337
                case CompanyUser::STATUS_CANCELLED:
338
                    $data['link_request'] = $this->url()->fromRoute('company/request', ['id' => $company->uuid]);
339
                    break;
775 stevensc 340
            }
1 efrain 341
        } else {
776 stevensc 342
            $data['link_request'] = $this->url()->fromRoute('company/request', ['id' => $company->uuid]);
1 efrain 343
        }
776 stevensc 344
    }
345
 
346
    /**
347
     * Agrega enlaces de acciones de seguimiento
348
     */
349
    private function addFollowActionLinks(&$data, $company, $currentUser)
350
    {
351
        $followerInfo = $this->getFollowerInfo($company->id, $currentUser->id);
770 stevensc 352
 
776 stevensc 353
        if ($followerInfo['follower_record']) {
354
            $data['link_unfollow'] = $this->url()->fromRoute('company/unfollow', ['id' => $company->uuid]);
770 stevensc 355
        } else {
356
            $data['link_follow'] = $this->url()->fromRoute('company/follow', ['id' => $company->uuid]);
357
        }
1 efrain 358
    }
359
 
360
    public function followAction()
361
    {
362
        $currentUserPlugin = $this->plugin('currentUserPlugin');
363
        $currentUser = $currentUserPlugin->getUser();
364
 
365
        $flashMessenger = $this->plugin('FlashMessenger');
366
        $request = $this->getRequest();
367
        $id = $this->params()->fromRoute('id');
368
 
369
        if(!$id) {
370
            $data = [
371
                'success'   => false,
372
                'data'   => 'ERROR_INVALID_PARAMETER'
373
            ];
374
 
375
            return new JsonModel($data);
376
        }
377
 
378
 
379
 
380
        $companyMapper = CompanyMapper::getInstance($this->adapter);
381
        $company = $companyMapper->fetchOneByUuidAndNetworkId($id, $currentUser->network_id);
382
        if(!$company) {
383
            $data = [
384
                'success'   => false,
385
                'data'   => 'ERROR_RECORD_NOT_FOUND'
386
            ];
387
 
388
            return new JsonModel($data);
389
        }
390
 
391
        if($company->status != Company::STATUS_ACTIVE) {
392
            $data = [
393
                'success'   => false,
394
                'data'   => 'ERROR_COMPANY_IS_NOT_ACTIVE'
395
            ];
396
 
397
            return new JsonModel($data);
398
        }
399
 
400
        $request = $this->getRequest();
401
 
402
        $currentUserPlugin = $this->plugin('currentUserPlugin');
403
        $currentUser = $currentUserPlugin->getUser();
404
 
405
 
406
        if($request->isPost()) {
407
            $flash = Functions::sanitizeFilterString($this->params()->fromPost('flash', 'false'));
408
            $flash = $flash === 'true'? true: false;
409
 
410
            $companyFollowerMapper = CompanyFollowerMapper::getInstance($this->adapter);
411
            $companyFollower = $companyFollowerMapper->fetchOneByCompanyIdAndUserId($company->id, $currentUser->id);
412
            if($companyFollower) {
413
                $data = [
414
                    'success' => false,
415
                    'data' => 'ERROR_YOU_ALREADY_FOLLOW_THIS_COMPANY'
416
                ];
417
 
418
            } else {
419
                $companyFollower = new CompanyFollower();
420
                $companyFollower->company_id = $company->id;
421
                $companyFollower->follower_id = $currentUser->id;
422
 
423
                $result = $companyFollowerMapper->insert($companyFollower);
424
                if($result) {
425
                    if($flash) {
426
                        $flashMessenger->addSuccessMessage('LABEL_STARTED_FOLLOWING_THIS_COMPANY');
427
 
428
                        $data = [
429
                            'success' => true,
430
                            'data' => [
431
                                'message' =>'LABEL_STARTED_FOLLOWING_THIS_COMPANY',
432
                                'reload' => true
433
                             ]
434
                        ];
435
                    } else {
436
                        $data = [
437
                            'success' => true,
438
                            'data' => 'LABEL_STARTED_FOLLOWING_THIS_COMPANY'
439
                        ];
440
                    }
441
                } else {
442
                    $data = [
443
                        'success' => false,
444
                        'data' => $companyFollowerMapper->getError()
445
                    ];
446
                }
447
            }
448
        } else {
449
            $data = [
450
                'success' => false,
451
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
452
            ];
453
        }
454
 
455
        return new JsonModel($data);
456
    }
457
 
458
    public function unfollowAction()
459
    {
460
        $currentUserPlugin = $this->plugin('currentUserPlugin');
461
        $currentUser = $currentUserPlugin->getUser();
462
 
463
        $flashMessenger = $this->plugin('FlashMessenger');
464
        $request = $this->getRequest();
465
        $id = $this->params()->fromRoute('id');
466
 
467
        if(!$id) {
468
            $data = [
469
                'success'   => false,
470
                'data'   => 'ERROR_INVALID_PARAMETER'
471
            ];
472
 
473
            return new JsonModel($data);
474
        }
475
 
476
 
477
 
478
        $companyMapper = CompanyMapper::getInstance($this->adapter);
479
        $company = $companyMapper->fetchOneByUuidAndNetworkId($id, $currentUser->network_id);
480
        if(!$company) {
481
            $data = [
482
                'success'   => false,
483
                'data'   => 'ERROR_RECORD_NOT_FOUND'
484
            ];
485
 
486
            return new JsonModel($data);
487
        }
488
 
489
        if($company->status != Company::STATUS_ACTIVE) {
490
            $data = [
491
                'success'   => false,
492
                'data'   => 'ERROR_COMPANY_IS_NOT_ACTIVE'
493
            ];
494
 
495
            return new JsonModel($data);
496
        }
497
 
498
        $request = $this->getRequest();
499
 
500
        $currentUserPlugin = $this->plugin('currentUserPlugin');
501
        $currentUser = $currentUserPlugin->getUser();
502
 
503
 
504
        if($request->isPost()) {
505
            $flash =  Functions::sanitizeFilterString($this->params()->fromPost('flash', 'false'));
506
            $flash = $flash === 'true'? true: false;
507
 
508
 
509
            $companyFollowerMapper = CompanyFollowerMapper::getInstance($this->adapter);
510
            $companyFollower = $companyFollowerMapper->fetchOneByCompanyIdAndUserId($company->id, $currentUser->id);
511
            if($companyFollower) {
512
                $result = $companyFollowerMapper->deleteByCompanyIdAndUserId($company->id, $currentUser->id);
513
                if($result) {
514
                    if($flash) {
515
                        $flashMessenger->addSuccessMessage('LABEL_YOU_STOPPED_FOLLOWING_THIS_COMPANY_SUCCESS');
516
 
517
                        $data = [
518
                            'success' => true,
519
                            'data' => [
520
                                'message' => 'LABEL_YOU_STOPPED_FOLLOWING_THIS_COMPANY_SUCCESS',
521
                                'reload' => true
522
                             ],
523
                        ];
524
                    } else {
525
                        $data = [
526
                            'success' => true,
527
                            'data' => 'LABEL_YOU_STOPPED_FOLLOWING_THIS_COMPANY_SUCCESS',
528
                        ];
529
                    }
530
                } else {
531
                    $data = [
532
                        'success' => false,
533
                        'data' => $companyFollowerMapper->getError()
534
                    ];
535
                }
536
            } else {
537
                $data = [
538
                    'success' => false,
539
                    'data' => 'ERROR_YOU DONT_FOLLOW_THIS_COMPANY'
540
                ];
541
            }
542
        } else {
543
            $data = [
544
                'success' => false,
545
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
546
            ];
547
        }
548
 
549
        return new JsonModel($data);
550
    }
551
 
552
    public function leaveAction()
553
    {
554
        $flashMessenger = $this->plugin('FlashMessenger');
555
        $currentUserPlugin = $this->plugin('currentUserPlugin');
556
        $currentUser = $currentUserPlugin->getUser();
557
 
558
 
559
        $request = $this->getRequest();
560
        if($request->isPost()) {
561
            $id = $this->params()->fromRoute('id');
562
            $flash =  Functions::sanitizeFilterString($this->params()->fromPost('flash', 'false'));
563
            $flash = $flash === 'true'? true: false;
564
 
565
 
566
            if(!$id) {
567
                return new JsonModel([
568
                    'success' => false,
569
                    'data' => 'ERROR_INVALID_PARAMETER'
570
                ]);
571
 
572
            }
573
 
574
            $companyMapper = CompanyMapper::getInstance($this->adapter);
575
            $company = $companyMapper->fetchOneByUuidAndNetworkId($id, $currentUser->network_id);
576
 
577
            if(!$company) {
578
                return new JsonModel([
579
                    'success' => false,
580
                    'data' => 'ERROR_COMPANY_NOT_FOUND'
581
                ]);
582
            }
583
 
584
            if($company->status != Company::STATUS_ACTIVE) {
585
                return new JsonModel([
586
                    'success' => false,
587
                    'data' => 'ERROR_COMPANY_IS_NOT_ACTIVE'
588
                ]);
589
            }
590
 
591
 
592
            $companyUserMapper = CompanyUserMapper::getInstance($this->adapter);
593
            $companyUser = $companyUserMapper->fetchOneByCompanyIdAndUserId($company->id, $currentUser->id);
594
            if($companyUser) {
595
 
596
                if($companyUser->status == CompanyUser::STATUS_ACCEPTED ) {
597
 
598
                    $companyUser->status = CompanyUser::STATUS_CANCELLED;
599
                    if($companyUserMapper->update($companyUser)) {
600
                        if($flash) {
601
                            $flashMessenger->addSuccessMessage('LABEL_YOU_STOP_WORKING_WITH_THIS_COMPANY');
602
                            return new JsonModel([
603
                                'success' => true,
604
                                'data' =>  [
605
                                    'message' => 'LABEL_YOU_STOP_WORKING_WITH_THIS_COMPANY',
606
                                    'reload' => true
607
                                ]
608
                            ]);
609
                        } else {
610
                            return new JsonModel([
611
                                'success' => true,
612
                                'data' =>  'LABEL_YOU_STOP_WORKING_WITH_THIS_COMPANY',
613
                            ]);
614
                        }
615
 
616
 
617
                    } else {
618
                        return new JsonModel([
619
                            'success' => false,
620
                            'data' => $companyUserMapper->getError()
621
                        ]);
622
                    }
623
 
624
 
625
                } else {
626
                    return new JsonModel([
627
                        'success' => false,
628
                        'data' => 'ERROR_COMPANY_YOU_ARE_NOT_AN_ACTIVE_USER_OF_THIS_COMPANY'
629
                    ]);
630
                }
631
 
632
            } else {
633
                return new JsonModel([
634
                    'success' => false,
635
                    'data' => 'ERROR_GROUP_YOU_NOT_MEMBER'
636
                ]);
637
            }
638
 
639
 
640
 
641
 
642
        } else {
643
 
644
            return new JsonModel([
645
                'success' => false,
646
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
647
            ]);
648
        }
649
    }
650
 
651
    public function cancelAction()
652
    {
653
        $flashMessenger = $this->plugin('FlashMessenger');
654
        $currentUserPlugin = $this->plugin('currentUserPlugin');
655
        $currentUser = $currentUserPlugin->getUser();
656
 
657
        $request = $this->getRequest();
658
        if($request->isPost()) {
659
            $id = $this->params()->fromRoute('id');
660
            $flash =  Functions::sanitizeFilterString($this->params()->fromPost('flash', 'false'));
661
            $flash = $flash === 'true'? true: false;
662
 
663
            if(!$id) {
664
                return new JsonModel([
665
                    'success' => false,
666
                    'data' => 'ERROR_INVALID_PARAMETER'
667
                ]);
668
 
669
            }
670
 
671
            $companyMapper = CompanyMapper::getInstance($this->adapter);
672
            $company = $companyMapper->fetchOneByUuid($id);
673
 
674
            if(!$company) {
675
                return new JsonModel([
676
                    'success' => false,
677
                    'data' => 'ERROR_COMPANY_NOT_FOUND'
678
                ]);
679
            }
680
 
681
            if($company->status != Company::STATUS_ACTIVE) {
682
                return new JsonModel([
683
                    'success' => false,
684
                    'data' => 'ERROR_COMPANY_IS_NOT_ACTIVE'
685
                ]);
686
            }
687
 
688
            $companyUserMapper = CompanyUserMapper::getInstance($this->adapter);
689
            $companyUser = $companyUserMapper->fetchOneByCompanyIdAndUserId($company->id, $currentUser->id);
690
            if($companyUser) {
691
 
692
                if( $companyUser->status == CompanyUser::STATUS_SENT) {
693
                    $companyUser->status = CompanyUser::STATUS_CANCELLED;
694
                    if($companyUserMapper->update($companyUser)) {
695
                        if($flash) {
696
                            $flashMessenger->addSuccessMessage('LABEL_COMPANY_REQUEST_CANCELLED');
697
                            return new JsonModel([
698
                                'success' => true,
699
                                'data' =>  [
700
                                    'message' => 'LABEL_COMPANY_REQUEST_CANCELLED',
701
                                    'reload' => true,
702
                                 ]
703
                            ]);
704
                        } else {
705
 
706
                            return new JsonModel([
707
                                'success' => true,
708
                                'data' =>  'LABEL_COMPANY_REQUEST_CANCELLED'
709
                           ]);
710
                        }
711
                    } else {
712
                        return new JsonModel([
713
                            'success' => false,
714
                            'data' => $companyUserMapper->getError()
715
                        ]);
716
                    }
717
 
718
 
719
                } else {
720
                    return new JsonModel([
721
                        'success' => false,
722
                        'data' => 'ERROR_COMPANY_THERE_IS_NO_PENDING_REQUEST_TO_CANCEL'
723
                    ]);
724
                }
725
 
726
            } else {
727
                return new JsonModel([
728
                    'success' => false,
729
                    'data' =>'ERROR_COMPANY_YOU_HAVE_NOT_INVITED_THIS_COMPANY'
730
                ]);
731
            }
732
 
733
 
734
 
735
 
736
        } else {
737
 
738
            return new JsonModel([
739
                'success' => false,
740
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
741
            ]);
742
        }
743
    }
744
 
745
    public function rejectAction()
746
    {
747
        $flashMessenger = $this->plugin('FlashMessenger');
748
        $currentUserPlugin = $this->plugin('currentUserPlugin');
749
        $currentUser = $currentUserPlugin->getUser();
750
 
751
        $request = $this->getRequest();
752
        if($request->isPost()) {
753
            $id = $this->params()->fromRoute('id');
754
            $flash =  Functions::sanitizeFilterString($this->params()->fromPost('flash', 'false'));
755
            $flash = $flash === 'true'? true: false;
756
 
757
            if(!$id) {
758
                return new JsonModel([
759
                    'success' => false,
760
                    'data' => 'ERROR_INVALID_PARAMETER'
761
                ]);
762
 
763
            }
764
 
765
            $companyMapper = CompanyMapper::getInstance($this->adapter);
766
            $company = $companyMapper->fetchOneByUuid($id);
767
 
768
            if(!$company) {
769
                return new JsonModel([
770
                    'success' => false,
771
                    'data' => 'ERROR_COMPANY_NOT_FOUND'
772
                ]);
773
            }
774
 
775
            if($company->status != Company::STATUS_ACTIVE) {
776
                return new JsonModel([
777
                    'success' => false,
778
                    'data' => 'ERROR_COMPANY_IS_NOT_ACTIVE'
779
                ]);
780
            }
781
 
782
            $companyUserMapper = CompanyUserMapper::getInstance($this->adapter);
783
            $companyUser = $companyUserMapper->fetchOneByCompanyIdAndUserId($company->id, $currentUser->id);
784
            if($companyUser) {
785
 
786
                if($companyUser->status == CompanyUser::STATUS_ADMIN_WILL_ADD ) {
787
 
788
                    $companyUser->status = CompanyUser::STATUS_CANCELLED;
789
                    if($companyUserMapper->update($companyUser)) {
790
 
791
                        if($flash) {
792
                            $flashMessenger->addSuccessMessage('LABEL_YOU_REJECTED_WORKING_WITH_THIS_COMPANY');
793
                            return new JsonModel([
794
                                'success' => true,
795
                                'data' =>  [
796
                                    'message' => 'LABEL_YOU_REJECTED_WORKING_WITH_THIS_COMPANY',
797
                                    'reload' => true
798
                                 ]
799
                            ]);
800
 
801
                        } else {
802
                            return new JsonModel([
803
                                'success' => true,
804
                                'data' =>  'LABEL_YOU_REJECTED_WORKING_WITH_THIS_COMPANY'
805
                            ]);
806
                        }
807
 
808
 
809
                    } else {
810
                        return new JsonModel([
811
                            'success' => false,
812
                            'data' => $companyUserMapper->getError()
813
                        ]);
814
                    }
815
 
816
                } else {
817
                    return new JsonModel([
818
                        'success' => false,
819
                        'data' => 'ERROR_COMPANY_THERE_IS_NO_PENDING_REQUEST_TO_CANCEL'
820
                    ]);
821
                }
822
 
823
            } else {
824
                return new JsonModel([
825
                    'success' => false,
826
                    'data' =>'ERROR_COMPANY_YOU_HAVE_NOT_INVITED_THIS_COMPANY'
827
                ]);
828
            }
829
 
830
 
831
 
832
 
833
        } else {
834
 
835
            return new JsonModel([
836
                'success' => false,
837
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
838
            ]);
839
        }
840
    }
841
 
842
    public function acceptAction()
843
    {
844
        $flashMessenger = $this->plugin('FlashMessenger');
845
        $currentUserPlugin = $this->plugin('currentUserPlugin');
846
        $currentUser = $currentUserPlugin->getUser();
847
 
848
        $request = $this->getRequest();
849
        if($request->isPost()) {
850
            $id = $this->params()->fromRoute('id');
851
            $flash =  Functions::sanitizeFilterString($this->params()->fromPost('flash', 'false'));
852
            $flash = $flash === 'true'? true: false;
853
 
854
            if(!$id) {
855
                return new JsonModel([
856
                    'success' => false,
857
                    'data' => 'ERROR_INVALID_PARAMETER'
858
                ]);
859
 
860
            }
861
 
862
            $companyMapper = CompanyMapper::getInstance($this->adapter);
863
            $company = $companyMapper->fetchOneByUuid($id);
864
 
865
            if(!$company) {
866
                return new JsonModel([
867
                    'success' => false,
868
                    'data' => 'ERROR_COMPANY_NOT_FOUND'
869
                ]);
870
            }
871
 
872
            if($company->status != Company::STATUS_ACTIVE) {
873
                return new JsonModel([
874
                    'success' => false,
875
                    'data' => 'ERROR_COMPANY_IS_NOT_ACTIVE'
876
                ]);
877
            }
878
 
879
            $companyUserMapper = CompanyUserMapper::getInstance($this->adapter);
880
            $companyUser = $companyUserMapper->fetchOneByCompanyIdAndUserId($company->id, $currentUser->id);
881
            if($companyUser) {
882
 
883
                if($companyUser->status == CompanyUser::STATUS_ADMIN_WILL_ADD) {
884
 
885
                    $companyUser->status = CompanyUser::STATUS_ACCEPTED;
886
                    if($companyUserMapper->update($companyUser)) {
887
                        if($flash) {
888
                            $flashMessenger->addSuccessMessage('LABEL_YOU_STARTED_WORKING_WITH_THIS_COMPANY' );
889
 
890
                            return new JsonModel([
891
                                'success' => true,
892
                                'data' => [
893
                                    'message' => 'LABEL_YOU_STARTED_WORKING_WITH_THIS_COMPANY',
894
                                    'reload' => true
895
                                ]
896
                            ]);
897
                        } else {
898
                            return new JsonModel([
899
                                'success' => true,
900
                                'data' => 'LABEL_YOU_STARTED_WORKING_WITH_THIS_COMPANY',
901
                            ]);
902
                        }
903
 
904
                    } else {
905
                        return new JsonModel([
906
                            'success' => false,
907
                            'data' => $companyUserMapper->getError()
908
                        ]);
909
                    }
910
                } else {
911
                    return new JsonModel([
912
                        'success' => false,
913
                        'data' => 'ERROR_COMPANY_YOU_HAVE_NOT_INVITED_THIS_COMPANY'
914
                    ]);
915
                }
916
 
917
            } else {
918
                return new JsonModel([
919
                    'success' => false,
920
                    'data' => 'ERROR_COMPANY_YOU_HAVE_NOT_INVITED_THIS_COMPANY'
921
                ]);
922
            }
923
        } else {
924
 
925
            return new JsonModel([
926
                'success' => false,
927
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
928
            ]);
929
        }
930
    }
931
 
932
    public function requestAction()
933
    {
934
        $flashMessenger = $this->plugin('FlashMessenger');
935
        $currentUserPlugin = $this->plugin('currentUserPlugin');
936
        $currentUser = $currentUserPlugin->getUser();
937
 
938
        $request = $this->getRequest();
939
        if($request->isPost()) {
940
            $id = $this->params()->fromRoute('id');
941
            $flash =  Functions::sanitizeFilterString($this->params()->fromPost('flash', 'false'));
942
            $flash = $flash === 'true'? true: false;
943
 
944
            if(!$id) {
945
                return new JsonModel([
946
                    'success' => false,
947
                    'data' => 'ERROR_INVALID_PARAMETER'
948
                ]);
949
 
950
            }
951
 
952
            $companyMapper = CompanyMapper::getInstance($this->adapter);
953
            $company = $companyMapper->fetchOneByUuid($id);
954
 
955
            if(!$company) {
956
                return new JsonModel([
957
                    'success' => false,
958
                    'data' => 'ERROR_COMPANY_NOT_FOUND'
959
                ]);
960
            }
961
 
962
            if($company->status != Company::STATUS_ACTIVE) {
963
                return new JsonModel([
964
                    'success' => false,
965
                    'data' => 'ERROR_COMPANY_IS_NOT_ACTIVE'
966
                ]);
967
            }
968
 
969
            $companyUserMapper = CompanyUserMapper::getInstance($this->adapter);
970
            $companyUser = $companyUserMapper->fetchOneByCompanyIdAndUserId($company->id, $currentUser->id);
971
            if($companyUser) {
972
                if($companyUser->status == CompanyUser::STATUS_ACCEPTED) {
973
                    return new JsonModel([
974
                        'success' => false,
975
                        'data' => 'ERROR_COMPANY_YOU_ARE_USER'
976
                    ]);
977
                }
978
                if($companyUser->status == CompanyUser::STATUS_REJECTED) {
979
                    return new JsonModel([
980
                        'success' => false,
981
                        'data' => 'ERROR_COMPANY_YOUR REQUEST WAS PREVIOUSLY REJECTED'
982
                    ]);
983
                }
984
 
985
            }
986
 
987
            if($companyUser) {
988
                $companyUser->status    = CompanyUser::STATUS_SENT;
989
                $result = $companyUserMapper->update($companyUser);
990
 
991
 
992
            } else {
993
                $companyUser = new CompanyUser();
994
                $companyUser->company_id    = $company->id;
995
                $companyUser->user_id       = $currentUser->id;
996
                $companyUser->status        = CompanyUser::STATUS_SENT;
997
 
998
                $result = $companyUserMapper->insert($companyUser);
999
            }
1000
 
1001
            if($result) {
1002
                if($flash) {
1003
                    $flashMessenger->addSuccessMessage('LABEL_COMPANY_REQUEST_SENT' );
1004
 
1005
                    return new JsonModel([
1006
                        'success' => true,
1007
                        'data' => [
1008
                            'message' => 'LABEL_COMPANY_REQUEST_SENT',
1009
                            'reload' => true
1010
                         ]
1011
                    ]);
1012
                } else {
1013
                    return new JsonModel([
1014
                        'success' => true,
1015
                        'data' => 'LABEL_COMPANY_REQUEST_SENT'
1016
                    ]);
1017
                }
1018
                return new JsonModel([
1019
                    'success' => true,
1020
                    'data' => 'LABEL_COMPANY_REQUEST_SENT'
1021
                ]);
1022
 
1023
            } else {
1024
                return new JsonModel([
1025
                    'success' => false,
1026
                    'data' => $companyUserMapper->getError()
1027
                ]);
1028
            }
1029
 
1030
 
1031
        } else {
1032
 
1033
            return new JsonModel([
1034
                'success' => false,
1035
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
1036
            ]);
1037
        }
1038
 
1039
    }
1040
 
1041
    public function invitationsReceivedAction()
1042
    {
1043
        $currentUserPlugin = $this->plugin('currentUserPlugin');
1044
        $currentUser = $currentUserPlugin->getUser();
1045
 
1046
        $request = $this->getRequest();
1047
        if($request->isGet()) {
1048
 
1049
 
1050
 
1051
                $search = Functions::sanitizeFilterString($this->params()->fromQuery('search', ''));
1052
 
1053
 
1054
 
1055
                $queryMapper = QueryMapper::getInstance($this->adapter);
1056
 
1057
 
1058
 
1059
 
1060
                $select = $queryMapper->getSql()->select();
1061
                $select->columns([ 'uuid', 'name', 'image']);
1062
                $select->from(['c' => CompanyMapper::_TABLE]);
1063
                $select->join(['cu' => CompanyUserMapper::_TABLE], 'cu.company_id  = c.id', []);
1064
                $select->where->equalTo('cu.user_id', $currentUser->id);
1065
                $select->where->equalTo('cu.status', CompanyUser::STATUS_ADMIN_WILL_ADD);
1066
                $select->where->equalTo('c.status', Company::STATUS_ACTIVE);
1067
 
1068
                if($search) {
1069
                    $select->where->like('c.name', '%' . $search . '%');
1070
                }
1071
                $select->order('name ASC');
1072
 
1073
               // echo $select->getSqlString($this->adapter->platform); exit;
1074
 
333 www 1075
                $storage = Storage::getInstance($this->config, $this->adapter);
1 efrain 1076
                $records = $queryMapper->fetchAll($select);
1077
 
1078
                $items = [];
1079
                foreach($records as $record)
1080
                {
1081
                    $item = [
1082
                        'name' => $record['name'],
283 www 1083
                        'image' => $storage->getCompanyImageForCodeAndFilename($record['uuid'],  $record['image']),
1 efrain 1084
                        'link_view' => $this->url()->fromRoute('company/view', ['id' => $record['uuid'] ]),
1085
                        'link_accept' => $this->url()->fromRoute('company/accept', ['id' => $record['uuid'] ]),
1086
                        'link_reject' => $this->url()->fromRoute('company/reject', ['id' => $record['uuid'] ]),
1087
                    ];
1088
 
1089
 
1090
                    array_push($items, $item);
1091
                }
1092
 
1093
                $response = [
1094
                    'success' => true,
1095
                    'data' => $items
1096
                ];
1097
 
1098
                return new JsonModel($response);
1099
 
1100
 
1101
 
1102
 
1103
        } else {
1104
            return new JsonModel([
1105
                'success' => false,
1106
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
1107
            ]);
1108
        }
1109
    }
1110
 
1111
    public function requestsSentAction()
1112
    {
1113
        $currentUserPlugin = $this->plugin('currentUserPlugin');
1114
        $currentUser = $currentUserPlugin->getUser();
1115
 
1116
        $request = $this->getRequest();
1117
        if($request->isGet()) {
1118
 
1119
 
1120
 
1121
                $search = Functions::sanitizeFilterString($this->params()->fromQuery('search', ''));
1122
 
1123
 
1124
 
1125
                $queryMapper = QueryMapper::getInstance($this->adapter);
1126
 
1127
                $select = $queryMapper->getSql()->select();
1128
                $select->columns([ 'uuid', 'name', 'image']);
1129
                $select->from(['c' => CompanyMapper::_TABLE]);
1130
                $select->join(['cu' => CompanyUserMapper::_TABLE], 'cu.company_id  = c.id', ['status']);
1131
                $select->where->equalTo('cu.user_id', $currentUser->id);
1132
                $select->where->equalTo('cu.status', CompanyUser::STATUS_SENT);
1133
                $select->where->equalTo('c.status', Company::STATUS_ACTIVE);
1134
 
1135
 
1136
                if($search) {
1137
                    $select->where->like('c.name', '%' . $search . '%');
1138
                }
1139
                $select->order('name ASC');
1140
 
1141
                //echo $select2->getSqlString($this->adapter->platform); exit;
1142
 
333 www 1143
                $storage = Storage::getInstance($this->config, $this->adapter);
1 efrain 1144
                $records = $queryMapper->fetchAll($select);
1145
 
1146
                $items = [];
1147
                foreach($records as $record)
1148
                {
1149
                    $item = [
1150
                        'name' => $record['name'],
283 www 1151
                        'image' => $storage->getCompanyImageForCodeAndFilename($record['uuid'], $record['image']),
1 efrain 1152
                        'link_view' => $this->url()->fromRoute('company/view', ['id' => $record['uuid'] ]),
1153
                        'link_cancel' => $this->url()->fromRoute('company/cancel', ['id' => $record['uuid'] ]),
1154
                    ];
1155
 
1156
 
1157
                    array_push($items, $item);
1158
                }
1159
 
1160
                $response = [
1161
                    'success' => true,
1162
                    'data' => $items
1163
                ];
1164
 
1165
                return new JsonModel($response);
1166
 
1167
 
1168
        } else {
1169
            return new JsonModel([
1170
                'success' => false,
1171
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
1172
            ]);
1173
        }
1174
    }
1175
 
1176
 
1177
    public function iWorkWithAction()
1178
    {
1179
        $currentUserPlugin = $this->plugin('currentUserPlugin');
1180
        $currentUser = $currentUserPlugin->getUser();
1181
 
1182
        $request = $this->getRequest();
1183
        if($request->isGet()) {
1184
 
1185
 
1186
                $search = Functions::sanitizeFilterString($this->params()->fromQuery('search', ''));
1187
 
1188
 
1189
 
1190
                $queryMapper = QueryMapper::getInstance($this->adapter);
1191
 
1192
                $select = $queryMapper->getSql()->select();
1193
                $select->columns([ 'id', 'uuid', 'name', 'image']);
1194
                $select->from(['c' => CompanyMapper::_TABLE]);
1195
                $select->join(['cu' => CompanyUserMapper::_TABLE], 'cu.company_id  = c.id', []);
1196
                $select->where->equalTo('cu.user_id', $currentUser->id);
1197
                $select->where->equalTo('cu.status', CompanyUser::STATUS_ACCEPTED);
1198
                $select->where->equalTo('c.status', Company::STATUS_ACTIVE);
1199
                $select->where->equalTo('cu.owner', CompanyUser::OWNER_NO);
1200
                $select->where->equalTo('cu.creator', CompanyUser::CREATOR_NO);
1201
 
1202
                if($search) {
1203
                    $select->where->like('c.name', '%' . $search . '%');
1204
                }
1205
                $select->order('name ASC');
1206
 
1207
                /*
1208
                 * select uuid, name, image from tbl_companies  as c
1209
                 inner join tbl_company_users as cu on c.id = cu.company_id
1210
                 and cu.user_id  = 2 and c.status  = 'a' and cu.status = 'aa'
1211
                 */
1212
 
1213
                //echo $select->getSqlString($this->adapter->platform); exit;
1214
 
1215
                $companyUserMapper = CompanyUserMapper::getInstance($this->adapter);
1216
 
1217
 
1218
                $records = $queryMapper->fetchAll($select);
1219
 
333 www 1220
                $storage = Storage::getInstance($this->config, $this->adapter);
283 www 1221
 
1 efrain 1222
                $items = [];
1223
                foreach($records as $record)
1224
                {
1225
 
1226
                    $companyUser = $companyUserMapper->fetchOneByCompanyIdAndUserId($record['id'], $currentUser->id);
1227
                    if($companyUser && $companyUser->status == CompanyUser::STATUS_ACCEPTED && $companyUser->backend == CompanyUser::BACKEND_YES ) {
1228
                        $link_my_company = $this->url()->fromRoute('backend/signin-company', ['id' => $record['uuid'] ]);
1229
                    } else {
1230
                        $link_my_company = '';
1231
                    }
1232
 
1233
                    $item = [
1234
                        'name' => $record['name'],
283 www 1235
                        'image' => $storage->getCompanyImageForCodeAndFilename($record['uuid'],  $record['image']),
1 efrain 1236
                        'link_view' => $this->url()->fromRoute('company/view', ['id' => $record['uuid'] ]),
1237
                        'link_leave' => $this->url()->fromRoute('company/leave', ['id' => $record['uuid'] ]),
1238
                        'link_my_company' => $link_my_company
1239
                    ];
1240
 
1241
                    array_push($items, $item);
1242
                }
1243
 
1244
 
1245
 
1246
                $response = [
1247
                    'success' => true,
1248
                    'data' => $items
1249
                ];
1250
 
1251
                return new JsonModel($response);
1252
 
1253
 
1254
        } else {
1255
            return new JsonModel([
1256
                'success' => false,
1257
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
1258
            ]);
1259
        }
1260
    }
1261
 
1262
 
1263
    /**
1264
     *
1265
     * Generación del listado de perfiles
1266
     * {@inheritDoc}
1267
     * @see \Laminas\Mvc\Controller\AbstractActionController::indexAction()
1268
     */
1269
    public function followingCompaniesAction()
1270
    {
1271
        $currentUserPlugin = $this->plugin('currentUserPlugin');
1272
        $currentUser = $currentUserPlugin->getUser();
1273
 
1274
        $request = $this->getRequest();
1275
        if($request->isGet()) {
1276
 
1277
 
1278
                $search = Functions::sanitizeFilterString($this->params()->fromQuery('search', ''));
1279
                $queryMapper = QueryMapper::getInstance($this->adapter);
1280
 
1281
                $select = $queryMapper->getSql()->select();
1282
                $select->columns(['id', 'uuid', 'name', 'status',  'image']);
1283
                $select->from(['c' => CompanyMapper::_TABLE]);
1284
                $select->join(['cf' => CompanyFollowerMapper::_TABLE],'cf.company_id = c.id' ,['follower_id']);
1285
                $select->where->equalTo('follower_id', $currentUser->id);
1286
                $select->where->equalTo('c.status', Company::STATUS_ACTIVE);
1287
 
1288
                if($search) {
1289
                    $select->where->like('c.name', '%' . $search . '%');
1290
                }
1291
                $select->order('name ASC');
1292
 
1293
                $records = $queryMapper->fetchAll($select);
333 www 1294
                $storage = Storage::getInstance($this->config, $this->adapter);
1 efrain 1295
 
1296
                $items = [];
1297
                foreach($records as $record)
1298
                {
1299
                    $item = [
1300
                        'name' => $record['name'],
283 www 1301
                        'image' => $storage->getCompanyImageForCodeAndFilename($record['uuid'], $record['image']),
1 efrain 1302
                        'link_view' => $this->url()->fromRoute('company/view', ['id' => $record['uuid']]),
1303
                        'link_unfollow' => $this->url()->fromRoute('company/unfollow', ['id' => $record['uuid']]),
1304
                    ];
1305
 
1306
                    array_push($items, $item);
1307
                }
1308
 
1309
                $response = [
1310
                    'success' => true,
1311
                    'data' => $items,
1312
                    'sql' =>  $select->getSqlString($this->adapter->platform)
1313
 
1314
                ];
1315
 
1316
                return new JsonModel($response);
1317
 
1318
 
1319
        } else {
1320
            return new JsonModel([
1321
                'success' => false,
1322
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
1323
            ]);
1324
        }
1325
 
1326
    }
1327
 
1328
}