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