]> git.mxchange.org Git - friendica.git/blob - src/Module/Profile/Contacts.php
Merge pull request #8952 from annando/contact-template
[friendica.git] / src / Module / Profile / Contacts.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
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\Profile;
32 use Friendica\Module\BaseProfile;
33 use Friendica\Module\Contact as ModuleContact;
34
35 class Contacts extends BaseProfile
36 {
37         public static function content(array $parameters = [])
38         {
39                 if (DI::config()->get('system', 'block_public') && !Session::isAuthenticated()) {
40                         throw new \Friendica\Network\HTTPException\NotFoundException(DI::l10n()->t('User not found.'));
41                 }
42
43                 $a = DI::app();
44
45                 //@TODO: Get value from router parameters
46                 $nickname = $a->argv[1];
47                 $type = ($a->argv[3] ?? '') ?: 'all';
48
49                 Nav::setSelected('home');
50
51                 $user = DBA::selectFirst('user', [], ['nickname' => $nickname, 'blocked' => false]);
52                 if (!DBA::isResult($user)) {
53                         throw new \Friendica\Network\HTTPException\NotFoundException(DI::l10n()->t('User not found.'));
54                 }
55
56                 $a->profile_uid  = $user['uid'];
57
58                 Profile::load($a, $nickname);
59
60                 $is_owner = $a->profile['uid'] == local_user();
61
62                 $o = self::getTabsHTML($a, 'contacts', $is_owner, $nickname);
63
64                 if (!count($a->profile) || $a->profile['hide-friends']) {
65                         notice(DI::l10n()->t('Permission denied.'));
66                         return $o;
67                 }
68
69                 $condition = [
70                         'uid'     => $a->profile['uid'],
71                         'blocked' => false,
72                         'pending' => false,
73                         'hidden'  => false,
74                         'archive' => false,
75                         'network' => [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS, Protocol::FEED]
76                 ];
77
78                 switch ($type) {
79                         case 'followers': $condition['rel'] = [1, 3]; break;
80                         case 'following': $condition['rel'] = [2, 3]; break;
81                         case 'mutuals': $condition['rel'] = 3; break;
82                 }
83
84                 $total = DBA::count('contact', $condition);
85
86                 $pager = new Pager(DI::l10n(), DI::args()->getQueryString());
87
88                 $params = ['order' => ['name' => false], 'limit' => [$pager->getStart(), $pager->getItemsPerPage()]];
89
90                 $contacts_stmt = DBA::select('contact', [], $condition, $params);
91
92                 if (!DBA::isResult($contacts_stmt)) {
93                         notice(DI::l10n()->t('No contacts.'));
94                         return $o;
95                 }
96
97                 $contacts = [];
98
99                 while ($contact = DBA::fetch($contacts_stmt)) {
100                         if ($contact['self']) {
101                                 continue;
102                         }
103                         $contacts[] = ModuleContact::getContactTemplateVars($contact);
104                 }
105
106                 DBA::close($contacts_stmt);
107
108                 switch ($type) {
109                         case 'followers':    $title = DI::l10n()->tt('Follower (%s)', 'Followers (%s)', $total); break;
110                         case 'following':    $title = DI::l10n()->tt('Following (%s)', 'Following (%s)', $total); break;
111                         case 'mutuals':      $title = DI::l10n()->tt('Mutual friend (%s)', 'Mutual friends (%s)', $total); break;
112
113                         case 'all': default: $title = DI::l10n()->tt('Contact (%s)', 'Contacts (%s)', $total); break;
114                 }
115
116                 $tpl = Renderer::getMarkupTemplate('profile/contacts.tpl');
117                 $o .= Renderer::replaceMacros($tpl, [
118                         '$title'    => $title,
119                         '$nickname' => $nickname,
120                         '$type'     => $type,
121
122                         '$all_label' => DI::l10n()->t('All contacts'),
123                         '$followers_label' => DI::l10n()->t('Followers'),
124                         '$following_label' => DI::l10n()->t('Following'),
125                         '$mutuals_label' => DI::l10n()->t('Mutual friends'),
126
127                         '$contacts' => $contacts,
128                         '$paginate' => $pager->renderFull($total),
129                 ]);
130
131                 return $o;
132         }
133 }