]> git.mxchange.org Git - friendica.git/blob - mod/common.php
Use the function from the contact template instead
[friendica.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\Pager;
24 use Friendica\Core\Renderer;
25 use Friendica\Database\DBA;
26 use Friendica\DI;
27 use Friendica\Model;
28 use Friendica\Module;
29 use Friendica\Util\Strings;
30
31 function common_content(App $a)
32 {
33         $o = '';
34
35         $cmd = $a->argv[1];
36         $uid = intval($a->argv[2]);
37         $cid = intval($a->argv[3]);
38         $zcid = 0;
39
40         if (!local_user()) {
41                 notice(DI::l10n()->t('Permission denied.'));
42                 return;
43         }
44
45         if ($cmd !== 'loc' && $cmd != 'rem') {
46                 return;
47         }
48
49         if (!$uid) {
50                 return;
51         }
52
53         if ($cmd === 'loc' && $cid) {
54                 $contact = DBA::selectFirst('contact', ['name', 'url', 'photo', 'uid', 'id'], ['id' => $cid, 'uid' => $uid]);
55
56                 if (DBA::isResult($contact)) {
57                         DI::page()['aside'] = "";
58                         Model\Profile::load($a, "", Model\Contact::getByURL($contact["url"], false));
59                 }
60         } else {
61                 $contact = DBA::selectFirst('contact', ['name', 'url', 'photo', 'uid', 'id'], ['self' => true, 'uid' => $uid]);
62
63                 if (DBA::isResult($contact)) {
64                         $vcard_widget = Renderer::replaceMacros(Renderer::getMarkupTemplate('widget/vcard.tpl'), [
65                                 '$name'  => $contact['name'],
66                                 '$photo' => $contact['photo'],
67                                 'url'    => 'contact/' . $cid
68                         ]);
69
70                         if (empty(DI::page()['aside'])) {
71                                 DI::page()['aside'] = '';
72                         }
73                         DI::page()['aside'] .= $vcard_widget;
74                 }
75         }
76
77         if (!DBA::isResult($contact)) {
78                 return;
79         }
80
81         if (!$cid && Model\Profile::getMyURL()) {
82                 $contact = DBA::selectFirst('contact', ['id'], ['nurl' => Strings::normaliseLink(Model\Profile::getMyURL()), 'uid' => $uid]);
83                 if (DBA::isResult($contact)) {
84                         $cid = $contact['id'];
85                 } else {
86                         $gcontact = DBA::selectFirst('gcontact', ['id'], ['nurl' => Strings::normaliseLink(Model\Profile::getMyURL())]);
87                         if (DBA::isResult($gcontact)) {
88                                 $zcid = $gcontact['id'];
89                         }
90                 }
91         }
92
93         if ($cid == 0 && $zcid == 0) {
94                 return;
95         }
96
97         if ($cid) {
98                 $total = Model\GContact::countCommonFriends($uid, $cid);
99         } else {
100                 $total = Model\GContact::countCommonFriendsZcid($uid, $zcid);
101         }
102
103         if ($total < 1) {
104                 notice(DI::l10n()->t('No contacts in common.'));
105                 return $o;
106         }
107
108         $pager = new Pager(DI::l10n(), DI::args()->getQueryString());
109
110         if ($cid) {
111                 $common_friends = Model\GContact::commonFriends($uid, $cid, $pager->getStart(), $pager->getItemsPerPage());
112         } else {
113                 $common_friends = Model\GContact::commonFriendsZcid($uid, $zcid, $pager->getStart(), $pager->getItemsPerPage());
114         }
115
116         if (!DBA::isResult($common_friends)) {
117                 return $o;
118         }
119
120         $id = 0;
121
122         $entries = [];
123         foreach ($common_friends as $common_friend) {
124                 $contact = Model\Contact::getByURLForUser($common_friend['url'], local_user());
125                 if (!empty($contact)) {
126                         $entries[] = Module\Contact::getContactTemplateVars($contact, ++$id);
127                 }
128         }
129
130         $title = '';
131         $tab_str = '';
132         if ($cmd === 'loc' && $cid && local_user() == $uid) {
133                 $tab_str = Module\Contact::getTabsHTML($a, $contact, 5);
134         } else {
135                 $title = DI::l10n()->t('Common Friends');
136         }
137
138         $tpl = Renderer::getMarkupTemplate('viewcontact_template.tpl');
139
140         $o .= Renderer::replaceMacros($tpl, [
141                 '$title'    => $title,
142                 '$tab_str'  => $tab_str,
143                 '$contacts' => $entries,
144                 '$paginate' => $pager->renderFull($total),
145         ]);
146
147         return $o;
148 }