Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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