]> git.mxchange.org Git - friendica.git/blob - src/Module/Notifications/Introductions.php
Merge pull request #9343 from vinzv/9337-fix-pwa-manifest
[friendica.git] / src / Module / Notifications / Introductions.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
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\Notifications;
23
24 use Friendica\Content\ContactSelector;
25 use Friendica\Content\Nav;
26 use Friendica\Core\Protocol;
27 use Friendica\Core\Renderer;
28 use Friendica\Database\DBA;
29 use Friendica\DI;
30 use Friendica\Model\User;
31 use Friendica\Module\BaseNotifications;
32 use Friendica\Object\Notification\Introduction;
33
34 /**
35  * Prints notifications about introduction
36  */
37 class Introductions extends BaseNotifications
38 {
39         /**
40          * @inheritDoc
41          */
42         public static function getNotifications()
43         {
44                 $id  = (int)DI::args()->get(2, 0);
45                 $all = DI::args()->get(2) == 'all';
46
47                 $notifications = [
48                         'ident'         => 'introductions',
49                         'notifications' => DI::notificationIntro()->getList($all, self::$firstItemNum, self::ITEMS_PER_PAGE, $id),
50                 ];
51
52                 return [
53                         'header'        => DI::l10n()->t('Notifications'),
54                         'notifications' => $notifications,
55                 ];
56         }
57
58         public static function content(array $parameters = [])
59         {
60                 Nav::setSelected('introductions');
61
62                 $all = DI::args()->get(2) == 'all';
63
64                 $notificationContent   = [];
65                 $notificationNoContent = '';
66
67                 $notificationResult = self::getNotifications();
68                 $notifications      = $notificationResult['notifications'] ?? [];
69                 $notificationHeader = $notificationResult['header'] ?? '';
70
71                 $notificationSuggestions = Renderer::getMarkupTemplate('notifications/suggestions.tpl');
72                 $notificationTemplate    = Renderer::getMarkupTemplate('notifications/intros.tpl');
73
74                 // The link to switch between ignored and normal connection requests
75                 $notificationShowLink = [
76                         'href' => (!$all ? 'notifications/intros/all' : 'notifications/intros'),
77                         'text' => (!$all ? DI::l10n()->t('Show Ignored Requests') : DI::l10n()->t('Hide Ignored Requests')),
78                 ];
79
80                 $owner = User::getOwnerDataById(local_user());
81         
82                 // Loop through all introduction notifications.This creates an array with the output html for each
83                 // introduction
84                 /** @var Introduction $notification */
85                 foreach ($notifications['notifications'] as $notification) {
86
87                         // There are two kind of introduction. Contacts suggested by other contacts and normal connection requests.
88                         // We have to distinguish between these two because they use different data.
89                         switch ($notification->getLabel()) {
90                                 case 'friend_suggestion':
91                                         $notificationContent[] = Renderer::replaceMacros($notificationSuggestions, [
92                                                 '$type'                  => $notification->getLabel(),
93                                                 '$str_notification_type' => DI::l10n()->t('Notification type:'),
94                                                 '$str_type'              => $notification->getType(),
95                                                 '$intro_id'              => $notification->getIntroId(),
96                                                 '$lbl_madeby'            => DI::l10n()->t('Suggested by:'),
97                                                 '$madeby'                => $notification->getMadeBy(),
98                                                 '$madeby_url'            => $notification->getMadeByUrl(),
99                                                 '$madeby_zrl'            => $notification->getMadeByZrl(),
100                                                 '$madeby_addr'           => $notification->getMadeByAddr(),
101                                                 '$contact_id'            => $notification->getContactId(),
102                                                 '$photo'                 => $notification->getPhoto(),
103                                                 '$fullname'              => $notification->getName(),
104                                                 '$dfrn_url'              => $owner['url'],
105                                                 '$url'                   => $notification->getUrl(),
106                                                 '$zrl'                   => $notification->getZrl(),
107                                                 '$lbl_url'               => DI::l10n()->t('Profile URL'),
108                                                 '$addr'                  => $notification->getAddr(),
109                                                 '$action'                => 'follow',
110                                                 '$approve'               => DI::l10n()->t('Approve'),
111                                                 '$note'                  => $notification->getNote(),
112                                                 '$ignore'                => DI::l10n()->t('Ignore'),
113                                                 '$discard'               => DI::l10n()->t('Discard'),
114                                         ]);
115                                         break;
116
117                                 // Normal connection requests
118                                 default:
119                                         if ($notification->getNetwork() === Protocol::DFRN) {
120                                                 $lbl_knowyou = DI::l10n()->t('Claims to be known to you: ');
121                                                 $knowyou     = ($notification->getKnowYou() ? DI::l10n()->t('Yes') : DI::l10n()->t('No'));
122                                         } else {
123                                                 $lbl_knowyou = '';
124                                                 $knowyou = '';
125                                         }
126
127                                         $helptext  = DI::l10n()->t('Shall your connection be bidirectional or not?');
128                                         $helptext2 = DI::l10n()->t('Accepting %s as a friend allows %s to subscribe to your posts, and you will also receive updates from them in your news feed.', $notification->getName(), $notification->getName());
129                                         $helptext3 = DI::l10n()->t('Accepting %s as a subscriber allows them to subscribe to your posts, but you will not receive updates from them in your news feed.', $notification->getName());
130                 
131                                         $friend = ['duplex', DI::l10n()->t('Friend'), '1', $helptext2, true];
132                                         $follower = ['duplex', DI::l10n()->t('Subscriber'), '0', $helptext3, false];
133
134                                         $contact = DBA::selectFirst('contact', ['network', 'protocol'], ['id' => $notification->getContactId()]);
135
136                                         if (($contact['network'] != Protocol::DFRN) || ($contact['protocol'] == Protocol::ACTIVITYPUB)) {
137                                                 $action = 'follow_confirm';
138                                         } else {
139                                                 $action = 'dfrn_confirm';
140                                         }
141
142                                         $header = $notification->getName();
143
144                                         if ($notification->getAddr() != '') {
145                                                 $header .= ' <' . $notification->getAddr() . '>';
146                                         }
147
148                                         $header .= ' (' . ContactSelector::networkToName($notification->getNetwork(), $notification->getUrl()) . ')';
149
150                                         if ($notification->getNetwork() != Protocol::DIASPORA) {
151                                                 $discard = DI::l10n()->t('Discard');
152                                         } else {
153                                                 $discard = '';
154                                         }
155
156                                         $notificationContent[] = Renderer::replaceMacros($notificationTemplate, [
157                                                 '$type'                  => $notification->getLabel(),
158                                                 '$header'                => $header,
159                                                 '$str_notification_type' => DI::l10n()->t('Notification type:'),
160                                                 '$str_type'              => $notification->getType(),
161                                                 '$dfrn_id'               => $notification->getDfrnId(),
162                                                 '$uid'                   => $notification->getUid(),
163                                                 '$intro_id'              => $notification->getIntroId(),
164                                                 '$contact_id'            => $notification->getContactId(),
165                                                 '$photo'                 => $notification->getPhoto(),
166                                                 '$fullname'              => $notification->getName(),
167                                                 '$location'              => $notification->getLocation(),
168                                                 '$lbl_location'          => DI::l10n()->t('Location:'),
169                                                 '$about'                 => $notification->getAbout(),
170                                                 '$lbl_about'             => DI::l10n()->t('About:'),
171                                                 '$keywords'              => $notification->getKeywords(),
172                                                 '$lbl_keywords'          => DI::l10n()->t('Tags:'),
173                                                 '$hidden'                => ['hidden', DI::l10n()->t('Hide this contact from others'), $notification->isHidden(), ''],
174                                                 '$lbl_connection_type'   => $helptext,
175                                                 '$friend'                => $friend,
176                                                 '$follower'              => $follower,
177                                                 '$url'                   => $notification->getUrl(),
178                                                 '$zrl'                   => $notification->getZrl(),
179                                                 '$lbl_url'               => DI::l10n()->t('Profile URL'),
180                                                 '$addr'                  => $notification->getAddr(),
181                                                 '$lbl_knowyou'           => $lbl_knowyou,
182                                                 '$lbl_network'           => DI::l10n()->t('Network:'),
183                                                 '$network'               => ContactSelector::networkToName($notification->getNetwork(), $notification->getUrl()),
184                                                 '$knowyou'               => $knowyou,
185                                                 '$approve'               => DI::l10n()->t('Approve'),
186                                                 '$note'                  => $notification->getNote(),
187                                                 '$ignore'                => DI::l10n()->t('Ignore'),
188                                                 '$discard'               => $discard,
189                                                 '$action'                => $action,
190                                         ]);
191                                         break;
192                         }
193                 }
194
195                 if (count($notifications['notifications']) == 0) {
196                         notice(DI::l10n()->t('No introductions.'));
197                         $notificationNoContent = DI::l10n()->t('No more %s notifications.', $notifications['ident']);
198                 }
199
200                 return self::printContent($notificationHeader, $notificationContent, $notificationNoContent, $notificationShowLink);
201         }
202 }