Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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