]> git.mxchange.org Git - friendica.git/blob - src/Module/Contact/Contacts.php
Adhere feedback
[friendica.git] / src / Module / Contact / Contacts.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, 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\App;
25 use Friendica\BaseModule;
26 use Friendica\Content\Pager;
27 use Friendica\Content\Widget;
28 use Friendica\Core\L10n;
29 use Friendica\Core\Renderer;
30 use Friendica\Core\Session\Capability\IHandleUserSessions;
31 use Friendica\Model;
32 use Friendica\Model\User;
33 use Friendica\Module;
34 use Friendica\Module\Response;
35 use Friendica\Network\HTTPException;
36 use Friendica\Util\Profiler;
37 use Psr\Log\LoggerInterface;
38
39 class Contacts extends BaseModule
40 {
41         /** @var IHandleUserSessions */
42         private $userSession;
43         /** @var App\Page */
44         private $page;
45
46         public function __construct(App\Page $page, IHandleUserSessions $userSession, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, array $server, array $parameters = [])
47         {
48                 parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
49
50                 $this->userSession = $userSession;
51                 $this->page = $page;
52         }
53
54         protected function content(array $request = []): string
55         {
56                 if (!$this->userSession->getLocalUserId()) {
57                         throw new HTTPException\ForbiddenException();
58                 }
59
60                 $cid = $this->parameters['id'];
61                 $type = $this->parameters['type'] ?? 'all';
62                 $accounttype = $request['accounttype'] ?? '';
63                 $accounttypeid = User::getAccountTypeByString($accounttype);
64
65                 if (!$cid) {
66                         throw new HTTPException\BadRequestException($this->t('Invalid contact.'));
67                 }
68
69                 $contact = Model\Contact::getById($cid, []);
70                 if (empty($contact)) {
71                         throw new HTTPException\NotFoundException($this->t('Contact not found.'));
72                 }
73
74                 $localContactId = Model\Contact::getPublicIdByUserId($this->userSession->getLocalUserId());
75
76                 $this->page['aside'] = Widget\VCard::getHTML($contact);
77
78                 $condition = [
79                         'blocked' => false,
80                         'self'    => false,
81                         'hidden'  => false,
82                         'failed'  => false,
83                 ];
84
85                 if (isset($accounttypeid)) {
86                         $condition['contact-type'] = $accounttypeid;
87                 }
88
89                 $noresult_label = $this->t('No known contacts.');
90
91                 switch ($type) {
92                         case 'followers':
93                                 $total = Model\Contact\Relation::countFollowers($cid, $condition);
94                                 break;
95                         case 'following':
96                                 $total = Model\Contact\Relation::countFollows($cid, $condition);
97                                 break;
98                         case 'mutuals':
99                                 $total = Model\Contact\Relation::countMutuals($cid, $condition);
100                                 break;
101                         case 'common':
102                                 $total = Model\Contact\Relation::countCommon($localContactId, $cid, $condition);
103                                 $noresult_label = $this->t('No common contacts.');
104                                 break;
105                         default:
106                                 $total = Model\Contact\Relation::countAll($cid, $condition);
107                 }
108
109                 $pager = new Pager($this->l10n, $this->args->getQueryString(), 30);
110                 $desc = '';
111
112                 switch ($type) {
113                         case 'followers':
114                                 $friends = Model\Contact\Relation::listFollowers($cid, $condition, $pager->getItemsPerPage(), $pager->getStart());
115                                 $title = $this->tt('Follower (%s)', 'Followers (%s)', $total);
116                                 break;
117                         case 'following':
118                                 $friends = Model\Contact\Relation::listFollows($cid, $condition, $pager->getItemsPerPage(), $pager->getStart());
119                                 $title = $this->tt('Following (%s)', 'Following (%s)', $total);
120                                 break;
121                         case 'mutuals':
122                                 $friends = Model\Contact\Relation::listMutuals($cid, $condition, $pager->getItemsPerPage(), $pager->getStart());
123                                 $title = $this->tt('Mutual friend (%s)', 'Mutual friends (%s)', $total);
124                                 $desc = $this->t(
125                                         'These contacts both follow and are followed by <strong>%s</strong>.',
126                                         htmlentities($contact['name'], ENT_COMPAT, 'UTF-8')
127                                 );
128                                 break;
129                         case 'common':
130                                 $friends = Model\Contact\Relation::listCommon($localContactId, $cid, $condition, $pager->getItemsPerPage(), $pager->getStart());
131                                 $title = $this->tt('Common contact (%s)', 'Common contacts (%s)', $total);
132                                 $desc = $this->t(
133                                         'Both <strong>%s</strong> and yourself have publicly interacted with these contacts (follow, comment or likes on public posts).',
134                                         htmlentities($contact['name'], ENT_COMPAT, 'UTF-8')
135                                 );
136                                 break;
137                         default:
138                                 $friends = Model\Contact\Relation::listAll($cid, $condition, $pager->getItemsPerPage(), $pager->getStart());
139                                 $title = $this->tt('Contact (%s)', 'Contacts (%s)', $total);
140                 }
141
142                 $o = Module\Contact::getTabsHTML($contact, Module\Contact::TAB_CONTACTS);
143
144                 $tabs = self::getContactFilterTabs('contact/' . $cid, $type, true);
145
146                 // Contact list is obtained from the visited contact, but the contact display is visitor dependent
147                 $contacts = array_map(
148                         function ($contact) {
149                                 $contact = Model\Contact::selectFirst(
150                                         [],
151                                         ['uri-id' => $contact['uri-id'], 'uid' => [0, $this->userSession->getLocalUserId()]],
152                                         ['order' => ['uid' => 'DESC']]
153                                 );
154                                 return Module\Contact::getContactTemplateVars($contact);
155                         },
156                         $friends
157                 );
158
159                 $tpl = Renderer::getMarkupTemplate('profile/contacts.tpl');
160                 $o .= Renderer::replaceMacros($tpl, [
161                         '$title'    => $title,
162                         '$desc'     => $desc,
163                         '$tabs'     => $tabs,
164
165                         '$noresult_label'  => $noresult_label,
166
167                         '$contacts' => $contacts,
168                         '$paginate' => $pager->renderFull($total),
169                 ]);
170
171                 $this->page['aside'] .= Widget::accountTypes($_SERVER['REQUEST_URI'], $accounttype);
172
173                 return $o;
174         }
175 }