Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 240 | Rev 3674 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

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