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