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