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