Proyectos de Subversion LeadersLinked - Services

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
 
3
declare(strict_types=1);
4
 
5
namespace LeadersLinked\Controller;
6
 
7
use Laminas\Db\Adapter\AdapterInterface;
8
 
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
     *
25
     * @var \Laminas\Db\Adapter\AdapterInterface
26
     */
27
    private $adapter;
28
 
29
    /**
30
     *
31
     * @var \LeadersLinked\Cache\CacheInterface
32
     */
33
    private $cache;
34
 
35
 
36
    /**
37
     *
38
     * @var \Laminas\Log\LoggerInterface
39
     */
40
    private $logger;
41
 
42
    /**
43
     *
44
     * @var array
45
     */
46
    private $config;
47
 
48
 
49
    /**
50
     *
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
61
     * @param array $config
62
     * @param \Laminas\Mvc\I18n\Translator $translator
63
     */
64
    public function __construct($adapter, $cache, $logger, $config, $translator)
65
    {
66
        $this->adapter      = $adapter;
67
        $this->cache        = $cache;
68
        $this->logger       = $logger;
69
        $this->config       = $config;
70
        $this->translator   = $translator;
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
                $notificationMapper = NotificationMapper::getInstance($this->adapter);
90
                $now = $notificationMapper->getDatebaseNow();
91
 
92
                $records = $notificationMapper->fetchAllByUserId($currentUser->id);
93
 
94
 
95
 
96
                $items = [];
97
                foreach ($records as $record) {
98
 
99
                    /*
100
                    if($record->read == Notification::NO) {
101
                       $notificationMapper->markAsReadById($record->id);
102
                    }*/
103
 
104
                    array_push($items, [
105
                        'message' => $record->message,
106
                        'link' => $record->url,
107
                        'link_delete' => $this->url()->fromRoute('notifications/delete', ['id' => $record->uuid]),
108
                        'time_elapsed' => Functions::timeAgo($record->added_on, $now),
109
                        'time' => $record->added_on
110
                    ]);
111
                }
112
 
113
                usort($items, function ($a, $b) {
114
 
115
                    if ($a['time'] == $b['time']) {
116
                        return 0;
117
                    } else {
118
                        return $a['time'] < $b['time'] ? -1 : 1;
119
                    }
120
                });
121
 
122
                $response = [
123
                    'success' => true,
124
                    'data' => $items
125
                ];
126
 
127
        } else {
128
            $response = [
129
                'success' => false,
130
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
131
            ];
132
        }
133
 
134
        return new JsonModel($response);
135
    }
136
 
137
    public function unreadsAction()
138
    {
139
        $request = $this->getRequest();
140
        if ($request->isGet()) {
141
 
142
            $currentUserPlugin = $this->plugin('currentUserPlugin');
143
            $currentUser = $currentUserPlugin->getUser();
144
 
145
            $notificationMapper = NotificationMapper::getInstance($this->adapter);
146
            $now = $notificationMapper->getDatebaseNow();
147
 
148
            $records = $notificationMapper->fetchAllsUnreadByUserId($currentUser->id);
149
 
150
            $items = [];
151
            foreach ($records as $record) {
152
                array_push($items, [
153
                    'message' => $record->message,
154
                    'link' => $record->url,
155
                    'link_mark_read' => $this->url()->fromRoute('notifications/mark-read', ['id' => $record->uuid]),
156
                    'link_delete' => $this->url()->fromRoute('notifications/delete', ['id' => $record->uuid]),
157
                    'time_elapsed' => Functions::timeAgo($record->added_on, $now),
158
                    'time' => $record->added_on
159
                ]);
160
            }
161
 
162
            usort($items, function ($a, $b) {
163
 
164
                if ($a['time'] == $b['time']) {
165
                    return 0;
166
                } else {
167
                    return $a['time'] < $b['time'] ? -1 : 1;
168
                }
169
            });
170
 
171
            $total = $notificationMapper->fetchUnreadNotificationsCount($currentUser->id);
172
 
173
            $response = [
174
                'success' => true,
175
                'data' => [
176
                    'notifications' =>  $items,
177
                    'total' => $total,
178
                ],
179
 
180
            ];
181
        } else {
182
            $response = [
183
                'success' => false,
184
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
185
            ];
186
        }
187
 
188
        return new JsonModel($response);
189
    }
190
 
191
    public function markAllReadAction()
