]> git.mxchange.org Git - friendica.git/blob - src/Content/Widget/ContactBlock.php
e4bf8e7eaf80d8e499944258379eeb22fa313923
[friendica.git] / src / Content / Widget / ContactBlock.php
1 <?php
2
3 /*
4  * @file src/Content/Widget/ContactBlock.php
5  */
6
7 namespace Friendica\Content\Widget;
8
9 use Friendica\Content\Text\HTML;
10 use Friendica\Core\Hook;
11 use Friendica\Core\L10n;
12 use Friendica\Core\PConfig;
13 use Friendica\Core\Protocol;
14 use Friendica\Core\Renderer;
15 use Friendica\Database\DBA;
16 use Friendica\DI;
17 use Friendica\Model\Contact;
18 use Friendica\Model\User;
19
20 /**
21  * ContactBlock widget
22  *
23  * @author Hypolite Petovan
24  */
25 class ContactBlock
26 {
27         /**
28          * Get HTML for contact block
29          *
30          * @template widget/contacts.tpl
31          * @hook contact_block_end (contacts=>array, output=>string)
32          * @return string
33          */
34         public static function getHTML(array $profile)
35         {
36                 $o = '';
37
38                 $shown = DI::pConfig()->get($profile['uid'], 'system', 'display_friend_count', 24);
39                 if ($shown == 0) {
40                         return $o;
41                 }
42
43                 if (!empty($profile['hide-friends'])) {
44                         return $o;
45                 }
46
47                 $contacts = [];
48
49                 $total = DBA::count('contact', [
50                         'uid' => $profile['uid'],
51                         'self' => false,
52                         'blocked' => false,
53                         'pending' => false,
54                         'hidden' => false,
55                         'archive' => false,
56                         'network' => [Protocol::DFRN, Protocol::ACTIVITYPUB, Protocol::OSTATUS, Protocol::DIASPORA, Protocol::FEED],
57                 ]);
58
59                 $contacts_title = L10n::t('No contacts');
60
61                 $micropro = [];
62
63                 if ($total) {
64                         // Only show followed for personal accounts, followers for pages
65                         if ((($profile['account-type'] ?? '') ?: User::ACCOUNT_TYPE_PERSON) == User::ACCOUNT_TYPE_PERSON) {
66                                 $rel = [Contact::SHARING, Contact::FRIEND];
67                         } else {
68                                 $rel = [Contact::FOLLOWER, Contact::FRIEND];
69                         }
70
71                         $contact_ids_stmt = DBA::select('contact', ['id'], [
72                                 'uid' => $profile['uid'],
73                                 'self' => false,
74                                 'blocked' => false,
75                                 'pending' => false,
76                                 'hidden' => false,
77                                 'archive' => false,
78                                 'rel' => $rel,
79                                 'network' => Protocol::FEDERATED,
80                         ], ['limit' => $shown]);
81
82                         if (DBA::isResult($contact_ids_stmt)) {
83                                 $contact_ids = [];
84                                 while($contact = DBA::fetch($contact_ids_stmt)) {
85                                         $contact_ids[] = $contact["id"];
86                                 }
87
88                                 $contacts_stmt = DBA::select('contact', ['id', 'uid', 'addr', 'url', 'name', 'thumb', 'network'], ['id' => $contact_ids]);
89
90                                 if (DBA::isResult($contacts_stmt)) {
91                                         $contacts_title = L10n::tt('%d Contact', '%d Contacts', $total);
92                                         $micropro = [];
93
94                                         while ($contact = DBA::fetch($contacts_stmt)) {
95                                                 $contacts[] = $contact;
96                                                 $micropro[] = HTML::micropro($contact, true, 'mpfriend');
97                                         }
98                                 }
99
100                                 DBA::close($contacts_stmt);
101                         }
102
103                         DBA::close($contact_ids_stmt);
104                 }
105
106                 $tpl = Renderer::getMarkupTemplate('widget/contacts.tpl');
107                 $o = Renderer::replaceMacros($tpl, [
108                         '$contacts' => $contacts_title,
109                         '$nickname' => $profile['nickname'],
110                         '$viewcontacts' => L10n::t('View Contacts'),
111                         '$micropro' => $micropro,
112                 ]);
113
114                 $arr = ['contacts' => $contacts, 'output' => $o];
115
116                 Hook::callAll('contact_block_end', $arr);
117
118                 return $o;
119         }
120 }