Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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

Rev Autor Línea Nro. Línea
3262 efrain 1
<?php
2
 
3
declare(strict_types=1);
4
 
5
namespace LeadersLinked\Controller;
6
 
7
use Laminas\Db\Adapter\AdapterInterface;
8
use Laminas\Cache\Storage\Adapter\AbstractAdapter;
9
use Laminas\Mvc\Controller\AbstractActionController;
10
use Laminas\Log\LoggerInterface;
11
use Laminas\View\Model\ViewModel;
12
use Laminas\View\Model\JsonModel;
13
use LeadersLinked\Model\Page;
14
use LeadersLinked\Mapper\NotificationMapper;
15
use LeadersLinked\Mapper\CompanyMapper;
16
use LeadersLinked\Mapper\CompanyUserMapper;
17
use LeadersLinked\Model\Company;
18
use LeadersLinked\Mapper\PageMapper;
19
use LeadersLinked\Mapper\MessageMapper;
20
use LeadersLinked\Mapper\UserMapper;
21
use LeadersLinked\Mapper\UserProfileMapper;
22
use LeadersLinked\Mapper\CompanyUserRoleMapper;
23
use LeadersLinked\Model\Role;
24
use LeadersLinked\Library\Functions;
25
use LeadersLinked\Mapper\ConnectionMapper;
26
use LeadersLinked\Mapper\LocationMapper;
27
use LeadersLinked\Mapper\PostMapper;
28
use LeadersLinked\Mapper\ProfileVisitMapper;
29
use LeadersLinked\Model\Post;
30
use LeadersLinked\Mapper\UtilMapper;
31
use LeadersLinked\Model\Notification;
32
 
