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