1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Apps\Controller\Front\Profile; |
4
|
|
|
|
5
|
|
|
use Apps\ActiveRecord\UserNotification; |
6
|
|
|
use Apps\Model\Front\Profile\EntityNotificationsList; |
7
|
|
|
use Ffcms\Core\App; |
8
|
|
|
use Ffcms\Core\Arch\View; |
9
|
|
|
use Ffcms\Core\Exception\ForbiddenException; |
10
|
|
|
use Ffcms\Core\Network\Request; |
11
|
|
|
use Ffcms\Core\Network\Response; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Trait ActionNotifications |
15
|
|
|
* @package Apps\Controller\Front\Profile |
16
|
|
|
* @property View $view |
17
|
|
|
* @property Request $request |
18
|
|
|
* @property Response $response |
19
|
|
|
*/ |
20
|
|
|
trait ActionNotifications |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Show user notifications |
24
|
|
|
* @param string $type |
25
|
|
|
* @return string |
26
|
|
|
* @throws ForbiddenException |
27
|
|
|
*/ |
28
|
|
|
public function notifications(?string $type = 'all'): ?string |
29
|
|
|
{ |
30
|
|
|
if (!App::$User->isAuth()) { |
31
|
|
|
throw new ForbiddenException(); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
// get page index and current user object |
35
|
|
|
$page = (int)$this->request->query->get('page', 0); |
36
|
|
|
$offset = $page * static::NOTIFY_PER_PAGE; |
|
|
|
|
37
|
|
|
$user = App::$User->identity(); |
38
|
|
|
|
39
|
|
|
// try to find notifications in database as active record |
40
|
|
|
$query = UserNotification::where('user_id', '=', $user->id) |
41
|
|
|
->orderBy('created_at', 'DESC'); |
42
|
|
|
if ($type === 'unread') { |
43
|
|
|
$query = $query->where('readed', '=', 0); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
// get total row count for pagination |
47
|
|
|
$totalCount = $query->count(); |
48
|
|
|
|
49
|
|
|
// get current records as object and build response |
50
|
|
|
$records = $query->skip($offset)->take(static::NOTIFY_PER_PAGE); |
51
|
|
|
$data = $records->get(); |
52
|
|
|
$model = new EntityNotificationsList($data); |
53
|
|
|
$model->make(); |
54
|
|
|
|
55
|
|
|
// update reader records |
56
|
|
|
$records->update(['readed' => 1]); |
57
|
|
|
|
58
|
|
|
return $this->view->render('profile/notifications', [ |
59
|
|
|
'model' => $model, |
60
|
|
|
'pagination' => [ |
61
|
|
|
'page' => $page, |
62
|
|
|
'step' => static::NOTIFY_PER_PAGE, |
63
|
|
|
'total' => $totalCount |
64
|
|
|
] |
65
|
|
|
]); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|