]> git.mxchange.org Git - friendica.git/blob - src/Module/Notifications/Notifications.php
af8b14512b35795515aaef0ff9e5ad254e82c166
[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\Navigation\Notifications\Collection\FormattedNotifications;
29 use Friendica\Navigation\Notifications\ValueObject\FormattedNotification;
30 use Friendica\Network\HTTPException\InternalServerErrorException;
31
32 /**
33  * Prints all notification types except introduction:
34  * - Network
35  * - System
36  * - Personal
37  * - Home
38  */
39 class Notifications extends BaseNotifications
40 {
41         /**
42          * {@inheritDoc}
43          */
44         public static function getNotifications()
45         {
46                 $notificationHeader = '';
47                 $notifications = [];
48
49                 /** @var \Friendica\Navigation\Notifications\Factory\FormattedNotification $factory */
50                 $factory = DI::getDice()->create(\Friendica\Navigation\Notifications\Factory\FormattedNotification::class);
51
52                 if ((DI::args()->get(1) == 'network')) {
53                         $notificationHeader = DI::l10n()->t('Network Notifications');
54                         $notifications      = [
55                                 'ident'        => FormattedNotification::NETWORK,
56                                 'notifications' => $factory->getNetworkList(self::$showAll, self::$firstItemNum, self::ITEMS_PER_PAGE),
57                         ];
58                 } elseif ((DI::args()->get(1) == 'system')) {
59                         $notificationHeader = DI::l10n()->t('System Notifications');
60                         $notifications      = [
61                                 'ident'        => FormattedNotification::SYSTEM,
62                                 'notifications' => $factory->getSystemList(self::$showAll, self::$firstItemNum, self::ITEMS_PER_PAGE),
63                         ];
64                 } elseif ((DI::args()->get(1) == 'personal')) {
65                         $notificationHeader = DI::l10n()->t('Personal Notifications');
66                         $notifications      = [
67                                 'ident'        => FormattedNotification::PERSONAL,
68                                 'notifications' => $factory->getPersonalList(self::$showAll, self::$firstItemNum, self::ITEMS_PER_PAGE),
69                         ];
70                 } elseif ((DI::args()->get(1) == 'home')) {
71                         $notificationHeader = DI::l10n()->t('Home Notifications');
72                         $notifications      = [
73                                 'ident'        => FormattedNotification::HOME,
74                                 'notifications' => $factory->getHomeList(self::$showAll, self::$firstItemNum, self::ITEMS_PER_PAGE),
75                         ];
76                 } else {
77                         DI::baseUrl()->redirect('notifications');
78                 }
79
80                 return [
81                         'header'        => $notificationHeader,
82                         'notifications' => $notifications,
83                 ];
84         }
85
86         public static function content(array $parameters = [])
87         {
88                 Nav::setSelected('notifications');
89
90                 $notificationContent   = [];
91                 $notificationNoContent = '';
92
93                 $notificationResult = self::getNotifications();
94                 $notifications      = $notificationResult['notifications'] ?? [];
95                 $notificationHeader = $notificationResult['header'] ?? '';
96
97                 if (!empty($notifications['notifications'])) {
98                         $notificationTemplates = [
99                                 'like'         => 'notifications/likes_item.tpl',
100                                 'dislike'      => 'notifications/dislikes_item.tpl',
101                                 'attend'       => 'notifications/attend_item.tpl',
102                                 'attendno'     => 'notifications/attend_item.tpl',
103                                 'attendmaybe'  => 'notifications/attend_item.tpl',
104                                 'friend'       => 'notifications/friends_item.tpl',
105                                 'comment'      => 'notifications/comments_item.tpl',
106                                 'post'         => 'notifications/posts_item.tpl',
107                                 'notification' => 'notifications/notification.tpl',
108                         ];
109                         // Loop trough ever notification This creates an array with the output html for each
110                         // notification and apply the correct template according to the notificationtype (label).
111                         /** @var FormattedNotification $Notification */
112                         foreach ($notifications['notifications'] as $Notification) {
113                                 $notificationArray = $Notification->toArray();
114
115                                 $notificationTemplate = Renderer::getMarkupTemplate($notificationTemplates[$notificationArray['label']]);
116
117                                 $notificationContent[] = Renderer::replaceMacros($notificationTemplate, [
118                                         '$notification' => $notificationArray
119                                 ]);
120                         }
121                 } else {
122                         $notificationNoContent = DI::l10n()->t('No more %s notifications.', $notificationResult['ident']);
123                 }
124
125                 $notificationShowLink = [
126                         'href' => (self::$showAll ? 'notifications/' . $notifications['ident'] : 'notifications/' . $notifications['ident'] . '?show=all'),
127                         'text' => (self::$showAll ? DI::l10n()->t('Show unread') : DI::l10n()->t('Show all')),
128                 ];
129
130                 return self::printContent($notificationHeader, $notificationContent, $notificationNoContent, $notificationShowLink);
131         }
132 }