Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 5660 | Rev 6849 | 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
5501 anderson 2
 
1 www 3
/**
4
 *
5
 * Controlador: Mis Perfiles
6
 *
7
 */
5501 anderson 8
 
1 www 9
declare(strict_types=1);
10
 
11
namespace LeadersLinked\Controller;
12
 
13
use Laminas\Db\Adapter\AdapterInterface;
6749 efrain 14
use LeadersLinked\Cache\CacheInterface;
1 www 15
use Laminas\Mvc\Controller\AbstractActionController;
16
use Laminas\Log\LoggerInterface;
17
use Laminas\View\Model\ViewModel;
18
 
19
use LeadersLinked\Mapper\GroupMapper;
20
use LeadersLinked\Mapper\GroupMemberMapper;
21
use LeadersLinked\Mapper\GroupTypeMapper;
22
use LeadersLinked\Model\Group;
23
use LeadersLinked\Model\GroupMember;
24
use LeadersLinked\Mapper\IndustryMapper;
25
use LeadersLinked\Form\Feed\CreateGroupCompanyForm;
26
use LeadersLinked\Form\Feed\ShareGroupCompanyForm;
27
use LeadersLinked\Mapper\UserBlockedMapper;
28
use LeadersLinked\Mapper\QueryMapper;
29
use LeadersLinked\Mapper\UserMapper;
30
use LeadersLinked\Mapper\UserProfileMapper;
31
use LeadersLinked\Model\User;
32
use Laminas\View\Model\JsonModel;
33
use LeadersLinked\Mapper\EmailTemplateMapper;
34
use LeadersLinked\Model\EmailTemplate;
35
use LeadersLinked\Mapper\UserNotificationSettingMapper;
36
use LeadersLinked\Library\QueueEmail;
37
use Laminas\Db\Sql\Expression;
38
use LeadersLinked\Model\Notification;
39
use LeadersLinked\Mapper\NotificationMapper;
6749 efrain 40
use LeadersLinked\Library\Functions;
1 www 41
 
42
 
