]> git.mxchange.org Git - friendica.git/blob - src/Module/BaseNotifications.php
be2ad16b2bb678e95753ffc98aff33d05b869db4
[friendica.git] / src / Module / BaseNotifications.php
1 <?php
2
3 namespace Friendica\Module;
4
5 use Exception;
6 use Friendica\BaseModule;
7 use Friendica\Content\Pager;
8 use Friendica\Core\Renderer;
9 use Friendica\Core\System;
10 use Friendica\DI;
11 use Friendica\Model\Notify;
12 use Friendica\Network\HTTPException\ForbiddenException;
13
14 abstract class BaseNotifications extends BaseModule
15 {
16         /** @var array Array of URL parameters */
17         const URL_TYPES = [
18                 Notify::NETWORK  => 'network',
19                 Notify::SYSTEM   => 'system',
20                 Notify::HOME     => 'home',
21                 Notify::PERSONAL => 'personal',
22                 Notify::INTRO    => 'intros',
23         ];
24
25         /** @var array Array of the allowed notifies and their printable name */
26         const PRINT_TYPES = [
27                 Notify::NETWORK  => 'Network',
28                 Notify::SYSTEM   => 'System',
29                 Notify::HOME     => 'Home',
30                 Notify::PERSONAL => 'Personal',
31                 Notify::INTRO    => 'Introductions',
32         ];
33
34         /** @var array The array of access keys for notify pages */
35         const ACCESS_KEYS = [
36                 Notify::NETWORK  => 'w',
37                 Notify::SYSTEM   => 'y',
38                 Notify::HOME     => 'h',
39                 Notify::PERSONAL => 'r',
40                 Notify::INTRO    => 'i',
41         ];
42
43         const PER_PAGE = 20;
44
45         protected static $show;
46         protected static $start;
47
48         /**
49          * Collects all notifies from the backend
50          *
51          * @return array The determined notification array
52          *               ['header', 'notifs']
53          */
54         abstract public static function getNotifies();
55
56         public static function init(array $parameters = [])
57         {
58                 if (!local_user()) {
59                         throw new ForbiddenException(DI::l10n()->t('Permission denied.'));
60                 }
61
62                 $page = ($_REQUEST['page'] ?? 0) ?: 1;
63
64                 self::$start = ($page * self::PER_PAGE) - self::PER_PAGE;
65                 self::$show = ($_REQUEST['show'] ?? '') === 'all';
66         }
67
68         public static function post(array $parameters = [])
69         {
70                 $request_id = DI::args()->get(1);
71
72                 if ($request_id === 'all') {
73                         return;
74                 }
75
76                 if ($request_id) {
77                         $intro = DI::intro()->selectFirst(['id' => $request_id, 'uid' => local_user()]);
78
79                         switch ($_POST['submit']) {
80                                 case DI::l10n()->t('Discard'):
81                                         $intro->discard();
82                                         break;
83                                 case DI::l10n()->t('Ignore'):
84                                         $intro->ignore();
85                                         break;
86                         }
87
88                         DI::baseUrl()->redirect('notifications/intros');
89                 }
90         }
91
92         public static function rawContent(array $parameters = [])
93         {
94                 // If the last argument of the query is NOT json, return
95                 if (DI::args()->get(DI::args()->getArgc() - 1) !== 'json') {
96                         return;
97                 }
98
99                 System::jsonExit(static::getNotifies()['notifs'] ?? []);
100         }
101
102         public static function printContent(string $notif_header, array $notif_content, string $notif_nocontent, array $notif_show_lnk)
103         {
104                 // Get the nav tabs for the notification pages
105                 $tabs = self::getTabs();
106
107                 // Set the pager
108                 $pager = new Pager(DI::args()->getQueryString(), self::PER_PAGE);
109
110                 $notif_tpl = Renderer::getMarkupTemplate('notifications.tpl');
111                 return Renderer::replaceMacros($notif_tpl, [
112                         '$notif_header'    => $notif_header ?? DI::l10n()->t('Notifications'),
113                         '$tabs'            => $tabs,
114                         '$notif_content'   => $notif_content,
115                         '$notif_nocontent' => $notif_nocontent,
116                         '$notif_show_lnk'  => $notif_show_lnk,
117                         '$notif_paginate'  => $pager->renderMinimal(count($notif_content))
118                 ]);
119         }
120
121         /**
122          * List of pages for the Notifications TabBar
123          *
124          * @return array with with notifications TabBar data
125          * @throws Exception
126          */
127         private static function getTabs()
128         {
129                 $selected = DI::args()->get(1, '');
130
131                 $tabs = [];
132
133                 foreach (self::URL_TYPES as $type => $url) {
134                         $tabs[] = [
135                                 'label'     => DI::l10n()->t(self::PRINT_TYPES[$type]),
136                                 'url'       => 'notifications/' . $url,
137                                 'sel'       => (($selected == $url) ? 'active' : ''),
138                                 'id'        => $type . '-tab',
139                                 'accesskey' => self::ACCESS_KEYS[$type],
140                         ];
141                 }
142
143                 return $tabs;
144         }
145 }