Proyectos de Subversion LeadersLinked - Services

Rev

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