Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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

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