Proyectos de Subversion LeadersLinked - Services

Rev

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