]> git.mxchange.org Git - friendica.git/blob - src/Module/BaseNotifications.php
Merge pull request #13128 from annando/owa
[friendica.git] / src / Module / BaseNotifications.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;
23
24 use Exception;
25 use Friendica\App;
26 use Friendica\App\Arguments;
27 use Friendica\BaseModule;
28 use Friendica\Content\Pager;
29 use Friendica\Core\L10n;
30 use Friendica\Core\Renderer;
31 use Friendica\Core\Session\Capability\IHandleUserSessions;
32 use Friendica\Core\System;
33 use Friendica\Navigation\Notifications\ValueObject\FormattedNotify;
34 use Friendica\Network\HTTPException\ForbiddenException;
35 use Friendica\Util\Profiler;
36 use Psr\Log\LoggerInterface;
37
38 /**
39  * Base Module for each tab of the notification display
40  *
41  * General possibility to print it as JSON as well
42  */
43 abstract class BaseNotifications extends BaseModule
44 {
45         /** @var array Array of URL parameters */
46         const URL_TYPES = [
47                 FormattedNotify::NETWORK  => 'network',
48                 FormattedNotify::SYSTEM   => 'system',
49                 FormattedNotify::HOME     => 'home',
50                 FormattedNotify::PERSONAL => 'personal',
51                 FormattedNotify::INTRO    => 'intros',
52         ];
53
54         /** @var array Array of the allowed notifications and their printable name */
55         const PRINT_TYPES = [
56                 FormattedNotify::NETWORK  => 'Network',
57                 FormattedNotify::SYSTEM   => 'System',
58                 FormattedNotify::HOME     => 'Home',
59                 FormattedNotify::PERSONAL => 'Personal',
60                 FormattedNotify::INTRO    => 'Introductions',
61         ];
62
63         /** @var array The array of access keys for notification pages */
64         const ACCESS_KEYS = [
65                 FormattedNotify::NETWORK  => 'w',
66                 FormattedNotify::SYSTEM   => 'y',
67                 FormattedNotify::HOME     => 'h',
68                 FormattedNotify::PERSONAL => 'r',
69                 FormattedNotify::INTRO    => 'i',
70         ];
71
72         /** @var int The default count of items per page */
73         const ITEMS_PER_PAGE = 20;
74         /** @var int The default limit of notifications per page */
75         const DEFAULT_PAGE_LIMIT = 80;
76
77         /** @var boolean True, if ALL entries should get shown */
78         protected $showAll;
79         /** @var int The determined start item of the current page */
80         protected $firstItemNum;
81
82         /** @var Arguments */
83         protected $args;
84
85         /**
86          * Collects all notifications from the backend
87          *
88          * @return array The determined notification array
89          *               ['header', 'notifications']
90          */
91         abstract public function getNotifications();
92
93         public function __construct(L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, IHandleUserSessions $session, array $server, array $parameters = [])
94         {
95                 parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
96
97                 if (!$session->getLocalUserId()) {
98                         throw new ForbiddenException($this->t('Permission denied.'));
99                 }
100
101                 $page = ($_REQUEST['page'] ?? 0) ?: 1;
102
103                 $this->firstItemNum = ($page * self::ITEMS_PER_PAGE) - self::ITEMS_PER_PAGE;
104                 $this->showAll      = ($_REQUEST['show'] ?? '') === 'all';
105         }
106
107         protected function rawContent(array $request = [])
108         {
109                 // If the last argument of the query is NOT json, return
110                 if ($this->args->get($this->args->getArgc() - 1) !== 'json') {
111                         return;
112                 }
113
114                 // Set the pager
115                 $pager = new Pager($this->l10n, $this->args->getQueryString(), self::ITEMS_PER_PAGE);
116
117                 // Add additional informations (needed for json output)
118                 $notifications = [
119                         'notifications' => $this->getNotifications(),
120                         'items_page'    => $pager->getItemsPerPage(),
121                         'page'          => $pager->getPage(),
122                 ];
123
124                 System::jsonExit($notifications);
125         }
126
127         /**
128          * Shows the printable result of notifications for a specific tab
129          *
130          * @param string $header        The notification header
131          * @param array  $notifications The array with the notifications
132          * @param string $noContent     The string in case there are no notifications
133          * @param array  $showLink      The possible links at the top
134          *
135          * @return string The rendered output
136          *
137          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
138          */
139         protected function printContent(string $header, array $notifications, string $noContent, array $showLink)
140         {
141                 // Get the nav tabs for the notification pages
142                 $tabs = $this->getTabs();
143
144                 // Set the pager
145                 $pager = new Pager($this->l10n, $this->args->getQueryString(), self::ITEMS_PER_PAGE);
146
147                 $notif_tpl = Renderer::getMarkupTemplate('notifications/notifications.tpl');
148                 return Renderer::replaceMacros($notif_tpl, [
149                         '$header'        => $header ?? $this->t('Notifications'),
150                         '$tabs'          => $tabs,
151                         '$notifications' => $notifications,
152                         '$noContent'     => $noContent,
153                         '$showLink'      => $showLink,
154                         '$paginate'      => $pager->renderMinimal(count($notifications))
155                 ]);
156         }
157
158         /**
159          * List of pages for the Notifications TabBar
160          *
161          * @return array with notifications TabBar data
162          * @throws Exception
163          */
164         private function getTabs()
165         {
166                 $selected = $this->args->get(1, '');
167
168                 $tabs = [];
169
170                 foreach (self::URL_TYPES as $type => $url) {
171                         $tabs[] = [
172                                 'label'     => $this->t(self::PRINT_TYPES[$type]),
173                                 'url'       => 'notifications/' . $url,
174                                 'sel'       => (($selected == $url) ? 'active' : ''),
175                                 'id'        => $type . '-tab',
176                                 'accesskey' => self::ACCESS_KEYS[$type],
177                         ];
178                 }
179
180                 return $tabs;
181         }
182 }