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