]> git.mxchange.org Git - friendica.git/blob - src/Content/Widget/ContactBlock.php
Merge pull request #11265 from k-alin/6606-k-alin-mysql-unix-socket
[friendica.git] / src / Content / Widget / ContactBlock.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, 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\Text\HTML;
25 use Friendica\Core\Hook;
26 use Friendica\Core\Protocol;
27 use Friendica\Core\Renderer;
28 use Friendica\Database\DBA;
29 use Friendica\DI;
30 use Friendica\Model\Contact;
31 use Friendica\Model\User;
32
33 /**
34  * ContactBlock widget
35  *
36  * @author Hypolite Petovan
37  */
38 class ContactBlock
39 {
40         /**
41          * Get HTML for contact block
42          *
43          * @template widget/contacts.tpl
44          * @hook contact_block_end (contacts=>array, output=>string)
45          * @return string
46          */
47         public static function getHTML(array $profile, int $visitor_uid = null)
48         {
49                 $o = '';
50
51                 if (is_null($visitor_uid) || ($visitor_uid == $profile['uid'])) {
52                         $contact_uid = $profile['uid'];
53                 } else {
54                         $contact_uid = 0;
55                 }
56
57                 $shown = DI::pConfig()->get($profile['uid'], 'system', 'display_friend_count', 24);
58                 if ($shown == 0) {
59                         return $o;
60                 }
61
62                 if (!empty($profile['hide-friends'])) {
63                         return $o;
64                 }
65
66                 $contacts = [];
67
68                 $total = DBA::count('contact', [
69                         'uid' => $profile['uid'],
70                         'self' => false,
71                         'blocked' => false,
72                         'pending' => false,
73                         'hidden' => false,
74                         'archive' => false,
75                         'failed' => false,
76                         'network' => [Protocol::DFRN, Protocol::ACTIVITYPUB, Protocol::OSTATUS, Protocol::DIASPORA, Protocol::FEED],
77                 ]);
78
79                 $contacts_title = DI::l10n()->t('No contacts');
80
81                 $micropro = [];
82
83                 if ($total) {
84                         // Only show followed for personal accounts, followers for pages
85                         if ((($profile['account-type'] ?? '') ?: User::ACCOUNT_TYPE_PERSON) == User::ACCOUNT_TYPE_PERSON) {
86                                 $rel = [Contact::SHARING, Contact::FRIEND];
87                         } else {
88                                 $rel = [Contact::FOLLOWER, Contact::FRIEND];
89                         }
90
91                         $personal_contacts = DBA::selectToArray('contact', ['uri-id'], [
92                                 'uid' => $profile['uid'],
93                                 'self' => false,
94                                 'blocked' => false,
95                                 'pending' => false,
96                                 'hidden' => false,
97                                 'archive' => false,
98                                 'rel' => $rel,
99                                 'network' => Protocol::FEDERATED,
100                         ], ['limit' => $shown]);
101
102                         $contact_uriids = array_column($personal_contacts, 'uri-id');
103
104                         if (!empty($contact_uriids)) {
105                                 $contacts_stmt = DBA::select('contact', ['id', 'uid', 'addr', 'url', 'name', 'thumb', 'avatar', 'network'], ['uri-id' => $contact_uriids, 'uid' => $contact_uid]);
106
107                                 if (DBA::isResult($contacts_stmt)) {
108                                         $contacts_title = DI::l10n()->tt('%d Contact', '%d Contacts', $total);
109                                         $micropro = [];
110
111                                         while ($contact = DBA::fetch($contacts_stmt)) {
112                                                 $contacts[] = $contact;
113                                                 $micropro[] = HTML::micropro($contact, true, 'mpfriend');
114                                         }
115                                 }
116
117                                 DBA::close($contacts_stmt);
118                         }
119                 }
120
121                 $tpl = Renderer::getMarkupTemplate('widget/contacts.tpl');
122                 $o = Renderer::replaceMacros($tpl, [
123                         '$contacts' => $contacts_title,
124                         '$nickname' => $profile['nickname'],
125                         '$viewcontacts' => DI::l10n()->t('View Contacts'),
126                         '$micropro' => $micropro,
127                 ]);
128
129                 $arr = ['contacts' => $contacts, 'output' => $o];
130
131                 Hook::callAll('contact_block_end', $arr);
132
133                 return $o;
134         }
135 }