Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 6749 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 www 1
<?php
2
 
3
declare(strict_types=1);
4
 
5
namespace LeadersLinked\Library;
6
 
7
 
8
use Laminas\Db\Adapter\AdapterInterface;
6849 efrain 9
 
1 www 10
use Laminas\View\Renderer\PhpRenderer;
11
use Laminas\Log\LoggerInterface;
12
use Laminas\Authentication\AuthenticationService;
13
use LeadersLinked\Mapper\NotificationMapper;
14
use LeadersLinked\Mapper\MessageMapper;
15
use LeadersLinked\Mapper\ConnectionMapper;
16
use LeadersLinked\Mapper\QueryMapper;
17
use Laminas\Paginator\Adapter\DbSelect;
18
use Laminas\Paginator\Paginator;
19
use LeadersLinked\Mapper\UserMapper;
20
use LeadersLinked\Mapper\FeedMapper;
21
use LeadersLinked\Mapper\GroupMapper;
22
use LeadersLinked\Mapper\JobMapper;
23
use LeadersLinked\Mapper\CompanyMapper;
24
use Laminas\View\Model\ViewModel;
25
use LeadersLinked\Model\Message;
26
use LeadersLinked\Model\Connection;
27
use LeadersLinked\Model\Notification;
28
use LeadersLinked\Mapper\UserExperienceMapper;
29
use Laminas\Db\Sql\Select;
30
 
6388 efrain 31
 
