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