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