Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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

Rev Autor Línea Nro. Línea
1 www 1
<?php
2
declare(strict_types=1);
3
 
4
namespace LeadersLinked\Controller;
5
 
6
use Laminas\Db\Adapter\AdapterInterface;
7
use Laminas\Cache\Storage\Adapter\AbstractAdapter;
8
use Laminas\Mvc\Controller\AbstractActionController;
9
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
10
use Laminas\Log\LoggerInterface;
11
use Laminas\View\Model\ViewModel;
12
use Laminas\View\Model\JsonModel;
13
use LeadersLinked\Mapper\CompanyFollowerMapper;
14
use LeadersLinked\Mapper\QueryMapper;
15
use LeadersLinked\Mapper\UserMapper;
16
use LeadersLinked\Library\Functions;
17
use LeadersLinked\Mapper\UserPasswordMapper;
18
use LeadersLinked\Form\AccountSetting\NotificationSettingForm;
19
use LeadersLinked\Mapper\UserNotificationSettingMapper;
20
 
21
use LeadersLinked\Form\AccountSetting\ChangePasswordForm;
22
use LeadersLinked\Form\AccountSetting\ChangeImageForm;
23
use LeadersLinked\Library\Image;
24
use LeadersLinked\Form\AccountSetting\LocationForm;
25
use LeadersLinked\Model\Location;
26
use LeadersLinked\Model\User;
27
use LeadersLinked\Mapper\LocationMapper;
28
use LeadersLinked\Form\AccountSetting\PrivacySettingForm;
29
use LeadersLinked\Mapper\UserProfileMapper;
30
use LeadersLinked\Form\AccountSetting\BasicForm;
31
use LeadersLinked\Mapper\ConnectionMapper;
32
use LeadersLinked\Mapper\ProfileVisitMapper;
33
use LeadersLinked\Mapper\GroupMemberMapper;
34
use LeadersLinked\Model\GroupMember;
35
use LeadersLinked\Mapper\UserExperienceMapper;
36
use LeadersLinked\Model\UserExperience;
37
use LeadersLinked\Mapper\GroupMapper;
38
use LeadersLinked\Model\Group;
39
use Laminas\Db\Sql\Expression;
40
use LeadersLinked\Mapper\CompanyUserMapper;
41
use LeadersLinked\Model\CompanyUser;
42
use LeadersLinked\Model\UserType;
43
use LeadersLinked\Mapper\CompanyMicrolearningCapsuleUserMapper;
44
use LeadersLinked\Model\Notification;
45
use LeadersLinked\Mapper\NotificationMapper;
46
use LeadersLinked\Mapper\EmailTemplateMapper;
47
use LeadersLinked\Model\EmailTemplate;
48
use LeadersLinked\Library\QueueEmail;
49
use LeadersLinked\Mapper\PostMapper;
50
use LeadersLinked\Mapper\CompanyMapper;
51
use LeadersLinked\Model\Company;
52
use LeadersLinked\Model\Connection;
2444 efrain 53
use Laminas\Navigation\Navigation;
1 www 54
 
