Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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