Proyectos de Subversion LeadersLinked - Services

Rev

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