]> git.mxchange.org Git - friendica.git/blob - src/Module/Profile/Contacts.php
Merge pull request #8019 from nupplaphil/task/replace_getClass
[friendica.git] / src / Module / Profile / Contacts.php
1 <?php
2
3 namespace Friendica\Module\Profile;
4
5 use Friendica\BaseModule;
6 use Friendica\Content\ContactSelector;
7 use Friendica\Content\Nav;
8 use Friendica\Content\Pager;
9 use Friendica\Core\Config;
10 use Friendica\Core\L10n;
11 use Friendica\Core\Protocol;
12 use Friendica\Core\Renderer;
13 use Friendica\Core\Session;
14 use Friendica\Database\DBA;
15 use Friendica\DI;
16 use Friendica\Model\Contact;
17 use Friendica\Model\Profile;
18 use Friendica\Util\Proxy as ProxyUtils;
19
20 class Contacts extends BaseModule
21 {
22         public static function content(array $parameters = [])
23         {
24                 if (Config::get('system', 'block_public') && !Session::isAuthenticated()) {
25                         throw new \Friendica\Network\HTTPException\NotFoundException(L10n::t('User not found.'));
26                 }
27
28                 $a = DI::app();
29
30                 //@TODO: Get value from router parameters
31                 $nickname = $a->argv[1];
32                 $type = ($a->argv[3] ?? '') ?: 'all';
33
34                 Nav::setSelected('home');
35
36                 $user = DBA::selectFirst('user', [], ['nickname' => $nickname, 'blocked' => false]);
37                 if (!DBA::isResult($user)) {
38                         throw new \Friendica\Network\HTTPException\NotFoundException(L10n::t('User not found.'));
39                 }
40
41                 $a->profile_uid  = $user['uid'];
42
43                 Profile::load($a, $nickname);
44
45                 $is_owner = $a->profile['profile_uid'] == local_user();
46
47                 // tabs
48                 $o = Profile::getTabs($a, 'contacts', $is_owner, $nickname);
49
50                 if (!count($a->profile) || $a->profile['hide-friends']) {
51                         notice(L10n::t('Permission denied.') . EOL);
52                         return $o;
53                 }
54
55                 $condition = [
56                         'uid'     => $a->profile['uid'],
57                         'blocked' => false,
58                         'pending' => false,
59                         'hidden'  => false,
60                         'archive' => false,
61                         'network' => [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS, Protocol::FEED]
62                 ];
63
64                 switch ($type) {
65                         case 'followers': $condition['rel'] = [1, 3]; break;
66                         case 'following': $condition['rel'] = [2, 3]; break;
67                         case 'mutuals': $condition['rel'] = 3; break;
68                 }
69
70                 $total = DBA::count('contact', $condition);
71
72                 $pager = new Pager(DI::args()->getQueryString());
73
74                 $params = ['order' => ['name' => false], 'limit' => [$pager->getStart(), $pager->getItemsPerPage()]];
75
76                 $contacts_stmt = DBA::select('contact', [], $condition, $params);
77
78                 if (!DBA::isResult($contacts_stmt)) {
79                         info(L10n::t('No contacts.') . EOL);
80                         return $o;
81                 }
82
83                 $contacts = [];
84
85                 while ($contact = DBA::fetch($contacts_stmt)) {
86                         if ($contact['self']) {
87                                 continue;
88                         }
89
90                         $contact_details = Contact::getDetailsByURL($contact['url'], $a->profile['uid'], $contact);
91
92                         $contacts[] = [
93                                 'id'           => $contact['id'],
94                                 'img_hover'    => L10n::t('Visit %s\'s profile [%s]', $contact_details['name'], $contact['url']),
95                                 'photo_menu'   => Contact::photoMenu($contact),
96                                 'thumb'        => ProxyUtils::proxifyUrl($contact_details['thumb'], false, ProxyUtils::SIZE_THUMB),
97                                 'name'         => substr($contact_details['name'], 0, 20),
98                                 'username'     => $contact_details['name'],
99                                 'details'      => $contact_details['location'],
100                                 'tags'         => $contact_details['keywords'],
101                                 'about'        => $contact_details['about'],
102                                 'account_type' => Contact::getAccountType($contact_details),
103                                 'url'          => Contact::magicLink($contact['url']),
104                                 'sparkle'      => '',
105                                 'itemurl'      => $contact_details['addr'] ? : $contact['url'],
106                                 'network'      => ContactSelector::networkToName($contact['network'], $contact['url'], $contact['protocol']),
107                         ];
108                 }
109
110                 DBA::close($contacts_stmt);
111
112                 switch ($type) {
113                         case 'followers':    $title = L10n::tt('Follower (%s)', 'Followers (%s)', $total); break;
114                         case 'following':    $title = L10n::tt('Following (%s)', 'Following (%s)', $total); break;
115                         case 'mutuals':      $title = L10n::tt('Mutual friend (%s)', 'Mutual friends (%s)', $total); break;
116
117                         case 'all': default: $title = L10n::tt('Contact (%s)', 'Contacts (%s)', $total); break;
118                 }
119
120                 $tpl = Renderer::getMarkupTemplate('profile/contacts.tpl');
121                 $o .= Renderer::replaceMacros($tpl, [
122                         '$title'    => $title,
123                         '$nickname' => $nickname,
124                         '$type'     => $type,
125
126                         '$all_label' => L10n::t('All contacts'),
127                         '$followers_label' => L10n::t('Followers'),
128                         '$following_label' => L10n::t('Following'),
129                         '$mutuals_label' => L10n::t('Mutual friends'),
130
131                         '$contacts' => $contacts,
132                         '$paginate' => $pager->renderFull($total),
133                 ]);
134
135                 return $o;
136         }
137 }