55
class HelperController extends AbstractActionController
56
{
57
    /**
58
     *
59
     * @var AdapterInterface
60
     */
61
    private $adapter;
62
 
63
 
64
    /**
65
     *
66
     * @var AbstractAdapter
67
     */
68
    private $cache;
69
 
70
    /**
71
     *
72
     * @var  LoggerInterface
73
     */
74
    private $logger;
75
 
76
    /**
77
     *
78
     * @var array
79
     */
80
    private $config;
81
 
82
 
2444 efrain 83
    /**
84
     *
85
     * @var array
86
     */
87
    private $navigation;
1 www 88
 
89
    /**
90
     *
91
     * @param AdapterInterface $adapter
92
     * @param AbstractAdapter $cache
93
     * @param LoggerInterface $logger
94
     * @param array $config
2444 efrain 95
     * @param array $navigation
1 www 96
     */
2444 efrain 97
    public function __construct($adapter, $cache , $logger, $config, $navigation)
1 www 98
    {
99
        $this->adapter      = $adapter;
100
        $this->cache        = $cache;
101
        $this->logger       = $logger;
102
        $this->config       = $config;
2444 efrain 103
        $this->navigation   = $navigation;
1 www 104
    }
105
 
106
 
107
    /**
108
     * Recuperamos las personas que pueda conocer
109
     * tiene que enviarse un petición GET a la siguiente url: /helpers/people-you-may-know
110
     * retorna un json en caso de ser  positivo
111
     * [
112
     *  'success' : true,
113
     *  'data' : [
114
     *      [
115
     *        'id'      => 'id del usuario encriptado',
116
     *        'name'    => 'nombre del usuario',
117
     *        'image'   => 'imagen del usuario',
118
     *        'profile' => 'url del profile',
119
     *     ]
120
     * ]
121
     * En caso de ser negativo
122
     * [
123
     *  'success' : false,
124
     *  'data' : mensaje de error
125
     * ]
126
     * @return \Laminas\View\Model\JsonModel
127
     */
128
    public function peopleYouMayKnowAction()
129
    {
130
        $request = $this->getRequest();
131
        if($request->isGet()) {
132
 
133
            $currentUserPlugin = $this->plugin('currentUserPlugin');
134
            $currentUser = $currentUserPlugin->getUser();
135
 
136
            $connectionMapper = ConnectionMapper::getInstance($this->adapter);
137
            $first_degree_connections_ids = $connectionMapper->fetchAllConnectionsByUserReturnIds($currentUser->id);
138
            $first_degree_connections_ids = $first_degree_connections_ids ? $first_degree_connections_ids : [0];
139
 
140
 
141
 
142
            $second_degree_connections_ids = $connectionMapper->fetchAllSecondDegreeConnectionsForUserIdReturnIds($currentUser->id);
143
            $second_degree_connections_ids = $second_degree_connections_ids ? $second_degree_connections_ids : [0];
144
 
145
 
146
 
147
 
148
            /*Usuarios de la empresas donde trabajo o soy dueño */
149
            $company_user_ids = [];
150
            $companyUserMapper = CompanyUserMapper::getInstance($this->adapter);
151
 
152
            $records = $companyUserMapper->fetchAllByUserId($currentUser->id);
153
            foreach($records as $record)
154
            {
155
 
156
                if($record->status != CompanyUser::STATUS_ACCEPTED) {
157
                    continue;
158
                }
159
 
160
                $otherUsers = $companyUserMapper->fetchAllByCompanyId($record->company_id);
161
                foreach($otherUsers as $otherUser)
162
                {
163
                    if($currentUser->id != $otherUser->user_id && $otherUser->creator == CompanyUser::CREATOR_NO && $otherUser->status == CompanyUser::STATUS_ACCEPTED) {
164
 
165
                        if(!in_array($otherUser->user_id, $company_user_ids)) {
166
                            array_push($company_user_ids, $otherUser->user_id);
167
                        }
168
                    }
169
                }
170
            }
171
            $company_user_ids =  $company_user_ids ? $company_user_ids : [0];
172
 
173
            /* Usuario de los grupos donde soy dueño o participo */
174
 
175
            $groupMemberMapper = GroupMemberMapper::getInstance($this->adapter);
176
 
177
            $group_member_ids = [];
178
 
179
            $records = $groupMemberMapper->fetchAllByUserId($currentUser->id);
180
            foreach($records as $record)
181
            {
182
                if($record->status != GroupMember::STATUS_ACCEPTED) {
183
                    continue;
184
                }
185
 
186
                $otherUsers = $groupMemberMapper->fetchAllByGroupId($record->group_id);
187
                foreach($otherUsers as $otherUser)
188
                {
189
                    if($currentUser->id != $otherUser->user_id && $otherUser->status == GroupMember::STATUS_ACCEPTED) {
190
 
191
                        if(!in_array($otherUser->user_id, $group_member_ids)) {
192
                            array_push($group_member_ids, $otherUser->user_id);
193
                        }
194
                    }
195
                }
196
 
197
 
198
            }
199
 
200
            $group_member_ids = $group_member_ids ? $group_member_ids : [0];
201
 
202
 
203
 
204
            /* Usuarios con que comparto capsulas */
205
            $capsule_user_ids = [];
206
            $capsuleUserMapper = CompanyMicrolearningCapsuleUserMapper::getInstance($this->adapter);
207
 
208
            $company_ids = [];
209
            $records = $capsuleUserMapper->fetchAllActiveByUserId($currentUser->id);
210
            foreach($records as $record)
211
            {
212
                if(!in_array($record->company_id,$company_ids)) {
213
                    array_push($company_ids, $record->company_id);
214
                }
215
            }
216
 
217
 
218
 
219
            foreach($company_ids as $company_id)
220
            {
221
                $otherUsers = $capsuleUserMapper->fetchAllUserIdsForCapsulesActiveByCompanyId($company_id);
222
                foreach($otherUsers as $user_id)
223
                {
224
                    if($currentUser->id != $user_id ) {
225
 
226
                        if(!in_array($user_id, $capsule_user_ids)) {
227
                            array_push($capsule_user_ids, $user_id);
228
                        }
229
                    }
230
                }
231
            }
232
 
233
            $capsule_user_ids = $capsule_user_ids ? $capsule_user_ids : [0];
234
 
235
 
236
            $other_users = array_unique(array_merge(
237
                $second_degree_connections_ids,
238
                $company_user_ids,
239
                $group_member_ids,
240
                $capsule_user_ids
241
            ));
242
 
243
 
244
 
245
 
246
 
247
 
248
 
249
 
250
            $items = [];
251
            $queryMapper = QueryMapper::getInstance($this->adapter);
252
            $select = $queryMapper->getSql()->select();
253
            $select->columns(['id', 'uuid',  'first_name','last_name', 'image']);
254
            $select->from(['u' => UserMapper::_TABLE]);
255
            $select->where->in('u.id', $other_users);
256
            $select->where->notIn('u.id', $first_degree_connections_ids);
257
            $select->where->notEqualTo('u.id', $currentUser->id);
258
            $select->where->equalTo('u.status', User::STATUS_ACTIVE);
259
            $select->where->in('u.usertype_id', [UserType::ADMIN, UserType::USER]);
260
            $select->order(['first_name','last_name']);
261
 
262
                //echo $select->getSqlString($this->adapter->platform); exit;
263
 
264
            $records = $queryMapper->fetchAll($select);
265
            foreach($records as $record)
266
            {
267
 
268
                $relation = [];
269
                if(in_array($record['id'], $second_degree_connections_ids)) {
270
                    array_push($relation, 'LABEL_RELATION_TYPE_SECOND_GRADE');
271
                }
272
                if(in_array($record['id'], $company_user_ids)) {
273
                    array_push($relation, 'LABEL_RELATION_TYPE_COMPANY_USER');
274
                }
275
                if(in_array($record['id'], $group_member_ids)) {
276
                    array_push($relation, 'LABEL_RELATION_TYPE_GROUP_MEMBER');
277
                }
278
                if(in_array($record['id'], $capsule_user_ids)) {
279
                    array_push($relation, 'LABEL_RELATION_TYPE_CAPSULE_USER');
280
                }
281
 
282
 
283
                $connection = $connectionMapper->fetchOneByUserId1AndUserId2($currentUser->id, $record['id']);
284
 
285
                $item = [
286
                    'id'    => $record['uuid'],
287
                    'name'  => trim($record['first_name'] . ' ' . $record['last_name']),
288
                    'image' => $this->url()->fromRoute('storage', ['code' => $record['uuid'], 'type' => 'user', 'filename' => $record['image']]),
289
                    'profile'   => $this->url()->fromRoute('profile/view', ['id' => $record['uuid'] ]),
290
                    'relation' => $relation,
291
                    'link_cancel'   => '',
292
                    'link_request'  => '',
293
                ];
294
 
295
                if($connection) {
296
                    switch($connection->status)
297
                    {
298
                        case Connection::STATUS_SENT :
299
                            $item['link_cancel'] = $this->url()->fromRoute('connection/delete',['id' => $record['uuid'] ]);
300
                            break;
301
 
302
                        case Connection::STATUS_ACCEPTED :
303
                            $item['link_cancel'] = $this->url()->fromRoute('connection/cancel',['id' => $record['uuid'] ]);
304
                            break;
305
 
306
                        default :
307
                            $item['link_request'] = $this->url()->fromRoute('connection/request',['id' => $record['uuid'] ]);
308
                            break;
309
 
310
                    }
311
 
312
 
313
                } else {
314
                    $item['link_request'] = $this->url()->fromRoute('connection/request',['id' => $record['uuid'] ]);
315
                }
316
 
317
 
318
                array_push($items, $item);
319
            }
320
 
321
            return new JsonModel([
322
                'success' => true,
323
                'data' => $items
324
            ]);
325
 
326
 
327
        } else {
328
            return new JsonModel([
329
                'success' => false,
330
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
331
            ]);
332
        }
333
 
334
    }
335
 
336
    /**
337
     * Recuperamos las personas que pueda conocer
338
     * tiene que enviarse un petición GET a la siguiente url: /helpers/people-viewed-profile/:user_profile_id
339
     * retorna un json en caso de ser  positivo
340
     * [
341
     *  'success' : true,
342
     *  'data' : [
343
     *      [
344
     *        'id'      => 'id del usuario encriptado',
345
     *        'name'    => 'nombre del usuario',
346
     *        'image'   => 'imagen del usuario',
347
     *        'profile' => 'url del profile',
348
     *     ]
349
     * ]
350
     * En caso de ser negativo
351
     * [
352
     *  'success' : false,
353
     *  'data' : mensaje de error
354
     * ]
355
     * @return \Laminas\View\Model\JsonModel
356
     */
357
    public function peopleViewedProfileAction()
358
    {
359
        $request = $this->getRequest();
360
        if($request->isGet()) {
361
 
362
            $items = [];
363
            $user_profile_id = $this->params()->fromRoute('user_profile_id');
364
 
365
            $items = [];
366
 
367
            $mapper = QueryMapper::getInstance($this->adapter);
368
            $select = $mapper->getSql()->select(ProfileVisitMapper::_TABLE);
369
            $select->columns(['user_id' => new Expression('DISTINCT(visitor_id)')]);
370
            $select->where->equalTo('user_profile_id', $user_profile_id);
371
            $records = $mapper->fetchAll($select);
372
 
373
            if($records) {
374
 
375
                $user_ids = [];
376
                foreach($records as $record)
377
                {
378
                    array_push($user_ids, $record['user_id']);
379
                }
380
 
381
                $mapper = QueryMapper::getInstance($this->adapter);
382
                $select = $mapper->getSql()->select( UserMapper::_TABLE);
383
                $select->columns(['id', 'uuid', 'first_name', 'last_name', 'image']);
384
                $select->where->in('id', $user_ids);
385
                $select->where->equalTo('status',User::STATUS_ACTIVE);
386
                $select->order(['last_name ASC', 'first_name ASC']);
387
 
388
                $records = $mapper->fetchAll($select);
389
                foreach($records as $record)
390
                {
391
                    array_push($items, [
392
                        'id'        => $record['uuid'],
393
                        'name'      => trim($record['first_name'] . ' ' . $record['last_name']),
394
                        'image'     => $this->url()->fromRoute('storage', ['code' => $record['uuid'], 'type' => 'user', 'filename' => $record['image']]),
395
                        'profile'   => $this->url()->fromRoute('profile/view', ['id' => $record['uuid'] ]),
396
                    ]);
397
                }
398
            }
399
 
400
 
401
            return new JsonModel([
402
                'success' => true,
403
                'data' => $items
404
            ]);
405
 
406
        } else {
407
            return new JsonModel([
408
                'success' => false,
409
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
410
            ]);
411
        }
412
    }
413
 
414
 
415
    /**
416
     * Recuperamos los seguidores de la empresa
417
     * tiene que enviarse un petición GET a la siguiente url: /helpers/company-follower/:company_id
418
     * retorna un json en caso de ser  positivo
419
     * [
420
     *  'success' : true,
421
     *  'data' : [
422
     *      [
423
     *        'id'      => 'id del usuario encriptado',
424
     *        'name'    => 'nombre del usuario',
425
     *        'image'   => 'imagen del usuario',
426
     *        'profile' => 'url del profile',
427
     *     ]
428
     * ]
429
     * En caso de ser negativo
430
     * [
431
     *  'success' : false,
432
     *  'data' : mensaje de error
433
     * ]
434
     * @return \Laminas\View\Model\JsonModel
435
     */
436
    public function companyFollowerAction()
437
    {
438
 
439
 
440
        $request = $this->getRequest();
441
        if($request->isGet()) {
442
 
443
            $company_uuid  = $this->params()->fromRoute('company_id');
444
 
445
            $companyMapper = CompanyMapper::getInstance($this->adapter);
446
            $company = $companyMapper->fetchOneByUuid($company_uuid);
447
 
448
            $items = [];
449
            if($company && $company->status == Company::STATUS_ACTIVE) {
450
 
451
 
452
                //print_r($company);
453
 
454
                $companyFollowerMapper = CompanyFollowerMapper::getInstance($this->adapter);
455
                $records = $companyFollowerMapper->fetchAllByCompanyId($company->id);
456
 
457
                $ids = [];
458
                foreach($records as $record)
459
                {
460
                    if(!in_array($record->follower_id, $ids)) {
461
                        array_push($ids, $record->follower_id);
462
                    }
463
                }
464
 
465
                //print_r($records);
466
 
467
 
468
                if($ids) {
469
 
470
                    $mapper = QueryMapper::getInstance($this->adapter);
471
                    $select = $mapper->getSql()->select(UserMapper::_TABLE);
472
                    $select->columns(['id', 'uuid', 'first_name', 'last_name', 'image']);
473
                    $select->where->in('id',$ids);
474
                    $select->where->equalTo('status',User::STATUS_ACTIVE);
475
                    $select->order(['last_name','first_name']);
476
 
477
                    //echo $select->getSqlString($this->adapter->platform); exit;
478
 
479
 
480
 
481
                    $records = $mapper->fetchAll($select);
482
                    foreach($records as $record)
483
                    {
484
 
485
 
486
                        array_push($items, [
487
                            'id'        => $record['uuid'],
488
                            'name'      => trim($record['first_name'] . ' ' . $record['last_name']),
489
                            'image'     => $this->url()->fromRoute('storage', ['code' => $record['uuid'], 'type' => 'user', 'filename' => $record['image']]),
490
                            'profile'   => $this->url()->fromRoute('profile/view', ['id' => $record['uuid'] ]),
491
                        ]);
492
                    }
493
                }
494
            }
495
 
496
            return new JsonModel([
497
                'success' => true,
498
                'data' => $items
499
            ]);
500
 
501
        } else {
502
            return new JsonModel([
503
                'success' => false,
504
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
505
            ]);
506
        }
507
    }
508
 
509
    public function companySuggestionAction()
510
    {
511
        $request = $this->getRequest();
512
        if($request->isGet()) {
513
 
514
            $company_id = $this->params()->fromRoute('company_id');
515
 
516
            $companyMapper = CompanyMapper::getInstance($this->adapter);
517
            $company = $companyMapper->fetchOneByUuid($company_id);
518
 
519
            $items = [];
520
            if($company && $company->status == Company::STATUS_ACTIVE) {
521
 
522
 
523
                $mapper = QueryMapper::getInstance($this->adapter);
524
                $select = $mapper->getSql()->select(CompanyMapper::_TABLE);
525
                $select->columns(['id', 'uuid', 'name', 'image']);
526
                $select->where->notEqualTo('id', $company->id);
527
                $select->where->equalTo('status',Company::STATUS_ACTIVE);
528
                $select->where->equalTo('industry_id', $company->industry_id);
529
              //  $select->where->equalTo('company_size_id', $company->company_size_id);
530
                $select->order(['name']);
531
 
532
 
533
                //echo $select->getSqlString($this->adapter->platform); exit;
534
 
535
                $records = $mapper->fetchAll($select);
536
                foreach($records as $record)
537
                {
538
                    array_push($items, [
539
                        'id'        => $record['uuid'],
540
                        'name'      => trim($record['name']),
541
                        'image'     => $this->url()->fromRoute('storage', ['code' => $record['uuid'], 'type' => 'company', 'filename' => $record['image']]),
542
                        'profile'   => $this->url()->fromRoute('company/view', ['id' => $record['uuid'] ]),
543
                    ]);
544
                }
545
            }
546
 
547
            return new JsonModel([
548
                'success' => true,
549
                'data' => $items
550
            ]);
551
 
552
        } else {
553
            return new JsonModel([
554
                'success' => false,
555
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
556
            ]);
557
        }
558
    }
559
 
560
    /**
561
     * Recuperamos los miembros del grupo
562
     * tiene que enviarse un petición GET a la siguiente url: /helpers/group-members/:group_id
563
     * retorna un json en caso de ser  positivo
564
     * [
565
     *  'success' : true,
566
     *  'data' : [
567
     *      [
568
     *        'id'      => 'id del usuario encriptado',
569
     *        'name'    => 'nombre del usuario',
570
     *        'image'   => 'imagen del usuario',
571
     *        'profile' => 'url del profile',
572
     *     ]
573
     * ]
574
     * En caso de ser negativo
575
     * [
576
     *  'success' : false,
577
     *  'data' : mensaje de error
578
     * ]
579
     * @return \Laminas\View\Model\JsonModel
580
     */
581
    public function groupMembersAction()
582
    {
583
        $currentUserPlugin = $this->plugin('currentUserPlugin');
584
        $currentUser = $currentUserPlugin->getUser();
585
 
586
        $request = $this->getRequest();
587
        if($request->isGet()) {
588
 
589
            $group_uuid = $this->params()->fromRoute('group_id');
590
 
591
            $groupMapper = GroupMapper::getInstance($this->adapter);
592
            $group = $groupMapper->fetchOneByUuid($group_uuid);
593
 
594
            $items = [];
595
            if($group && $group->status == Group::STATUS_ACTIVE) {
596
 
597
                $mapper = QueryMapper::getInstance($this->adapter);
598
                $select = $mapper->getSql()->select();
599
                $select->columns(['id', 'uuid', 'first_name', 'last_name', 'image']);
600
                $select->from(['u' => UserMapper::_TABLE]);
601
                $select->join(['gm' => GroupMemberMapper::_TABLE], 'gm.user_id = u.id ', ['user_id', 'status']);
602
                $select->join(['g' => GroupMapper::_TABLE], 'gm.group_id = g.id', ['group_uuid' => 'uuid']  );
603
                $select->where->equalTo('g.uuid', $group_uuid);
604
 
605
                if($group->user_id == $currentUser->id) {
606
                    $select->where->in('gm.status', [
607
                        GroupMember::STATUS_ACCEPTED,
608
                        GroupMember::STATUS_ADDED_BY_ADMIN,
609
                        GroupMember::STATUS_AUTO_JOIN,
610
                        GroupMember::STATUS_JOINING_REQUESTED,
611
                    ]);
612
                } else {
613
                    $select->where->in('gm.status', [GroupMember::STATUS_ACCEPTED, GroupMember::STATUS_AUTO_JOIN]);
614
                }
615
 
616
 
617
                $select->where->equalTo('u.status', User::STATUS_ACTIVE);
618
                $select->order(['last_name', 'first_name']);
619
 
620
                //echo $select->getSqlString($this->adapter->platform);
621
 
622
 
623
 
624
 
625
                $records = $mapper->fetchAll($select);
626
                foreach($records as $record)
627
                {
628
 
629
                    $actions = [];
630
                    if($group->user_id == $currentUser->id) {
631
                        if($record['id'] != $currentUser->id) {
632
 
633
 
634
 
635
                            switch($record['status'])
636
                            {
637
                                case GroupMember::STATUS_JOINING_REQUESTED :
638
                                    $actions['link_approve'] = $this->url()->fromRoute('helpers/group-members/approve', ['group_id' => $group->uuid, 'user_id' => $record['uuid']]);
639
                                    $actions['link_reject'] = $this->url()->fromRoute('helpers/group-members/reject', ['group_id' => $group->uuid, 'user_id' => $record['uuid']]);
640
                                    break;
641
 
642
                                case GroupMember::STATUS_ACCEPTED :
643
                                case GroupMember::STATUS_AUTO_JOIN :
644
                                case GroupMember::STATUS_ADDED_BY_ADMIN :
645
                                    $actions['link_cancel'] = $this->url()->fromRoute('helpers/group-members/cancel', ['group_id' => $group->uuid, 'user_id' => $record['uuid']]);
646
                                    break;
647
                            }
648
                        }
649
 
650
 
651
 
652
                    }
653
 
654
                    array_push($items, [
655
                        'id'        => $record['uuid'],
656
                        'name'      => trim($record['first_name'] . ' ' . $record['last_name']),
657
                        'image'     => $this->url()->fromRoute('storage', ['code' => $record['uuid'], 'type' => 'user', 'filename' => $record['image']]),
658
                        'profile'   => $this->url()->fromRoute('profile/view', ['id' => $record['uuid']]),
659
                        'actions'   => $actions,
660
 
661
                    ]);
662
                }
663
            }
664
 
665
            return new JsonModel([
666
                'success' => true,
667
                'data' => [
668
                    'items' => $items,
669
                    'link_invite' => $group->user_id == $currentUser->id ? $this->url()->fromRoute('helpers/group-members/invite',['group_id' => $group->uuid])  : '',
670
                ]
671
            ]);
672
 
673
        } else {
674
            return new JsonModel([
675
                'success' => false,
676
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
677
            ]);
678
        }
679
    }
680
 
681
    public function groupMemberInviteAction()
682
    {
683
 
684
 
685
        $currentUserPlugin = $this->plugin('currentUserPlugin');
686
        $currentUser = $currentUserPlugin->getUser();
687
 
688
        $group_uuid = $this->params()->fromRoute('group_id');
689
 
690
        $groupMapper = GroupMapper::getInstance($this->adapter);
691
        $group = $groupMapper->fetchOneByUuid($group_uuid);
692
 
693
        if(!$group) {
694
            return new JsonModel([
695
                'success' => false,
696
                'data' => 'ERROR_GROUP_NOT_FOUND'
697
            ]);
698
        }
699
 
700
        if($group->status != Group::STATUS_ACTIVE) {
701
            return new JsonModel([
702
                'success' => false,
703
                'data' => 'ERROR_GROUP_IS_NOT_ACTIVE'
704
            ]);
705
        }
706
 
707
        if($currentUser->id != $group->user_id) {
708
            return new JsonModel([
709
                'success' => false,
710
                'data' => 'ERROR_GROUP_IS_NOT_YOU_ARE_THE_OWNER_OF_THIS_GROUP'
711
            ]);
712
        }
713
 
714
 
715
        $request = $this->getRequest();
716
        if($request->isGet()) {
717
            $search = filter_var($this->params()->fromQuery('search', ''));
718
            if(strlen($search) >= 3) {
719
 
720
                $userMapper = UserMapper::getInstance($this->adapter);
721
                $records  = $userMapper->fetchAllSuggestForInvitationByGroupId($group->id, $search);
722
 
723
                $users = [];
724
                foreach($records as $record)
725
                {
726
                    array_push($users, [
727
                        'value' => $record->uuid,
728
                        'text' => trim($record->first_name . ' ' . $record->last_name) . ' (' . $record->email . ')'
729
 
730
                    ]);
731
                }
732
 
733
                return new JsonModel([
734
                    'success' => true,
735
                    'data' => $users
736
                ]);
737
 
738
 
739
 
740
 
741
            } else {
742
                return new JsonModel([
743
                    'success' => true,
744
                    'data' => [
745
 
746
                    ]
747
                ]);
748
            }
749
 
750
 
751
 
752
        }
753
        else if($request->isPost()) {
754
            $uuid = $this->params()->fromPost('id');
755
            if(!$uuid) {
756
                return new JsonModel([
757
                    'success'   => false,
758
                    'data'      => 'ERROR_INVALID_PARAMETER'
759
                ]);
760
            }
761
 
762
            $userMapper = UserMapper::getInstance($this->adapter);
763
            $user = $userMapper->fetchOneByUuid($uuid);
764
 
765
            if(!$user) {
766
                return new JsonModel([
767
                    'success'   => false,
768
                    'data'      => 'ERROR_USER_NOT_FOUND'
769
                ]);
770
            }
771
 
772
            if($user->status != User::STATUS_ACTIVE) {
773
                return new JsonModel([
774
                    'success'   => false,
775
                    'data'      => 'ERROR_USER_IS_INACTIVE'
776
                ]);
777
            }
778
 
779
            if($group->user_id == $user->id) {
780
                return new JsonModel([
781
                    'success'   => false,
782
                    'data'      => 'ERROR_GROUP_YOU_ARE_THE_OWNER_OF_THIS_GROUP'
783
                ]);
784
            }
785
 
786
            $groupMemberMapper = GroupMemberMapper::getInstance($this->adapter);
787
            $groupMember = $groupMemberMapper->fetchOneByGroupIdAndUserId($group->id, $user->id);
788
 
789
 
790
 
791
            if($groupMember) {
792
                $result = false;
793
 
794
                switch($groupMember->status)
795
                {
796
                    case GroupMember::STATUS_ACCEPTED:
797
                    case GroupMember::STATUS_AUTO_JOIN:
798
 
799
                        return new JsonModel([
800
                            'success'   => false,
801
                            'data'      => 'ERROR_GROUP_YOU_ARE_MEMBER',
802
                        ]);
803
                        break;
804
 
805
 
806
                    case $groupMember->status == GroupMember::STATUS_REJECTED :
807
                    case $groupMember->status == GroupMember::STATUS_CANCELLED :
808
 
809
                        $groupMember->status = GroupMember::STATUS_ADDED_BY_ADMIN;
810
                        $groupMember->joining_request_on = date('H-m-d H:i:s');
811
 
812
                        $result = $groupMemberMapper->update($groupMember);
813
                        break;
814
 
815
                    case GroupMember::STATUS_ADDED_BY_ADMIN :
816
                    case  GroupMember::STATUS_JOINING_REQUESTED :
817
                        return new JsonModel([
818
                            'success'   => false,
819
                            'data'      => 'ERROR_GROUP_THERE_IS_A_PENDING_REQUEST'
820
                        ]);
821
                        break;
822
 
823
                    default :
824
                        return new JsonModel([
825
                            'success'   => false,
826
                            'data'      => 'ERROR_UNKNOWN_OPERATION'
827
                        ]);
828
                        break;
829
 
830
                }
831
 
832
 
833
 
834
 
835
 
836
 
837
            } else {
838
 
839
 
840
 
841
                $groupMember = new GroupMember();
842
                $groupMember->user_id = $user->id;
843
                $groupMember->group_id = $group->id;
844
                $groupMember->status = GroupMember::STATUS_ADDED_BY_ADMIN;
845
                $groupMember->joining_request_on = date('H-m-d H:i:s');
846
 
847
                $result = $groupMemberMapper->insert($groupMember);
848
 
849
 
850
            }
851
 
852
            if($result) {
853
 
854
                $notification = new Notification();
855
                $notification->type     = Notification::TYPE_RECEIVE_INVITATION_GROUP;
856
                $notification->read     = Notification::NO;
857
                $notification->user_id  = $user->id;
858
                $notification->group_id = $group->id;
859
                $notification->message  = 'LABEL_NOTIFICATION_RECEIVE_INVITATION_GROUP';
860
                $notification->url      = $this->url()->fromRoute('group/view',['id' => $group->uuid]);
861
 
862
                $notificationMapper = NotificationMapper::getInstance($this->adapter);
863
                $notificationMapper->insert($notification);
864
 
865
                $userNotificationMapper = UserNotificationSettingMapper::getInstance($this->adapter);
866
                $userNotification = $userNotificationMapper->fetchOne($user->id);
867
 
868
                if($userNotification && $userNotification->receive_invitation_group)
869
                {
870
                    $emailTemplateMapper = EmailTemplateMapper::getInstance($this->adapter);
871
                    $emailTemplate = $emailTemplateMapper->fetchOne(EmailTemplate::ID_RECEIVE_INVITATION_GROUP);
872
 
873
                    if($emailTemplate) {
874
                        $arrayCont = [
875
                            'firstname'             => $currentUser->first_name,
876
                            'lastname'              => $currentUser->last_name,
877
                            'other_user_firstname'  => $user->first_name,
878
                            'other_user_lastname'   => $user->last_name,
879
                            'company_name'          => '',
880
                            'group_name'            => $group->name,
881
                            'content'               => '',
882
                            'code'                  => '',
883
                            'link'                  => $this->url()->fromRoute('group/view', ['id' => $group->uuid], ['force_canonical' => true])
884
                        ];
885
 
886
                        $email = new QueueEmail($this->adapter);
887
                        $email->processEmailTemplate($emailTemplate, $arrayCont, $user->email, trim($user->first_name . ' ' . $user->last_name));
888
                    }
889
                }
890
 
891
                return new JsonModel([
892
                    'success'   => true,
893
                    'data'      => 'LABEL_GROUP_REQUEST_SUCCESS'
894
                ]);
895
 
896
            } else {
897
 
898
                return new JsonModel([
899
                    'success'   => false,
900
                    'data'      => 'ERROR_GROUP_REQUEST_COULD_NOT_BE_SENT'
901
                ]);
902
 
903
            }
904
 
905
 
906
 
907
        } else {
908
            return new JsonModel([
909
                'success' => false,
910
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
911
            ]);
912
        }
913
    }
914
 
915
    public function  groupMemberCancelAction()
916
    {
917
        $currentUserPlugin = $this->plugin('currentUserPlugin');
918
        $currentUser = $currentUserPlugin->getUser();
919
 
920
        $request = $this->getRequest();
921
        if($request->isPost()) {
922
            $group_uuid = $this->params()->fromRoute('group_id');
923
            $user_uuid  = $this->params()->fromRoute('user_id');
924
 
925
            $groupMapper = GroupMapper::getInstance($this->adapter);
926
            $group = $groupMapper->fetchOneByUuid($group_uuid);
927
 
928
            if(!$group) {
929
                return new JsonModel([
930
                    'success' => false,
931
                    'data' => 'ERROR_GROUP_NOT_FOUND'
932
                ]);
933
            }
934
 
935
            if($group->status != Group::STATUS_ACTIVE) {
936
                return new JsonModel([
937
                    'success' => false,
938
                    'data' => 'ERROR_GROUP_IS_NOT_ACTIVE'
939
                ]);
940
            }
941
 
942
            if($currentUser->id != $group->user_id) {
943
                return new JsonModel([
944
                    'success' => false,
945
                    'data' => 'ERROR_GROUP_IS_NOT_YOU_ARE_THE_OWNER_OF_THIS_GROUP'
946
                ]);
947
            }
948
 
949
            $userMapper = UserMapper::getInstance($this->adapter);
950
            $user = $userMapper->fetchOneByUuid($user_uuid);
951
 
952
            if(!$user) {
953
                return new JsonModel([
954
                    'success' => false,
955
                    'data' => 'ERROR_USER_NOT_FOUND'
956
                ]);
957
            }
958
 
959
            if($user->id == $currentUser->id) {
960
                return new JsonModel([
961
                    'success' => false,
962
                    'data' => 'ERROR_GROUP_YOU_ARE_THE_OWNER_OF_THIS_GROUP'
963
                ]);
964
            }
965
 
966
 
967
 
968
            $groupMemberMapper = GroupMemberMapper::getInstance($this->adapter);
969
            $groupMember = $groupMemberMapper->fetchOneByGroupIdAndUserId($group->id, $user->id);
970
            if($groupMember) {
971
 
972
                if($groupMember->status == GroupMember::STATUS_ACCEPTED ||
973
                    $groupMember->status == GroupMember::STATUS_ADDED_BY_ADMIN ||
974
                    $groupMember->status == GroupMember::STATUS_AUTO_JOIN) {
975
 
976
                    $groupMember->status = GroupMember::STATUS_CANCELLED;
977
                    if($groupMemberMapper->update($groupMember)) {
978
 
979
                        return new JsonModel([
980
                            'success' => true,
981
                            'data' =>  'LABEL_GROUP_MEMBER_CANCELLED_SUCCESS'
982
                        ]);
983
 
984
                    } else {
985
                        return new JsonModel([
986
                            'success' => true,
987
                            'data' =>    'LABEL_GROUP_MEMBER_CANCELLED_FAILED'
988
                        ]);
989
                    }
990
                }
991
 
992
            }
993
 
994
 
995
            return new JsonModel([
996
                'success' => false,
997
                'data' => 'ERROR_GROUP_REQUEST_OR_MEMBER_NOT_FOUND_TO_CANCEL'
998
            ]);
999
 
1000
 
1001
 
1002
 
1003
 
1004
        } else {
1005
 
1006
            return new JsonModel([
1007
                'success' => false,
1008
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
1009
            ]);
1010
        }
1011
    }
1012
 
1013
    public function  groupMemberRejectAction()
1014
    {
1015
        $currentUserPlugin = $this->plugin('currentUserPlugin');
1016
        $currentUser = $currentUserPlugin->getUser();
1017
 
1018
        $request = $this->getRequest();
1019
        if($request->isPost()) {
1020
            $group_uuid = $this->params()->fromRoute('group_id');
1021
            $user_uuid  = $this->params()->fromRoute('user_id');
1022
 
1023
            $groupMapper = GroupMapper::getInstance($this->adapter);
1024
            $group = $groupMapper->fetchOneByUuid($group_uuid);
1025
 
1026
            if(!$group) {
1027
                return new JsonModel([
1028
                    'success' => false,
1029
                    'data' => 'ERROR_GROUP_NOT_FOUND'
1030
                ]);
1031
            }
1032
 
1033
            if($group->status != Group::STATUS_ACTIVE) {
1034
                return new JsonModel([
1035
                    'success' => false,
1036
                    'data' => 'ERROR_GROUP_IS_NOT_ACTIVE'
1037
                ]);
1038
            }
1039
 
1040
            if($currentUser->id != $group->user_id) {
1041
                return new JsonModel([
1042
                    'success' => false,
1043
                    'data' => 'ERROR_GROUP_IS_NOT_YOU_ARE_THE_OWNER_OF_THIS_GROUP'
1044
                ]);
1045
            }
1046
 
1047
            $userMapper = UserMapper::getInstance($this->adapter);
1048
            $user = $userMapper->fetchOneByUuid($user_uuid);
1049
 
1050
            if(!$user) {
1051
                return new JsonModel([
1052
                    'success' => false,
1053
                    'data' => 'ERROR_USER_NOT_FOUND'
1054
                ]);
1055
            }
1056
 
1057
            if($user->id == $currentUser->id) {
1058
                return new JsonModel([
1059
                    'success' => false,
1060
                    'data' => 'ERROR_GROUP_YOU_ARE_THE_OWNER_OF_THIS_GROUP'
1061
                ]);
1062
            }
1063
 
1064
 
1065
 
1066
            $groupMemberMapper = GroupMemberMapper::getInstance($this->adapter);
1067
            $groupMember = $groupMemberMapper->fetchOneByGroupIdAndUserId($group->id, $user->id);
1068
            if($groupMember) {
1069
 
1070
                if($groupMember->status == GroupMember::STATUS_JOINING_REQUESTED) {
1071
 
1072
                        $groupMember->status = GroupMember::STATUS_REJECTED;
1073
                        if($groupMemberMapper->update($groupMember)) {
1074
 
1075
                            return new JsonModel([
1076
                                'success' => true,
1077
                                'data' =>  'LABEL_GROUP_MEMBER_REJECTED_SUCCESS'
1078
                            ]);
1079
 
1080
                        } else {
1081
                            return new JsonModel([
1082
                                'success' => true,
1083
                                'data' =>    'LABEL_GROUP_MEMBER_REJECTED_FAILED'
1084
                            ]);
1085
                        }
1086
                    }
1087
 
1088
            }
1089
 
1090
 
1091
            return new JsonModel([
1092
                'success' => false,
1093
                'data' => 'ERROR_GROUP_THERE_IS_NO_PENDING_REQUEST_TO_REJECT'
1094
            ]);
1095
 
1096
 
1097
 
1098
 
1099
 
1100
        } else {
1101
 
1102
            return new JsonModel([
1103
                'success' => false,
1104
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
1105
            ]);
1106
        }
1107
    }
1108
 
1109
    public function  groupMemberApproveAction()
1110
    {
1111
        $currentUserPlugin = $this->plugin('currentUserPlugin');
1112
        $currentUser = $currentUserPlugin->getUser();
1113
 
1114
        $request = $this->getRequest();
1115
        if($request->isPost()) {
1116
            $group_uuid = $this->params()->fromRoute('group_id');
1117
            $user_uuid  = $this->params()->fromRoute('user_id');
1118
 
1119
            $groupMapper = GroupMapper::getInstance($this->adapter);
1120
            $group = $groupMapper->fetchOneByUuid($group_uuid);
1121
 
1122
            if(!$group) {
1123
                return new JsonModel([
1124
                    'success' => false,
1125
                    'data' => 'ERROR_GROUP_NOT_FOUND'
1126
                ]);
1127
            }
1128
 
1129
            if($group->status != Group::STATUS_ACTIVE) {
1130
                return new JsonModel([
1131
                    'success' => false,
1132
                    'data' => 'ERROR_GROUP_IS_NOT_ACTIVE'
1133
                ]);
1134
            }
1135
 
1136
            if($currentUser->id != $group->user_id) {
1137
                return new JsonModel([
1138
                    'success' => false,
1139
                    'data' => 'ERROR_GROUP_IS_NOT_YOU_ARE_THE_OWNER_OF_THIS_GROUP'
1140
                ]);
1141
            }
1142
 
1143
            $userMapper = UserMapper::getInstance($this->adapter);
1144
            $user = $userMapper->fetchOneByUuid($user_uuid);
1145
 
1146
            if(!$user) {
1147
                return new JsonModel([
1148
                    'success' => false,
1149
                    'data' => 'ERROR_USER_NOT_FOUND'
1150
                ]);
1151
            }
1152
 
1153
            if($user->id == $currentUser->id) {
1154
                return new JsonModel([
1155
                    'success' => false,
1156
                    'data' => 'ERROR_GROUP_YOU_ARE_THE_OWNER_OF_THIS_GROUP'
1157
                ]);
1158
            }
1159
 
1160
 
1161
 
1162
            $groupMemberMapper = GroupMemberMapper::getInstance($this->adapter);
1163
            $groupMember = $groupMemberMapper->fetchOneByGroupIdAndUserId($group->id, $user->id);
1164
            if($groupMember) {
1165
 
1166
                if($groupMember->status == GroupMember::STATUS_JOINING_REQUESTED) {
1167
 
1168
                    $groupMember->status = GroupMember::STATUS_ACCEPTED;
1169
                    if($groupMemberMapper->update($groupMember)) {
1170
 
1171
 
1172
                        $notification = new Notification();
1173
                        $notification->type     = Notification::TYPE_ACCEPT_MY_REQUEST_JOIN_GROUP;
1174
                        $notification->read     = Notification::NO;
1175
                        $notification->user_id  = $user->id;
1176
                        $notification->group_id = $group->id;
1177
                        $notification->message  = 'LABEL_NOTIFICATION_ACCEPT_MY_REQUEST_JOIN_GROUP';
1178
                        $notification->url      = $this->url()->fromRoute('group/view', ['id' => $group->uuid]);
1179
 
1180
                        $notificationMapper = NotificationMapper::getInstance($this->adapter);
1181
                        $notificationMapper->insert($notification);
1182
 
1183
                        $userNotificationMapper = UserNotificationSettingMapper::getInstance($this->adapter);
1184
                        $userNotification = $userNotificationMapper->fetchOne($user->id);
1185
 
1186
                        if($userNotification && $userNotification->receive_invitation_group)
1187
                        {
1188
                            $emailTemplateMapper = EmailTemplateMapper::getInstance($this->adapter);
1189
                            $emailTemplate = $emailTemplateMapper->fetchOne(EmailTemplate::ID_ACCEPT_MY_REQUEST_JOIN_GROUP);
1190
 
1191
                            if($emailTemplate) {
1192
                                $arrayCont = [
1193
                                    'firstname'             => $currentUser->first_name,
1194
                                    'lastname'              => $currentUser->last_name,
1195
                                    'other_user_firstname'  => $user->first_name,
1196
                                    'other_user_lastname'   => $user->last_name,
1197
                                    'company_name'          => '',
1198
                                    'group_name'            => $group->name,
1199
                                    'content'               => '',
1200
                                    'code'                  => '',
1201
                                    'link'                  => $this->url()->fromRoute('group/view', ['id' => $group->uuid], ['force_canonical' => true])
1202
                                ];
1203
 
1204
                                $email = new QueueEmail($this->adapter);
1205
                                $email->processEmailTemplate($emailTemplate, $arrayCont, $user->email, trim($user->first_name . ' ' . $user->last_name));
1206
                            }
1207
                        }
1208
 
1209
                        return new JsonModel([
1210
                            'success' => true,
1211
                            'data' =>  'LABEL_GROUP_MEMBER_APPROVED_SUCCESS'
1212
                        ]);
1213
 
1214
                    } else {
1215
                        return new JsonModel([
1216
                            'success' => true,
1217
                            'data' =>    'LABEL_GROUP_MEMBER_APPROVED_FAILED'
1218
                        ]);
1219
                    }
1220
                }
1221
 
1222
            }
1223
 
1224
 
1225
            return new JsonModel([
1226
                'success' => false,
1227
                'data' => 'ERROR_GROUP_THERE_IS_NO_PENDING_REQUEST_TO_APPROVED'
1228
            ]);
1229
 
1230
 
1231
 
1232
 
1233
 
1234
        } else {
1235
 
1236
            return new JsonModel([
1237
                'success' => false,
1238
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
1239
            ]);
1240
        }
1241
    }
1242
 
1243
 
1244
    /**
1245
     * Recuperamos los grupos sugeridos
1246
     * tiene que enviarse un petición GET a la siguiente url: /helpers/groups-suggestion/:group_id
1247
     * retorna un json en caso de ser  positivo
1248
     * [
1249
     *  'success' : true,
1250
     *  'data' : [
1251
     *      [
1252
     *        'id'      => 'id del grupo encriptado',
1253
     *        'name'    => 'nombre del grupo',
1254
     *        'image'   => 'imagen del grupo',
1255
     *        'profile' => 'url del profile',
1256
     *     ]
1257
     * ]
1258
     * En caso de ser negativo
1259
     * [
1260
     *  'success' : false,
1261
     *  'data' : mensaje de error
1262
     * ]
1263
     * @return \Laminas\View\Model\JsonModel
1264
     */
1265
    public function groupsSuggestionAction()
1266
    {
1267
 
1268
        $request = $this->getRequest();
1269
        if($request->isGet()) {
1270
 
1271
            $currentUserPlugin = $this->plugin('currentUserPlugin');
1272
            $currentUser = $currentUserPlugin->getUser();
1273
 
3298 efrain 1274
 
3285 efrain 1275
 
1 www 1276
            $connectionMapper = ConnectionMapper::getInstance($this->adapter);
1277
            $first_degree_connections_ids = $connectionMapper->fetchAllConnectionsByUserReturnIds($currentUser->id);
1278
            $first_degree_connections_ids = $first_degree_connections_ids ? $first_degree_connections_ids : [0];
1279
 
1280
 
1281
 
1282
            $second_degree_connections_ids = $connectionMapper->fetchAllSecondDegreeConnectionsForUserIdReturnIds($currentUser->id);
1283
            $second_degree_connections_ids = $second_degree_connections_ids ? $second_degree_connections_ids : [0];
1284
 
1285
 
1286
            /*Usuarios de la empresas donde trabajo o soy dueño */
1287
            $company_user_ids = [];
1288
            $companyUserMapper = CompanyUserMapper::getInstance($this->adapter);
1289
 
1290
            $records = $companyUserMapper->fetchAllByUserId($currentUser->id);
1291
            foreach($records as $record)
1292
            {
1293
 
1294
                if($record->status != CompanyUser::STATUS_ACCEPTED) {
1295
                    continue;
1296
                }
1297
 
1298
                $otherUsers = $companyUserMapper->fetchAllByCompanyId($record->company_id);
1299
                foreach($otherUsers as $otherUser)
1300
                {
1301
                    if($currentUser->id != $otherUser->user_id && $otherUser->creator == CompanyUser::CREATOR_NO && $otherUser->status == CompanyUser::STATUS_ACCEPTED) {
1302
 
1303
                        if(!in_array($otherUser->user_id, $company_user_ids)) {
1304
                            array_push($company_user_ids, $otherUser->user_id);
1305
                        }
1306
                    }
1307
                }
1308
            }
1309
            $company_user_ids =  $company_user_ids ? $company_user_ids : [0];
1310
 
1311
            /* Usuario de los grupos donde soy dueño o participo */
1312
 
1313
            $groupMemberMapper = GroupMemberMapper::getInstance($this->adapter);
1314
 
1315
            $group_member_ids = [];
1316
 
1317
            $records = $groupMemberMapper->fetchAllByUserId($currentUser->id);
1318
            foreach($records as $record)
1319
            {
1320
                if($record->status != GroupMember::STATUS_ACCEPTED) {
1321
                    continue;
1322
                }
1323
 
1324
                $otherUsers = $groupMemberMapper->fetchAllByGroupId($record->group_id);
1325
                foreach($otherUsers as $otherUser)
1326
                {
1327
                    if($currentUser->id != $otherUser->user_id && $otherUser->status == GroupMember::STATUS_ACCEPTED) {
1328
 
1329
                        if(!in_array($otherUser->user_id, $group_member_ids)) {
1330
                            array_push($group_member_ids, $otherUser->user_id);
1331
                        }
1332
                    }
1333
                }
1334
 
1335
 
1336
            }
1337
 
1338
            $group_member_ids = $group_member_ids ? $group_member_ids : [0];
1339
 
1340
            /* Usuarios con que comparto capsulas */
1341
            $capsule_user_ids = [];
1342
            $capsuleUserMapper = CompanyMicrolearningCapsuleUserMapper::getInstance($this->adapter);
1343
 
1344
            $company_ids = [];
1345
            $records = $capsuleUserMapper->fetchAllActiveByUserId($currentUser->id);
1346
            foreach($records as $record)
1347
            {
1348
                if(!in_array($record->company_id,$company_ids)) {
1349
                    array_push($company_ids, $record->company_id);
1350
                }
1351
            }
1352
 
1353
            foreach($company_ids as $company_id)
1354
            {
1355
                $otherUsers = $capsuleUserMapper->fetchAllUserIdsForCapsulesActiveByCompanyId($company_id);
1356
                foreach($otherUsers as $user_id)
1357
                {
1358
                    if($currentUser->id != $user_id ) {
1359
 
1360
                        if(!in_array($user_id, $capsule_user_ids)) {
1361
                            array_push($capsule_user_ids, $user_id);
1362
                        }
1363
                    }
1364
                }
1365
            }
1366
 
1367
            $capsule_user_ids = $capsule_user_ids ? $capsule_user_ids : [0];
1368
 
3285 efrain 1369
 
1 www 1370
            $other_users = array_unique(array_merge(
3285 efrain 1371
 
1 www 1372
                $second_degree_connections_ids,
1373
                $company_user_ids,
1374
                $group_member_ids,
1375
                $capsule_user_ids
1376
            ));
1377
 
3285 efrain 1378
            $queryMapper = QueryMapper::getInstance($this->adapter);
1379
 
1 www 1380
 
1381
            $groupMemberMapper = GroupMemberMapper::getInstance($this->adapter);
1382
            $group_ids = $groupMemberMapper->fetchAllGroupIdsByUserIds( $other_users);
1383
            $group_ids = $group_ids ? $group_ids : [0];
3285 efrain 1384
 
1 www 1385
 
1386
 
3285 efrain 1387
 
1 www 1388
            $select = $queryMapper->getSql()->select();
3285 efrain 1389
            $select->columns(['id', 'uuid', 'name','image','status','privacy', 'priority' => new Expression('0') ]);
1 www 1390
            $select->from(['g' => GroupMapper::_TABLE]);
1391
            $select->where->equalTo('privacy', Group::PRIVACY_IS_PUBLIC);
1392
            $select->where->equalTo('status', Group::STATUS_ACTIVE);
3285 efrain 1393
            $select->where->in('g.id', $group_ids);
1 www 1394
            $select->where->notEqualTo('g.user_id', $currentUser->id);
3285 efrain 1395
            $select->order('name ASC');
1 www 1396
 
3285 efrain 1397
 
1398
 
1 www 1399
            //echo $select->getSqlString($this->adapter->platform); exit;
3285 efrain 1400
 
1401
            $records = $queryMapper->fetchAll($select);
1402
            usort($records, function ($a, $b) {
1403
                if( $a['priority'] == $b['priority'] ) {
1404
                    return 0;
1405
                } else {
1406
                    return $a['priority'] < $b['priority'] ? 1 : -1;
1407
                }
1408
            });
1409
 
1410
 
1 www 1411
            $items = [];
3285 efrain 1412
 
1 www 1413
            foreach($records as $record)
1414
            {
1415
 
1416
                array_push($items, [
1417
                    'id'        => $record['uuid'],
1418
                    'name'      => trim($record['name']),
1419
                    'image'     => $this->url()->fromRoute('storage', ['code' => $record['uuid'], 'type' => 'group', 'filename' => $record['image']]),
1420
                    'profile'   => $this->url()->fromRoute('group/view', ['id' => $record['uuid'] ]),
3285 efrain 1421
                    'priority'  => $record['priority'],
1422
 
1 www 1423
                ]);
1424
 
1425
            }
1426
 
1427
            return new JsonModel([
1428
                'success' => true,
1429
                'data' => $items
1430
            ]);
1431
 
1432
 
1433
        } else {
1434
            return new JsonModel([
1435
                'success' => false,
1436
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
1437
            ]);
1438
        }
1439
    }
1440
 
1441
    public function postsAction()
1442
    {
1443
        $request = $this->getRequest();
1444
        if($request->isGet()) {
1445
            $items = [];
1446
            $postMapper = PostMapper::getInstance($this->adapter);
1447
            $posts = $postMapper->fetchAllActives();
1448
 
1449
            //print_r($posts);
1450
 
1451
            foreach($posts as $post)
1452
            {
1453
                $dt = \DateTime::createFromFormat('Y-m-d', $post->date);
1454
                array_push($items, [
2919 efrain 1455
                    'image' => $this->url()->fromRoute('storage', ['code' => $post->uuid, 'type' => 'post', 'filename' => $post->image  ]),
1456
                    'date' => $dt->format('d/m/Y'),
1 www 1457
                   'title' => $post->title,
1458
                   'link' => $this->url()->fromRoute('post', ['id' => $post->uuid]),
1459
                ]);
1460
 
1461
            }
1462
 
1463
            return new JsonModel([
1464
                'success' => true,
1465
                'data' => $items
1466
            ]);
1467
        }
1468
 
1469
        return new JsonModel([
1470
            'success' => false,
1471
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
1472
        ]);
1473
    }
1474
 
1475
 
1476
 
1477
    public function searchPeopleAction()
1478
    {
1479
        $request = $this->getRequest();
1480
        if($request->isGet()) {
1481
 
1482
 
1483
            $currentUserPlugin = $this->plugin('currentUserPlugin');
1484
            $currentUser = $currentUserPlugin->getUser();
1485
 
1486
            $search = trim(filter_var( $this->params()->fromQuery('search'), FILTER_SANITIZE_STRING)) ;
1487
            if(strlen($search) >= 3) {
1488
 
1489
 
1490
                $userMapper = UserMapper::getInstance($this->adapter);
1491
                $records  = $userMapper->fetchAllSuggest($search);
1492
 
1493
                $users = [];
1494
                foreach($records as $record)
1495
                {
1496
                    if($currentUser->id == $record->id) {
1497
                        continue;
1498
                    }
1499
 
1500
 
1501
                    array_push($users, [
1502
                        'value' => $record->uuid,
1503
                        'text' => trim($record->first_name . ' ' . $record->last_name) . ' (' . $record->email . ')'
1504
 
1505
                    ]);
1506
                }
1507
 
1508
                return new JsonModel([
1509
                    'success' => true,
1510
                    'data' => $users
1511
                ]);
1512
 
1513
 
1514
 
1515
 
1516
            } else {
1517
                return new JsonModel([
1518
                    'success' => true,
1519
                    'data' => [
1520
 
1521
                    ]
1522
                ]);
1523
            }
1524
 
1525
 
1526
 
1527
 
1528
        } else {
1529
            return new JsonModel([
1530
                'success' => false,
1531
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
1532
            ]);
1533
        }
1534
    }
1535
 
2444 efrain 1536
    public function footerAction()
1537
    {
1538
        $request = $this->getRequest();
1539
        if($request->isGet()) {
1540
 
1541
 
1542
            $links = isset($this->navigation['footer']) ?  $this->navigation['footer'] : [];
1543
 
1544
 
1545
            $data = [];
1546
            foreach($links as $link)
1547
            {
1548
                $data[ $link['route'] ] = $link['label'];
1549
            }
1550
 
1551
 
1552
            return new JsonModel([
1553
               'success' => true,
1554
               'data' => $data,
1555
            ]);
1556
 
1557
        } else {
1558
            return new JsonModel([
1559
                'success' => false,
1560
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
1561
            ]);
1562
        }
1563
    }
1564
 
3298 efrain 1565
 
1566
    /**
1567
     * Recuperamos los grupos sugeridos
1568
     * tiene que enviarse un petición GET a la siguiente url: /helpers/groups-suggestion/:group_id
1569
     * retorna un json en caso de ser  positivo
1570
     * [
1571
     *  'success' : true,
1572
     *  'data' : [
1573
     *      [
1574
     *        'id'      => 'id del grupo encriptado',
1575
     *        'name'    => 'nombre del grupo',
1576
     *        'image'   => 'imagen del grupo',
1577
     *        'profile' => 'url del profile',
1578
     *     ]
1579
     * ]
1580
     * En caso de ser negativo
1581
     * [
1582
     *  'success' : false,
1583
     *  'data' : mensaje de error
1584
     * ]
1585
     * @return \Laminas\View\Model\JsonModel
1586
     */
1587
    public function myGroupsAction()
1588
    {
1589
 
1590
        $request = $this->getRequest();
1591
        if($request->isGet()) {
1592
 
1593
            $currentUserPlugin = $this->plugin('currentUserPlugin');
1594
            $currentUser = $currentUserPlugin->getUser();
1595
 
1596
 
1597
            $queryMapper = QueryMapper::getInstance($this->adapter);
1598
            $select = $queryMapper->getSql()->select();
1599
            $select->columns(['id', 'uuid', 'name','image','status','privacy', 'priority' => new Expression('0') ]);
1600
            $select->from(['g' => GroupMapper::_TABLE]);
1601
            $select->where->equalTo('status', Group::STATUS_ACTIVE);
1602
            $select->where->equalTo('g.user_id', $currentUser->id);
1603
            $select->order('name ASC');
1604
 
1605
 
1606
            $items = [];
1607
 
1608
            $records = $queryMapper->fetchAll($select);
1609
            foreach($records as $record)
1610
            {
1611
 
1612
                array_push($items, [
1613
                    'id'        => $record['uuid'],
1614
                    'name'      => trim($record['name']),
1615
                    'image'     => $this->url()->fromRoute('storage', ['code' => $record['uuid'], 'type' => 'group', 'filename' => $record['image']]),
1616
                    'profile'   => $this->url()->fromRoute('group/view', ['id' => $record['uuid'] ]),
1617
                    'priority'  => $record['priority'],
1618
 
1619
                ]);
1620
 
1621
            }
1622
 
1623
            return new JsonModel([
1624
                'success' => true,
1625
                'data' => $items
1626
            ]);
1627
 
1628
 
1629
        } else {
1630
            return new JsonModel([
1631
                'success' => false,
1632
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
1633
            ]);
1634
        }
1635
    }
1636
 
1 www 1637
}