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