]> git.mxchange.org Git - friendica.git/blob - mod/viewcontacts.php
Move $pager and $page_offset out of App
[friendica.git] / mod / viewcontacts.php
1 <?php
2 /**
3  * @file mod/viewcontacts.php
4  */
5
6 use Friendica\App;
7 use Friendica\Content\ContactSelector;
8 use Friendica\Content\Nav;
9 use Friendica\Content\Pager;
10 use Friendica\Core\Config;
11 use Friendica\Core\L10n;
12 use Friendica\Core\Protocol;
13 use Friendica\Core\System;
14 use Friendica\Database\DBA;
15 use Friendica\Model\Contact;
16 use Friendica\Model\Profile;
17 use Friendica\Util\Proxy as ProxyUtils;
18
19 function viewcontacts_init(App $a)
20 {
21         if (Config::get('system', 'block_public') && !local_user() && !remote_user()) {
22                 System::httpExit(403, ["title" => L10n::t('Access denied.')]);
23         }
24
25         if ($a->argc < 2) {
26                 System::httpExit(403, ["title" => L10n::t('Access denied.')]);
27         }
28
29         Nav::setSelected('home');
30
31         $nick = $a->argv[1];
32         $r = q("SELECT * FROM `user` WHERE `nickname` = '%s' AND `blocked` = 0 LIMIT 1",
33                 DBA::escape($nick)
34         );
35
36         if (!DBA::isResult($r)) {
37                 System::httpExit(404, ["title" => L10n::t('Page not found.')]);
38         }
39
40         $a->data['user'] = $r[0];
41         $a->profile_uid = $r[0]['uid'];
42         $is_owner = (local_user() && (local_user() == $a->profile_uid));
43
44         Profile::load($a, $a->argv[1]);
45 }
46
47 function viewcontacts_content(App $a)
48 {
49         if (Config::get('system', 'block_public') && !local_user() && !remote_user()) {
50                 notice(L10n::t('Public access denied.') . EOL);
51                 return;
52         }
53
54         $is_owner = $a->profile['profile_uid'] == local_user();
55
56         $o = "";
57
58         // tabs
59         $o .= Profile::getTabs($a, $is_owner, $a->data['user']['nickname']);
60
61         if (!count($a->profile) || $a->profile['hide-friends']) {
62                 notice(L10n::t('Permission denied.') . EOL);
63                 return $o;
64         }
65
66         $total = 0;
67         $r = q("SELECT COUNT(*) AS `total` FROM `contact`
68                 WHERE `uid` = %d AND NOT `blocked` AND NOT `pending`
69                         AND NOT `hidden` AND NOT `archive`
70                         AND `network` IN ('%s', '%s', '%s')",
71                 intval($a->profile['uid']),
72                 DBA::escape(Protocol::DFRN),
73                 DBA::escape(Protocol::DIASPORA),
74                 DBA::escape(Protocol::OSTATUS)
75         );
76         if (DBA::isResult($r)) {
77                 $total = $r[0]['total'];
78         }
79         $pager = new Pager($a->query_string, $total);
80
81         $r = q("SELECT * FROM `contact`
82                 WHERE `uid` = %d AND NOT `blocked` AND NOT `pending`
83                         AND NOT `hidden` AND NOT `archive`
84                         AND `network` IN ('%s', '%s', '%s')
85                 ORDER BY `name` ASC LIMIT %d, %d",
86                 intval($a->profile['uid']),
87                 DBA::escape(Protocol::DFRN),
88                 DBA::escape(Protocol::DIASPORA),
89                 DBA::escape(Protocol::OSTATUS),
90                 $pager->getStart(),
91                 $pager->getItemsPerPage()
92         );
93         if (!DBA::isResult($r)) {
94                 info(L10n::t('No contacts.').EOL);
95                 return $o;
96         }
97
98         $contacts = [];
99
100         foreach ($r as $rr) {
101                 /// @TODO This triggers an E_NOTICE if 'self' is not there
102                 if ($rr['self']) {
103                         continue;
104                 }
105
106                 $contact_details = Contact::getDetailsByURL($rr['url'], $a->profile['uid'], $rr);
107
108                 $contacts[] = [
109                         'id' => $rr['id'],
110                         'img_hover' => L10n::t('Visit %s\'s profile [%s]', $contact_details['name'], $rr['url']),
111                         'photo_menu' => Contact::photoMenu($rr),
112                         'thumb' => ProxyUtils::proxifyUrl($contact_details['thumb'], false, ProxyUtils::SIZE_THUMB),
113                         'name' => htmlentities(substr($contact_details['name'], 0, 20)),
114                         'username' => htmlentities($contact_details['name']),
115                         'details'       => $contact_details['location'],
116                         'tags'          => $contact_details['keywords'],
117                         'about'         => $contact_details['about'],
118                         'account_type'  => Contact::getAccountType($contact_details),
119                         'url' => Contact::magicLink($rr['url']),
120                         'sparkle' => '',
121                         'itemurl' => (($contact_details['addr'] != "") ? $contact_details['addr'] : $rr['url']),
122                         'network' => ContactSelector::networkToName($rr['network'], $rr['url']),
123                 ];
124         }
125
126
127         $tpl = get_markup_template("viewcontact_template.tpl");
128         $o .= replace_macros($tpl, [
129                 '$title' => L10n::t('Contacts'),
130                 '$contacts' => $contacts,
131                 '$paginate' => $pager->renderFull(),
132         ]);
133
134         return $o;
135 }