Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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