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