Proyectos de Subversion LeadersLinked - Services

Rev

Rev 60 | Ir a la última revisión | | Ultima modificación | Ver Log |

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