]> git.mxchange.org Git - friendica.git/blob - src/Module/Notifications/Notifications.php
Merge pull request #8179 from MrPetovan/bug/notices
[friendica.git] / src / Module / Notifications / Notifications.php
1 <?php
2
3 namespace Friendica\Module\Notifications;
4
5 use Friendica\Content\Nav;
6 use Friendica\Core\Renderer;
7 use Friendica\DI;
8 use Friendica\Module\BaseNotifications;
9 use Friendica\Object\Notification\Notification;
10
11 /**
12  * Prints all notification types except introduction:
13  * - Network
14  * - System
15  * - Personal
16  * - Home
17  */
18 class Notifications extends BaseNotifications
19 {
20         /**
21          * {@inheritDoc}
22          */
23         public static function getNotifications()
24         {
25                 $notificationHeader = '';
26                 /** @var Notification[] $notifications */
27                 $notifications = [];
28
29                 // Get the network notifications
30                 if ((DI::args()->get(1) == 'network')) {
31                         $notificationHeader = DI::l10n()->t('Network Notifications');
32                         $notifications      = [
33                                 'ident'        => Notification::NETWORK,
34                                 'notifications' => DI::notification()->getNetworkList(self::$showAll, self::$firstItemNum, self::ITEMS_PER_PAGE),
35                         ];
36
37                         // Get the system notifications
38                 } elseif ((DI::args()->get(1) == 'system')) {
39                         $notificationHeader = DI::l10n()->t('System Notifications');
40                         $notifications      = [
41                                 'ident'        => Notification::SYSTEM,
42                                 'notifications' => DI::notification()->getSystemList(self::$showAll, self::$firstItemNum, self::ITEMS_PER_PAGE),
43                         ];
44
45                         // Get the personal notifications
46                 } elseif ((DI::args()->get(1) == 'personal')) {
47                         $notificationHeader = DI::l10n()->t('Personal Notifications');
48                         $notifications      = [
49                                 'ident'        => Notification::PERSONAL,
50                                 'notifications' => DI::notification()->getPersonalList(self::$showAll, self::$firstItemNum, self::ITEMS_PER_PAGE),
51                         ];
52
53                         // Get the home notifications
54                 } elseif ((DI::args()->get(1) == 'home')) {
55                         $notificationHeader = DI::l10n()->t('Home Notifications');
56                         $notifications      = [
57                                 'ident'        => Notification::HOME,
58                                 'notifications' => DI::notification()->getHomeList(self::$showAll, self::$firstItemNum, self::ITEMS_PER_PAGE),
59                         ];
60                         // fallback - redirect to main page
61                 } else {
62                         DI::baseUrl()->redirect('notifications');
63                 }
64
65                 return [
66                         'header'        => $notificationHeader,
67                         'notifications' => $notifications,
68                 ];
69         }
70
71         public static function content(array $parameters = [])
72         {
73                 Nav::setSelected('notifications');
74
75                 $notificationContent   = [];
76                 $notificationNoContent = '';
77
78                 $notificationResult = self::getNotifications();
79                 $notifications      = $notificationResult['notifications'] ?? [];
80                 $notificationHeader = $notificationResult['header'] ?? '';
81
82
83                 if (!empty($notifications['notifications'])) {
84                         // Loop trough ever notification This creates an array with the output html for each
85                         // notification and apply the correct template according to the notificationtype (label).
86                         /** @var Notification $notification */
87                         foreach ($notifications['notifications'] as $notification) {
88                                 $notification_templates = [
89                                         'like'         => 'notifications/likes_item.tpl',
90                                         'dislike'      => 'notifications/dislikes_item.tpl',
91                                         'attend'       => 'notifications/attend_item.tpl',
92                                         'attendno'     => 'notifications/attend_item.tpl',
93                                         'attendmaybe'  => 'notifications/attend_item.tpl',
94                                         'friend'       => 'notifications/friends_item.tpl',
95                                         'comment'      => 'notifications/comments_item.tpl',
96                                         'post'         => 'notifications/posts_item.tpl',
97                                         'notification' => 'notifications/notification.tpl',
98                                 ];
99
100                                 $notificationTemplate = Renderer::getMarkupTemplate($notification_templates[$notification->getLabel()]);
101
102                                 $notificationContent[] = Renderer::replaceMacros($notificationTemplate, [
103                                         '$item_label' => $notification->getLabel(),
104                                         '$item_link'  => $notification->getLink(),
105                                         '$item_image' => $notification->getImage(),
106                                         '$item_url'   => $notification->getUrl(),
107                                         '$item_text'  => $notification->getText(),
108                                         '$item_when'  => $notification->getWhen(),
109                                         '$item_ago'   => $notification->getAgo(),
110                                         '$item_seen'  => $notification->isSeen(),
111                                 ]);
112                         }
113                 } else {
114                         $notificationNoContent = DI::l10n()->t('No more %s notifications.', $notifications['ident']);
115                 }
116
117                 $notificationShowLink = [
118                         'href' => (self::$showAll ? 'notifications/' . $notifications['ident'] : 'notifications/' . $notifications['ident'] . '?show=all'),
119                         'text' => (self::$showAll ? DI::l10n()->t('Show unread') : DI::l10n()->t('Show all')),
120                 ];
121
122                 return self::printContent($notificationHeader, $notificationContent, $notificationNoContent, $notificationShowLink);
123         }
124 }