Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 3712 | Rev 6749 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 www 1
<?php
2
/**
3
 *
4
 * Controlador: Mis Perfiles
5
 *
6
 */
7
declare(strict_types=1);
8
 
9
namespace LeadersLinked\Controller;
10
 
11
use Laminas\Db\Adapter\AdapterInterface;
12
use Laminas\Cache\Storage\Adapter\AbstractAdapter;
13
use Laminas\Mvc\Controller\AbstractActionController;
14
use Laminas\Log\LoggerInterface;
15
use Laminas\View\Model\ViewModel;
16
use Laminas\View\Model\JsonModel;
17
use Laminas\Db\Sql\Expression;
18
use LeadersLinked\Model\User;
19
use LeadersLinked\Mapper\ConnectionMapper;
20
use LeadersLinked\Mapper\UserMapper;
21
use LeadersLinked\Model\Connection;
22
use Laminas\Paginator\Adapter\DbSelect;
23
use Laminas\Paginator\Paginator;
24
use LeadersLinked\Mapper\UserBlockedMapper;
25
use LeadersLinked\Model\UserBlocked;
26
use LeadersLinked\Mapper\QueryMapper;
27
use LeadersLinked\Model\UserType;
28
use LeadersLinked\Model\CompanyUser;
29
use LeadersLinked\Mapper\GroupMemberMapper;
30
use LeadersLinked\Model\GroupMember;
31
use LeadersLinked\Mapper\CompanyUserMapper;
32
use LeadersLinked\Mapper\CompanyMicrolearningCapsuleUserMapper;
33
use LeadersLinked\Model\Notification;
34
use LeadersLinked\Mapper\NotificationMapper;
35
use LeadersLinked\Mapper\UserNotificationSettingMapper;
36
use LeadersLinked\Mapper\EmailTemplateMapper;
37
use LeadersLinked\Model\EmailTemplate;
38
use LeadersLinked\Library\QueueEmail;
4842 efrain 39
use LeadersLinked\Model\Network;
1 www 40
 
