]> git.mxchange.org Git - friendica.git/blob - src/Module/Notifications/Notifications.php
Merge branch '2021.03-rc' into copyright-2021
[friendica.git] / src / Module / Notifications / Notifications.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Module\Notifications;
23
24 use Friendica\Content\Nav;
25 use Friendica\Core\Renderer;
26 use Friendica\DI;
27 use Friendica\Module\BaseNotifications;
28 use Friendica\Object\Notification\Notification;
29
30 /**
31  * Prints all notification types except introduction:
32  * - Network
33  * - System
34  * - Personal
35  * - Home
36  */
37 class Notifications extends BaseNotifications
38 {
39         /**
40          * {@inheritDoc}
41          */
42         public static function getNotifications()
43         {
44                 $notificationHeader = '';
45                 /** @var Notification[] $notifications */
46                 $notifications = [];
47
48                 // Get the network notifications
49                 if ((DI::args()->get(1) == 'network')) {
50                         $notificationHeader = DI::l10n()->t('Network Notifications');
51                         $notifications      = [
52                                 'ident'        => Notification::NETWORK,
53                                 'notifications' => DI::notification()->getNetworkList(self::$showAll, self::$firstItemNum, self::ITEMS_PER_PAGE),
54                         ];
55
56                         // Get the system notifications
57                 } elseif ((DI::args()->get(1) == 'system')) {
58                         $notificationHeader = DI::l10n()->t('System Notifications');
59                         $notifications      = [
60                                 'ident'        => Notification::SYSTEM,
61                                 'notifications' => DI::notification()->getSystemList(self::$showAll, self::$firstItemNum, self::ITEMS_PER_PAGE),
62                         ];
63
64                         // Get the personal notifications
65                 } elseif ((DI::args()->get(1) == 'personal')) {
66                         $notificationHeader = DI::l10n()->t('Personal Notifications');
67                         $notifications      = [
68                                 'ident'        => Notification::PERSONAL,
69                                 'notifications' => DI::notification()->getPersonalList(self::$showAll, self::$firstItemNum, self::ITEMS_PER_PAGE),
70                         ];
71
72                         // Get the home notifications
73                 } elseif ((DI::args()->get(1) == 'home')) {
74                         $notificationHeader = DI::l10n()->t('Home Notifications');
75                         $notifications      = [
76                                 'ident'        => Notification::HOME,
77                                 'notifications' => DI::notification()->getHomeList(self::$showAll, self::$firstItemNum, self::ITEMS_PER_PAGE),
78                         ];
79                         // fallback - redirect to main page
80                 } else {
81                         DI::baseUrl()->redirect('notifications');
82                 }
83
84                 return [
85                         'header'        => $notificationHeader,
86                         'notifications' => $notifications,
87                 ];
88         }
89
90         public static function content(array $parameters = [])
91         {
92                 Nav::setSelected('notifications');
93
94                 $notificationContent   = [];
95                 $notificationNoContent = '';
96
97                 $notificationResult = self::getNotifications();
98                 $notifications      = $notificationResult['notifications'] ?? [];
99                 $notificationHeader = $notificationResult['header'] ?? '';
100
101
102                 if (!empty($notifications['notifications'])) {
103                         // Loop trough ever notification This creates an array with the output html for each
104                         // notification and apply the correct template according to the notificationtype (label).
105                         /** @var Notification $notification */
106                         foreach ($notifications['notifications'] as $notification) {
107                                 $notification_templates = [
108                                         'like'         => 'notifications/likes_item.tpl',
109                                         'dislike'      => 'notifications/dislikes_item.tpl',
110                                         'attend'       => 'notifications/attend_item.tpl',
111                                         'attendno'     => 'notifications/attend_item.tpl',
112                                         'attendmaybe'  => 'notifications/attend_item.tpl',
113                                         'friend'       => 'notifications/friends_item.tpl',
114                                         'comment'      => 'notifications/comments_item.tpl',
115                                         'post'         => 'notifications/posts_item.tpl',
116                                         'notification' => 'notifications/notification.tpl',
117                                 ];
118
119                                 $notificationTemplate = Renderer::getMarkupTemplate($notification_templates[$notification->getLabel()]);
120
121                                 $notificationContent[] = Renderer::replaceMacros($notificationTemplate, [
122                                         '$item_label' => $notification->getLabel(),
123                                         '$item_link'  => $notification->getLink(),
124                                         '$item_image' => $notification->getImage(),
125                                         '$item_url'   => $notification->getUrl(),
126                                         '$item_text'  => $notification->getText(),
127                                         '$item_when'  => $notification->getWhen(),
128                                         '$item_ago'   => $notification->getAgo(),
129                                         '$item_seen'  => $notification->isSeen(),
130                                 ]);
131                         }
132                 } else {
133                         $notificationNoContent = DI::l10n()->t('No more %s notifications.', $notifications['ident']);
134                 }
135
136                 $notificationShowLink = [
137                         'href' => (self::$showAll ? 'notifications/' . $notifications['ident'] : 'notifications/' . $notifications['ident'] . '?show=all'),
138                         'text' => (self::$showAll ? DI::l10n()->t('Show unread') : DI::l10n()->t('Show all')),
139                 ];
140
141                 return self::printContent($notificationHeader, $notificationContent, $notificationNoContent, $notificationShowLink);
142         }
143 }