43
class GroupController extends AbstractActionController
44
{
45
    /**
46
     *
47
     * @var AdapterInterface
48
     */
49
    private $adapter;
5501 anderson 50
 
51
 
1 www 52
    /**
53
     *
6749 efrain 54
     * @var CacheInterface
1 www 55
     */
56
    private $cache;
5501 anderson 57
 
1 www 58
    /**
59
     *
60
     * @var  LoggerInterface
61
     */
62
    private $logger;
63
 
5501 anderson 64
 
1 www 65
    /**
66
     *
67
     * @var array
68
     */
69
    private $config;
5501 anderson 70
 
1 www 71
    /**
72
     *
73
     * @param AdapterInterface $adapter
6749 efrain 74
     * @param CacheInterface $cache
1 www 75
     * @param LoggerInterface $logger
76
     * @param array $config
77
     */
5501 anderson 78
    public function __construct($adapter, $cache, $logger,  $config)
1 www 79
    {
80
        $this->adapter      = $adapter;
81
        $this->cache        = $cache;
82
        $this->logger       = $logger;
83
        $this->config       = $config;
5501 anderson 84
    }
1 www 85
 
86
    /**
87
     *
88
     * Generación del listado de perfiles
89
     * {@inheritDoc}
90
     * @see \Laminas\Mvc\Controller\AbstractActionController::indexAction()
91
     */
92
    public function indexAction()
93
    {
5501 anderson 94
 
1 www 95
        $this->layout()->setTemplate('layout/layout.phtml');
96
        $viewModel = new ViewModel();
97
        $viewModel->setTemplate('leaders-linked/group/index.phtml');
5501 anderson 98
        return $viewModel;
1 www 99
    }
5501 anderson 100
 
101
 
1 www 102
    /**
103
     * Presenta el perfil con las opciónes de edición de cada sección
104
     * @return \Laminas\Http\Response|\Laminas\View\Model\ViewModel|\Laminas\View\Model\JsonModel
105
     */
106
    public function viewAction()
107
    {
108
        $request = $this->getRequest();
5501 anderson 109
 
1 www 110
        $currentUserPlugin = $this->plugin('currentUserPlugin');
111
        $currentUser = $currentUserPlugin->getUser();
5501 anderson 112
 
1 www 113
        $flashMessenger = $this->plugin('FlashMessenger');
114
        $id = $this->params()->fromRoute('id');
5501 anderson 115
 
1 www 116
        $isJson = false;
5501 anderson 117
 
1 www 118
        $headers  = $request->getHeaders();
5501 anderson 119
        if ($headers->has('Accept')) {
1 www 120
            $accept = $headers->get('Accept');
5501 anderson 121
 
1 www 122
            $prioritized = $accept->getPrioritized();
5501 anderson 123
 
124
            foreach ($prioritized as $key => $value) {
1 www 125
                $raw = trim($value->getRaw());
5501 anderson 126
 
127
                if (!$isJson) {
1 www 128
                    $isJson = strpos($raw, 'json');
129
                }
130
            }
131
        }
5501 anderson 132
 
133
        if ($isJson) {
134
 
1 www 135
            $flashMessenger = $this->plugin('FlashMessenger');
5501 anderson 136
 
137
            if (!$id) {
1 www 138
                return new JsonModel([
139
                    'success' => false,
140
                    'data' => 'ERROR_INVALID_PARAMETER'
141
                ]);
142
            }
5501 anderson 143
 
1 www 144
            $groupMapper = GroupMapper::getInstance($this->adapter);
3648 efrain 145
            $group = $groupMapper->fetchOneByUuidAndNetworkId($id, $currentUser->network_id);
5501 anderson 146
 
147
            if (!$group) {
1 www 148
                return new JsonModel([
149
                    'success' => false,
150
                    'data' => 'ERROR_GROUP_NOT_FOUND'
151
                ]);
152
            }
5501 anderson 153
 
154
            if ($group->status != Group::STATUS_ACTIVE) {
1 www 155
                return new JsonModel([
156
                    'success' => false,
157
                    'data' => 'ERROR_GROUP_IS_NOT_ACTIVE'
158
                ]);
159
            }
5501 anderson 160
 
241 efrain 161
            $userBlockedMapper = UserBlockedMapper::getInstance($this->adapter);
162
            $userBlocked = $userBlockedMapper->fetchOneByUserIdAndBlockedId($group->user_id, $currentUser->id);
5501 anderson 163
 
164
            if ($userBlocked) {
241 efrain 165
                $flashMessenger->addErrorMessage('ERROR_UNAUTHORIZED');
166
                return $this->redirect()->toRoute('dashboard');
167
            }
5501 anderson 168
 
241 efrain 169
            $userMapper = UserMapper::getInstance($this->adapter);
170
            $ownerGroup = $userMapper->fetchOne($group->user_id);
5501 anderson 171
 
241 efrain 172
            $values = [
173
                Group::ACCESSIBILITY_AUTO_JOIN => 'LABEL_AUTO_JOIN',
174
                Group::ACCESSIBILITY_REQUEST_TO_JOIN => 'LABEL_REQUEST_TO_JOIN',
175
                Group::ACCESSIBILITY_ADMIN_WILL_ADD => 'LABEL_ADMIN_WILL_ADD',
176
            ];
5501 anderson 177
 
241 efrain 178
            $accessibility = $values[$group->accessibility];
5501 anderson 179
 
241 efrain 180
            $values = [
181
                Group::PRIVACY_IS_PRIVATE => 'LABEL_PRIVATE',
182
                Group::PRIVACY_IS_PUBLIC => 'LABEL_PUBLIC'
183
            ];
5501 anderson 184
 
241 efrain 185
            $privacy = $values[$group->privacy];
5501 anderson 186
 
241 efrain 187
            $industryMapper = IndustryMapper::getInstance($this->adapter);
188
            $record = $industryMapper->fetchOne($group->industry_id);
5501 anderson 189
 
241 efrain 190
            $industry = $record->name;
5501 anderson 191
 
241 efrain 192
            $groupTypeMapper = GroupTypeMapper::getInstance($this->adapter);
193
            $record = $groupTypeMapper->fetchOne($group->type_id);
5501 anderson 194
 
241 efrain 195
            $group_type = $record->name;
1 www 196
            $groupMemberMapper = GroupMemberMapper::getInstance($this->adapter);
5501 anderson 197
 
241 efrain 198
            $groupMember = $groupMemberMapper->fetchOneByGroupIdAndUserId($group->id, $group->user_id);
5501 anderson 199
            if ($groupMember) {
200
 
201
                if ($groupMember->status != GroupMember::STATUS_ACCEPTED) {
241 efrain 202
                    $groupMember->status = $groupMember::STATUS_ACCEPTED;
203
                    $groupMemberMapper->update($groupMember);
204
                }
205
            } else {
206
                $groupMember = new GroupMember();
207
                $groupMember->user_id = $group->user_id;
208
                $groupMember->group_id = $group->id;
209
                $groupMember->status = GroupMember::STATUS_ACCEPTED;
5501 anderson 210
 
241 efrain 211
                $groupMemberMapper->insert($groupMember);
212
            }
5501 anderson 213
 
214
 
241 efrain 215
            $total_members = $groupMemberMapper->fetchTotalByGroupId($group->id);
5501 anderson 216
 
217
 
241 efrain 218
            $member = $groupMemberMapper->fetchOneByGroupIdAndUserId($group->id, $currentUser->id);
219
            $show_feeds = false;
5501 anderson 220
            if ($member) {
221
                if ($member->status == GroupMember::STATUS_ACCEPTED || $member->status == GroupMember::STATUS_AUTO_JOIN) {
241 efrain 222
                    $show_feeds = true;
223
                }
224
            }
5501 anderson 225
 
241 efrain 226
            $queryMapper = QueryMapper::getInstance($this->adapter);
227
            $select = $queryMapper->getSql()->select();
5501 anderson 228
            $select->columns(['user_id' => 'id',  'user_uuid' => 'uuid', 'first_name', 'last_name', 'image']);
241 efrain 229
            $select->from(['u' => UserMapper::_TABLE]);
5501 anderson 230
            $select->join(['tgm' => GroupMemberMapper::_TABLE], 'tgm.user_id = u.id', []);
231
            $select->join(['up' => UserProfileMapper::_TABLE], 'up.user_id = u.id', ['user_profile_id' => 'id', 'user_profile_uuid' => 'uuid']);
241 efrain 232
            $select->where->equalTo('u.status', User::STATUS_ACTIVE);
5501 anderson 233
            $select->where->equalTo('tgm.status', GroupMember::STATUS_ACCEPTED);
234
 
241 efrain 235
            // echo $select->getSqlString($this->adapter->platform); exit;
5501 anderson 236
 
241 efrain 237
            $members = [];
5501 anderson 238
 
241 efrain 239
            $records = $queryMapper->fetchAll($select);
5501 anderson 240
            foreach ($records as $record) {
241 efrain 241
                $userBlocked = $userBlockedMapper->fetchOneByUserIdAndBlockedId($record['user_id'], $currentUser->id);
5501 anderson 242
 
241 efrain 243
                array_push($members, [
244
                    'name' => trim($record['first_name'] . ' ' . $record['last_name']),
5501 anderson 245
                    'image' => $this->url()->fromRoute('storage', ['code' => $record['user_uuid'], 'type' => 'user', 'filename' => $record['image']]),
246
                    'link_profile' => $userBlocked ? '' : $this->url()->fromRoute('profile/view', ['id' => $record['user_profile_uuid']])
247
 
241 efrain 248
                ]);
249
            }
5501 anderson 250
 
251
 
241 efrain 252
            $groupMemberMapper = GroupMemberMapper::getInstance($this->adapter);
1 www 253
            $groupMember = $groupMemberMapper->fetchOneByGroupIdAndUserId($group->id, $currentUser->id);
5501 anderson 254
 
1 www 255
            $data = [
241 efrain 256
                'total_members'         => $total_members,
5501 anderson 257
                'accessibility'         => $accessibility,
241 efrain 258
                'privacy'               => $privacy,
259
                'industry'              => $industry,
260
                'group_id'              => $group->id,
261
                'group_type'            => $group_type,
262
                'group_uuid'            => $group->uuid,
5615 anderson 263
                'name'                  => trim($group->name),
241 efrain 264
                'image'                 => $group->image,
265
                'cover'                 => $group->cover,
266
                'overview'              => $group->description,
267
                'website'               => $group->website,
268
                'members'               => $members,
2737 www 269
                'link_inmail'           => $ownerGroup->id != $currentUser->id ?  $this->url()->fromRoute('inmail', ['id' => $ownerGroup->uuid]) : '',
1 www 270
                'link_request' => '',
271
                'link_accept' => '',
272
                'link_cancel' => '',
273
                'link_leave' => '',
274
            ];
5501 anderson 275
 
276
 
277
 
278
            if ($groupMember) {
279
                if ($ownerGroup->id != $groupMember->user_id && $groupMember->status == GroupMember::STATUS_ACCEPTED || $groupMember->status == GroupMember::STATUS_AUTO_JOIN) {
280
                    $data['link_leave'] = $this->url()->fromRoute('group/leave', ['id' => $group->uuid]);
1 www 281
                }
5501 anderson 282
                if ($groupMember->status == GroupMember::STATUS_ADDED_BY_ADMIN) {
283
                    $data['link_accept'] = $this->url()->fromRoute('group/accept', ['id' => $group->uuid]);
284
                    $data['link_cancel'] = $this->url()->fromRoute('group/cancel', ['id' => $group->uuid]);
1 www 285
                }
5501 anderson 286
                if ($groupMember->status == GroupMember::STATUS_JOINING_REQUESTED) {
287
                    $data['link_cancel'] = $this->url()->fromRoute('group/cancel', ['id' => $group->uuid]);
1 www 288
                }
5501 anderson 289
                if (in_array($group->accessibility, [Group::ACCESSIBILITY_AUTO_JOIN, Group::ACCESSIBILITY_REQUEST_TO_JOIN]) && $groupMember->status == GroupMember::STATUS_CANCELLED) {
1 www 290
 
291
                    $userBlockedMapper = UserBlockedMapper::getInstance($this->adapter);
292
                    $userBlocked = $userBlockedMapper->fetchOneByUserIdAndBlockedId($group->id, $currentUser->id);
5501 anderson 293
 
294
                    if (!$userBlocked) {
295
                        $data['link_request'] = $this->url()->fromRoute('group/request', ['id' => $group->uuid]);;
1 www 296
                    }
297
                }
298
            } else {
5501 anderson 299
 
300
                if (in_array($group->accessibility, [Group::ACCESSIBILITY_AUTO_JOIN, Group::ACCESSIBILITY_REQUEST_TO_JOIN])) {
301
 
1 www 302
                    $userBlockedMapper = UserBlockedMapper::getInstance($this->adapter);
303
                    $userBlocked = $userBlockedMapper->fetchOneByUserIdAndBlockedId($group->id, $currentUser->id);
5501 anderson 304
 
305
                    if (!$userBlocked) {
306
                        $data['link_request'] = $this->url()->fromRoute('group/request', ['id' => $group->uuid]);;
1 www 307
                    }
308
                }
309
            }
5501 anderson 310
 
311
 
1 www 312
            return new JsonModel([
313
                'success'   => true,
314
                'data'      => $data
315
            ]);
316
        } else {
5501 anderson 317
 
318
 
319
 
320
            if (!$id) {
1 www 321
                $flashMessenger->addErrorMessage('ERROR_INVALID_PARAMETER');
322
                return $this->redirect()->toRoute('dashboard');
323
            }
5501 anderson 324
 
1 www 325
            $groupMapper = GroupMapper::getInstance($this->adapter);
326
            $group = $groupMapper->fetchOneByUuid($id);
5501 anderson 327
            if (!$group) {
1 www 328
                $flashMessenger->addErrorMessage('ERROR_RECORD_NOT_FOUND');
329
                return $this->redirect()->toRoute('dashboard');
330
            }
5501 anderson 331
 
332
 
333
 
334
 
335
 
336
 
1 www 337
            $userBlockedMapper = UserBlockedMapper::getInstance($this->adapter);
338
            $userBlocked = $userBlockedMapper->fetchOneByUserIdAndBlockedId($group->user_id, $currentUser->id);
5501 anderson 339
 
340
            if ($userBlocked) {
1 www 341
                $flashMessenger->addErrorMessage('ERROR_UNAUTHORIZED');
342
                return $this->redirect()->toRoute('dashboard');
343
            }
5501 anderson 344
 
1 www 345
            $userMapper = UserMapper::getInstance($this->adapter);
346
            $ownerGroup = $userMapper->fetchOne($group->user_id);
5501 anderson 347
 
1 www 348
            $values = [
349
                Group::ACCESSIBILITY_AUTO_JOIN => 'LABEL_AUTO_JOIN',
350
                Group::ACCESSIBILITY_REQUEST_TO_JOIN => 'LABEL_REQUEST_TO_JOIN',
351
                Group::ACCESSIBILITY_ADMIN_WILL_ADD => 'LABEL_ADMIN_WILL_ADD',
352
            ];
5501 anderson 353
 
1 www 354
            $accessibility = $values[$group->accessibility];
5501 anderson 355
 
1 www 356
            $values = [
357
                Group::PRIVACY_IS_PRIVATE => 'LABEL_PRIVATE',
358
                Group::PRIVACY_IS_PUBLIC => 'LABEL_PUBLIC'
359
            ];
5501 anderson 360
 
1 www 361
            $privacy = $values[$group->privacy];
5501 anderson 362
 
1 www 363
            $industryMapper = IndustryMapper::getInstance($this->adapter);
364
            $record = $industryMapper->fetchOne($group->industry_id);
5501 anderson 365
 
1 www 366
            $industry = $record->name;
5501 anderson 367
 
1 www 368
            $groupTypeMapper = GroupTypeMapper::getInstance($this->adapter);
369
            $record = $groupTypeMapper->fetchOne($group->type_id);
5501 anderson 370
 
1 www 371
            $group_type = $record->name;
372
            $groupMemberMapper = GroupMemberMapper::getInstance($this->adapter);
5501 anderson 373
 
1 www 374
            $groupMember = $groupMemberMapper->fetchOneByGroupIdAndUserId($group->id, $group->user_id);
5501 anderson 375
            if ($groupMember) {
376
 
377
                if ($groupMember->status != GroupMember::STATUS_ACCEPTED) {
1 www 378
                    $groupMember->status = $groupMember::STATUS_ACCEPTED;
379
                    $groupMemberMapper->update($groupMember);
380
                }
381
            } else {
382
                $groupMember = new GroupMember();
383
                $groupMember->user_id = $group->user_id;
384
                $groupMember->group_id = $group->id;
385
                $groupMember->status = GroupMember::STATUS_ACCEPTED;
5501 anderson 386
 
1 www 387
                $groupMemberMapper->insert($groupMember);
388
            }
5501 anderson 389
 
390
 
1 www 391
            $total_members = $groupMemberMapper->fetchTotalByGroupId($group->id);
5501 anderson 392
 
393
 
1 www 394
            $member = $groupMemberMapper->fetchOneByGroupIdAndUserId($group->id, $currentUser->id);
395
            $show_feeds = false;
5501 anderson 396
            if ($member) {
397
                if ($member->status == GroupMember::STATUS_ACCEPTED || $member->status == GroupMember::STATUS_AUTO_JOIN) {
1 www 398
                    $show_feeds = true;
399
                }
400
            }
5501 anderson 401
 
1 www 402
            $queryMapper = QueryMapper::getInstance($this->adapter);
403
            $select = $queryMapper->getSql()->select();
5501 anderson 404
            $select->columns(['user_id' => 'id',  'user_uuid' => 'uuid', 'first_name', 'last_name', 'image']);
1 www 405
            $select->from(['u' => UserMapper::_TABLE]);
5501 anderson 406
            $select->join(['tgm' => GroupMemberMapper::_TABLE], 'tgm.user_id = u.id', []);
407
            $select->join(['up' => UserProfileMapper::_TABLE], 'up.user_id = u.id', ['user_profile_id' => 'id', 'user_profile_uuid' => 'uuid']);
1 www 408
            $select->where->equalTo('u.status', User::STATUS_ACTIVE);
5501 anderson 409
            $select->where->equalTo('tgm.status', GroupMember::STATUS_ACCEPTED);
410
 
411
            // echo $select->getSqlString($this->adapter->platform); exit;
412
 
1 www 413
            $members = [];
5501 anderson 414
 
1 www 415
            $records = $queryMapper->fetchAll($select);
5501 anderson 416
            foreach ($records as $record) {
1 www 417
                $userBlocked = $userBlockedMapper->fetchOneByUserIdAndBlockedId($record['user_id'], $currentUser->id);
5501 anderson 418
 
1 www 419
                array_push($members, [
420
                    'name' => trim($record['first_name'] . ' ' . $record['last_name']),
5501 anderson 421
                    'image' => $this->url()->fromRoute('storage', ['code' => $record['user_uuid'], 'type' => 'user', 'filename' => $record['image']]),
422
                    'link_profile' => $userBlocked ? '' : $this->url()->fromRoute('profile/view', ['id' => $record['user_profile_uuid']])
423
 
1 www 424
                ]);
425
            }
5501 anderson 426
 
1 www 427
            $formFeed = new CreateGroupCompanyForm();
428
            $formShare = new ShareGroupCompanyForm();
5501 anderson 429
 
430
 
1 www 431
            $this->layout()->setTemplate('layout/layout.phtml');
432
            $viewModel = new ViewModel();
5501 anderson 433
 
434
            if ($show_feeds) {
1 www 435
                $viewModel->setTemplate('leaders-linked/group/view-with-feeds.phtml');
436
            } else {
437
                $viewModel->setTemplate('leaders-linked/group/view-without-feeds.phtml');
438
            }
5501 anderson 439
 
440
 
441
 
1 www 442
            $notificationMapper = NotificationMapper::getInstance($this->adapter);
443
            $notificationMapper->markAllNotificationsAsReadByUserIdAndGroupId($currentUser->id, $group->id);
4632 efrain 444
 
5501 anderson 445
 
446
 
447
 
448
 
449
 
1 www 450
            $viewModel->setVariables([
451
                'user_id'               => $currentUser->id,
452
                'total_members'         => $total_members,
5501 anderson 453
                'accessibility'         => $accessibility,
1 www 454
                'privacy'               => $privacy,
455
                'industry'              => $industry,
456
                'group_id'              => $group->id,
457
                'group_type'            => $group_type,
458
                'group_uuid'            => $group->uuid,
5660 anderson 459
                'name'                  => trim($group->name),
1 www 460
                'image'                 => $group->image,
461
                'cover'                 => $group->cover,
462
                'overview'              => $group->description,
463
                'website'               => $group->website,
464
                'formFeed'              => $formFeed,
465
                'formShare'             => $formShare,
466
                'members'               => $members,
2737 www 467
                'link_inmail'           => $ownerGroup->id != $currentUser->id ? $this->url()->fromRoute('inmail', ['id' => $ownerGroup->uuid]) : '',
4632 efrain 468
                'owner_id'              => $ownerGroup->id,
469
                'owner_first_name'      => $ownerGroup->first_name,
5501 anderson 470
                'owner_last_name'       => $ownerGroup->last_name,
471
                'owner_image'           => $this->url()->fromRoute('storage', ['type' => 'user', 'code' => $ownerGroup->uuid, 'filename' => $ownerGroup->image]),
472
 
1 www 473
            ]);
5501 anderson 474
            return $viewModel;
1 www 475
        }
476
    }
5501 anderson 477
 
1 www 478
    public function leaveAction()
479
    {
480
        $flashMessenger = $this->plugin('FlashMessenger');
481
        $currentUserPlugin = $this->plugin('currentUserPlugin');
482
        $currentUser = $currentUserPlugin->getUser();
5501 anderson 483
 
1 www 484
        $request = $this->getRequest();
5501 anderson 485
        if ($request->isPost()) {
1 www 486
            $id = $this->params()->fromRoute('id');
6749 efrain 487
            $flash =  Functions::sanitizeFilterString($this->params()->fromPost('flash', 'false'));
5501 anderson 488
            $flash = $flash === 'true' ? true : false;
489
 
490
            if (!$id) {
1 www 491
                return new JsonModel([
492
                    'success' => false,
493
                    'data' => 'ERROR_INVALID_PARAMETER'
494
                ]);
495
            }
5501 anderson 496
 
1 www 497
            $groupMapper = GroupMapper::getInstance($this->adapter);
3648 efrain 498
            $group = $groupMapper->fetchOneByUuidAndNetworkId($id, $currentUser->network_id);
5501 anderson 499
 
500
            if (!$group) {
1 www 501
                return new JsonModel([
502
                    'success' => false,
503
                    'data' => 'ERROR_GROUP_NOT_FOUND'
504
                ]);
505
            }
5501 anderson 506
 
507
            if ($group->status != Group::STATUS_ACTIVE) {
1 www 508
                return new JsonModel([
509
                    'success' => false,
510
                    'data' => 'ERROR_GROUP_IS_NOT_ACTIVE'
511
                ]);
512
            }
5501 anderson 513
 
514
            if ($group->user_id == $currentUser->id) {
1 www 515
                return new JsonModel([
516
                    'success' => false,
517
                    'data' => 'ERROR_GROUP_YOU_ARE_THE_OWNER_OF_THIS_GROUP'
518
                ]);
519
            }
5501 anderson 520
 
1 www 521
            $groupMemberMapper = GroupMemberMapper::getInstance($this->adapter);
522
            $groupMember = $groupMemberMapper->fetchOneByGroupIdAndUserId($group->id, $currentUser->id);
5501 anderson 523
            if ($groupMember) {
524
 
525
                if ($groupMember->status == GroupMember::STATUS_ACCEPTED || $groupMember->status == GroupMember::STATUS_AUTO_JOIN) {
526
 
1 www 527
                    $groupMember->status = GroupMember::STATUS_CANCELLED;
5501 anderson 528
                    if ($groupMemberMapper->update($groupMember)) {
529
                        if ($flash) {
530
 
1 www 531
                            $flashMessenger->addSuccessMessage('LABEL_GROUP_YOU_HAVE_LEFT_THE_GROUP');
5501 anderson 532
 
1 www 533
                            return new JsonModel([
534
                                'success' => true,
535
                                'data' =>  [
536
                                    'message' => 'LABEL_GROUP_YOU_HAVE_LEFT_THE_GROUP',
537
                                    'reload' => true
5501 anderson 538
                                ]
1 www 539
                            ]);
540
                        } else {
541
                            return new JsonModel([
542
                                'success' => true,
543
                                'data' =>  'LABEL_GROUP_YOU_HAVE_LEFT_THE_GROUP',
544
                            ]);
545
                        }
546
                    } else {
547
                        return new JsonModel([
548
                            'success' => false,
549
                            'data' => $groupMemberMapper->getError()
550
                        ]);
551
                    }
552
                } else {
553
                    return new JsonModel([
554
                        'success' => false,
555
                        'data' => 'ERROR_GROUP_THERE_IS_NO_PENDING_REQUEST_TO_CANCEL'
556
                    ]);
557
                }
558
            } else {
559
                return new JsonModel([
560
                    'success' => false,
561
                    'data' => 'ERROR_GROUP_YOU_NOT_MEMBER'
562
                ]);
563
            }
564
        } else {
5501 anderson 565
 
1 www 566
            return new JsonModel([
567
                'success' => false,
568
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
569
            ]);
570
        }
571
    }
5501 anderson 572
 
1 www 573
    public function cancelAction()
574
    {
575
        $flashMessenger = $this->plugin('FlashMessenger');
576
        $currentUserPlugin = $this->plugin('currentUserPlugin');
577
        $currentUser = $currentUserPlugin->getUser();
5501 anderson 578
 
1 www 579
        $request = $this->getRequest();
5501 anderson 580
        if ($request->isPost()) {
1 www 581
            $id = $this->params()->fromRoute('id');
6749 efrain 582
            $flash =  Functions::sanitizeFilterString($this->params()->fromPost('flash', 'false'));
5501 anderson 583
            $flash = $flash === 'true' ? true : false;
584
 
585
            if (!$id) {
1 www 586
                return new JsonModel([
587
                    'success' => false,
588
                    'data' => 'ERROR_INVALID_PARAMETER'
589
                ]);
590
            }
5501 anderson 591
 
1 www 592
            $groupMapper = GroupMapper::getInstance($this->adapter);
3648 efrain 593
            $group = $groupMapper->fetchOneByUuidAndNetworkId($id, $currentUser->network_id);
5501 anderson 594
 
595
            if (!$group) {
1 www 596
                return new JsonModel([
597
                    'success' => false,
598
                    'data' => 'ERROR_GROUP_NOT_FOUND'
599
                ]);
600
            }
5501 anderson 601
 
602
            if ($group->status != Group::STATUS_ACTIVE) {
1 www 603
                return new JsonModel([
604
                    'success' => false,
605
                    'data' => 'ERROR_GROUP_IS_NOT_ACTIVE'
606
                ]);
607
            }
5501 anderson 608
 
1 www 609
            $groupMemberMapper = GroupMemberMapper::getInstance($this->adapter);
610
            $groupMember = $groupMemberMapper->fetchOneByGroupIdAndUserId($group->id, $currentUser->id);
5501 anderson 611
            if ($groupMember) {
612
 
613
                if ($groupMember->status == GroupMember::STATUS_JOINING_REQUESTED || $groupMember->status == GroupMember::STATUS_ADDED_BY_ADMIN) {
614
 
1 www 615
                    $groupMember->status = GroupMember::STATUS_CANCELLED;
5501 anderson 616
                    if ($groupMemberMapper->update($groupMember)) {
617
                        if ($flash) {
1 www 618
                            $flashMessenger->addSuccessMessage('LABEL_GROUP_REQUEST_CANCELLED');
5501 anderson 619
 
1 www 620
                            return new JsonModel([
621
                                'success' => true,
622
                                'data' =>  [
623
                                    'message' => 'LABEL_GROUP_REQUEST_CANCELLED',
624
                                    'reload' => true
5501 anderson 625
                                ]
1 www 626
                            ]);
627
                        } else {
628
                            return new JsonModel([
629
                                'success' => true,
630
                                'data' =>  'LABEL_GROUP_REQUEST_CANCELLED'
631
                            ]);
632
                        }
633
                    } else {
634
                        return new JsonModel([
635
                            'success' => false,
636
                            'data' => $groupMemberMapper->getError()
637
                        ]);
638
                    }
639
                } else {
640
                    return new JsonModel([
641
                        'success' => false,
642
                        'data' => 'ERROR_GROUP_THERE_IS_NO_PENDING_REQUEST_TO_CANCEL'
643
                    ]);
644
                }
645
            } else {
646
                return new JsonModel([
647
                    'success' => false,
648
                    'data' => 'ERROR_GROUP_YOU_NOT_MEMBER'
649
                ]);
650
            }
5501 anderson 651
        } else {
1 www 652
 
653
            return new JsonModel([
654
                'success' => false,
655
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
656
            ]);
657
        }
658
    }
5501 anderson 659
 
660
 
1 www 661
    public function rejectAction()
662
    {
663
        $flashMessenger = $this->plugin('FlashMessenger');
664
        $currentUserPlugin = $this->plugin('currentUserPlugin');
665
        $currentUser = $currentUserPlugin->getUser();
5501 anderson 666
 
1 www 667
        $request = $this->getRequest();
5501 anderson 668
        if ($request->isPost()) {
1 www 669
            $id = $this->params()->fromRoute('id');
6749 efrain 670
            $flash =  Functions::sanitizeFilterString($this->params()->fromPost('flash', 'false'));
5501 anderson 671
            $flash = $flash === 'true' ? true : false;
672
 
673
            if (!$id) {
1 www 674
                return new JsonModel([
675
                    'success' => false,
676
                    'data' => 'ERROR_INVALID_PARAMETER'
677
                ]);
678
            }
5501 anderson 679
 
1 www 680
            $groupMapper = GroupMapper::getInstance($this->adapter);
3648 efrain 681
            $group = $groupMapper->fetchOneByUuidAndNetworkId($id, $currentUser->network_id);
5501 anderson 682
 
683
            if (!$group) {
1 www 684
                return new JsonModel([
685
                    'success' => false,
686
                    'data' => 'ERROR_GROUP_NOT_FOUND'
687
                ]);
688
            }
5501 anderson 689
 
690
            if ($group->status != Group::STATUS_ACTIVE) {
1 www 691
                return new JsonModel([
692
                    'success' => false,
693
                    'data' => 'ERROR_GROUP_IS_NOT_ACTIVE'
694
                ]);
695
            }
5501 anderson 696
 
1 www 697
            $groupMemberMapper = GroupMemberMapper::getInstance($this->adapter);
698
            $groupMember = $groupMemberMapper->fetchOneByGroupIdAndUserId($group->id, $currentUser->id);
5501 anderson 699
            if ($groupMember) {
700
 
701
                if ($groupMember->status == GroupMember::STATUS_ADDED_BY_ADMIN) {
702
 
1362 efrain 703
                    $groupMember->status = GroupMember::STATUS_REJECTED;
5501 anderson 704
                    if ($groupMemberMapper->update($groupMember)) {
705
                        if ($flash) {
1 www 706
                            $flashMessenger->addSuccessMessage('LABEL_GROUP_REQUEST_REJECTED');
5501 anderson 707
 
1 www 708
                            return new JsonModel([
709
                                'success' => true,
710
                                'data' =>  [
1362 efrain 711
                                    'message' => 'LABEL_GROUP_REQUEST_REJECTED',
1 www 712
                                    'reload' => true
713
                                ]
714
                            ]);
715
                        } else {
716
                            return new JsonModel([
717
                                'success' => true,
1362 efrain 718
                                'data' =>  'LABEL_GROUP_REQUEST_REJECTED'
1 www 719
                            ]);
720
                        }
721
                    } else {
722
                        return new JsonModel([
723
                            'success' => false,
724
                            'data' => $groupMemberMapper->getError()
725
                        ]);
726
                    }
727
                } else {
728
                    return new JsonModel([
729
                        'success' => false,
730
                        'data' => 'ERROR_GROUP_THERE_IS_NO_PENDING_REQUEST_TO_CANCEL'
731
                    ]);
732
                }
733
            } else {
734
                return new JsonModel([
735
                    'success' => false,
736
                    'data' => 'ERROR_GROUP_YOU_NOT_MEMBER'
737
                ]);
738
            }
739
        } else {
5501 anderson 740
 
1 www 741
            return new JsonModel([
742
                'success' => false,
743
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
744
            ]);
745
        }
746
    }
747
 
748
    public function acceptAction()
749
    {
750
        $flashMessenger = $this->plugin('FlashMessenger');
751
        $currentUserPlugin = $this->plugin('currentUserPlugin');
752
        $currentUser = $currentUserPlugin->getUser();
5501 anderson 753
 
1 www 754
        $request = $this->getRequest();
5501 anderson 755
        if ($request->isPost()) {
1 www 756
            $id = $this->params()->fromRoute('id');
6749 efrain 757
            $flash =  Functions::sanitizeFilterString($this->params()->fromPost('flash', 'false'));
5501 anderson 758
            $flash = $flash === 'true' ? true : false;
759
 
760
            if (!$id) {
1 www 761
                return new JsonModel([
762
                    'success' => false,
763
                    'data' => 'ERROR_INVALID_PARAMETER'
764
                ]);
765
            }
5501 anderson 766
 
1 www 767
            $groupMapper = GroupMapper::getInstance($this->adapter);
3648 efrain 768
            $group = $groupMapper->fetchOneByUuidAndNetworkId($id, $currentUser->network_id);
5501 anderson 769
 
770
            if (!$group) {
1 www 771
                return new JsonModel([
772
                    'success' => false,
773
                    'data' => 'ERROR_GROUP_NOT_FOUND'
774
                ]);
775
            }
5501 anderson 776
 
777
            if ($group->status != Group::STATUS_ACTIVE) {
1 www 778
                return new JsonModel([
779
                    'success' => false,
780
                    'data' => 'ERROR_GROUP_IS_NOT_ACTIVE'
781
                ]);
782
            }
5501 anderson 783
 
1 www 784
            $groupMemberMapper = GroupMemberMapper::getInstance($this->adapter);
785
            $groupMember = $groupMemberMapper->fetchOneByGroupIdAndUserId($group->id, $currentUser->id);
5501 anderson 786
            if ($groupMember) {
787
 
788
                if ($groupMember->status == GroupMember::STATUS_ADDED_BY_ADMIN) {
789
 
1 www 790
                    $groupMember->status = GroupMember::STATUS_ACCEPTED;
5501 anderson 791
                    if ($groupMemberMapper->update($groupMember)) {
792
                        if ($flash) {
1 www 793
                            $flashMessenger->addSuccessMessage('LABEL_GROUP_AUTO_JOIN_SUCCESS');
5501 anderson 794
 
1 www 795
                            return new JsonModel([
796
                                'success' => true,
797
                                'data' => [
798
                                    'message' => 'LABEL_GROUP_AUTO_JOIN_SUCCESS',
799
                                    'reload' => true
5501 anderson 800
                                ]
1 www 801
                            ]);
802
                        } else {
803
                            return new JsonModel([
804
                                'success' => true,
805
                                'data' => 'LABEL_GROUP_AUTO_JOIN_SUCCESS'
806
                            ]);
807
                        }
808
                    } else {
809
                        return new JsonModel([
810
                            'success' => false,
811
                            'data' => $groupMemberMapper->getError()
812
                        ]);
813
                    }
814
                } else {
815
                    return new JsonModel([
816
                        'success' => false,
817
                        'data' => 'ERROR_GROUP_YOU_HAVE_NOT_INVITED_THIS_GROUP'
818
                    ]);
819
                }
820
            } else {
821
                return new JsonModel([
822
                    'success' => false,
823
                    'data' => 'ERROR_GROUP_YOU_NOT_MEMBER'
824
                ]);
825
            }
826
        } else {
5501 anderson 827
 
1 www 828
            return new JsonModel([
829
                'success' => false,
830
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
831
            ]);
832
        }
833
    }
5501 anderson 834
 
835
 
1 www 836
    public function requestAction()
837
    {
838
        $flashMessenger = $this->plugin('FlashMessenger');
839
        $currentUserPlugin = $this->plugin('currentUserPlugin');
840
        $currentUser = $currentUserPlugin->getUser();
5501 anderson 841
 
1 www 842
        $request = $this->getRequest();
5501 anderson 843
        if ($request->isPost()) {
1 www 844
            $id = $this->params()->fromRoute('id');
6749 efrain 845
            $flash =  Functions::sanitizeFilterString($this->params()->fromPost('flash', 'false'));
5501 anderson 846
            $flash = $flash === 'true' ? true : false;
847
 
848
            if (!$id) {
1 www 849
                return new JsonModel([
850
                    'success' => false,
851
                    'data' => 'ERROR_INVALID_PARAMETER'
852
                ]);
853
            }
5501 anderson 854
 
1 www 855
            $groupMapper = GroupMapper::getInstance($this->adapter);
3648 efrain 856
            $group = $groupMapper->fetchOneByUuidAndNetworkId($id, $currentUser->network_id);
5501 anderson 857
 
858
            if (!$group) {
1 www 859
                return new JsonModel([
860
                    'success' => false,
861
                    'data' => 'ERROR_GROUP_NOT_FOUND'
862
                ]);
863
            }
5501 anderson 864
 
865
            if ($group->status != Group::STATUS_ACTIVE) {
1 www 866
                return new JsonModel([
867
                    'success' => false,
868
                    'data' => 'ERROR_GROUP_IS_NOT_ACTIVE'
869
                ]);
870
            }
5501 anderson 871
 
1 www 872
            $groupMemberMapper = GroupMemberMapper::getInstance($this->adapter);
873
            $groupMember = $groupMemberMapper->fetchOneByGroupIdAndUserId($group->id, $currentUser->id);
5501 anderson 874
            if ($groupMember) {
875
                if ($groupMember->status == GroupMember::STATUS_ACCEPTED || $groupMember->status == GroupMember::STATUS_AUTO_JOIN) {
1 www 876
                    return new JsonModel([
877
                        'success' => false,
5501 anderson 878
                        'data' => 'ERROR_GROUP_YOU_ARE_MEMBER'
1 www 879
                    ]);
880
                }
5501 anderson 881
                if ($groupMember->status == GroupMember::STATUS_REJECTED) {
1 www 882
                    return new JsonModel([
883
                        'success' => false,
884
                        'data' => 'ERROR_GROUP_YOUR_REQUEST_WAS_PREVIOUSLY_REJECTED'
885
                    ]);
886
                }
5501 anderson 887
            }
1 www 888
 
5501 anderson 889
            if ($group->privacy == Group::PRIVACY_IS_PRIVATE || $group->accessibility == Group::ACCESSIBILITY_ADMIN_WILL_ADD) {
1 www 890
                return new JsonModel([
891
                    'success' => false,
892
                    'data' => 'ERROR_GROUP_ACCESS_ONLY_BY_ADMIN_INVITATION'
893
                ]);
894
            }
5501 anderson 895
 
896
 
897
            if ($groupMember) {
898
                if ($group->accessibility == Group::ACCESSIBILITY_AUTO_JOIN) {
1 www 899
                    $groupMember->status    = GroupMember::STATUS_AUTO_JOIN;
900
                    $groupMember->joined_on = new Expression('NOW()');
901
                }
5501 anderson 902
 
903
                if ($group->accessibility == Group::ACCESSIBILITY_REQUEST_TO_JOIN) {
1 www 904
                    $groupMember->status                = GroupMember::STATUS_JOINING_REQUESTED;
905
                    $groupMember->joining_request_on    = new Expression('NOW()');
906
                }
5501 anderson 907
 
1 www 908
                $result = $groupMemberMapper->update($groupMember);
909
            } else {
910
                $groupMember = new GroupMember();
911
                $groupMember->group_id  = $group->id;
912
                $groupMember->user_id   = $currentUser->id;
5501 anderson 913
                if ($group->accessibility == Group::ACCESSIBILITY_AUTO_JOIN) {
1 www 914
                    $groupMember->status    = GroupMember::STATUS_AUTO_JOIN;
915
                }
5501 anderson 916
 
917
                if ($group->accessibility == Group::ACCESSIBILITY_REQUEST_TO_JOIN) {
1 www 918
                    $groupMember->status    = GroupMember::STATUS_JOINING_REQUESTED;
919
                }
5501 anderson 920
 
1 www 921
                $result = $groupMemberMapper->insert($groupMember);
922
            }
5501 anderson 923
 
924
            if ($result) {
925
 
1 www 926
                $userMapper = UserMapper::getInstance($this->adapter);
927
                $userOwnerGroup = $userMapper->fetchOne($group->user_id);
5501 anderson 928
 
1 www 929
                $notification = new Notification();
930
                $notification->type     = Notification::TYPE_RECEIVE_REQUEST_JOIN_MY_GROUP;
931
                $notification->read     = Notification::NO;
932
                $notification->user_id  = $userOwnerGroup->id;
933
                $notification->group_id = $group->id;
934
                $notification->message  = 'LABEL_NOTIFICATION_RECEIVE_REQUEST_JOIN_MY_GROUP';
935
                $notification->url      = $this->url()->fromRoute('group/view', ['id' => $group->uuid]);
5501 anderson 936
 
1 www 937
                $notificationMapper = NotificationMapper::getInstance($this->adapter);
938
                $notificationMapper->insert($notification);
5501 anderson 939
 
940
 
941
 
1 www 942
                $userNotificationSettingMapper = UserNotificationSettingMapper::getInstance($this->adapter);
943
                $userNotificationSetting = $userNotificationSettingMapper->fetchOne($group->user_id);
5501 anderson 944
 
945
 
946
                if ($userNotificationSetting->receive_request_join_my_group) {
1 www 947
                    $emailTemplateMapper = EmailTemplateMapper::getInstance($this->adapter);
3712 efrain 948
                    $emailTemplate = $emailTemplateMapper->fetchOneByCodeAndNetworkId(EmailTemplate::CODE_RECEIVE_REQUEST_JOIN_MY_GROUP, $currentUser->network_id);
5501 anderson 949
                    if ($emailTemplate) {
1 www 950
                        $arrayCont = [
951
                            'firstname'             => $currentUser->first_name,
952
                            'lastname'              => $currentUser->last_name,
953
                            'other_user_firstname'  => $userOwnerGroup->first_name,
954
                            'other_user_lastname'   => $userOwnerGroup->last_name,
955
                            'company_name'          => '',
956
                            'group_name'            => $group->name,
957
                            'content'               => '',
958
                            'code'                  => '',
959
                            'link'                  => $this->url()->fromRoute('group/view', ['id' => $group->uuid], ['force_canonical' => true])
960
                        ];
5501 anderson 961
 
1 www 962
                        $email = new QueueEmail($this->adapter);
963
                        $email->processEmailTemplate($emailTemplate, $arrayCont, $userOwnerGroup->email, trim($userOwnerGroup->first_name . ' ' . $userOwnerGroup->last_name));
964
                    }
965
                }
5501 anderson 966
 
967
                if ($group->accessibility == Group::ACCESSIBILITY_AUTO_JOIN) {
968
                    if ($flash) {
1 www 969
                        $flashMessenger->addSuccessMessage('LABEL_GROUP_AUTO_JOIN_SUCCESS');
5501 anderson 970
 
1 www 971
                        return new JsonModel([
972
                            'success' => true,
973
                            'data' => [
974
                                'message' => 'LABEL_GROUP_AUTO_JOIN_SUCCESS',
975
                                'reload' => true
976
                            ]
977
                        ]);
978
                    } else {
979
                        return new JsonModel([
980
                            'success' => true,
981
                            'data' => 'LABEL_GROUP_AUTO_JOIN_SUCCESS'
982
                        ]);
983
                    }
984
                }
5501 anderson 985
                if ($group->accessibility == Group::ACCESSIBILITY_REQUEST_TO_JOIN) {
986
                    if ($flash) {
1 www 987
                        $flashMessenger->addSuccessMessage('LABEL_GROUP_REQUEST_SUCCESS');
5501 anderson 988
 
1 www 989
                        return new JsonModel([
990
                            'success' => true,
991
                            'data' => [
992
                                'message' => 'LABEL_GROUP_REQUEST_SUCCESS',
993
                                'reload' => true
5501 anderson 994
                            ]
1 www 995
                        ]);
996
                    } else {
997
                        return new JsonModel([
998
                            'success' => true,
999
                            'data' => 'LABEL_GROUP_REQUEST_SUCCESS'
1000
                        ]);
1001
                    }
1002
                }
1003
            } else {
1004
                return new JsonModel([
1005
                    'success' => false,
1006
                    'data' => $groupMapper->getError()
1007
                ]);
1008
            }
1009
        } else {
5501 anderson 1010
 
1 www 1011
            return new JsonModel([
1012
                'success' => false,
1013
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
1014
            ]);
1015
        }
1016
    }
5501 anderson 1017
 
1 www 1018
    /**
1019
     *
1020
     * Generación del listado de perfiles
1021
     * {@inheritDoc}
1022
     * @see \Laminas\Mvc\Controller\AbstractActionController::indexAction()
1023
     */
1024
    public function joinedGroupsAction()
1025
    {
1026
        $currentUserPlugin = $this->plugin('currentUserPlugin');
1027
        $currentUser = $currentUserPlugin->getUser();
5501 anderson 1028
 
1 www 1029
        $request = $this->getRequest();
5501 anderson 1030
        if ($request->isGet()) {
1031
 
1032
 
1 www 1033
            $headers  = $request->getHeaders();
1034
            $isJson = false;
5501 anderson 1035
            if ($headers->has('Accept')) {
1 www 1036
                $accept = $headers->get('Accept');
5501 anderson 1037
 
1 www 1038
                $prioritized = $accept->getPrioritized();
5501 anderson 1039
 
1040
                foreach ($prioritized as $key => $value) {
1 www 1041
                    $raw = trim($value->getRaw());
5501 anderson 1042
 
1043
                    if (!$isJson) {
1 www 1044
                        $isJson = strpos($raw, 'json');
1045
                    }
1046
                }
1047
            }
5501 anderson 1048
 
1049
            if ($isJson) {
1 www 1050
                $values = [
1051
                    Group::PRIVACY_IS_PRIVATE => 'LABEL_PRIVATE',
1052
                    Group::PRIVACY_IS_PUBLIC => 'LABEL_PUBLIC'
1053
                ];
5501 anderson 1054
 
6749 efrain 1055
                $search = Functions::sanitizeFilterString($this->params()->fromQuery('search', ''));
1 www 1056
 
5501 anderson 1057
 
1 www 1058
                $queryMapper = QueryMapper::getInstance($this->adapter);
5501 anderson 1059
 
1 www 1060
                $select = $queryMapper->getSql()->select();
1061
                $select->columns(['id', 'uuid', 'name', 'privacy',  'image']);
1062
                $select->from(['g' => GroupMapper::_TABLE]);
1063
                $select->join(['gm' => GroupMemberMapper::_TABLE], 'gm.group_id = g.id', []);
1064
                $select->where->equalTo('g.status', Group::STATUS_ACTIVE);
5501 anderson 1065
                $select->where->in('gm.status', [GroupMember::STATUS_ACCEPTED, GroupMember::STATUS_AUTO_JOIN]);
1 www 1066
                $select->where->equalTo('gm.user_id', $currentUser->id);
1067
                $select->where->notEqualTo('g.user_id', $currentUser->id);
3648 efrain 1068
                $select->where->equalTo('g.network_id', $currentUser->network_id);
5501 anderson 1069
 
1070
                if ($search) {
1 www 1071
                    $select->where->like('name', '%' . $search . '%');
1072
                }
1073
                $select->order('name ASC');
5501 anderson 1074
 
1 www 1075
                $records = $queryMapper->fetchAll($select);
5501 anderson 1076
 
1 www 1077
                $items = [];
5501 anderson 1078
                foreach ($records as $record) {
1079
 
1 www 1080
                    $item = [
1081
                        'name' => $record['name'],
1082
                        'privacy' => $values[$record['privacy']],
5501 anderson 1083
                        'image' => $this->url()->fromRoute('storage', ['type' => 'group', 'code' => $record['uuid'], 'filename' => $record['image']]),
1084
                        'link_view' => $this->url()->fromRoute('group/view', ['id' => $record['uuid']]),
1085
                        'link_leave' => $this->url()->fromRoute('group/leave', ['id' => $record['uuid']]),
1 www 1086
                    ];
5501 anderson 1087
 
1 www 1088
                    array_push($items, $item);
1089
                }
5501 anderson 1090
 
1091
 
1092
 
1 www 1093
                $response = [
1094
                    'success' => true,
1095
                    'data' => $items
1096
                ];
5501 anderson 1097
 
1 www 1098
                return new JsonModel($response);
1099
            } else {
1100
                $this->layout()->setTemplate('layout/layout.phtml');
1101
                $viewModel = new ViewModel();
1102
                $viewModel->setTemplate('leaders-linked/group/joined-groups.phtml');
5501 anderson 1103
                return $viewModel;
1 www 1104
            }
1105
        } else {
1106
            return new JsonModel([
1107
                'success' => false,
1108
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
1109
            ]);
1110
        }
1111
    }
5501 anderson 1112
 
1 www 1113
    public function invitationsReceivedAction()
1114
    {
1115
        $currentUserPlugin = $this->plugin('currentUserPlugin');
1116
        $currentUser = $currentUserPlugin->getUser();
5501 anderson 1117
 
1 www 1118
        $request = $this->getRequest();
5501 anderson 1119
        if ($request->isGet()) {
1120
 
1121
 
1 www 1122
            $headers  = $request->getHeaders();
1123
            $isJson = false;
5501 anderson 1124
            if ($headers->has('Accept')) {
1 www 1125
                $accept = $headers->get('Accept');
5501 anderson 1126
 
1 www 1127
                $prioritized = $accept->getPrioritized();
5501 anderson 1128
 
1129
                foreach ($prioritized as $key => $value) {
1 www 1130
                    $raw = trim($value->getRaw());
5501 anderson 1131
 
1132
                    if (!$isJson) {
1 www 1133
                        $isJson = strpos($raw, 'json');
1134
                    }
1135
                }
1136
            }
5501 anderson 1137
 
1138
            if ($isJson) {
6749 efrain 1139
                $search = Functions::sanitizeFilterString($this->params()->fromQuery('search', ''));
5501 anderson 1140
 
1 www 1141
                $queryMapper = QueryMapper::getInstance($this->adapter);
5501 anderson 1142
 
1 www 1143
                $select = $queryMapper->getSql()->select();
5501 anderson 1144
                $select->columns(['uuid', 'name', 'image']);
1 www 1145
                $select->from(['g' => GroupMapper::_TABLE]);
1146
                $select->join(['gm' => GroupMemberMapper::_TABLE], 'gm.group_id  = g.id', []);
1147
                $select->where->equalTo('gm.user_id', $currentUser->id);
1148
                $select->where->equalTo('gm.status', GroupMember::STATUS_ADDED_BY_ADMIN);
1149
                $select->where->equalTo('g.status', Group::STATUS_ACTIVE);
3648 efrain 1150
                $select->where->equalTo('g.network_id', $currentUser->network_id);
5501 anderson 1151
 
1152
                if ($search) {
1182 efrain 1153
                    $select->where->like('g.name', '%' . $search . '%');
1 www 1154
                }
1155
                $select->order('name ASC');
5501 anderson 1156
 
1 www 1157
                //echo $select2->getSqlString($this->adapter->platform); exit;
5501 anderson 1158
 
1 www 1159
                $records = $queryMapper->fetchAll($select);
5501 anderson 1160
 
1 www 1161
                $items = [];
5501 anderson 1162
                foreach ($records as $record) {
1 www 1163
                    $item = [
1164
                        'name' => $record['name'],
1165
                        'image' => $this->url()->fromRoute('storage', ['type' => 'group', 'code' => $record['uuid'], 'filename' => $record['image']]),
5501 anderson 1166
                        'link_view' => $this->url()->fromRoute('group/view', ['id' => $record['uuid']]),
1167
                        'link_accept' => $this->url()->fromRoute('group/accept', ['id' => $record['uuid']]),
1168
                        'link_reject' => $this->url()->fromRoute('group/reject', ['id' => $record['uuid']]),
1 www 1169
                    ];
5501 anderson 1170
 
1171
 
1 www 1172
                    array_push($items, $item);
1173
                }
5501 anderson 1174
 
1 www 1175
                $response = [
1176
                    'success' => true,
1177
                    'data' => $items
1178
                ];
5501 anderson 1179
 
1 www 1180
                return new JsonModel($response);
1181
            } else {
1182
                $this->layout()->setTemplate('layout/layout.phtml');
1183
                $viewModel = new ViewModel();
1184
                $viewModel->setTemplate('leaders-linked/group/invitations-received.phtml');
5501 anderson 1185
                return $viewModel;
1 www 1186
            }
1187
        } else {
1188
            return new JsonModel([
1189
                'success' => false,
1190
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
1191
            ]);
1192
        }
1193
    }
5501 anderson 1194
 
1 www 1195
    public function requestsSentAction()
1196
    {
1197
        $currentUserPlugin = $this->plugin('currentUserPlugin');
1198
        $currentUser = $currentUserPlugin->getUser();
5501 anderson 1199
 
1 www 1200
        $request = $this->getRequest();
5501 anderson 1201
        if ($request->isGet()) {
1202
 
1203
 
1 www 1204
            $headers  = $request->getHeaders();
1205
            $isJson = false;
5501 anderson 1206
            if ($headers->has('Accept')) {
1 www 1207
                $accept = $headers->get('Accept');
5501 anderson 1208
 
1 www 1209
                $prioritized = $accept->getPrioritized();
5501 anderson 1210
 
1211
                foreach ($prioritized as $key => $value) {
1 www 1212
                    $raw = trim($value->getRaw());
5501 anderson 1213
 
1214
                    if (!$isJson) {
1 www 1215
                        $isJson = strpos($raw, 'json');
1216
                    }
1217
                }
1218
            }
5501 anderson 1219
 
1220
            if ($isJson) {
6749 efrain 1221
                $search = Functions::sanitizeFilterString($this->params()->fromQuery('search', ''));
5501 anderson 1222
 
1223
 
1224
 
1 www 1225
                $queryMapper = QueryMapper::getInstance($this->adapter);
5501 anderson 1226
 
1 www 1227
                $select = $queryMapper->getSql()->select();
5501 anderson 1228
                $select->columns(['uuid', 'name', 'image']);
1 www 1229
                $select->from(['g' => GroupMapper::_TABLE]);
1230
                $select->join(['gm' => GroupMemberMapper::_TABLE], 'gm.group_id  = g.id', []);
1231
                $select->where->equalTo('gm.user_id', $currentUser->id);
1232
                $select->where->equalTo('gm.status', GroupMember::STATUS_JOINING_REQUESTED);
1233
                $select->where->equalTo('g.status', Group::STATUS_ACTIVE);
3648 efrain 1234
                $select->where->equalTo('g.network_id', $currentUser->network_id);
5501 anderson 1235
 
1236
 
1237
                if ($search) {
1182 efrain 1238
                    $select->where->like('g.name', '%' . $search . '%');
1 www 1239
                }
1240
                $select->order('name ASC');
5501 anderson 1241
 
1 www 1242
                //echo $select2->getSqlString($this->adapter->platform); exit;
5501 anderson 1243
 
1 www 1244
                $records = $queryMapper->fetchAll($select);
5501 anderson 1245
 
1 www 1246
                $items = [];
5501 anderson 1247
                foreach ($records as $record) {
1 www 1248
                    $item = [
1249
                        'name' => $record['name'],
1250
                        'image' => $this->url()->fromRoute('storage', ['type' => 'group', 'code' => $record['uuid'], 'filename' => $record['image']]),
5501 anderson 1251
                        'link_view' => $this->url()->fromRoute('group/view', ['id' => $record['uuid']]),
1252
                        'link_cancel' => $this->url()->fromRoute('group/cancel', ['id' => $record['uuid']]),
1 www 1253
                    ];
5501 anderson 1254
 
1255
 
1 www 1256
                    array_push($items, $item);
1257
                }
5501 anderson 1258
 
1 www 1259
                $response = [
1260
                    'success' => true,
1261
                    'data' => $items
1262
                ];
5501 anderson 1263
 
1 www 1264
                return new JsonModel($response);
1265
            } else {
1266
                $this->layout()->setTemplate('layout/layout.phtml');
1267
                $viewModel = new ViewModel();
1268
                $viewModel->setTemplate('leaders-linked/group/requests-sent.phtml');
5501 anderson 1269
                return $viewModel;
1 www 1270
            }
1271
        } else {
1272
            return new JsonModel([
1273
                'success' => false,
1274
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
1275
            ]);
1276
        }
1277
    }
1278
}