]> git.mxchange.org Git - friendica.git/blob - mod/common.php
Docs: add a note on adding `use` on theme.php
[friendica.git] / mod / common.php
1 <?php
2 /**
3  * @file include/common.php
4  */
5 use Friendica\App;
6 use Friendica\Content\ContactSelector;
7 use Friendica\Core\L10n;
8 use Friendica\Database\DBM;
9 use Friendica\Model\Contact;
10 use Friendica\Model\GContact;
11 use Friendica\Model\Profile;
12
13 require_once 'include/dba.php';
14 require_once 'mod/contacts.php';
15
16 function common_content(App $a)
17 {
18         $o = '';
19
20         $cmd = $a->argv[1];
21         $uid = intval($a->argv[2]);
22         $cid = intval($a->argv[3]);
23         $zcid = 0;
24
25         if (!local_user()) {
26                 notice(L10n::t('Permission denied.') . EOL);
27                 return;
28         }
29
30         if ($cmd !== 'loc' && $cmd != 'rem') {
31                 return;
32         }
33
34         if (!$uid) {
35                 return;
36         }
37
38         if ($cmd === 'loc' && $cid) {
39                 $contact = dba::selectFirst('contact', ['name', 'url', 'photo'], ['id' => $cid, 'uid' => $uid]);
40
41                 if (DBM::is_result($contact)) {
42                         $a->page['aside'] = "";
43                         Profile::load($a, "", 0, Contact::getDetailsByURL($contact["url"]));
44                 }
45         } else {
46                 $contact = dba::selectFirst('contact', ['name', 'url', 'photo'], ['self' => true, 'uid' => $uid]);
47
48                 if (DBM::is_result($contact)) {
49                         $vcard_widget = replace_macros(get_markup_template("vcard-widget.tpl"), [
50                                 '$name' => htmlentities($contact['name']),
51                                 '$photo' => $contact['photo'],
52                                 'url' => 'contacts/' . $cid
53                         ]);
54
55                         if (!x($a->page, 'aside')) {
56                                 $a->page['aside'] = '';
57                         }
58                         $a->page['aside'] .= $vcard_widget;
59                 }
60         }
61
62         if (!DBM::is_result($contact)) {
63                 return;
64         }
65
66         if (!$cid && Profile::getMyURL()) {
67                 $contact = dba::selectFirst('contact', ['id'], ['nurl' => normalise_link(Profile::getMyURL()), 'uid' => $uid]);
68                 if (DBM::is_result($contact)) {
69                         $cid = $contact['id'];
70                 } else {
71                         $gcontact = dba::selectFirst('gcontact', ['id'], ['nurl' => normalise_link(Profile::getMyURL())]);
72                         if (DBM::is_result($gcontact)) {
73                                 $zcid = $gcontact['id'];
74                         }
75                 }
76         }
77
78         if ($cid == 0 && $zcid == 0) {
79                 return;
80         }
81
82         if ($cid) {
83                 $t = GContact::countCommonFriends($uid, $cid);
84         } else {
85                 $t = GContact::countCommonFriendsZcid($uid, $zcid);
86         }
87
88         if ($t > 0) {
89                 $a->set_pager_total($t);
90         } else {
91                 notice(L10n::t('No contacts in common.') . EOL);
92                 return $o;
93         }
94
95         if ($cid) {
96                 $r = GContact::commonFriends($uid, $cid, $a->pager['start'], $a->pager['itemspage']);
97         } else {
98                 $r = GContact::commonFriendsZcid($uid, $zcid, $a->pager['start'], $a->pager['itemspage']);
99         }
100
101         if (!DBM::is_result($r)) {
102                 return $o;
103         }
104
105         $id = 0;
106
107         $entries = [];
108         foreach ($r as $rr) {
109                 //get further details of the contact
110                 $contact_details = Contact::getDetailsByURL($rr['url'], $uid);
111
112                 // $rr['id'] is needed to use contact_photo_menu()
113                 /// @TODO Adding '/" here avoids E_NOTICE on missing constants
114                 $rr['id'] = $rr['cid'];
115
116                 $photo_menu = Contact::photoMenu($rr);
117
118                 $entry = [
119                         'url'          => $rr['url'],
120                         'itemurl'      => defaults($contact_details, 'addr', $rr['url']),
121                         'name'         => $contact_details['name'],
122                         'thumb'        => proxy_url($contact_details['thumb'], false, PROXY_SIZE_THUMB),
123                         'img_hover'    => htmlentities($contact_details['name']),
124                         'details'      => $contact_details['location'],
125                         'tags'         => $contact_details['keywords'],
126                         'about'        => $contact_details['about'],
127                         'account_type' => Contact::getAccountType($contact_details),
128                         'network'      => ContactSelector::networkToName($contact_details['network'], $contact_details['url']),
129                         'photo_menu'   => $photo_menu,
130                         'id'           => ++$id,
131                 ];
132                 $entries[] = $entry;
133         }
134
135         $title = '';
136         $tab_str = '';
137         if ($cmd === 'loc' && $cid && local_user() == $uid) {
138                 $tab_str = contacts_tab($a, $cid, 4);
139         } else {
140                 $title = L10n::t('Common Friends');
141         }
142
143         $tpl = get_markup_template('viewcontact_template.tpl');
144
145         $o .= replace_macros($tpl, [
146                 '$title'    => $title,
147                 '$tab_str'  => $tab_str,
148                 '$contacts' => $entries,
149                 '$paginate' => paginate($a),
150         ]);
151
152         return $o;
153 }