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