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