]> git.mxchange.org Git - friendica.git/blob - src/Module/BaseNotifications.php
Merge pull request #11005 from nupplaphil/feat/module_di
[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;
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\System;
32 use Friendica\Navigation\Notifications\ValueObject\FormattedNotification;
33 use Friendica\Network\HTTPException\ForbiddenException;
34 use Friendica\Util\Profiler;
35 use Psr\Log\LoggerInterface;
36
37 /**
38  * Base Module for each tab of the notification display
39  *
40  * General possibility to print it as JSON as well
41  */
42 abstract class BaseNotifications extends BaseModule
43 {
44         /** @var array Array of URL parameters */
45         const URL_TYPES = [
46                 FormattedNotification::NETWORK  => 'network',
47                 FormattedNotification::SYSTEM   => 'system',
48                 FormattedNotification::HOME     => 'home',
49                 FormattedNotification::PERSONAL => 'personal',
50                 FormattedNotification::INTRO    => 'intros',
51         ];
52
53         /** @var array Array of the allowed notifications and their printable name */
54         const PRINT_TYPES = [
55                 FormattedNotification::NETWORK  => 'Network',
56                 FormattedNotification::SYSTEM   => 'System',
57                 FormattedNotification::HOME     => 'Home',
58                 FormattedNotification::PERSONAL => 'Personal',
59                 FormattedNotification::INTRO    => 'Introductions',
60         ];
61
62         /** @var array The array of access keys for notification pages */
63         const ACCESS_KEYS = [
64                 FormattedNotification::NETWORK  => 'w',
65                 FormattedNotification::SYSTEM   => 'y',
66                 FormattedNotification::HOME     => 'h',
67                 FormattedNotification::PERSONAL => 'r',
68                 FormattedNotification::INTRO    => 'i',
69         ];
70
71         /** @var int The default count of items per page */
72         const ITEMS_PER_PAGE = 20;
73         /** @var int The default limit of notifications per page */
74         const DEFAULT_PAGE_LIMIT = 80;
75
76         /** @var boolean True, if ALL entries should get shown */
77         protected $showAll;
78         /** @var int The determined start item of the current page */
79         protected $firstItemNum;
80
81         /** @var Arguments */
82         protected $args;
83
84         /**
85          * Collects all notifications from the backend
86          *
87          * @return array The determined notification array
88          *               ['header', 'notifications']
89          */
90         abstract public function getNotifications();
91
92         public function __construct(L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, array $server, array $parameters = [])
93         {
94                 parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
95
96                 if (!local_user()) {
97                         throw new ForbiddenException($this->t('Permission denied.'));
98                 }
99
100                 $page = ($_REQUEST['page'] ?? 0) ?: 1;
101
102                 $this->firstItemNum = ($page * self::ITEMS_PER_PAGE) - self::ITEMS_PER_PAGE;
103                 $this->showAll      = ($_REQUEST['show'] ?? '') === 'all';
104         }
105
106         protected function rawContent(array $request = [])
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 = $this->getTabs();
142
143                 // Set the pager
144                 $pager = new Pager($this->l10n, $this->args->getQueryString(), self::ITEMS_PER_PAGE);
145
146                 $notif_tpl = Renderer::getMarkupTemplate('notifications/notifications.tpl');
147                 return Renderer::replaceMacros($notif_tpl, [
148                         '$header'        => $header ?? $this->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 function getTabs()
164         {
165                 $selected = $this->args->get(1, '');
166
167                 $tabs = [];
168
169                 foreach (self::URL_TYPES as $type => $url) {
170                         $tabs[] = [
171                                 'label'     => $this->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 }