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