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