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