Proyectos de Subversion LeadersLinked - Services

Rev

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