Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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