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);
|
|
|
117 |
|
|
|
118 |
$items = [];
|
|
|
119 |
foreach ($records as $record) {
|
|
|
120 |
|
|
|
121 |
|
|
|
122 |
if($record->read == Notification::NO) {
|
|
|
123 |
$notificationMapper->markAsReadByUserId($currentUser->id, $record->id);
|
|
|
124 |
}
|
|
|
125 |
|
|
|
126 |
array_push($items, [
|
|
|
127 |
'message' => $record->message,
|
|
|
128 |
'link' => $record->url,
|
|
|
129 |
'link_delete' => $this->url()->fromRoute('notifications/delete', ['id' => $record->uuid]),
|
|
|
130 |
'time_elapsed' => Functions::timeAgo( $record->added_on, $now),
|
|
|
131 |
'time' => $record->added_on
|
|
|
132 |
]);
|
|
|
133 |
}
|
|
|
134 |
|
|
|
135 |
usort($items, function ($a, $b) {
|
|
|
136 |
|
|
|
137 |
if ($a['time'] == $b['time']) {
|
|
|
138 |
return 0;
|
|
|
139 |
} else {
|
|
|
140 |
return $a['time'] < $b['time'] ? -1 : 1;
|
|
|
141 |
}
|
|
|
142 |
|
|
|
143 |
});
|
|
|
144 |
|
|
|
145 |
$response = [
|
|
|
146 |
'success' => true,
|
|
|
147 |
'data' => $items
|
|
|
148 |
];
|
|
|
149 |
} else {
|
|
|
150 |
|
|
|
151 |
if ($user->location_id) {
|
|
|
152 |
$locationMapper = LocationMapper::getInstance($this->adapter);
|
|
|
153 |
$location = $locationMapper->fetchOne($user->location_id);
|
|
|
154 |
|
|
|
155 |
$country = $location->country;
|
|
|
156 |
} else {
|
|
|
157 |
$country = '';
|
|
|
158 |
}
|
|
|
159 |
|
|
|
160 |
$profileVisitMapper = ProfileVisitMapper::getInstance($this->adapter);
|
|
|
161 |
$visits = $profileVisitMapper->getTotalByVisitedId($currentUser->id);
|
|
|
162 |
|
|
|
163 |
$connectionMapper = ConnectionMapper::getInstance($this->adapter);
|
|
|
164 |
$connections = $connectionMapper->fetchTotalConnectionByUser($currentUser->id);
|
|
|
165 |
|
|
|
166 |
$this->layout()->setTemplate('layout/layout.phtml');
|
|
|
167 |
$viewModel = new ViewModel();
|
|
|
168 |
$viewModel->setVariables([
|
|
|
169 |
'image' => $this->url()->fromRoute('storage', ['type' => 'user', 'code' => $user->uuid, 'filename' => $user->image]),
|
|
|
170 |
'fullname' => trim($user->first_name . ' ' . $user->last_name),
|
|
|
171 |
'description' => empty($userProfile->description) ? '' : trim($userProfile->description),
|
|
|
172 |
'country' => $country,
|
|
|
173 |
'visits' => $visits,
|
|
|
174 |
'connections' => $connections
|
|
|
175 |
]);
|
|
|
176 |
|
|
|
177 |
$viewModel->setTemplate('leaders-linked/notifications/index.phtml');
|
|
|
178 |
return $viewModel;
|
|
|
179 |
}
|
|
|
180 |
} else {
|
|
|
181 |
$response = [
|
|
|
182 |
'success' => false,
|
|
|
183 |
'data' => 'ERROR_METHOD_NOT_ALLOWED'
|
|
|
184 |
];
|
|
|
185 |
}
|
|
|
186 |
|
|
|
187 |
return new JsonModel($response);
|
|
|
188 |
}
|
|
|
189 |
|
|
|
190 |
public function unreadsAction()
|
|
|
191 |
{
|
|
|
192 |
$request = $this->getRequest();
|
|
|
193 |
if ($request->isGet()) {
|
|
|
194 |
|
|
|
195 |
$utilMapper = UtilMapper::getInstance($this->adapter);
|
|
|
196 |
$now = $utilMapper->getDatebaseNow();
|
|
|
197 |
|
|
|
198 |
$currentUserPlugin = $this->plugin('currentUserPlugin');
|
|
|
199 |
$currentUser = $currentUserPlugin->getUser();
|
|
|
200 |
|
|
|
201 |
$notificationMapper = NotificationMapper::getInstance($this->adapter);
|
|
|
202 |
$records = $notificationMapper->fetchAllsUnreadByUserId($currentUser->id);
|
|
|
203 |
|
|
|
204 |
$items = [];
|
|
|
205 |
foreach ($records as $record) {
|
|
|
206 |
array_push($items, [
|
|
|
207 |
'message' => $record->message,
|
|
|
208 |
'link' => $record->url,
|
|
|
209 |
'link_mark_read' => $this->url()->fromRoute('notifications/mark-read', ['id' => $record->uuid]),
|
|
|
210 |
'link_delete' => $this->url()->fromRoute('notifications/delete', ['id' => $record->uuid]),
|
|
|
211 |
'time_elapsed' => Functions::timeAgo($record->added_on, $now),
|
|
|
212 |
'time' => $record->added_on
|
|
|
213 |
]);
|
|
|
214 |
}
|
|
|
215 |
|
|
|
216 |
usort($items, function ($a, $b) {
|
|
|
217 |
|
|
|
218 |
if ($a['time'] == $b['time']) {
|
|
|
219 |
return 0;
|
|
|
220 |
} else {
|
|
|
221 |
return $a['time'] < $b['time'] ? -1 : 1;
|
|
|
222 |
}
|
|
|
223 |
});
|
|
|
224 |
|
|
|
225 |
$total = $notificationMapper->fetchUnreadNotificationsCount($currentUser->id);
|
|
|
226 |
|
|
|
227 |
$response = [
|
|
|
228 |
'success' => true,
|
|
|
229 |
'data' => [
|
|
|
230 |
'notifications' => $items,
|
|
|
231 |
'total' => $total,
|
|
|
232 |
],
|
|
|
233 |
|
|
|
234 |
];
|
|
|
235 |
|
|
|
236 |
} else {
|
|
|
237 |
$response = [
|
|
|
238 |
'success' => false,
|
|
|
239 |
'data' => 'ERROR_METHOD_NOT_ALLOWED'
|
|
|
240 |
];
|
|
|
241 |
}
|
|
|
242 |
|
|
|
243 |
return new JsonModel($response);
|
|
|
244 |
}
|
|
|
245 |
|
|
|
246 |
public function markAllReadAction()
|
|
|
247 |
{
|
|
|
248 |
|
|
|
249 |
$request = $this->getRequest();
|
|
|
250 |
|
|
|
251 |
if ($request->isPost()) {
|
|
|
252 |
|
|
|
253 |
|
|
|
254 |
|
|
|
255 |
$currentUserPlugin = $this->plugin('currentUserPlugin');
|
|
|
256 |
$currentUser = $currentUserPlugin->getUser();
|
|
|
257 |
|
|
|
258 |
|
|
|
259 |
$notificationMapper = NotificationMapper::getInstance($this->adapter);
|
|
|
260 |
|
|
|
261 |
$result = $notificationMapper->markAllAsReadByUserId($currentUser->id);
|
|
|
262 |
|
|
|
263 |
if ($result) {
|
|
|
264 |
$this->logger->info('Se marco como leidas todas las notificaciones de usuario: ' . $currentUser->id, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
|
|
|
265 |
$response = [
|
|
|
266 |
'success' => true,
|
|
|
267 |
'data' => 'LABEL_RECORD_UPDATED'
|
|
|
268 |
];
|
|
|
269 |
} else {
|
|
|
270 |
$response = [
|
|
|
271 |
'success' => false,
|
|
|
272 |
'data' => $notificationMapper->getError()
|
|
|
273 |
];
|
|
|
274 |
}
|
|
|
275 |
|
|
|
276 |
|
|
|
277 |
}else{
|
|
|
278 |
$response = [
|
|
|
279 |
'success' => false,
|
|
|
280 |
'data' => 'ERROR_METHOD_NOT_ALLOWED'
|
|
|
281 |
];
|
|
|
282 |
|
|
|
283 |
}
|
|
|
284 |
|
|
|
285 |
return new JsonModel($response);
|
|
|
286 |
}
|
|
|
287 |
|
|
|
288 |
public function markReadAction()
|
|
|
289 |
{
|
|
|
290 |
|
|
|
291 |
$request = $this->getRequest();
|
|
|
292 |
|
|
|
293 |
if ($request->isPost()) {
|
|
|
294 |
|
|
|
295 |
$uuid = $this->params()->fromRoute('uuid');
|
|
|
296 |
|
|
|
297 |
$currentUserPlugin = $this->plugin('currentUserPlugin');
|
|
|
298 |
$currentUser = $currentUserPlugin->getUser();
|
|
|
299 |
|
|
|
300 |
|
|
|
301 |
$notificationMapper = NotificationMapper::getInstance($this->adapter);
|
|
|
302 |
$notification = $notificationMapper->fetchOneByUuid($uuid);
|
|
|
303 |
|
|
|
304 |
if(!$notification) {
|
|
|
305 |
$response = [
|
|
|
306 |
'success' => false,
|
|
|
307 |
'data' => 'ERROR_NOTIFICATION_NOT_FOUND'
|
|
|
308 |
];
|
|
|
309 |
|
|
|
310 |
return new JsonModel($response);
|
|
|
311 |
|
|
|
312 |
}
|
|
|
313 |
|
|
|
314 |
if($currentUser->id != $notification->user_id) {
|
|
|
315 |
$response = [
|
|
|
316 |
'success' => false,
|
|
|
317 |
'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS_TO_THIS_NOTIFICATION'
|
|
|
318 |
];
|
|
|
319 |
|
|
|
320 |
return new JsonModel($response);
|
|
|
321 |
}
|
|
|
322 |
|
|
|
323 |
$result = $notificationMapper->markAsReadById($notification->id);
|
|
|
324 |
if ($result) {
|
|
|
325 |
$this->logger->info('Se marco una notificación del usuario: ' . $currentUser->id, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
|
|
|
326 |
$response = [
|
|
|
327 |
'success' => true,
|
|
|
328 |
'data' => 'LABEL_RECORD_UPDATED'
|
|
|
329 |
];
|
|
|
330 |
} else {
|
|
|
331 |
$response = [
|
|
|
332 |
'success' => false,
|
|
|
333 |
'data' => $notificationMapper->getError()
|
|
|
334 |
];
|
|
|
335 |
}
|
|
|
336 |
|
|
|
337 |
|
|
|
338 |
}else{
|
|
|
339 |
$response = [
|
|
|
340 |
'success' => false,
|
|
|
341 |
'data' => 'ERROR_METHOD_NOT_ALLOWED'
|
|
|
342 |
];
|
|
|
343 |
|
|
|
344 |
}
|
|
|
345 |
|
|
|
346 |
return new JsonModel($response);
|
|
|
347 |
}
|
|
|
348 |
|
|
|
349 |
public function deleteAction()
|
|
|
350 |
{
|
|
|
351 |
|
|
|
352 |
$request = $this->getRequest();
|
|
|
353 |
|
|
|
354 |
if ($request->isPost()) {
|
|
|
355 |
|
|
|
356 |
$uuid = $this->params()->fromRoute('uuid');
|
|
|
357 |
|
|
|
358 |
$currentUserPlugin = $this->plugin('currentUserPlugin');
|
|
|
359 |
$currentUser = $currentUserPlugin->getUser();
|
|
|
360 |
|
|
|
361 |
|
|
|
362 |
$notificationMapper = NotificationMapper::getInstance($this->adapter);
|
|
|
363 |
$notification = $notificationMapper->fetchOneByUuid($uuid);
|
|
|
364 |
|
|
|
365 |
if(!$notification) {
|
|
|
366 |
$response = [
|
|
|
367 |
'success' => false,
|
|
|
368 |
'data' => 'ERROR_NOTIFICATION_NOT_FOUND'
|
|
|
369 |
];
|
|
|
370 |
|
|
|
371 |
return new JsonModel($response);
|
|
|
372 |
|
|
|
373 |
}
|
|
|
374 |
|
|
|
375 |
if($currentUser->id != $notification->user_id) {
|
|
|
376 |
$response = [
|
|
|
377 |
'success' => false,
|
|
|
378 |
'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS_TO_THIS_NOTIFICATION'
|
|
|
379 |
];
|
|
|
380 |
|
|
|
381 |
return new JsonModel($response);
|
|
|
382 |
}
|
|
|
383 |
|
|
|
384 |
$result = $notificationMapper->deleteById($notification->id);
|
|
|
385 |
if ($result) {
|
|
|
386 |
$this->logger->info('Se borro una notificación del usuario: ' . $currentUser->id, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
|
|
|
387 |
$response = [
|
|
|
388 |
'success' => true,
|
|
|
389 |
'data' => 'LABEL_RECORD_DELETED'
|
|
|
390 |
];
|
|
|
391 |
} else {
|
|
|
392 |
$response = [
|
|
|
393 |
'success' => false,
|
|
|
394 |
'data' => $notificationMapper->getError()
|
|
|
395 |
];
|
|
|
396 |
}
|
|
|
397 |
|
|
|
398 |
|
|
|
399 |
}else{
|
|
|
400 |
$response = [
|
|
|
401 |
'success' => false,
|
|
|
402 |
'data' => 'ERROR_METHOD_NOT_ALLOWED'
|
|
|
403 |
];
|
|
|
404 |
|
|
|
405 |
}
|
|
|
406 |
|
|
|
407 |
return new JsonModel($response);
|
|
|
408 |
}
|
|
|
409 |
|
|
|
410 |
|
|
|
411 |
public function clearAction()
|
|
|
412 |
{
|
|
|
413 |
|
|
|
414 |
$request = $this->getRequest();
|
|
|
415 |
|
|
|
416 |
if ($request->isPost()) {
|
|
|
417 |
|
|
|
418 |
|
|
|
419 |
$currentUserPlugin = $this->plugin('currentUserPlugin');
|
|
|
420 |
$currentUser = $currentUserPlugin->getUser();
|
|
|
421 |
|
|
|
422 |
|
|
|
423 |
$notificationMapper = NotificationMapper::getInstance($this->adapter);
|
|
|
424 |
|
|
|
425 |
$result = $notificationMapper->deleteAllByUserId($currentUser->id);
|
|
|
426 |
if ($result) {
|
|
|
427 |
$this->logger->info('Se borraron todas las notificaciones del usuario: ' . $currentUser->id, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
|
|
|
428 |
$response = [
|
|
|
429 |
'success' => true,
|
|
|
430 |
'data' => 'LABEL_RECORD_DELETED'
|
|
|
431 |
];
|
|
|
432 |
} else {
|
|
|
433 |
$response = [
|
|
|
434 |
'success' => false,
|
|
|
435 |
'data' => $notificationMapper->getError()
|
|
|
436 |
];
|
|
|
437 |
}
|
|
|
438 |
|
|
|
439 |
|
|
|
440 |
}else{
|
|
|
441 |
$response = [
|
|
|
442 |
'success' => false,
|
|
|
443 |
'data' => 'ERROR_METHOD_NOT_ALLOWED'
|
|
|
444 |
];
|
|
|
445 |
|
|
|
446 |
}
|
|
|
447 |
|
|
|
448 |
return new JsonModel($response);
|
|
|
449 |
}
|
|
|
450 |
|
|
|
451 |
}
|