]> git.mxchange.org Git - friendica.git/blob - src/Module/Profile/Contacts.php
head fixings
[friendica.git] / src / Module / Profile / 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\Profile;
23
24 use Friendica\Content\Nav;
25 use Friendica\Content\Pager;
26 use Friendica\Core\Protocol;
27 use Friendica\Core\Renderer;
28 use Friendica\Core\Session;
29 use Friendica\Database\DBA;
30 use Friendica\DI;
31 use Friendica\Model;
32 use Friendica\Module;
33 use Friendica\Network\HTTPException;
34
35 class Contacts extends Module\BaseProfile
36 {
37         protected function content(array $request = []): string
38         {
39                 if (DI::config()->get('system', 'block_public') && !Session::isAuthenticated()) {
40                         throw new HTTPException\NotFoundException(DI::l10n()->t('User not found.'));
41                 }
42
43                 $a = DI::app();
44
45                 $nickname = $this->parameters['nickname'];
46                 $type = $this->parameters['type'] ?? 'all';
47
48                 $profile = Model\Profile::load($a, $nickname);
49                 if (empty($profile)) {
50                         throw new HTTPException\NotFoundException(DI::l10n()->t('User not found.'));
51                 }
52
53                 $is_owner = $profile['uid'] == local_user();
54
55                 if ($profile['hide-friends'] && !$is_owner) {
56                         throw new HTTPException\ForbiddenException(DI::l10n()->t('Permission denied.'));
57                 }
58
59                 Nav::setSelected('home');
60
61                 $o = self::getTabsHTML($a, 'contacts', $is_owner, $profile['nickname'], $profile['hide-friends']);
62
63                 $tabs = self::getContactFilterTabs('profile/' . $nickname, $type, Session::isAuthenticated() && $profile['uid'] != local_user());
64
65                 $condition = [
66                         'uid'     => $profile['uid'],
67                         'blocked' => false,
68                         'pending' => false,
69                         'hidden'  => false,
70                         'archive' => false,
71                         'failed'  => false,
72                         'self'    => false,
73                         'network' => [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS, Protocol::FEED]
74                 ];
75
76                 switch ($type) {
77                         case 'followers': $condition['rel'] = [Model\Contact::FOLLOWER, Model\Contact::FRIEND]; break;
78                         case 'following': $condition['rel'] = [Model\Contact::SHARING,  Model\Contact::FRIEND]; break;
79                         case 'mutuals':   $condition['rel'] = Model\Contact::FRIEND; break;
80                 }
81
82                 $total = DBA::count('contact', $condition);
83
84                 $pager = new Pager(DI::l10n(), DI::args()->getQueryString(), 30);
85
86                 $params = ['order' => ['name' => false], 'limit' => [$pager->getStart(), $pager->getItemsPerPage()]];
87
88                 $contacts = array_map(
89                         [Module\Contact::class, 'getContactTemplateVars'],
90                         Model\Contact::selectToArray([], $condition, $params)
91                 );
92
93                 $desc = '';
94                 switch ($type) {
95                         case 'followers':
96                                 $title = DI::l10n()->tt('Follower (%s)', 'Followers (%s)', $total);
97                                 break;
98                         case 'following':
99                                 $title = DI::l10n()->tt('Following (%s)', 'Following (%s)', $total);
100                                 break;
101                         case 'mutuals':
102                                 $title = DI::l10n()->tt('Mutual friend (%s)', 'Mutual friends (%s)', $total);
103                                 $desc = DI::l10n()->t(
104                                         'These contacts both follow and are followed by <strong>%s</strong>.',
105                                         htmlentities($profile['name'], ENT_COMPAT, 'UTF-8')
106                                 );
107                                 break;
108                         case 'all':
109                         default:
110                                 $title = DI::l10n()->tt('Contact (%s)', 'Contacts (%s)', $total);
111                                 break;
112                 }
113
114                 $tpl = Renderer::getMarkupTemplate('profile/contacts.tpl');
115                 $o .= Renderer::replaceMacros($tpl, [
116                         '$title'    => $title,
117                         '$desc'     => $desc,
118                         '$tabs'     => $tabs,
119
120                         '$noresult_label'  => DI::l10n()->t('No contacts.'),
121
122                         '$contacts' => $contacts,
123                         '$paginate' => $pager->renderFull($total),
124                 ]);
125
126                 return $o;
127         }
128 }