33
class NotificationController extends AbstractActionController
34
{
35
    /**
36
     *
37
     * @var AdapterInterface
38
     */
39
    private $adapter;
40
 
41
 
42
    /**
43
     *
44
     * @var AbstractAdapter
45
     */
46
    private $cache;
47
 
48
    /**
49
     *
50
     * @var  LoggerInterface
51
     */
52
    private $logger;
53
 
54
 
55
    /**
56
     *
57
     * @var array
58
     */
59
    private $config;
60
 
61
    /**
62
     *
63
     * @param AdapterInterface $adapter
64
     * @param AbstractAdapter $cache
65
     * @param LoggerInterface $logger
66
     * @param array $config
67
     */
68
    public function __construct($adapter, $cache, $logger,  $config)
69
    {
70
        $this->adapter      = $adapter;
71
        $this->cache        = $cache;
72
        $this->logger       = $logger;
73
        $this->config       = $config;
74
    }
75
 
76
 
77
 
78
    public function indexAction()
79
    {
80
        $request = $this->getRequest();
81
        if ($request->isGet()) {
82
 
83
            $currentUserPlugin = $this->plugin('currentUserPlugin');
84
            $currentUser = $currentUserPlugin->getUser();
85
 
86
            $userMapper = UserMapper::getInstance($this->adapter);
87
            $user = $userMapper->fetchOne($currentUser->id);
88
 
89
            $userProfileMapper = UserProfileMapper::getInstance($this->adapter);
90
            $userProfile = $userProfileMapper->fetchOnePublicByUserId($currentUser->id);
91
 
92
            $headers  = $request->getHeaders();
93
 
94
            $isJson = false;
95
            if ($headers->has('Accept')) {
96
                $accept = $headers->get('Accept');
97
 
98
                $prioritized = $accept->getPrioritized();
99
 
100
                foreach ($prioritized as $key => $value) {
101
                    $raw = trim($value->getRaw());
102
 
103
                    if (!$isJson) {
104
                        $isJson = strpos($raw, 'json');
105
                    }
106
                }
107
            }
108
 
109
            if ($isJson) {
110
 
111
                $utilMapper = UtilMapper::getInstance($this->adapter);
112
                $now = $utilMapper->getDatebaseNow();
113
 
114
 
115
                $notificationMapper = NotificationMapper::getInstance($this->adapter);
116
                $records = $notificationMapper->fetchAllByUserId($currentUser->id);
3280 efrain 117
 
118
 
3262 efrain 119
 
120
                $items = [];
121
                foreach ($records as $record) {
122
 
3283 efrain 123
                    /*
3262 efrain 124
                    if($record->read == Notification::NO) {
3280 efrain 125
                       $notificationMapper->markAsReadById($record->id);
3283 efrain 126
                    }*/
3262 efrain 127
 
128
                    array_push($items, [
129
                        'message' => $record->message,
130
                        'link' => $record->url,
4632 efrain 131
                        'link_delete' => $this->url()->fromRoute('notifications/delete', ['id' => $record->uuid]),
3262 efrain 132
                        'time_elapsed' => Functions::timeAgo( $record->added_on, $now),
133
                        'time' => $record->added_on
134
                    ]);
135
                }
136
 
137
                usort($items, function ($a, $b) {
138
 
139
                    if ($a['time'] == $b['time']) {
140
                        return 0;
141
                    } else {
142
                        return $a['time'] < $b['time'] ? -1 : 1;
143
                    }
144
 
145
                });
146
 
147
                $response = [
148
                    'success' => true,
149
                    'data' => $items
150
                ];
151
            } else {
152
 
153
                if ($user->location_id) {
154
                    $locationMapper = LocationMapper::getInstance($this->adapter);
155
                    $location = $locationMapper->fetchOne($user->location_id);
156
 
157
                    $country = $location->country;
158
                } else {
159
                    $country = '';
160
                }
161
 
162
                $profileVisitMapper = ProfileVisitMapper::getInstance($this->adapter);
163
                $visits = $profileVisitMapper->getTotalByVisitedId($currentUser->id);
164
 
165
                $connectionMapper = ConnectionMapper::getInstance($this->adapter);
166
                $connections = $connectionMapper->fetchTotalConnectionByUser($currentUser->id);
167
 
168
                $this->layout()->setTemplate('layout/layout.phtml');
169
                $viewModel = new ViewModel();
170
                $viewModel->setVariables([
171
                    'image' => $this->url()->fromRoute('storage', ['type' => 'user', 'code' => $user->uuid, 'filename' => $user->image]),
172
                    'fullname' => trim($user->first_name . ' ' . $user->last_name),
173
                    'description' => empty($userProfile->description) ? '' :  trim($userProfile->description),
174
                    'country' => $country,
175
                    'visits' => $visits,
176
                    'connections' => $connections
177
                ]);
178
 
179
                $viewModel->setTemplate('leaders-linked/notifications/index.phtml');
180
                return $viewModel;
181
            }
182
        } else {
183
            $response = [
184
                'success' => false,
185
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
186
            ];
187
        }
188
 
189
        return new JsonModel($response);
190
    }
191
 
192
    public function unreadsAction()
193
    {
194
        $request = $this->getRequest();
195
        if ($request->isGet()) {
196
 
197
            $utilMapper = UtilMapper::getInstance($this->adapter);
198
            $now = $utilMapper->getDatebaseNow();
199
 
200
            $currentUserPlugin = $this->plugin('currentUserPlugin');
201
            $currentUser = $currentUserPlugin->getUser();
202
 
203
            $notificationMapper = NotificationMapper::getInstance($this->adapter);
204
            $records = $notificationMapper->fetchAllsUnreadByUserId($currentUser->id);
205
 
206
            $items = [];
207
            foreach ($records as $record) {
208
                array_push($items, [
209
                    'message' => $record->message,
210
                    'link' => $record->url,
211
                    'link_mark_read' => $this->url()->fromRoute('notifications/mark-read', ['id' => $record->uuid]),
212
                    'link_delete' => $this->url()->fromRoute('notifications/delete', ['id' => $record->uuid]),
213
                    'time_elapsed' => Functions::timeAgo($record->added_on, $now),
214
                    'time' => $record->added_on
215
                ]);
216
            }
217
 
218
            usort($items, function ($a, $b) {
219
 
220
                if ($a['time'] == $b['time']) {
221
                    return 0;
222
                } else {
223
                    return $a['time'] < $b['time'] ? -1 : 1;
224
                }
225
            });
226
 
227
            $total = $notificationMapper->fetchUnreadNotificationsCount($currentUser->id);
228
 
229
            $response = [
230
                'success' => true,
231
                'data' => [
232
                    'notifications' =>  $items,
233
                    'total' => $total,
234
                 ],
235
 
236
            ];
237
 
238
        } else {
239
            $response = [
240
                'success' => false,
241
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
242
            ];
243
        }
244
 
245
        return new JsonModel($response);
246
    }
247
 
248
    public function markAllReadAction()
249
    {
250
 
251
        $request = $this->getRequest();
252
 
253
        if ($request->isPost()) {
254
 
255
 
256
 
257
            $currentUserPlugin = $this->plugin('currentUserPlugin');
258
            $currentUser = $currentUserPlugin->getUser();
259
 
260
 
261
            $notificationMapper = NotificationMapper::getInstance($this->adapter);
262
 
263
            $result = $notificationMapper->markAllAsReadByUserId($currentUser->id);
264
 
265
            if ($result) {
266
                $this->logger->info('Se marco como leidas todas las notificaciones de usuario: ' . $currentUser->id, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
267
                $response = [
268
                    'success' => true,
269
                    'data' => 'LABEL_RECORD_UPDATED'
270
                ];
271
            } else {
272
                $response = [
273
                    'success' => false,
274
                    'data' => $notificationMapper->getError()
275
                ];
276
            }
277
 
278
 
279
        }else{
280
            $response = [
281
                'success' => false,
282
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
283
            ];
284
 
285
        }
286
 
287
        return new JsonModel($response);
288
    }
289
 
290
    public function markReadAction()
291
    {
292
 
293
        $request = $this->getRequest();
294
 
295
        if ($request->isPost()) {
296
 
3270 efrain 297
            $uuid = $this->params()->fromRoute('id');
3262 efrain 298
 
299
            $currentUserPlugin = $this->plugin('currentUserPlugin');
300
            $currentUser = $currentUserPlugin->getUser();
301
 
302
 
303
            $notificationMapper = NotificationMapper::getInstance($this->adapter);
304
            $notification = $notificationMapper->fetchOneByUuid($uuid);
305
 
306
            if(!$notification) {
307
                $response = [
308
                    'success' => false,
309
                    'data' => 'ERROR_NOTIFICATION_NOT_FOUND'
310
                ];
311
 
312
                return new JsonModel($response);
313
 
314
            }
315
 
316
            if($currentUser->id != $notification->user_id) {
317
                $response = [
318
                    'success' => false,
319
                    'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS_TO_THIS_NOTIFICATION'
320
                ];
321
 
322
                return new JsonModel($response);
323
            }
324
 
325
            $result = $notificationMapper->markAsReadById($notification->id);
326
            if ($result) {
327
                $this->logger->info('Se marco una notificación del usuario: ' . $currentUser->id, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
328
                $response = [
329
                    'success' => true,
330
                    'data' => 'LABEL_RECORD_UPDATED'
331
                ];
332
            } else {
333
                $response = [
334
                    'success' => false,
335
                    'data' => $notificationMapper->getError()
336
                ];
337
            }
338
 
339
 
340
        }else{
341
            $response = [
342
                'success' => false,
343
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
344
            ];
345
 
346
        }
347
 
348
        return new JsonModel($response);
349
    }
350
 
351
    public function deleteAction()
352
    {
353
 
354
        $request = $this->getRequest();
355
 
356
        if ($request->isPost()) {
357
 
3270 efrain 358
            $uuid = $this->params()->fromRoute('id');
3262 efrain 359
 
360
            $currentUserPlugin = $this->plugin('currentUserPlugin');
361
            $currentUser = $currentUserPlugin->getUser();
362
 
363
 
364
            $notificationMapper = NotificationMapper::getInstance($this->adapter);
365
            $notification = $notificationMapper->fetchOneByUuid($uuid);
366
 
367
            if(!$notification) {
368
                $response = [
369
                    'success' => false,
370
                    'data' => 'ERROR_NOTIFICATION_NOT_FOUND'
371
                ];
372
 
373
                return new JsonModel($response);
374
 
375
            }
376
 
377
            if($currentUser->id != $notification->user_id) {
378
                $response = [
379
                    'success' => false,
380
                    'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS_TO_THIS_NOTIFICATION'
381
                ];
382
 
383
                return new JsonModel($response);
384
            }
385
 
386
            $result = $notificationMapper->deleteById($notification->id);
387
            if ($result) {
388
                $this->logger->info('Se borro una notificación del usuario: ' . $currentUser->id, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
389
                $response = [
390
                    'success' => true,
391
                    'data' => 'LABEL_RECORD_DELETED'
392
                ];
393
            } else {
394
                $response = [
395
                    'success' => false,
396
                    'data' => $notificationMapper->getError()
397
                ];
398
            }
399
 
400
 
401
        }else{
402
            $response = [
403
                'success' => false,
404
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
405
            ];
406
 
407
        }
408
 
409
        return new JsonModel($response);
410
    }
411
 
412
 
413
    public function clearAction()
414
    {
415
 
416
        $request = $this->getRequest();
417
 
418
        if ($request->isPost()) {
419
 
420
 
421
            $currentUserPlugin = $this->plugin('currentUserPlugin');
422
            $currentUser = $currentUserPlugin->getUser();
423
 
424
 
425
            $notificationMapper = NotificationMapper::getInstance($this->adapter);
426
 
427
            $result = $notificationMapper->deleteAllByUserId($currentUser->id);
428
            if ($result) {
429
                $this->logger->info('Se borraron todas las notificaciones del usuario: ' . $currentUser->id, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
430
                $response = [
431
                    'success' => true,
432
                    'data' => 'LABEL_RECORD_DELETED'
433
                ];
434
            } else {
435
                $response = [
436
                    'success' => false,
437
                    'data' => $notificationMapper->getError()
438
                ];
439
            }
440
 
441
 
442
        }else{
443
            $response = [
444
                'success' => false,
445
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
446
            ];
447
 
448
        }
449
 
450
        return new JsonModel($response);
451
    }
452
 
453
}