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