Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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