]> git.mxchange.org Git - friendica.git/blob - src/Module/Contact/Contacts.php
Merge remote-tracking branch 'upstream/develop' into json-ld
[friendica.git] / src / Module / Contact / Contacts.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, the Friendica project
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 namespace Friendica\Module\Contact;
23
24 use Friendica\BaseModule;
25 use Friendica\Content\Pager;
26 use Friendica\Content\Widget;
27 use Friendica\Core\Renderer;
28 use Friendica\DI;
29 use Friendica\Model;
30 use Friendica\Model\User;
31 use Friendica\Module;
32 use Friendica\Network\HTTPException;
33
34 class Contacts extends BaseModule
35 {
36         protected function content(array $request = []): string
37         {
38                 $app = DI::app();
39
40                 if (!local_user()) {
41                         throw new HTTPException\ForbiddenException();
42                 }
43
44                 $cid = $this->parameters['id'];
45                 $type = $this->parameters['type'] ?? 'all';
46                 $accounttype = $_GET['accounttype'] ?? '';
47                 $accounttypeid = User::getAccountTypeByString($accounttype);
48
49                 if (!$cid) {
50                         throw new HTTPException\BadRequestException(DI::l10n()->t('Invalid contact.'));
51                 }
52
53                 $contact = Model\Contact::getById($cid, []);
54                 if (empty($contact)) {
55                         throw new HTTPException\NotFoundException(DI::l10n()->t('Contact not found.'));
56                 }
57
58                 $localContactId = Model\Contact::getPublicIdByUserId(local_user());
59
60                 DI::page()['aside'] = Widget\VCard::getHTML($contact);
61
62                 $condition = [
63                         'blocked' => false,
64                         'self' => false,
65                         'hidden' => false,
66                         'failed' => false,
67                 ];
68
69                 if (isset($accounttypeid)) {
70                         $condition['contact-type'] = $accounttypeid;
71                 }
72
73                 $noresult_label = DI::l10n()->t('No known contacts.');
74
75                 switch ($type) {
76                         case 'followers':
77                                 $total = Model\Contact\Relation::countFollowers($cid, $condition);
78                                 break;
79                         case 'following':
80                                 $total = Model\Contact\Relation::countFollows($cid, $condition);
81                                 break;
82                         case 'mutuals':
83                                 $total = Model\Contact\Relation::countMutuals($cid, $condition);
84                                 break;
85                         case 'common':
86                                 $total = Model\Contact\Relation::countCommon($localContactId, $cid, $condition);
87                                 $noresult_label = DI::l10n()->t('No common contacts.');
88                                 break;
89                         default:
90                                 $total = Model\Contact\Relation::countAll($cid, $condition);
91                 }
92
93                 $pager = new Pager(DI::l10n(), DI::args()->getQueryString(), 30);
94                 $desc = '';
95
96                 switch ($type) {
97                         case 'followers':
98                                 $friends = Model\Contact\Relation::listFollowers($cid, $condition, $pager->getItemsPerPage(), $pager->getStart());
99                                 $title = DI::l10n()->tt('Follower (%s)', 'Followers (%s)', $total);
100                                 break;
101                         case 'following':
102                                 $friends = Model\Contact\Relation::listFollows($cid, $condition, $pager->getItemsPerPage(), $pager->getStart());
103                                 $title = DI::l10n()->tt('Following (%s)', 'Following (%s)', $total);
104                                 break;
105                         case 'mutuals':
106                                 $friends = Model\Contact\Relation::listMutuals($cid, $condition, $pager->getItemsPerPage(), $pager->getStart());
107                                 $title = DI::l10n()->tt('Mutual friend (%s)', 'Mutual friends (%s)', $total);
108                                 $desc = DI::l10n()->t(
109                                         'These contacts both follow and are followed by <strong>%s</strong>.',
110                                         htmlentities($contact['name'], ENT_COMPAT, 'UTF-8')
111                                 );
112                                 break;
113                         case 'common':
114                                 $friends = Model\Contact\Relation::listCommon($localContactId, $cid, $condition, $pager->getItemsPerPage(), $pager->getStart());
115                                 $title = DI::l10n()->tt('Common contact (%s)', 'Common contacts (%s)', $total);
116                                 $desc = DI::l10n()->t(
117                                         'Both <strong>%s</strong> and yourself have publicly interacted with these contacts (follow, comment or likes on public posts).',
118                                         htmlentities($contact['name'], ENT_COMPAT, 'UTF-8')
119                                 );
120                                 break;
121                         default:
122                                 $friends = Model\Contact\Relation::listAll($cid, $condition, $pager->getItemsPerPage(), $pager->getStart());
123                                 $title = DI::l10n()->tt('Contact (%s)', 'Contacts (%s)', $total);
124                 }
125
126                 $o = Module\Contact::getTabsHTML($contact, Module\Contact::TAB_CONTACTS);
127
128                 $tabs = self::getContactFilterTabs('contact/' . $cid, $type, true);
129
130                 $contacts = array_map([Module\Contact::class, 'getContactTemplateVars'], $friends);
131
132                 $tpl = Renderer::getMarkupTemplate('profile/contacts.tpl');
133                 $o .= Renderer::replaceMacros($tpl, [
134                         '$title'    => $title,
135                         '$desc'     => $desc,
136                         '$tabs'     => $tabs,
137
138                         '$noresult_label'  => $noresult_label,
139
140                         '$contacts' => $contacts,
141                         '$paginate' => $pager->renderFull($total),
142                 ]);
143
144                 DI::page()['aside'] .= Widget::accountTypes($_SERVER['REQUEST_URI'], $accounttype);
145
146                 return $o;
147         }
148 }