1 www 32
class UserNotification
33
{
34
    const NOTIFICATION_TYPE_UNREAD = 'unread';
35
    const NOTIFICATION_TYPE_REGULAR = 'regular';
36
 
37
 
38
    /**
39
     *
40
     * @var AdapterInterface
41
     */
42
    private $adapter;
6849 efrain 43
 
1 www 44
 
45
    /**
46
     *
47
     * @var  LoggerInterface
48
     */
49
    private $logger;
50
 
51
 
52
    /**
53
     *
54
     * @var  PhpRenderer
55
     */
56
    private $phpRender;
57
 
58
    /**
59
     *
60
     * @var integer
61
     */
62
    private $session_user_id;
63
 
64
    /**
65
     *
66
     * @var integer
67
     */
68
    private $session_language_id;
69
 
70
    /**
71
     *
72
     * @param AdapterInterface $adapter
73
     * @param LoggerInterface $logger
74
     */
6849 efrain 75
    public function __construct(AdapterInterface $adapter, LoggerInterface $logger, PhpRenderer $phpRender)
1 www 76
    {
77
        $this->adapter          = $adapter;
78
        $this->logger           = $logger;
79
        $this->phpRender        = $phpRender;
80
 
81
        $authService = new AuthenticationService();
82
        if($authService->hasIdentity()) {
83
            $identity = $authService->getIdentity();
84
            $this->session_user_id = (int) $identity['user_id'];
85
        } else {
86
            $this->session_user_id = 0;
87
        }
88
 
89
        $this->session_language_id = 1;
90
    }
91
 
92
    /**
93
     *
94
     * @return int
95
     */
96
    public function getUnreadNotificationsCount()
97
    {
98
 
99
    }
100
    /**
101
     *
102
     * @return int
103
     */
104
    public function getUnreadMessagesCount()
105
    {
106
        $messageMapper = MessageMapper::getInstance($this->adapter);
107
        return $messageMapper->fetchUnreadMessagesCount($this->session_user_id);
108
    }
109
 
110
    /**
111
     *
112
     * @return int
113
     */
114
    public function getConnectionRequestCount()
115
    {
116
        $connectionMapper = ConnectionMapper::getInstance($this->adapter);
117
        return $connectionMapper->fetchConnectionRequestCount($this->session_user_id);
118
    }
119
 
120
    /**
121
     *
122
     * @return boolean
123
     */
124
    public function markNotificationsAsRead()
125
    {
126
        $notificationMapper = NotificationMapper::getInstance($this->adapter);
127
        return $notificationMapper->markNotificationsAsRead($this->session_user_id);
128
    }
129
 
130
    /**
131
     *
132
     * @param int $currentPage
133
     * @param string $notification_type
134
     * @return string
135
     */
136
    public function getNotifications($currentPage = 1, $notification_type = self::NOTIFICATION_TYPE_REGULAR)
137
    {
3138 efrain 138
 
6388 efrain 139
 
3138 efrain 140
 
6388 efrain 141
 
1 www 142
        $content = '';
143
 
144
        $queryMapper = QueryMapper::getInstance($this->adapter);
6388 efrain 145
        $now = $queryMapper->getDatebaseNow();
146
 
1 www 147
        $select = $queryMapper->getSql()->select(NotificationMapper::_TABLE);
148
        $select->where->equalTo('user_id', $this->session_user_id);
149
        if ($notification_type ==  self::NOTIFICATION_TYPE_UNREAD) {
150
            $select->where->and->equalTo('is_notified', UserNotification::IS_NOTIFIED_NO);
151
        }
152
        $select->order('id DESC');
153
 
154
        $dbSelect = new DbSelect($select, $this->adapter);
155
        $paginator = new Paginator($dbSelect);
156
        $paginator->setCurrentPageNumber($currentPage);
157
        $paginator->setItemCountPerPage(NO_OF_NOTIFICATIONS_PER_PAGE);
158
 
159
        $notifications = [];
160
        $items = $paginator->getCurrentItems();
161
 
162
 
163
        if ($items) {
164
            $notificationMapper = NotificationMapper::getInstance($this->adapter);
165
 
166
            foreach ($items as $item)
167
            {
168
 
3138 efrain 169
                $time_ago               = Functions::timeAgo($item['added_on'],  $now);
1 www 170
                $type                   = $item['type'];
171
                $action_by_user_id      = (int) $item['action_by_user_id'];
172
                $action_by_user_name    = '';
173
                $action_by_user_picture = '';
174
                $feed_id                = (int) $item['feed_id'];
175
                $group_id               = (int) $item['group_id'];
176
                $job_id                 = (int) $item['job_id'];
177
                $company_id             = (int) $item['company_id'];
178
                $post_title             = '';
179
                $group_name             = '';
180
                $job_title              = '';
181
                $company_name           = '';
182
 
183
                if ($action_by_user_id) {
184
                    $userMapper = UserMapper::getInstance($this->adapter);
185
                    $user = $userMapper->fetchOne($action_by_user_id);
186
                    $action_by_user_picture = $user->avatar;
187
                    $action_by_user_name = $user->first_name . ' '.  $user->last_name;
188
                }
189
                if ($feed_id) {
190
                    $feedMapper = FeedMapper::getInstance($this->adapter);
191
                    $feed = $feedMapper->fetchOne($feed_id);
192
                    $post_title = $feed->post_title;
193
                }
194
                if ($group_id) {
195
                    $groupMapper = GroupMapper::getInstance($this->adapter);
196
                    $group = $groupMapper->fetchOne($group_id);
197
                    $group_name = $group->group_name;
198
                }
199
                if ($job_id) {
200
                    $jobMapper = JobMapper::getInstance($this->adapter);
201
                    $job = $jobMapper->fetchOne($job_id);
202
                    $job_title = $job->job_title;
203
                }
204
                if ($company_id) {
205
                    $companyMapper = CompanyMapper::getInstance($this->adapter);
206
                    $company = $companyMapper->fetchOne($company_id);
207
 
208
 
209
                    $company_name = $company->company_name;
210
                }
211
 
212
                if($action_by_user_picture) {
213
                    $user_img = $this->url('storage', ['type' => 'user', 'filename' => 'th2_' . $action_by_user_picture, 'code' => Functions::encryptIt($action_by_user_id)]);
214
 
215
                } else {
216
                    $user_img = URL_USER_DEFAULT_AVATAR;
217
                }
218
 
219
                switch ($type) {
220
                    case Notification::TYPE_CONNECTION_REQUEST_ACCEPTED :
221
                        $notification_text  = LABEL_COM_DET_YOUR_CONNECTION_REQUEST_ACCEPTED .' '. $action_by_user_name;
222
                        $notification_url   = $this->url('user', ['user_id' =>  Functions::encryptIt($action_by_user_id)]);
223
                        $notification_title = LABEL_CONNECTION_REQUEST_ACCEPTED;
224
                        break;
225
 
226
 
227
                    case Notification::TYPE_LIKE :
228
                        $notification_text  = $action_by_user_name .' '. LABEL_LIKED_YOUR_POST . $post_title;
229
                        $notification_url   = $this->url('feed', ['feed_id' =>  Functions::encryptIt($feed_id)]);
230
                        $notification_title = $action_by_user_name .' '. LABEL_LIKED_YOUR_POST. $post_title;
231
                        break;
232
 
233
                    case Notification::TYPE_COMMENT :
234
                        $notification_text  = $action_by_user_name.' ' . LABEL_COMMENTED_ON_YOUR_POST.' ' . $post_title;
235
                        $notification_url   = $this->url('feed', ['feed_id' =>  Functions::encryptIt($feed_id)]);
236
                        $notification_title = $action_by_user_name.' ' . LABEL_COMMENTED_ON_YOUR_POST.' ' . $post_title;
237
                        break;
238
 
239
                    case Notification::TYPE_SHARE :
240
                        $notification_text  = $action_by_user_name.' ' . LABEL_SHARED_YOUR_POST.' ' . $post_title;
241
                        $notification_url   = $this->url('feed', ['feed_id' =>  Functions::encryptIt($feed_id)]);
242
                        $notification_title = $action_by_user_name.' ' . LABEL_SHARED_YOUR_POST.' ' . $post_title;
243
                        break;
244
 
245
                    case Notification::TYPE_RECEIVED_GROUP_JOINING_INVITATION :
246
                        $notification_text  = $action_by_user_name.' ' .  LABEL_SENT_INVITATION.' ' . $group_name;
247
                        $notification_url   = $this->url('group', ['group_id' =>  Functions::encryptIt($group_id)]);
248
                        $notification_title = LABEL_GROUP_JOINING_INVITATION;
249
                        break;
250
 
251
                    case Notification::TYPE_RECEIVED_GROUP_JOINING_REQUEST :
252
                        $notification_text  = $action_by_user_name.' ' . LABEL_SENT_INVITATION.' ' . $group_name;
253
                        $notification_url   = $this->url('group-received-invitation', ['group_id' =>  Functions::encryptIt($group_id)]); //get_group_detail_url($group_id).'?received-invitation';
254
                        $notification_title = LABEL_GROUP_JOINING_INVITATION;
255
                        break;
256
 
257
                    case Notification::TYPE_GROUP_JOINING_REQUEST_ACCEPTED :
258
                        $notification_text  = $action_by_user_name.' ' .  LABEL_ACCEPTED_YOUR_REQUEST_FOR_JOINING_GROUP.' '  . $group_name;
259
                        $notification_url   = $this->url('group', ['group_id' =>  Functions::encryptIt($group_id)]);
260
                        $notification_title = LABEL_GROUP_JOINING_REQUEST_ACCEPTED;
261
                        break;
262
 
263
                    case Notification::TYPE_APPLIED_ON_JOB :
264
                        $notification_text  = $action_by_user_name.' ' .  LABEL_APPLIED_ON_JOB.' ' . $job_title;
265
                        $notification_url   = $this->url('job-applicants', ['job_id' =>  Functions::encryptIt($job_id)]);
266
                        $notification_title = LABEL_APPLIED_ON_JOB_CAPITAL;
267
                        break;
268
 
269
                    case Notification::TYPE_FOLLOW_COMPANY :
270
                        $notification_text  = $action_by_user_name.' ' . LABEL_FOLLOWED_COMPANY.' ' . $company_name;
271
                        $notification_url   = $this->url('company-followers', ['company_id' =>  Functions::encryptIt($company_id)]); //get_company_detail_url($company_id).'?company-followers';
272
                        $notification_title = LABEL_FOLLOW_COMPANY;
273
                        break;
274
 
275
                    case Notification::TYPE_NEW_FEED_POSTED_IN_GROUP :
276
                        $notification_text  = $action_by_user_name.' ' . LABEL_POSTED_GROUP.' ' . $group_name;
277
                        $notification_url   = $this->url('group-feed', ['group_id' =>  Functions::encryptIt($group_id), 'feed-id' => Functions::encryptIt($feed_id)]); //get_group_detail_url($group_id).'?id='.encryptIt($feed_id).'#'.encryptIt($feed_id);
278
                        $notification_title = LABEL_NEW_POST;
279
                        break;
280
 
281
                    case Notification::TYPE_ADD_MEMBER_IN_PRIVATE_GROUP :
282
                        $notification_text  = $action_by_user_name.' ' .LABEL_ADDED_IN_GROUP.' '. $group_name;
283
                        $notification_url   = $this->url('group', ['group_id' =>  Functions::encryptIt($group_id)]);
284
                        $notification_title = LABEL_ADDED_MEMBER;
285
                        break;
286
 
287
                }
288
 
289
                array_push($notifications, [
290
                    'text'  => $notification_text,
291
                    'url'   => $notification_url,
292
                    'title' => $notification_title,
293
                    'time'  => $time_ago,
294
                    'image' => $user_img
295
                ]);
296
 
297
                $notificationMapper->markNotificationsAsNotified((int) $item['id'], $this->session_user_id);
298
            }
299
 
300
            $viewModel = new ViewModel();
301
            $viewModel->setTerminal(true);
302
            $viewModel->setTemplate('leaders-linked/notifications/single-notification-header.phtml');
303
            $viewModel->setVariables([
304
               'notifications' => $notifications
305
            ]);
306
 
307
            $content = $this->phpRender->render($viewModel);
308
 
309
            if($paginator->count() > 1) {
310
                $viewModel = new ViewModel();
311
                $viewModel->setTerminal(true);
312
                $viewModel->setTemplate('leaders-linked/notifications/view-all-notification.phtml');
313
                $viewModel->setVariables([
314
                    'view_all_notification_url' => $this->url('view-all-notification')
315
                ]);
316
 
317
                $content .= $this->phpRender->render($viewModel);
318
            }
319
 
320
        } else {
321
            $viewModel = new ViewModel();
322
            $viewModel->setTerminal(true);
323
            $viewModel->setTemplate('leaders-linked/notifications/no-result-found.phtml');
324
 
325
            $content = $this->phpRender->render($viewModel);
326
        }
327
        return $content;
328
    }
329
 
330
    /**
331
     *
332
     * @param int $currentPage
333
     * @return array
334
     */
335
    public function getMessages(int $currentPage = 1)
336
    {
6388 efrain 337
 
1 www 338
 
6388 efrain 339
 
1 www 340
        $queryMapper = QueryMapper::getInstance($this->adapter);
6388 efrain 341
        $now = $queryMapper->getDatebaseNow();
342
 
1 www 343
        $select = $queryMapper->getSql()->select(MessageMapper::_TABLE);
344
        $select->where->equalTo('user_id', $this->session_user_id)->and->equalTo('receiver_status', Message::STATUS_NORMAL);
345
        $select->group('conversation_id');
346
        $select->order('id DESC');
347
 
348
        $dbSelect = new DbSelect($select, $this->adapter);
349
        $paginator = new Paginator($dbSelect);
350
        $paginator->setCurrentPageNumber($currentPage);
351
        $paginator->setItemCountPerPage(NO_OF_NOTIFICATIONS_PER_PAGE);
352
 
353
        $content = '';
354
        $items = $paginator->getCurrentItems();
355
        if ($items) {
356
 
357
            $messages = [];
358
            foreach($items as $item)
359
            {
360
                $action_by_user_id      = (int) $item['sender_id'];
361
                $action_by_user_name    = '';
362
                $user_img               = '';
363
                if ($action_by_user_id) {
364
                    $userMapper = UserMapper::getInstance($this->adapter);
365
                    $user = $userMapper->fetchOne($action_by_user_id);
366
 
367
                    if($user->avatar) {
368
                        $user_img = $this->url('storage', ['type' => 'user', 'filename' => 'th2_' . $user->avatar,  'code' => Functions::encryptIt($user->id)]);
369
                    } else {
370
                        $user_img = URL_USER_DEFAULT_AVATAR;
371
                    }
372
                    $action_by_user_name = $user->first_name . ' ' . $user->last_name;
373
                }
374
 
375
                array_push($messages, [
376
                    'title'         => $action_by_user_name,
377
                    'text'          => $item['message'],
378
                    'url'           => $this->url('messaging/thread', ['conversation_id' => Functions::encryptIt($item['conversation_id'])] ).'/#message',
3138 efrain 379
                    'time'          => Functions::timeAgo($item['sent_on'], $now),
1 www 380
                    'image'         => $user_img,
630 efrain 381
                    'active_class'  => $item['is_read'] == Message::YES ? 'active' : ''
1 www 382
                ]);
383
 
384
            }
385
 
386
 
387
            $viewModel = new ViewModel();
388
            $viewModel->setTerminal(true);
389
            $viewModel->setTemplate('leaders-linked/notification/single-message-header.phtml');
390
            $viewModel->setVariables([
391
                'messages' => $messages
392
            ]);
393
 
394
            $content = $this->phpRender->render($viewModel);
395
 
396
            if ($paginator->count() > 1) {
397
                $viewModel = new ViewModel();
398
                $viewModel->setTerminal(true);
399
                $viewModel->setTemplate('leaders-linked/common/load-more-li.phtml');
400
                $viewModel->setVariables([
401
                    'load_more_link' => $this->url('load-more-messages/page', ['page' => $paginator->getCurrentPageNumber() + 1 ])
402
                ]);
403
 
404
                $content .= $this->phpRender->render($viewModel);
405
            }
406
            $consersationFound = true;
407
        } else {
408
            $consersationFound = false;
409
 
410
            $viewModel = new ViewModel();
411
            $viewModel->setTerminal(true);
412
            $viewModel->setTemplate('leaders-linked/common/no-result-found.phtml');
413
 
414
            $content = $this->phpRender->render($viewModel);
415
 
416
        }
417
 
418
        $viewModel = new ViewModel();
419
        $viewModel->setTerminal(true);
420
        $viewModel->setTemplate('leaders-linked/common/view-all-notification.phtml');
421
        $viewModel->setVariables([
422
            'view_all_notification_url' => $this->url('messaging')
423
        ]);
424
 
425
 
426
        $view_all_messages = $this->phpRender->render($viewModel);
427
        $response = [
428
            'consersationFound' => $consersationFound ? true : false,
429
            'content'           => $content,
430
            'view_all_messages' => $view_all_messages
431
        ];
432
        return $response;
433
    }
434
 
435
    /**
436
     *
437
     * @param int $currentPage
438
     * @param bool $invitation_pagination
439
     * @return string
440
     */
441
    public function getConnectionRequest(int $currentPage = 1, bool $invitation_pagination = true)
442
    {
443
        $queryMapper = QueryMapper::getInstance($this->adapter);
444
        $select = $queryMapper->getSql()->select();
445
        $select->columns(['id', 'request_from', 'request_to']);
446
        $select->from(['c' => ConnectionMapper::_TABLE]);
447
        $select->join(['au' => UserMapper::_TABLE], 'u.id = c.request_from', ['first_name','last_name', 'avatar']);
448
        $select->join(['ue' => UserExperienceMapper::_TABLE], 'ue.company_id = com.id', ['job_title'], Select::JOIN_LEFT);
449
        $select->join(['com' => CompanyMapper::_TABLE], 'ue.company_id = com.id', ['company_name'], Select::JOIN_LEFT);
450
        $select->where->equalTo('c.request_to', $this->session_user_id)->and->equalTo('status', Connection::STATUS_SENT);
451
        $select->group(' c.id');
452
        $select->order('id DESC');
453
 
454
        $dbSelect = new DbSelect($select, $this->adapter);
455
        $paginator = new Paginator($dbSelect);
456
        $paginator->setCurrentPageNumber($currentPage);
457
        $paginator->setItemCountPerPage(NO_OF_NOTIFICATIONS_PER_PAGE);
458
 
459
 
460
        $items = $paginator->getCurrentItems();
461
 
462
        if ($items) {
463
 
464
            $connections = [];
465
 
466
            foreach($items as $item)
467
            {
468
                $action_by_user_id      = $item['request_from'];
469
                $action_by_user_name    = $item['first_name'] . ' ' . $item['last_name'];
470
                $message_text           = $action_by_user_name .' '. LABEL_SENT_CONNECTION_REQUEST;
471
                $message_title          = $action_by_user_name;
472
                if($item['avatar']) {
473
                    $user_img = $this->url('storage', ['type' => 'user', 'filename' => $item['avatar'], 'code' => Functions::encryptIt($action_by_user_id)]);
474
                } else {
475
                    $user_img = URL_USER_DEFAULT_AVATAR;
476
                }
477
 
478
 
479
                array_push($connections, [
480
                    'user_img' => $user_img,
481
                    'message_title' => $message_title,
482
                    'message_text' => $message_text,
483
                    'encrypted_user_id' => Functions::encryptIt($action_by_user_id)
484
                ]);
485
 
486
 
487
            }
488
 
489
            $viewModel = new ViewModel();
490
            $viewModel->setTerminal(true);
491
            $viewModel->setTemplate('leaders-linked/common/single-connection-header.phtml');
492
            $viewModel->setVariables([
493
                'connections' => $connections
494
            ]);
495
 
496
            $content = $this->phpRender->render($viewModel);;
497
 
498
        } else {
499
            $viewModel = new ViewModel();
500
            $viewModel->setTerminal(true);
501
            $viewModel->setTemplate('leaders-linked/common/no-connection-requests.phtml');
502
            $content = $this->phpRender->render($viewModel);;
503
        }
504
 
505
        return $content;
506
    }
507
 
508
    /**
509
     *
510
     * @return string[]
511
     */
512
    public function getAllNotifications()
513
    {
514
        $response = [];
515
        $response['general_notifications']      = $this->getNotifications();
516
        $response['job_notifications']          = '';
517
        $response['company_notifications']      = '';
518
        $response['group_notifications']        = '';
519
 
520
        $messages                               = $this->getMessages();
521
        $response['consersationFound']          = $messages['consersationFound'];
522
        $response['messages']                   = $messages['content'];
523
        $response['view_all_messages']          = $messages['view_all_messages'];
524
 
525
        $response['connection_request']         = $this->getConnectionRequest();
526
        $response['notifications_count']        = $this->getUnreadNotificationsCount();
527
        $response['messages_count']             = $this->getUnreadMessagesCount();
528
        $response['connection_request_count']   = $this->getConnectionRequestCount();
529
        return $response;
530
    }
531
}