Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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