]> git.mxchange.org Git - friendica.git/blob - src/Content/Widget/ContactBlock.php
Merge pull request #8765 from annando/fix-pubkey
[friendica.git] / src / Content / Widget / ContactBlock.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\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)
48         {
49                 $o = '';
50
51                 $shown = DI::pConfig()->get($profile['uid'], 'system', 'display_friend_count', 24);
52                 if ($shown == 0) {
53                         return $o;
54                 }
55
56                 if (!empty($profile['hide-friends'])) {
57                         return $o;
58                 }
59
60                 $contacts = [];
61
62                 $total = DBA::count('contact', [
63                         'uid' => $profile['uid'],
64                         'self' => false,
65                         'blocked' => false,
66                         'pending' => false,
67                         'hidden' => false,
68                         'archive' => false,
69                         'network' => [Protocol::DFRN, Protocol::ACTIVITYPUB, Protocol::OSTATUS, Protocol::DIASPORA, Protocol::FEED],
70                 ]);
71
72                 $contacts_title = DI::l10n()->t('No contacts');
73
74                 $micropro = [];
75
76                 if ($total) {
77                         // Only show followed for personal accounts, followers for pages
78                         if ((($profile['account-type'] ?? '') ?: User::ACCOUNT_TYPE_PERSON) == User::ACCOUNT_TYPE_PERSON) {
79                                 $rel = [Contact::SHARING, Contact::FRIEND];
80                         } else {
81                                 $rel = [Contact::FOLLOWER, Contact::FRIEND];
82                         }
83
84                         $contact_ids_stmt = DBA::select('contact', ['id'], [
85                                 'uid' => $profile['uid'],
86                                 'self' => false,
87                                 'blocked' => false,
88                                 'pending' => false,
89                                 'hidden' => false,
90                                 'archive' => false,
91                                 'rel' => $rel,
92                                 'network' => Protocol::FEDERATED,
93                         ], ['limit' => $shown]);
94
95                         if (DBA::isResult($contact_ids_stmt)) {
96                                 $contact_ids = [];
97                                 while($contact = DBA::fetch($contact_ids_stmt)) {
98                                         $contact_ids[] = $contact["id"];
99                                 }
100
101                                 $contacts_stmt = DBA::select('contact', ['id', 'uid', 'addr', 'url', 'name', 'thumb', 'network'], ['id' => $contact_ids]);
102
103                                 if (DBA::isResult($contacts_stmt)) {
104                                         $contacts_title = DI::l10n()->tt('%d Contact', '%d Contacts', $total);
105                                         $micropro = [];
106
107                                         while ($contact = DBA::fetch($contacts_stmt)) {
108                                                 $contacts[] = $contact;
109                                                 $micropro[] = HTML::micropro($contact, true, 'mpfriend');
110                                         }
111                                 }
112
113                                 DBA::close($contacts_stmt);
114                         }
115
116                         DBA::close($contact_ids_stmt);
117                 }
118
119                 $tpl = Renderer::getMarkupTemplate('widget/contacts.tpl');
120                 $o = Renderer::replaceMacros($tpl, [
121                         '$contacts' => $contacts_title,
122                         '$nickname' => $profile['nickname'],
123                         '$viewcontacts' => DI::l10n()->t('View Contacts'),
124                         '$micropro' => $micropro,
125                 ]);
126
127                 $arr = ['contacts' => $contacts, 'output' => $o];
128
129                 Hook::callAll('contact_block_end', $arr);
130
131                 return $o;
132         }
133 }