Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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