Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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