Update copyright
[friendica.git/.git] / src / Module / Profile / Contacts.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, 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         public static function content(array $parameters = [])
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 = $parameters['nickname'];
46                 $type = $parameters['type'] ?? 'all';
47
48                 Model\Profile::load($a, $nickname);
49
50                 if (empty($a->profile)) {
51                         throw new HTTPException\NotFoundException(DI::l10n()->t('User not found.'));
52                 }
53
54                 $is_owner = $a->profile['uid'] == local_user();
55
56                 if (!empty($a->profile['hide-friends']) && !$is_owner) {
57                         throw new HTTPException\ForbiddenException(DI::l10n()->t('Permission denied.'));
58                 }
59
60                 Nav::setSelected('home');
61
62                 $o = self::getTabsHTML($a, 'contacts', $is_owner, $nickname);
63
64                 $tabs = self::getContactFilterTabs('profile/' . $nickname, $type, Session::isAuthenticated() && $a->profile['uid'] != local_user());
65
66                 $condition = [
67                         'uid'     => $a->profile['uid'],
68                         'blocked' => false,
69                         'pending' => false,
70                         'hidden'  => false,
71                         'archive' => false,
72                         'failed'  => false,
73                         'self'    => false,
74                         'network' => [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS, Protocol::FEED]
75                 ];
76
77                 switch ($type) {
78                         case 'followers': $condition['rel'] = [Model\Contact::FOLLOWER, Model\Contact::FRIEND]; break;
79                         case 'following': $condition['rel'] = [Model\Contact::SHARING,  Model\Contact::FRIEND]; break;
80                         case 'mutuals':   $condition['rel'] = Model\Contact::FRIEND; break;
81                 }
82
83                 $total = DBA::count('contact', $condition);
84
85                 $pager = new Pager(DI::l10n(), DI::args()->getQueryString(), 30);
86
87                 $params = ['order' => ['name' => false], 'limit' => [$pager->getStart(), $pager->getItemsPerPage()]];
88
89                 $contacts = array_map(
90                         [Module\Contact::class, 'getContactTemplateVars'],
91                         Model\Contact::selectToArray([], $condition, $params)
92                 );
93
94                 $desc = '';
95                 switch ($type) {
96                         case 'followers':
97                                 $title = DI::l10n()->tt('Follower (%s)', 'Followers (%s)', $total);
98                                 break;
99                         case 'following':
100                                 $title = DI::l10n()->tt('Following (%s)', 'Following (%s)', $total);
101                                 break;
102                         case 'mutuals':
103                                 $title = DI::l10n()->tt('Mutual friend (%s)', 'Mutual friends (%s)', $total);
104                                 $desc = DI::l10n()->t(
105                                         'These contacts both follow and are followed by <strong>%s</strong>.',
106                                         htmlentities($a->profile['name'], ENT_COMPAT, 'UTF-8')
107                                 );
108                                 break;
109                         case 'all':
110                         default:
111                                 $title = DI::l10n()->tt('Contact (%s)', 'Contacts (%s)', $total);
112                                 break;
113                 }
114
115                 $tpl = Renderer::getMarkupTemplate('profile/contacts.tpl');
116                 $o .= Renderer::replaceMacros($tpl, [
117                         '$title'    => $title,
118                         '$desc'     => $desc,
119                         '$tabs'     => $tabs,
120
121                         '$noresult_label'  => DI::l10n()->t('No contacts.'),
122
123                         '$contacts' => $contacts,
124                         '$paginate' => $pager->renderFull($total),
125                 ]);
126
127                 return $o;
128         }
129 }