Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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