41
class ConnectionController extends AbstractActionController
42
{
43
    /**
44
     *
45
     * @var AdapterInterface
46
     */
47
    private $adapter;
48
 
49
 
50
    /**
51
     *
52
     * @var AbstractAdapter
53
     */
54
    private $cache;
55
 
56
    /**
57
     *
58
     * @var  LoggerInterface
59
     */
60
    private $logger;
61
 
62
 
63
    /**
64
     *
65
     * @var array
66
     */
67
    private $config;
68
 
69
    /**
70
     *
71
     * @param AdapterInterface $adapter
72
     * @param AbstractAdapter $cache
73
     * @param LoggerInterface $logger
74
     * @param array $config
75
     */
76
    public function __construct($adapter, $cache , $logger,  $config)
77
    {
78
        $this->adapter      = $adapter;
79
        $this->cache        = $cache;
80
        $this->logger       = $logger;
81
        $this->config       = $config;
82
 
83
    }
84
 
85
    /**
86
     *
87
     * Generación del listado de perfiles
88
     * {@inheritDoc}
89
     * @see \Laminas\Mvc\Controller\AbstractActionController::indexAction()
90
     */
91
    public function indexAction()
92
    {
93
        return new JsonModel([
94
            'success' => false,
95
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
96
        ]);
97
    }
98
 
99
    public function myConnectionsAction()
100
    {
101
        $currentUserPlugin = $this->plugin('currentUserPlugin');
102
        $currentUser = $currentUserPlugin->getUser();
103
 
4842 efrain 104
        $currentNetworkPlugin = $this->plugin('currentNetworkPlugin');
105
        $currentNetwork= $currentNetworkPlugin->getNetwork();
106
 
107
 
1 www 108
        $request = $this->getRequest();
109
        if($request->isGet()) {
110
 
111
 
112
            $headers  = $request->getHeaders();
113
            $isJson = false;
114
            if($headers->has('Accept')) {
115
                $accept = $headers->get('Accept');
116
 
117
                $prioritized = $accept->getPrioritized();
118
 
119
                foreach($prioritized as $key => $value) {
120
                    $raw = trim($value->getRaw());
121
 
122
                    if(!$isJson) {
123
                        $isJson = strpos($raw, 'json');
124
                    }
125
 
126
                }
127
            }
128
 
129
            if($isJson) {
4842 efrain 130
 
131
 
132
 
133
 
134
                $page       = (int) filter_var($this->params()->fromQuery('page'), FILTER_SANITIZE_NUMBER_INT);
1 www 135
                $search = trim(filter_var($this->params()->fromQuery('search', ''), FILTER_SANITIZE_STRING));
136
 
137
 
138
                $connectionMapper = ConnectionMapper::getInstance($this->adapter);
139
 
4842 efrain 140
                if($currentNetwork->relationship_user_mode == Network::RELATIONSHIP_USER_MODE_USER_2_USER)  {
141
                    $user_ids = $connectionMapper->fetchAllConnectionsByUserReturnIds($currentUser->id);
142
 
143
                    $userBlockedMapper = UserBlockedMapper::getInstance($this->adapter);
144
                    $user_blocked_ids = $userBlockedMapper->fetchAllBlockedReturnIds($currentUser->id);
145
                    $user_ids = array_diff($user_ids, $user_blocked_ids);
146
                }
147
 
148
 
149
 
1 www 150
 
151
                $queryMapper = QueryMapper::getInstance($this->adapter);
152
 
153
                $select = $queryMapper->getSql()->select(UserMapper::_TABLE);
4842 efrain 154
 
155
                if($currentNetwork->relationship_user_mode == Network::RELATIONSHIP_USER_MODE_USER_2_USER)  {
156
                    $select->where->in('id', $user_ids);
157
                } else {
158
                    $select->where->notEqualTo('id', $currentUser->id);
159
                }
1 www 160
                $select->where->equalTo('status', User::STATUS_ACTIVE);
3648 efrain 161
                $select->where->equalTo('network_id', $currentUser->network_id);
1 www 162
 
163
                if($search) {
164
                    $select->where->NEST->like('first_name', '%' . $search . '%')->or->like('last_name', '%' . $search . '%')->UNNEST;
165
                }
166
                $select->order('first_name ASC, last_name ASC');
167
 
4842 efrain 168
                $dbSelect = new DbSelect($select, $this->adapter);
169
                $paginator = new Paginator($dbSelect);
170
                $paginator->setCurrentPageNumber($page ? $page : 1);
171
                $paginator->setItemCountPerPage(10);
172
 
173
 
174
                $records = $paginator->getCurrentItems();
1 www 175
                $items = [];
176
                foreach($records as $record)
177
                {
178
 
4842 efrain 179
 
1 www 180
 
4842 efrain 181
 
182
 
1 www 183
                    $item = [
184
                        'name' => trim($record['first_name'] . ' ' . $record['last_name']),
185
                        'image' => $this->url()->fromRoute('storage', ['type' => 'user', 'code' => $record['uuid'], 'filename' => $record['image'] ]),
186
                        'link_view' => $this->url()->fromRoute('profile/view', ['id' => $record['uuid']  ]),
187
                        'link_inmail'   => $this->url()->fromRoute('inmail', ['id' => $record['uuid']  ]),
188
 
4842 efrain 189
 
1 www 190
                    ];
191
 
4842 efrain 192
                    if($currentNetwork->relationship_user_mode == Network::RELATIONSHIP_USER_MODE_ALL_2_ALL) {
193
                        $item['link_cancel'] = '';
194
                        $item['link_block'] = '';
195
 
196
                    } else {
197
                        $item['link_cancel'] = $this->url()->fromRoute('connection/cancel', ['id' => $record['uuid']  ]);
198
                        $item['link_block'] = $this->url()->fromRoute('connection/block', ['id' => $record['uuid']  ]);
199
                    }
200
 
201
 
1 www 202
 
203
 
204
                    array_push($items, $item);
205
                }
206
 
207
 
208
 
209
                $response = [
210
                    'success' => true,
4842 efrain 211
                    'data' => [
212
                        'total' => [
213
                            'count' => $paginator->getTotalItemCount(),
214
                            'pages' => $paginator->getPages()->pageCount,
215
                        ],
216
                        'current' => [
217
                            'items'    => $items,
218
                            'page'     => $paginator->getCurrentPageNumber(),
219
                            'count'    => $paginator->getCurrentItemCount(),
220
                        ]
221
                    ]
1 www 222
                ];
4842 efrain 223
 
1 www 224
                return new JsonModel($response);
225
 
226
 
227
            } else {
228
 
229
                $notificationMapper = NotificationMapper::getInstance($this->adapter);
230
                $notificationMapper->markAllNotificationsAsReadByTypeAndUserId(Notification::TYPE_ACCEPT_MY_REQUEST_CONNECTION, $currentUser->id);
231
 
232
                $this->layout()->setTemplate('layout/layout.phtml');
233
                $viewModel = new ViewModel();
234
                $viewModel->setTemplate('leaders-linked/connection/my-connections.phtml');
235
                return $viewModel ;
236
            }
237
 
238
        } else {
239
            return new JsonModel([
240
                'success' => false,
241
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
242
            ]);
243
        }
244
    }
245
 
246
    public function invitationsSentAction()
247
    {
248
        $currentUserPlugin = $this->plugin('currentUserPlugin');
249
        $currentUser = $currentUserPlugin->getUser();
250
 
251
        $request = $this->getRequest();
252
        if($request->isGet()) {
253
 
254
 
255
            $headers  = $request->getHeaders();
256
            $isJson = false;
257
            if($headers->has('Accept')) {
258
                $accept = $headers->get('Accept');
259
 
260
                $prioritized = $accept->getPrioritized();
261
 
262
                foreach($prioritized as $key => $value) {
263
                    $raw = trim($value->getRaw());
264
 
265
                    if(!$isJson) {
266
                        $isJson = strpos($raw, 'json');
267
                    }
268
 
269
                }
270
            }
271
 
272
            if($isJson) {
273
                $search = trim(filter_var($this->params()->fromQuery('search', ''), FILTER_SANITIZE_STRING));
274
 
275
 
276
                $connectionMapper = ConnectionMapper::getInstance($this->adapter);
277
                $user_ids = $connectionMapper->fetchAllSentInvitationsReturnIds($currentUser->id);
278
 
279
                $userBlockedMapper = UserBlockedMapper::getInstance($this->adapter);
280
                $user_blocked_ids = $userBlockedMapper->fetchAllBlockedReturnIds($currentUser->id);
281
                $user_ids = array_diff($user_ids, $user_blocked_ids);
282
 
283
 
284
                $queryMapper = QueryMapper::getInstance($this->adapter);
285
 
286
                $select = $queryMapper->getSql()->select(UserMapper::_TABLE);
287
                $select->where->in('id', $user_ids);
288
                $select->where->equalTo('status', User::STATUS_ACTIVE);
3648 efrain 289
                $select->where->equalTo('network_id', $currentUser->network_id);
1 www 290
 
291
                if($search) {
292
                    $select->where->NEST->like('first_name', '%' . $search . '%')->or->like('last_name', '%' . $search . '%')->UNNEST;
293
                }
294
                $select->order('first_name ASC, last_name ASC');
295
 
296
                $records = $queryMapper->fetchAll($select);
297
                $items = [];
298
                foreach($records as $record)
299
                {
300
                    $item = [
301
                        'name' => trim($record['first_name'] . ' ' . $record['last_name']),
302
                        'image' => $this->url()->fromRoute('storage', ['type' => 'user', 'code' => $record['uuid'], 'filename' => $record['image'] ]),
303
                        'link_view' => $this->url()->fromRoute('profile/view', ['id' => $record['uuid']  ]),
304
                        'link_delete' => $this->url()->fromRoute('connection/delete', ['id' => $record['uuid']  ]),
305
                    ];
306
 
307
                    array_push($items, $item);
308
                }
309
 
310
                $response = [
311
                    'success' => true,
312
                    'data' => $items
313
                ];
314
 
315
                return new JsonModel($response);
316
            } else {
317
                $this->layout()->setTemplate('layout/layout.phtml');
318
                $viewModel = new ViewModel();
319
                $viewModel->setTemplate('leaders-linked/connection/invitations-sent.phtml');
320
                return $viewModel ;
321
            }
322
 
323
        } else {
324
            return new JsonModel([
325
                'success' => false,
326
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
327
            ]);
328
        }
329
    }
330
 
331
    public function invitationsReceivedAction()
332
    {
333
        $currentUserPlugin = $this->plugin('currentUserPlugin');
334
        $currentUser = $currentUserPlugin->getUser();
335
 
336
        $request = $this->getRequest();
337
        if($request->isGet()) {
338
 
339
 
340
            $headers  = $request->getHeaders();
341
            $isJson = false;
342
            if($headers->has('Accept')) {
343
                $accept = $headers->get('Accept');
344
 
345
                $prioritized = $accept->getPrioritized();
346
 
347
                foreach($prioritized as $key => $value) {
348
                    $raw = trim($value->getRaw());
349
 
350
                    if(!$isJson) {
351
                        $isJson = strpos($raw, 'json');
352
                    }
353
 
354
                }
355
            }
356
 
357
            if($isJson) {
358
                $search = trim(filter_var($this->params()->fromQuery('search', ''), FILTER_SANITIZE_STRING));
359
 
360
                $connectionMapper = ConnectionMapper::getInstance($this->adapter);
361
                $user_ids = $connectionMapper->fetchAllReceiveInvitationsReturnIds($currentUser->id);
362
 
363
 
364
                $userBlockedMapper = UserBlockedMapper::getInstance($this->adapter);
365
                $user_blocked_ids = $userBlockedMapper->fetchAllBlockedReturnIds($currentUser->id);
366
                $user_ids = array_diff($user_ids, $user_blocked_ids);
367
 
368
                $queryMapper = QueryMapper::getInstance($this->adapter);
369
                $select = $queryMapper->getSql()->select(UserMapper::_TABLE);
370
                $select->where->in('id', $user_ids);
371
                $select->where->equalTo('status', User::STATUS_ACTIVE);
3648 efrain 372
                $select->where->equalTo('network_id', $currentUser->network_id);
1 www 373
 
374
                if($search) {
375
                    $select->where->NEST->like('first_name', '%' . $search . '%')->or->like('last_name', '%' . $search . '%')->UNNEST;
376
                }
377
                $select->order('first_name ASC, last_name ASC');
378
 
379
                $records = $queryMapper->fetchAll($select);
380
                $items = [];
381
                foreach($records as $record)
382
                {
383
                    $item = [
384
                        'name' => trim($record['first_name'] . ' ' . $record['last_name']),
385
                        'image' => $this->url()->fromRoute('storage', ['type' => 'user', 'code' => $record['uuid'], 'filename' => $record['image'] ]),
386
                        'link_view' => $this->url()->fromRoute('profile/view', ['id' => $record['uuid']  ]),
387
                        'link_approve' => $this->url()->fromRoute('connection/approve', ['id' => $record['uuid']  ]),
388
                        'link_reject' => $this->url()->fromRoute('connection/reject', ['id' => $record['uuid']  ]),
389
 
390
                    ];
391
 
392
                    array_push($items, $item);
393
                }
394
 
395
                $response = [
396
                    'success' => true,
397
                    'data' => $items
398
                ];
399
 
400
                return new JsonModel($response);
401
            } else {
402
                $notificationMapper = NotificationMapper::getInstance($this->adapter);
403
                $notificationMapper->markAllNotificationsAsReadByTypeAndUserId(Notification::TYPE_RECEIVE_CONNECTION_REQUEST, $currentUser->id);
404
 
405
 
406
                $this->layout()->setTemplate('layout/layout.phtml');
407
                $viewModel = new ViewModel();
408
                $viewModel->setTemplate('leaders-linked/connection/invitations-received.phtml');
409
                return $viewModel ;
410
            }
411
 
412
        } else {
413
            return new JsonModel([
414
                'success' => false,
415
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
416
            ]);
417
        }
418
    }
419
 
420
    public function cancelAction()
421
    {
422
        $request = $this->getRequest();
423
        if($request->isPost()) {
424
            $currentUserPlugin = $this->plugin('currentUserPlugin');
425
            $currentUser = $currentUserPlugin->getUser();
426
 
427
            $id = $this->params()->fromRoute('id');
428
 
429
            $id = $this->params()->fromRoute('id');
430
            $userMapper = UserMapper::getInstance($this->adapter);
3648 efrain 431
            $user = $userMapper->fetchOneByUuidAndNetworkId($id, $currentUser->network_id);
1 www 432
 
433
            if(!$user) {
434
                return new JsonModel([
435
                    'success' => true,
436
                    'data' => 'ERROR_USER_NOT_FOUND'
437
                ]);
438
            }
439
 
440
 
441
 
442
            if($user->id == $currentUser->id || in_array($user->status, [User::STATUS_DELETED, User::STATUS_INACTIVE]) || $user->email_verified == User::EMAIL_VERIFIED_NO || $user->blocked == User::BLOCKED_YES) {
443
                return new JsonModel([
444
                    'success' => false,
445
                    'data' => 'ERROR_USER_UNAVAILABLE'
446
                ]);
447
            }
448
 
449
 
450
 
451
            $connectionMapper = ConnectionMapper::getInstance($this->adapter);
452
            $connection = $connectionMapper->fetchOneByUserId1AndUserId2($currentUser->id, $user->id);
453
            if($connection) {
454
                if($connection->status == Connection::STATUS_ACCEPTED) {
455
                    if($connectionMapper->cancel($connection)) {
456
                        return new JsonModel([
457
                            'success' => true,
458
                            'data' => 'LABEL_CONNECTION_CANCELED'
459
                        ]);
460
                    } else {
461
                        return new JsonModel([
462
                            'success' => true,
463
                            'data' => $connectionMapper->getError()
464
                        ]);
465
                    }
466
                } else {
467
                    return new JsonModel([
468
                        'success' => true,
469
                        'data' => 'LABEL_CONNECTION_CANCELED'
470
                    ]);
471
                }
472
            } else {
473
                return new JsonModel([
474
                    'success' => true,
475
                    'data' => 'LABEL_CONNECTION_NOT_FOUND'
476
                ]);
477
 
478
            }
479
 
480
        } else {
481
            return new JsonModel([
482
                'success' => false,
483
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
484
            ]);
485
        }
486
    }
487
 
488
    public function approveAction()
489
    {
490
        $request = $this->getRequest();
491
        if($request->isPost()) {
492
            $currentUserPlugin = $this->plugin('currentUserPlugin');
493
            $currentUser = $currentUserPlugin->getUser();
494
 
495
            $id = $this->params()->fromRoute('id');
496
            $userMapper = UserMapper::getInstance($this->adapter);
3648 efrain 497
            $user = $userMapper->fetchOneByUuidAndNetworkId($id, $currentUser->network_id);
1 www 498
 
499
            if($user) {
500
 
501
                if($user->id == $currentUser->id || in_array($user->status, [User::STATUS_DELETED, User::STATUS_INACTIVE]) || $user->email_verified == User::EMAIL_VERIFIED_NO || $user->blocked == User::BLOCKED_YES) {
502
                    return new JsonModel([
503
                        'success' => true,
504
                        'data' => 'ERROR_USER_UNAVAILABLE'
505
                    ]);
506
                }
507
 
508
                $connectionMapper = ConnectionMapper::getInstance($this->adapter);
509
                $connection = $connectionMapper->fetchOneByUserId1AndUserId2($currentUser->id, $user->id);
510
                if($connection) {
511
                    if($connection->status == Connection::STATUS_SENT) {
512
                        if($connectionMapper->approve($connection)) {
513
 
514
                            if($currentUser->id == $connection->request_to) {
515
                                $other_user_id = $connection->request_from;
516
                            } else {
517
                                $other_user_id = $connection->request_to;
518
                            }
519
 
520
                            $otherUser = $userMapper->fetchOne($other_user_id);
521
 
522
                            $notification = new Notification();
523
                            $notification->type     = Notification::TYPE_ACCEPT_MY_REQUEST_CONNECTION;
524
                            $notification->read     = Notification::NO;
525
                            $notification->user_id  = $otherUser->id;
526
                            $notification->message  = 'LABEL_NOTIFICATION_ACCEPT_MY_REQUEST_CONNECTION';
527
                            $notification->url      = $this->url()->fromRoute('connection/my-connections');
528
 
529
                            $notificationMapper = NotificationMapper::getInstance($this->adapter);
530
                            $notificationMapper->insert($notification);
531
 
532
                            $userNotificationMapper = UserNotificationSettingMapper::getInstance($this->adapter);
533
                            $userNotification = $userNotificationMapper->fetchOne($otherUser->id);
534
 
535
                            if($userNotification && $userNotification->receive_connection_request)
536
                            {
537
                                $emailTemplateMapper = EmailTemplateMapper::getInstance($this->adapter);
3712 efrain 538
                                $emailTemplate = $emailTemplateMapper->fetchOneByCodeAndNetworkId(EmailTemplate::CODE_ACCEPT_MY_REQUEST_CONNECTION, $currentUser->network_id);
1 www 539
 
540
                                if($emailTemplate) {
541
                                    $arrayCont = [
542
                                        'firstname'             => $currentUser->first_name,
543
                                        'lastname'              => $currentUser->last_name,
544
                                        'other_user_firstname'  => $otherUser->first_name,
545
                                        'other_user_lastname'   => $otherUser->last_name,
546
                                        'company_name'          => '',
547
                                        'group_name'            => '',
548
                                        'content'               => '',
549
                                        'code'                  => '',
550
                                        'link'                  => $this->url()->fromRoute('connection/my-connections', [], ['force_canonical' => true])
551
                                    ];
552
 
553
                                    $email = new QueueEmail($this->adapter);
554
                                    $email->processEmailTemplate($emailTemplate, $arrayCont, $otherUser->email, trim($otherUser->first_name . ' ' . $otherUser->last_name));
555
                                }
556
                            }
557
 
558
                            return new JsonModel([
559
                                'success' => true,
560
                                'data' => 'LABEL_CONNECTION_APPROVED'
561
                            ]);
562
                        } else {
563
                            return new JsonModel([
564
                                'success' => true,
565
                                'data' => $connectionMapper->getError()
566
                            ]);
567
                        }
568
                    } else {
569
                        return new JsonModel([
570
                            'success' => true,
571
                            'data' => 'LABEL_CONNECTION_NOT_PENDING'
572
                        ]);
573
                    }
574
                } else {
575
                    return new JsonModel([
576
                        'success' => true,
577
                        'data' => 'LABEL_CONNECTION_NOT_FOUND'
578
                    ]);
579
 
580
                }
581
            } else {
582
                return new JsonModel([
583
                    'success' => true,
584
                    'data' => 'ERROR_USER_NOT_FOUND'
585
                ]);
586
            }
587
        } else {
588
            return new JsonModel([
589
                'success' => false,
590
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
591
            ]);
592
        }
593
    }
594
 
595
    public function rejectAction()
596
    {
597
        $request = $this->getRequest();
598
        if($request->isPost()) {
599
            $currentUserPlugin = $this->plugin('currentUserPlugin');
600
            $currentUser = $currentUserPlugin->getUser();
601
 
602
            $id = $this->params()->fromRoute('id');
603
            $userMapper = UserMapper::getInstance($this->adapter);
3648 efrain 604
            $user = $userMapper->fetchOneByUuidAndNetworkId($id, $currentUser->network_id);
1 www 605
 
606
            if($user) {
607
 
608
                if($user->id == $currentUser->id || in_array($user->status, [User::STATUS_DELETED, User::STATUS_INACTIVE]) || $user->email_verified == User::EMAIL_VERIFIED_NO || $user->blocked == User::BLOCKED_YES) {
609
                    return new JsonModel([
610
                        'success' => true,
611
                        'data' => 'ERROR_USER_UNAVAILABLE'
612
                    ]);
613
                }
614
 
615
 
616
                $connectionMapper = ConnectionMapper::getInstance($this->adapter);
617
                $connection = $connectionMapper->fetchOneByUserId1AndUserId2($currentUser->id, $user->id);
618
                if($connection) {
619
                    if($connection->status == Connection::STATUS_SENT) {
620
                        if($connectionMapper->reject($connection)) {
621
                            return new JsonModel([
622
                                'success' => true,
623
                                'data' => 'LABEL_CONNECTION_REJECTED'
624
                            ]);
625
                        } else {
626
                            return new JsonModel([
627
                                'success' => true,
628
                                'data' => $connectionMapper->getError()
629
                            ]);
630
                        }
631
                    } else {
632
                        return new JsonModel([
633
                            'success' => true,
634
                            'data' => 'LABEL_CONNECTION_NOT_PENDING'
635
                        ]);
636
                    }
637
                } else {
638
                    return new JsonModel([
639
                        'success' => true,
640
                        'data' => 'LABEL_CONNECTION_NOT_FOUND'
641
                    ]);
642
 
643
                }
644
            } else {
645
                return new JsonModel([
646
                    'success' => false,
647
                    'data' => 'ERROR_USER_NOT_FOUND'
648
                ]);
649
            }
650
        } else {
651
            return new JsonModel([
652
                'success' => false,
653
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
654
            ]);
655
        }
656
    }
657
 
658
    public function requestAction()
659
    {
660
        $request = $this->getRequest();
661
        if($request->isPost()) {
662
            $currentUserPlugin = $this->plugin('currentUserPlugin');
663
            $currentUser = $currentUserPlugin->getUser();
664
 
665
            $id = $this->params()->fromRoute('id');
666
 
3648 efrain 667
 
1 www 668
            $userMapper = UserMapper::getInstance($this->adapter);
3648 efrain 669
            $user = $userMapper->fetchOneByUuidAndNetworkId($id, $currentUser->network_id);
1 www 670
 
671
            if($user) {
672
 
673
                if($user->id == $currentUser->id || in_array($user->status, [User::STATUS_DELETED, User::STATUS_INACTIVE]) || $user->email_verified == User::EMAIL_VERIFIED_NO || $user->blocked == User::BLOCKED_YES) {
674
                    return new JsonModel([
675
                        'success' => true,
676
                        'data' => 'ERROR_USER_UNAVAILABLE'
677
                    ]);
678
                }
679
 
680
 
681
                $send_notification = false;
682
 
683
                $connectionMapper = ConnectionMapper::getInstance($this->adapter);
684
                $connection = $connectionMapper->fetchOneByUserId1AndUserId2($currentUser->id, $user->id);
685
                if(!$connection) {
686
                    $userBlockedMapper = UserBlockedMapper::getInstance($this->adapter);
687
                    $userBlocked = $userBlockedMapper->fetchOneByUserIdAndBlockedId($currentUser->id, $user->id);
688
 
689
                    if(!$userBlocked ) {
690
                        $connection = new Connection();
691
                        $connection->request_from = $currentUser->id;
692
                        $connection->request_to = $user->id;
693
                        $connection->status = Connection::STATUS_SENT;
694
 
695
 
696
                        if($connectionMapper->insert($connection)) {
697
 
698
                            $send_notification = true;
699
 
700
                            $response = [
701
                                'success' => true,
702
                                'data' => 'LABEL_CONNECTION_REQUESTED'
703
                            ];
704
                        } else {
705
                            $response = [
706
                                'success' => true,
707
                                'data' => $connectionMapper->getError()
708
                            ];
709
                        }
710
                    } else {
711
                        $response = [
712
                            'success' => true,
713
                            'data' => 'ERROR_USER_NOT_FOUND'
714
                        ];
715
                    }
716
                } else {
717
 
718
 
719
                    $connection->request_from = $currentUser->id;
720
                    $connection->request_to = $user->id;
721
                    $connection->status = Connection::STATUS_SENT;
722
                    if( $connectionMapper->update($connection)) {
723
                        $send_notification = true;
724
                        $response = [
725
                            'success' => true,
726
                            'data' => 'LABEL_CONNECTION_REQUESTED'
727
                        ];
728
                    } else {
729
                        $response = [
730
                            'success' => true,
731
                            'data' => $connectionMapper->getError()
732
                        ];
733
                    }
734
 
735
 
736
 
737
                }
738
 
739
                if($send_notification) {
740
                    $notification = new Notification();
741
                    $notification->type     = Notification::TYPE_RECEIVE_CONNECTION_REQUEST;
742
                    $notification->read     = Notification::NO;
743
                    $notification->user_id  = $user->id;
744
                    $notification->message  = 'LABEL_NOTIFICATION_RECEIVE_CONNECTION_REQUEST';
745
                    $notification->url      = $this->url()->fromRoute('connection/invitations-received');
746
 
747
                    $notificationMapper = NotificationMapper::getInstance($this->adapter);
748
                    $notificationMapper->insert($notification);
749
 
750
                    $userNotificationMapper = UserNotificationSettingMapper::getInstance($this->adapter);
751
                    $userNotification = $userNotificationMapper->fetchOne($user->id);
752
 
753
                    if($userNotification && $userNotification->receive_connection_request)
754
                    {
755
                        $emailTemplateMapper = EmailTemplateMapper::getInstance($this->adapter);
3712 efrain 756
                        $emailTemplate = $emailTemplateMapper->fetchOneByCodeAndNetworkId(EmailTemplate::CODE_RECEIVE_CONNECTION_REQUEST, $currentUser->network_id);
1 www 757
 
758
                        if($emailTemplate) {
759
                            $arrayCont = [
760
                                'firstname'             => $currentUser->first_name,
761
                                'lastname'              => $currentUser->last_name,
762
                                'other_user_firstname'  => $user->first_name,
763
                                'other_user_lastname'   => $user->last_name,
764
                                'company_name'          => '',
765
                                'group_name'            => '',
766
                                'content'               => '',
767
                                'code'                  => '',
768
                                'link'                  => $this->url()->fromRoute('connection/invitations-received', [], ['force_canonical' => true])
769
                            ];
770
 
771
                            $email = new QueueEmail($this->adapter);
772
                            $email->processEmailTemplate($emailTemplate, $arrayCont, $user->email, trim($user->first_name . ' ' . $user->last_name));
773
                        }
774
                    }
775
                }
776
 
777
                return new JsonModel($response);
778
 
779
            } else {
780
                return new JsonModel([
781
                    'success' => false,
782
                    'data' => 'ERROR_METHOD_NOT_ALLOWED'
783
                ]);
784
            }
785
 
786
 
787
 
788
        }
789
 
790
        return new JsonModel([
791
            'success' => false,
792
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
793
        ]);
794
    }
795
 
796
 
797
    public function deleteAction()
798
    {
799
        $request = $this->getRequest();
800
        if($request->isPost()) {
801
            $currentUserPlugin = $this->plugin('currentUserPlugin');
802
            $currentUser = $currentUserPlugin->getUser();
803
 
804
            $id = $this->params()->fromRoute('id');
805
            $userMapper = UserMapper::getInstance($this->adapter);
3648 efrain 806
            $user = $userMapper->fetchOneByUuidAndNetworkId($id, $currentUser->network_id);
1 www 807
 
808
            if(!$user) {
809
                return new JsonModel([
810
                    'success' => true,
811
                    'data' => 'ERROR_USER_NOT_FOUND'
812
                ]);
813
            }
814
 
815
            if($user->id == $currentUser->id || in_array($user->status, [User::STATUS_DELETED, User::STATUS_INACTIVE]) || $user->email_verified == User::EMAIL_VERIFIED_NO || $user->blocked == User::BLOCKED_YES) {
816
 
817
                return new JsonModel([
818
                    'success' => true,
819
                    'data' => 'ERROR_USER_UNAVAILABLE'
820
                ]);
821
            }
822
 
823
 
824
 
825
            $connectionMapper = ConnectionMapper::getInstance($this->adapter);
826
            $connection = $connectionMapper->fetchOneByUserId1AndUserId2($currentUser->id, $user->id);
827
            if($connection) {
828
                if($connection->status == Connection::STATUS_SENT) {
829
                    if($connectionMapper->delete($connection)) {
830
                        return new JsonModel([
831
                            'success' => true,
832
                            'data' => 'LABEL_CONNECTION_DELETED'
833
                        ]);
834
                    } else {
835
                        return new JsonModel([
836
                            'success' => true,
837
                            'data' => $connectionMapper->getError()
838
                        ]);
839
                    }
840
                } else {
841
                    $connection->status = Connection::STATUS_CANCELLED;
842
                    if($connectionMapper->update($connection)) {
843
                        return new JsonModel([
844
                            'success' => true,
845
                            'data' => 'LABEL_CONNECTION_CANCELLED'
846
                        ]);
847
                    } else {
848
                        return new JsonModel([
849
                            'success' => true,
850
                            'data' => $connectionMapper->getError()
851
                        ]);
852
                    }
853
 
854
 
855
                    return new JsonModel([
856
                        'success' => true,
857
                        'data' => 'LABEL_CONNECTION_NOT_PENDING'
858
                    ]);
859
                }
860
            } else {
861
                return new JsonModel([
862
                    'success' => true,
863
                    'data' => 'LABEL_CONNECTION_NOT_FOUND'
864
                ]);
865
 
866
            }
867
        } else {
868
 
869
            return new JsonModel([
870
                'success' => false,
871
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
872
            ]);
873
        }
874
    }
875
 
876
    public function blockAction()
877
    {
878
        $request = $this->getRequest();
879
        if($request->isPost()) {
880
            $currentUserPlugin = $this->plugin('currentUserPlugin');
881
            $currentUser = $currentUserPlugin->getUser();
882
 
883
            $id = $this->params()->fromRoute('id');
884
            $userMapper = UserMapper::getInstance($this->adapter);
3648 efrain 885
            $user = $userMapper->fetchOneByUuidAndNetworkId($id, $currentUser->network_id);
1 www 886
            if($user) {
887
                if($user->id == $currentUser->id || in_array($user->status, [User::STATUS_DELETED, User::STATUS_INACTIVE]) || $user->email_verified == User::EMAIL_VERIFIED_NO || $user->blocked == User::BLOCKED_YES) {
888
                    return new JsonModel([
889
                        'success' => true,
890
                        'data' => 'ERROR_USER_UNAVAILABLE'
891
                    ]);
892
                }
893
 
894
                $userBlockedMapper = UserBlockedMapper::getInstance($this->adapter);
895
                $userBlocked = $userBlockedMapper->fetchOneByUserIdAndBlockedId($currentUser->id, $user->id);
896
                if($userBlocked) {
897
                    return new JsonModel([
898
                        'success' => false,
899
                        'data' =>  'ERROR_THIS_USER_WAS_ALREADY_BLOCKED'
900
                    ]);
901
                } else {
902
                    $userBlocked = new UserBlocked();
903
                    $userBlocked->user_id = $currentUser->id;
904
                    $userBlocked->blocked_id = $user->id;
905
 
906
                    if($userBlockedMapper->insert($userBlocked)) {
907
                        return new JsonModel([
908
                            'success' => true,
909
                            'data' => 'LABEL_USER_HAS_BEEN_BLOCKED'
910
                        ]);
911
                    } else {
912
                        return new JsonModel([
913
                            'success' => false,
914
                            'data' => $userBlockedMapper->getError()
915
                        ]);
916
                    }
917
                }
918
            } else {
919
                return new JsonModel([
920
                    'success' => false,
921
                    'data' =>  'ERROR_USER_NOT_FOUND'
922
                ]);
923
            }
924
        } else {
925
 
926
            return new JsonModel([
927
                'success' => false,
928
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
929
            ]);
930
        }
931
    }
932
 
933
    public function unblockAction()
934
    {
935
        $request = $this->getRequest();
936
        if($request->isPost()) {
937
            $currentUserPlugin = $this->plugin('currentUserPlugin');
938
            $currentUser = $currentUserPlugin->getUser();
939
 
940
            $id = $this->params()->fromRoute('id');
941
            $userMapper = UserMapper::getInstance($this->adapter);
3648 efrain 942
            $user = $userMapper->fetchOneByUuidAndNetworkId($id, $currentUser->network_id);
1 www 943
 
944
            if($user) {
945
                if($user->id == $currentUser->id || in_array($user->status, [User::STATUS_DELETED, User::STATUS_INACTIVE]) || $user->email_verified == User::EMAIL_VERIFIED_NO || $user->blocked == User::BLOCKED_YES) {
946
                    return new JsonModel([
947
                        'success' => true,
948
                       'data' => 'ERROR_USER_UNAVAILABLE'
949
                    ]);
950
                }
951
 
952
                $userBlockedMapper = UserBlockedMapper::getInstance($this->adapter);
953
                $userBlocked = $userBlockedMapper->fetchOneByUserIdAndBlockedId($currentUser->id, $user->id);
954
                if($userBlocked) {
955
                   if($userBlockedMapper->deleteByUserIdAndBlockedId($currentUser->id, $user->id)) {
956
                       return new JsonModel([
957
                           'success' => true,
958
                           'data' => 'LABEL_USER_HAS_BEEN_UNBLOCKED'
959
                        ]);
960
                    } else {
961
                        return new JsonModel([
962
                            'success' => false,
963
                            'data' => $userBlockedMapper->getError()
964
                        ]);
965
                    }
966
                } else {
967
                    return new JsonModel([
968
                        'success' => false,
969
                        'data' =>  'ERROR_USER_IS_NOT_BLOCKED'
970
                    ]);
971
                }
972
            } else {
973
                return new JsonModel([
974
                    'success' => false,
975
                    'data' =>  'ERROR_USER_NOT_FOUND'
976
                ]);
977
            }
978
 
979
 
980
        } else {
981
 
982
            return new JsonModel([
983
                'success' => false,
984
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
985
            ]);
986
        }
987
    }
988
 
989
 
990
    public function peopleBlockedAction()
991
    {
992
        $currentUserPlugin = $this->plugin('currentUserPlugin');
993
        $currentUser = $currentUserPlugin->getUser();
994
 
995
        $request = $this->getRequest();
996
        if($request->isGet()) {
997
 
998
 
999
            $headers  = $request->getHeaders();
1000
            $isJson = false;
1001
            if($headers->has('Accept')) {
1002
                $accept = $headers->get('Accept');
1003
 
1004
                $prioritized = $accept->getPrioritized();
1005
 
1006
                foreach($prioritized as $key => $value) {
1007
                    $raw = trim($value->getRaw());
1008
 
1009
                    if(!$isJson) {
1010
                        $isJson = strpos($raw, 'json');
1011
                    }
1012
 
1013
                }
1014
            }
1015
 
1016
            if($isJson) {
1017
                $search = trim(filter_var($this->params()->fromQuery('search', ''), FILTER_SANITIZE_STRING));
1018
 
1019
                $userBlockedMapper = UserBlockedMapper::getInstance($this->adapter);
1020
                $user_ids = $userBlockedMapper->fetchAllBlockedReturnIds($currentUser->id);
1021
 
1022
 
1023
                $queryMapper = QueryMapper::getInstance($this->adapter);
1024
 
1025
                $select = $queryMapper->getSql()->select(UserMapper::_TABLE);
1026
                $select->where->in('id', $user_ids);
1027
                $select->where->equalTo('status', User::STATUS_ACTIVE);
3648 efrain 1028
                $select->where->equalTo('network_id', $currentUser->network_id);
1 www 1029
 
1030
                if($search) {
1031
                    $select->where->NEST->like('first_name', '%' . $search . '%')->or->like('last_name', '%' . $search . '%')->UNNEST;
1032
                }
1033
                $select->order('first_name ASC, last_name ASC');
1034
 
1035
                //echo $select->getSqlString($this->adapter->platform); exit;
1036
 
1037
 
1038
                $records = $queryMapper->fetchAll($select);
1039
                $items = [];
1040
                foreach($records as $record)
1041
                {
1042
 
1043
                    $userBlocked = $userBlockedMapper->fetchOneByUserIdAndBlockedId($record['id'], $currentUser->id);
1044
                    $link_view = $userBlocked ? '' : $this->url()->fromRoute('profile/view', ['id' => $record['uuid']  ]);
1045
 
1046
                    $item = [
1047
                        'name' => trim($record['first_name'] . ' ' . $record['last_name']),
1048
                        'image' => $this->url()->fromRoute('storage', ['type' => 'user', 'code' => $record['uuid'], 'filename' => $record['image'] ]),
1049
                        'link_view' => $link_view,
1050
                        'link_unblock' => $this->url()->fromRoute('connection/unblock', ['id' => $record['uuid']  ]),
1051
                    ];
1052
 
1053
                    array_push($items, $item);
1054
                }
1055
 
1056
 
1057
 
1058
                $response = [
1059
                    'success' => true,
1060
                    'data' => $items
1061
                ];
1062
 
1063
                return new JsonModel($response);
1064
            } else {
1065
                $this->layout()->setTemplate('layout/layout.phtml');
1066
                $viewModel = new ViewModel();
1067
                $viewModel->setTemplate('leaders-linked/connection/people-blocked.phtml');
1068
                return $viewModel ;
1069
            }
1070
 
1071
        } else {
1072
            return new JsonModel([
1073
                'success' => false,
1074
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
1075
            ]);
1076
        }
1077
    }
1078
 
1079
 
1080
    public function peopleYouMayKnowAction()
1081
    {
1082
 
1083
        $currentUserPlugin = $this->plugin('currentUserPlugin');
1084
        $currentUser = $currentUserPlugin->getUser();
1085
 
1086
        $request = $this->getRequest();
1087
        if($request->isGet()) {
1088
 
1089
 
1090
            $headers  = $request->getHeaders();
1091
            $isJson = false;
1092
            if($headers->has('Accept')) {
1093
                $accept = $headers->get('Accept');
1094
 
1095
                $prioritized = $accept->getPrioritized();
1096
 
1097
                foreach($prioritized as $key => $value) {
1098
                    $raw = trim($value->getRaw());
1099
 
1100
                    if(!$isJson) {
1101
                        $isJson = strpos($raw, 'json');
1102
                    }
1103
 
1104
                }
1105
            }
1106
 
1107
            if($isJson) {
1108
                $page = (int) $this->params()->fromQuery('page');
1109
                $search = trim(filter_var($this->params()->fromQuery('search', ''), FILTER_SANITIZE_STRING));
1110
 
1111
 
1112
                $queryMapper = QueryMapper::getInstance($this->adapter);
1113
                /*
1114
                $select = $queryMapper->getSql()->select(UserExperienceMapper::_TABLE);
1115
                $select->columns(['industry_id' => new Expression('DISTINCT(industry_id)')]);
1116
                $select->where->equalTo('user_id', $currentUser->id);
1117
 
1118
 
1119
                $industry_ids = [];
1120
                $records = $queryMapper->fetchAll($select);
1121
                foreach($records as $record)
1122
                {
1123
                    array_push($industry_ids, $record['industry_id']);
1124
                }*/
1125
 
1126
                $connectionMapper = ConnectionMapper::getInstance($this->adapter);
1127
                $first_degree_connections_ids = $connectionMapper->fetchAllConnectionsByUserReturnIds($currentUser->id);
1128
                $first_degree_connections_ids = $first_degree_connections_ids ? $first_degree_connections_ids : [0];
1129
 
1130
 
1131
 
1132
                $second_degree_connections_ids = $connectionMapper->fetchAllSecondDegreeConnectionsForUserIdReturnIds($currentUser->id);
1133
                $second_degree_connections_ids = $second_degree_connections_ids ? $second_degree_connections_ids : [0];
1134
 
1135
 
1136
                /*Usuarios de la empresas donde trabajo o soy dueño */
1137
                $company_user_ids = [];
1138
                $companyUserMapper = CompanyUserMapper::getInstance($this->adapter);
1139
 
1140
                $records = $companyUserMapper->fetchAllByUserId($currentUser->id);
1141
                foreach($records as $record)
1142
                {
1143
 
1144
                    if($record->status != CompanyUser::STATUS_ACCEPTED) {
1145
                        continue;
1146
                    }
1147
 
1148
                    $otherUsers = $companyUserMapper->fetchAllByCompanyId($record->company_id);
1149
                    foreach($otherUsers as $otherUser)
1150
                    {
1151
                        if($currentUser->id != $otherUser->user_id && $otherUser->creator == CompanyUser::CREATOR_NO && $otherUser->status == CompanyUser::STATUS_ACCEPTED) {
1152
 
1153
                            if(!in_array($otherUser->user_id, $company_user_ids)) {
1154
                                array_push($company_user_ids, $otherUser->user_id);
1155
                            }
1156
                        }
1157
                    }
1158
                }
1159
                $company_user_ids =  $company_user_ids ? $company_user_ids : [0];
1160
 
1161
                /* Usuario de los grupos donde soy dueño o participo */
1162
 
1163
                $groupMemberMapper = GroupMemberMapper::getInstance($this->adapter);
1164
 
1165
                $group_member_ids = [];
1166
 
1167
                $records = $groupMemberMapper->fetchAllByUserId($currentUser->id);
1168
                foreach($records as $record)
1169
                {
1170
                    if($record->status != GroupMember::STATUS_ACCEPTED) {
1171
                        continue;
1172
                    }
1173
 
1174
                    $otherUsers = $groupMemberMapper->fetchAllByGroupId($record->group_id);
1175
                    foreach($otherUsers as $otherUser)
1176
                    {
1177
                        if($currentUser->id != $otherUser->user_id && $otherUser->status == GroupMember::STATUS_ACCEPTED) {
1178
 
1179
                            if(!in_array($otherUser->user_id, $group_member_ids)) {
1180
                                array_push($group_member_ids, $otherUser->user_id);
1181
                            }
1182
                        }
1183
                    }
1184
 
1185
 
1186
                }
1187
 
1188
                $group_member_ids = $group_member_ids ? $group_member_ids : [0];
1189
 
1190
                /* Usuarios con que comparto capsulas */
1191
                $capsule_user_ids = [];
1192
                $capsuleUserMapper = CompanyMicrolearningCapsuleUserMapper::getInstance($this->adapter);
1193
 
1194
                $company_ids = [];
1195
                $records = $capsuleUserMapper->fetchAllActiveByUserId($currentUser->id);
1196
                foreach($records as $record)
1197
                {
1198
                    if(!in_array($record->company_id,$company_ids)) {
1199
                        array_push($company_ids, $record->company_id);
1200
                    }
1201
                }
1202
 
1203
                foreach($company_ids as $company_id)
1204
                {
1205
                    $otherUsers = $capsuleUserMapper->fetchAllUserIdsForCapsulesActiveByCompanyId($company_id);
1206
                    foreach($otherUsers as $user_id)
1207
                    {
1208
                        if($currentUser->id != $user_id ) {
1209
 
1210
                            if(!in_array($user_id, $capsule_user_ids)) {
1211
                                array_push($capsule_user_ids, $user_id);
1212
                            }
1213
                        }
1214
                    }
1215
                }
1216
 
1217
                $capsule_user_ids = $capsule_user_ids ? $capsule_user_ids : [0];
1218
 
1219
 
1220
                $other_users = array_unique(array_merge(
1221
                    $second_degree_connections_ids,
1222
                    $company_user_ids,
1223
                    $group_member_ids,
1224
                    $capsule_user_ids
1225
                ));
1226
 
1227
                $queryMapper = QueryMapper::getInstance($this->adapter);
1228
                $select = $queryMapper->getSql()->select();
1229
                $select->columns(['id', 'uuid',  'first_name','last_name', 'image']);
1230
                $select->from(['u' => UserMapper::_TABLE]);
1231
                $select->where->in('u.id', $other_users);
1232
                $select->where->notIn('u.id', $first_degree_connections_ids);
1233
                $select->where->notEqualTo('u.id', $currentUser->id);
1234
                $select->where->equalTo('u.status', User::STATUS_ACTIVE);
1235
                $select->where->in('u.usertype_id', [UserType::ADMIN, UserType::USER]);
3648 efrain 1236
                $select->where->equalTo('u.network_id', $currentUser->network_id);
1 www 1237
 
1238
 
1239
 
1240
                if($search) {
1241
                    $select->where->NEST->like('first_name', '%' . $search . '%')->or->like('last_name', '%' . $search . '%')->UNNEST;
1242
                }
1243
 
1244
                $select->order(['first_name','last_name']);
1245
 
1246
 
1247
                //$select->order(new Expression("rand()"));
1248
 
1249
 
1250
                //echo $select->getSqlString($this->adapter->platform); exit;
1251
 
1252
 
1253
 
1254
                $dbSelect = new DbSelect($select, $this->adapter);
1255
                $paginator = new Paginator($dbSelect);
1256
                $paginator->setCurrentPageNumber($page ? $page : 1);
1257
                $paginator->setItemCountPerPage(12);
1258
 
1259
 
1260
                $items = [];
1261
                $records = $paginator->getCurrentItems();
1262
 
1263
                foreach($records as $record)
1264
                {
1265
                    $connection = $connectionMapper->fetchOneByUserId1AndUserId2($currentUser->id, $record['id']);
1266
 
1267
 
1268
 
1269
                    $relation = [];
1270
                    if(in_array($record['id'], $second_degree_connections_ids)) {
1271
                        array_push($relation, 'LABEL_RELATION_TYPE_SECOND_GRADE');
1272
                    }
1273
                    if(in_array($record['id'], $company_user_ids)) {
1274
                        array_push($relation, 'LABEL_RELATION_TYPE_COMPANY_USER');
1275
                    }
1276
                    if(in_array($record['id'], $group_member_ids)) {
1277
                        array_push($relation, 'LABEL_RELATION_TYPE_GROUP_MEMBER');
1278
                    }
1279
                    if(in_array($record['id'], $capsule_user_ids)) {
1280
                        array_push($relation, 'LABEL_RELATION_TYPE_CAPSULE_USER');
1281
                    }
1282
 
1283
                    $item = [
1284
                        'name' => trim($record['first_name'] . ' ' . $record['last_name']),
1285
                        'image' => $this->url()->fromRoute('storage', ['type' => 'user', 'code' => $record['uuid'], 'filename' => $record['image'] ]),
1286
                        'link_view' => $this->url()->fromRoute('profile/view', ['id' => $record['uuid']  ]),
1287
                        'link_inmail'   => $this->url()->fromRoute('inmail', ['id' => $record['uuid']  ]),
1288
                        'relation' => $relation,
1289
                        'link_cancel'   => '',
1290
                        'link_request'  => '',
1291
 
1292
                    ];
1293
 
1294
                    if($connection) {
1295
                        switch($connection->status)
1296
                        {
1297
                            case Connection::STATUS_SENT :
1298
                                $item['link_cancel'] = $this->url()->fromRoute('connection/delete',['id' => $record['uuid'] ]);
1299
                                break;
1300
 
1301
                            case Connection::STATUS_ACCEPTED :
1302
                                $item['link_cancel'] = $this->url()->fromRoute('connection/cancel',['id' => $record['uuid'] ]);
1303
                                break;
1304
 
1305
                            default :
1306
                                $item['link_request'] = $this->url()->fromRoute('connection/request',['id' => $record['uuid'] ]);
1307
                                break;
1308
 
1309
                        }
1310
 
1311
 
1312
                    } else {
1313
                        $item['link_request'] = $this->url()->fromRoute('connection/request',['id' => $record['uuid'] ]);
1314
                    }
1315
 
1316
                    array_push($items, $item);
1317
                }
1318
 
1319
                $response = [
1320
                    'success' => true,
1321
                    'data' => [
1322
                        'total' => [
1323
                            'count' => $paginator->getTotalItemCount(),
1324
                            'pages' => $paginator->getPages()->pageCount,
1325
                        ],
1326
                        'current' => [
1327
                            'items'    => $items,
1328
                            'page'     => $paginator->getCurrentPageNumber(),
1329
                        'count'    => $paginator->getCurrentItemCount(),
1330
                        ]
1331
                    ]
1332
                ];
1333
 
1334
 
1335
 
1336
 
1337
                return new JsonModel($response);
1338
            } else {
1339
                $this->layout()->setTemplate('layout/layout.phtml');
1340
                $viewModel = new ViewModel();
1341
                $viewModel->setTemplate('leaders-linked/connection/people-you-may-know.phtml');
1342
                return $viewModel ;
1343
            }
1344
 
1345
        } else {
1346
            return new JsonModel([
1347
                'success' => false,
1348
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
1349
            ]);
1350
        }
1351
    }
1352
}