Merge pull request #8492 from MrPetovan/task/8400-frio-jot-compose-link
[friendica.git/.git] / mod / common.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 use Friendica\App;
23 use Friendica\Content\ContactSelector;
24 use Friendica\Content\Pager;
25 use Friendica\Core\Renderer;
26 use Friendica\Database\DBA;
27 use Friendica\DI;
28 use Friendica\Model;
29 use Friendica\Module;
30 use Friendica\Util\Proxy as ProxyUtils;
31 use Friendica\Util\Strings;
32
33 function common_content(App $a)
34 {
35         $o = '';
36
37         $cmd = $a->argv[1];
38         $uid = intval($a->argv[2]);
39         $cid = intval($a->argv[3]);
40         $zcid = 0;
41
42         if (!local_user()) {
43                 notice(DI::l10n()->t('Permission denied.') . EOL);
44                 return;
45         }
46
47         if ($cmd !== 'loc' && $cmd != 'rem') {
48                 return;
49         }
50
51         if (!$uid) {
52                 return;
53         }
54
55         if ($cmd === 'loc' && $cid) {
56                 $contact = DBA::selectFirst('contact', ['name', 'url', 'photo', 'uid', 'id'], ['id' => $cid, 'uid' => $uid]);
57
58                 if (DBA::isResult($contact)) {
59                         DI::page()['aside'] = "";
60                         Model\Profile::load($a, "", Model\Contact::getDetailsByURL($contact["url"]));
61                 }
62         } else {
63                 $contact = DBA::selectFirst('contact', ['name', 'url', 'photo', 'uid', 'id'], ['self' => true, 'uid' => $uid]);
64
65                 if (DBA::isResult($contact)) {
66                         $vcard_widget = Renderer::replaceMacros(Renderer::getMarkupTemplate('widget/vcard.tpl'), [
67                                 '$name'  => $contact['name'],
68                                 '$photo' => $contact['photo'],
69                                 'url'    => 'contact/' . $cid
70                         ]);
71
72                         if (empty(DI::page()['aside'])) {
73                                 DI::page()['aside'] = '';
74                         }
75                         DI::page()['aside'] .= $vcard_widget;
76                 }
77         }
78
79         if (!DBA::isResult($contact)) {
80                 return;
81         }
82
83         if (!$cid && Model\Profile::getMyURL()) {
84                 $contact = DBA::selectFirst('contact', ['id'], ['nurl' => Strings::normaliseLink(Model\Profile::getMyURL()), 'uid' => $uid]);
85                 if (DBA::isResult($contact)) {
86                         $cid = $contact['id'];
87                 } else {
88                         $gcontact = DBA::selectFirst('gcontact', ['id'], ['nurl' => Strings::normaliseLink(Model\Profile::getMyURL())]);
89                         if (DBA::isResult($gcontact)) {
90                                 $zcid = $gcontact['id'];
91                         }
92                 }
93         }
94
95         if ($cid == 0 && $zcid == 0) {
96                 return;
97         }
98
99         if ($cid) {
100                 $total = Model\GContact::countCommonFriends($uid, $cid);
101         } else {
102                 $total = Model\GContact::countCommonFriendsZcid($uid, $zcid);
103         }
104
105         if ($total < 1) {
106                 notice(DI::l10n()->t('No contacts in common.') . EOL);
107                 return $o;
108         }
109
110         $pager = new Pager(DI::l10n(), DI::args()->getQueryString());
111
112         if ($cid) {
113                 $common_friends = Model\GContact::commonFriends($uid, $cid, $pager->getStart(), $pager->getItemsPerPage());
114         } else {
115                 $common_friends = Model\GContact::commonFriendsZcid($uid, $zcid, $pager->getStart(), $pager->getItemsPerPage());
116         }
117
118         if (!DBA::isResult($common_friends)) {
119                 return $o;
120         }
121
122         $id = 0;
123
124         $entries = [];
125         foreach ($common_friends as $common_friend) {
126                 //get further details of the contact
127                 $contact_details = Model\Contact::getDetailsByURL($common_friend['url'], $uid);
128
129                 // $rr['id'] is needed to use contact_photo_menu()
130                 /// @TODO Adding '/" here avoids E_NOTICE on missing constants
131                 $common_friend['id'] = $common_friend['cid'];
132
133                 $photo_menu = Model\Contact::photoMenu($common_friend);
134
135                 $entry = [
136                         'url'          => Model\Contact::magicLink($common_friend['url']),
137                         'itemurl'      => ($contact_details['addr'] ?? '') ?: $common_friend['url'],
138                         'name'         => $contact_details['name'],
139                         'thumb'        => ProxyUtils::proxifyUrl($contact_details['thumb'], false, ProxyUtils::SIZE_THUMB),
140                         'img_hover'    => $contact_details['name'],
141                         'details'      => $contact_details['location'],
142                         'tags'         => $contact_details['keywords'],
143                         'about'        => $contact_details['about'],
144                         'account_type' => Model\Contact::getAccountType($contact_details),
145                         'network'      => ContactSelector::networkToName($contact_details['network'], $contact_details['url']),
146                         'photo_menu'   => $photo_menu,
147                         'id'           => ++$id,
148                 ];
149                 $entries[] = $entry;
150         }
151
152         $title = '';
153         $tab_str = '';
154         if ($cmd === 'loc' && $cid && local_user() == $uid) {
155                 $tab_str = Module\Contact::getTabsHTML($a, $contact, 5);
156         } else {
157                 $title = DI::l10n()->t('Common Friends');
158         }
159
160         $tpl = Renderer::getMarkupTemplate('viewcontact_template.tpl');
161
162         $o .= Renderer::replaceMacros($tpl, [
163                 '$title'    => $title,
164                 '$tab_str'  => $tab_str,
165                 '$contacts' => $entries,
166                 '$paginate' => $pager->renderFull($total),
167         ]);
168
169         return $o;
170 }