"network/group" fragments are removed
[friendica.git/.git] / src / Content / Widget / VCard.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2024, 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\Content\Widget;
23
24 use Friendica\Content\ContactSelector;
25 use Friendica\Content\Text\BBCode;
26 use Friendica\Core\Logger;
27 use Friendica\Core\Protocol;
28 use Friendica\Core\Renderer;
29 use Friendica\DI;
30 use Friendica\Model\Contact;
31 use Friendica\Util\Strings;
32
33 /**
34  * VCard widget
35  *
36  * @author Michael Vogel
37  */
38 class VCard
39 {
40         /**
41          * Get HTML for vcard block
42          *
43          * @template widget/vcard.tpl
44          * @param array $contact
45          * @param bool  $hide_mention
46          * @return string
47          */
48         public static function getHTML(array $contact, bool $hide_mention = false): string
49         {
50                 if (!isset($contact['network']) || !isset($contact['id'])) {
51                         Logger::warning('Incomplete contact', ['contact' => $contact ?? []]);
52                 }
53
54                 $contact_url = Contact::getProfileLink($contact);
55
56                 if ($contact['network'] != '') {
57                         $network_link   = Strings::formatNetworkName($contact['network'], $contact_url);
58                         $network_avatar = ContactSelector::networkToIcon($contact['network'], $contact_url);
59                 } else {
60                         $network_link   = '';
61                         $network_avatar = '';
62                 }
63
64                 $follow_link      = '';
65                 $unfollow_link    = '';
66                 $wallmessage_link = '';
67                 $mention_label    = '';
68                 $mention_link     = '';
69                 $showgroup_link   = '';
70
71                 $photo   = Contact::getPhoto($contact);
72
73                 if (DI::userSession()->getLocalUserId()) {
74                         if ($contact['uid']) {
75                                 $id      = $contact['id'];
76                                 $rel     = $contact['rel'];
77                                 $pending = $contact['pending'];
78                         } else {
79                                 $pcontact = Contact::selectFirst([], ['uid' => DI::userSession()->getLocalUserId(), 'uri-id' => $contact['uri-id'], 'deleted' => false]);
80
81                                 $id      = $pcontact['id'] ?? 0;
82                                 $rel     = $pcontact['rel'] ?? Contact::NOTHING;
83                                 $pending = $pcontact['pending'] ?? false;
84
85                                 if (!empty($pcontact) && in_array($pcontact['network'], [Protocol::MAIL, Protocol::FEED])) {
86                                         $photo = Contact::getPhoto($pcontact);
87                                 }
88                         }
89
90                         if (empty($contact['self']) && Protocol::supportsFollow($contact['network'])) {
91                                 if (in_array($rel, [Contact::SHARING, Contact::FRIEND])) {
92                                         $unfollow_link = 'contact/unfollow?url=' . urlencode($contact_url) . '&auto=1';
93                                 } elseif (!$pending) {
94                                         $follow_link = 'contact/follow?url=' . urlencode($contact_url) . '&auto=1';
95                                 }
96                         }
97
98                         if (in_array($rel, [Contact::FOLLOWER, Contact::FRIEND]) && Contact::canReceivePrivateMessages($contact)) {
99                                 $wallmessage_link = 'message/new/' . $id;
100                         }
101
102                         if ($contact['contact-type'] == Contact::TYPE_COMMUNITY) {
103                                 if (!$hide_mention) {
104                                         $mention_label  = DI::l10n()->t('Post to group');
105                                         $mention_link   = 'compose/0?body=!' . $contact['addr'];
106                                 }
107                                 $showgroup_link = 'contact/' . $id . '/conversations';
108                         } elseif (!$hide_mention) {
109                                 $mention_label = DI::l10n()->t('Mention');
110                                 $mention_link  = 'compose/0?body=@' . $contact['addr'];
111                         }
112                 }
113
114                 return Renderer::replaceMacros(Renderer::getMarkupTemplate('widget/vcard.tpl'), [
115                         '$contact'          => $contact,
116                         '$photo'            => $photo,
117                         '$url'              => Contact::magicLinkByContact($contact, $contact_url),
118                         '$about'            => BBCode::convertForUriId($contact['uri-id'] ?? 0, $contact['about'] ?? ''),
119                         '$xmpp'             => DI::l10n()->t('XMPP:'),
120                         '$matrix'           => DI::l10n()->t('Matrix:'),
121                         '$location'         => DI::l10n()->t('Location:'),
122                         '$network_link'     => $network_link,
123                         '$network_avatar'   => $network_avatar,
124                         '$network'          => DI::l10n()->t('Network:'),
125                         '$account_type'     => Contact::getAccountType($contact['contact-type']),
126                         '$follow'           => DI::l10n()->t('Follow'),
127                         '$follow_link'      => $follow_link,
128                         '$unfollow'         => DI::l10n()->t('Unfollow'),
129                         '$unfollow_link'    => $unfollow_link,
130                         '$wallmessage'      => DI::l10n()->t('Message'),
131                         '$wallmessage_link' => $wallmessage_link,
132                         '$mention'          => $mention_label,
133                         '$mention_link'     => $mention_link,
134                         '$showgroup'        => DI::l10n()->t('View group'),
135                         '$showgroup_link'   => $showgroup_link,
136                 ]);
137         }
138 }