192
    {
193
 
194
        $request = $this->getRequest();
195
 
196
        if ($request->isPost()) {
197
 
198
 
199
 
200
            $currentUserPlugin = $this->plugin('currentUserPlugin');
201
            $currentUser = $currentUserPlugin->getUser();
202
 
203
 
204
            $notificationMapper = NotificationMapper::getInstance($this->adapter);
205
 
206
            $result = $notificationMapper->markAllAsReadByUserId($currentUser->id);
207
 
208
            if ($result) {
209
                $this->logger->info('Se marco como leidas todas las notificaciones de usuario: ' . $currentUser->id, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
210
                $response = [
211
                    'success' => true,
212
                    'data' => 'LABEL_RECORD_UPDATED'
213
                ];
214
            } else {
215
                $response = [
216
                    'success' => false,
217
                    'data' => $notificationMapper->getError()
218
                ];
219
            }
220
        } else {
221
            $response = [
222
                'success' => false,
223
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
224
            ];
225
        }
226
 
227
        return new JsonModel($response);
228
    }
229
 
230
    public function markReadAction()
231
    {
232
 
233
        $request = $this->getRequest();
234
 
235
        if ($request->isPost()) {
236
 
237
            $uuid = $this->params()->fromRoute('id');
238
 
239
            $currentUserPlugin = $this->plugin('currentUserPlugin');
240
            $currentUser = $currentUserPlugin->getUser();
241
 
242
 
243
            $notificationMapper = NotificationMapper::getInstance($this->adapter);
244
            $notification = $notificationMapper->fetchOneByUuid($uuid);
245
 
246
            if (!$notification) {
247
                $response = [
248
                    'success' => false,
249
                    'data' => 'ERROR_NOTIFICATION_NOT_FOUND'
250
                ];
251
 
252
                return new JsonModel($response);
253
            }
254
 
255
            if ($currentUser->id != $notification->user_id) {
256
                $response = [
257
                    'success' => false,
258
                    'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS_TO_THIS_NOTIFICATION'
259
                ];
260
 
261
                return new JsonModel($response);
262
            }
263
 
264
            $result = $notificationMapper->markAsReadById($notification->id);
265
            if ($result) {
266
                $this->logger->info('Se marco una notificación del 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
        } else {
278
            $response = [
279
                'success' => false,
280
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
281
            ];
282
        }
283
 
284
        return new JsonModel($response);
285
    }
286
 
287
    public function deleteAction()
288
    {
289
 
290
        $request = $this->getRequest();
291
 
292
        if ($request->isPost()) {
293
 
294
            $uuid = $this->params()->fromRoute('id');
295
 
296
            $currentUserPlugin = $this->plugin('currentUserPlugin');
297
            $currentUser = $currentUserPlugin->getUser();
298
 
299
 
300
            $notificationMapper = NotificationMapper::getInstance($this->adapter);
301
            $notification = $notificationMapper->fetchOneByUuid($uuid);
302
 
303
            if (!$notification) {
304
                $response = [
305
                    'success' => false,
306
                    'data' => 'ERROR_NOTIFICATION_NOT_FOUND'
307
                ];
308
 
309
                return new JsonModel($response);
310
            }
311
 
312
            if ($currentUser->id != $notification->user_id) {
313
                $response = [
314
                    'success' => false,
315
                    'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS_TO_THIS_NOTIFICATION'
316
                ];
317
 
318
                return new JsonModel($response);
319
            }
320
 
321
            $result = $notificationMapper->deleteById($notification->id);
322
            if ($result) {
323
                $this->logger->info('Se borro una notificación del usuario: ' . $currentUser->id, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
324
                $response = [
325
                    'success' => true,
326
                    'data' => 'LABEL_RECORD_DELETED'
327
                ];
328
            } else {
329
                $response = [
330
                    'success' => false,
331
                    'data' => $notificationMapper->getError()
332
                ];
333
            }
334
        } else {
335
            $response = [
336
                'success' => false,
337
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
338
            ];
339
        }
340
 
341
        return new JsonModel($response);
342
    }
343
 
344
 
345
    public function clearAction()
346
    {
347
 
348
        $request = $this->getRequest();
349
 
350
        if ($request->isPost()) {
351
 
352
 
353
            $currentUserPlugin = $this->plugin('currentUserPlugin');
354
            $currentUser = $currentUserPlugin->getUser();
355
 
356
 
357
            $notificationMapper = NotificationMapper::getInstance($this->adapter);
358
 
359
            $result = $notificationMapper->deleteAllByUserId($currentUser->id);
360
            if ($result) {
361
                $this->logger->info('Se borraron todas las notificaciones del usuario: ' . $currentUser->id, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
362
                $response = [
363
                    'success' => true,
364
                    'data' => 'LABEL_RECORD_DELETED'
365
                ];
366
            } else {
367
                $response = [
368
                    'success' => false,
369
                    'data' => $notificationMapper->getError()
370
                ];
371
            }
372
        } else {
373
            $response = [
374
                'success' => false,
375
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
376
            ];
377
        }
378
 
379
        return new JsonModel($response);
380
    }
381
}