Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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