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