]> git.mxchange.org Git - friendica.git/blob - src/Module/Contact/Contacts.php
Merge pull request #9373 from nupplaphil/task/server_env
[friendica.git] / src / Module / Contact / Contacts.php
1 <?php
2
3 namespace Friendica\Module\Contact;
4
5 use Friendica\BaseModule;
6 use Friendica\Content\Pager;
7 use Friendica\Core\Renderer;
8 use Friendica\Core\Session;
9 use Friendica\DI;
10 use Friendica\Model;
11 use Friendica\Module;
12 use Friendica\Network\HTTPException;
13
14 class Contacts extends BaseModule
15 {
16         public static function content(array $parameters = [])
17         {
18                 $app = DI::app();
19
20                 if (!local_user()) {
21                         throw new HTTPException\ForbiddenException();
22                 }
23
24                 $cid = $parameters['id'];
25                 $type = $parameters['type'] ?? 'all';
26
27                 if (!$cid) {
28                         throw new HTTPException\BadRequestException(DI::l10n()->t('Invalid contact.'));
29                 }
30
31                 $contact = Model\Contact::getById($cid, []);
32                 if (empty($contact)) {
33                         throw new HTTPException\NotFoundException(DI::l10n()->t('Contact not found.'));
34                 }
35
36                 $localContactId = Model\Contact::getPublicIdByUserId(local_user());
37
38                 Model\Profile::load($app, '', $contact);
39
40                 $condition = [
41                         'blocked' => false,
42                         'self' => false,
43                         'hidden' => false,
44                         'failed' => false,
45                 ];
46
47                 $noresult_label = DI::l10n()->t('No known contacts.');
48
49                 switch ($type) {
50                         case 'followers':
51                                 $total = Model\Contact\Relation::countFollowers($cid, $condition);
52                                 break;
53                         case 'following':
54                                 $total = Model\Contact\Relation::countFollows($cid, $condition);
55                                 break;
56                         case 'mutuals':
57                                 $total = Model\Contact\Relation::countMutuals($cid, $condition);
58                                 break;
59                         case 'common':
60                                 $condition = [
61                                         'NOT `self` AND NOT `blocked` AND NOT `hidden` AND `id` != ?',
62                                         $localContactId,
63                                 ];
64                                 $total = Model\Contact\Relation::countCommon($localContactId, $cid, $condition);
65                                 $noresult_label = DI::l10n()->t('No common contacts.');
66                                 break;
67                         default:
68                                 $total = Model\Contact\Relation::countAll($cid, $condition);
69                 }
70
71                 $pager = new Pager(DI::l10n(), DI::args()->getQueryString(), 30);
72                 $desc = '';
73
74                 switch ($type) {
75                         case 'followers':
76                                 $friends = Model\Contact\Relation::listFollowers($cid, $condition, $pager->getItemsPerPage(), $pager->getStart());
77                                 $title = DI::l10n()->tt('Follower (%s)', 'Followers (%s)', $total);
78                                 break;
79                         case 'following':
80                                 $friends = Model\Contact\Relation::listFollows($cid, $condition, $pager->getItemsPerPage(), $pager->getStart());
81                                 $title = DI::l10n()->tt('Following (%s)', 'Following (%s)', $total);
82                                 break;
83                         case 'mutuals':
84                                 $friends = Model\Contact\Relation::listMutuals($cid, $condition, $pager->getItemsPerPage(), $pager->getStart());
85                                 $title = DI::l10n()->tt('Mutual friend (%s)', 'Mutual friends (%s)', $total);
86                                 $desc = DI::l10n()->t(
87                                         'These contacts both follow and are followed by <strong>%s</strong>.',
88                                         htmlentities($contact['name'], ENT_COMPAT, 'UTF-8')
89                                 );
90                                 break;
91                         case 'common':
92                                 $friends = Model\Contact\Relation::listCommon($localContactId, $cid, $condition, $pager->getItemsPerPage(), $pager->getStart());
93                                 $title = DI::l10n()->tt('Common contact (%s)', 'Common contacts (%s)', $total);
94                                 $desc = DI::l10n()->t(
95                                         'Both <strong>%s</strong> and yourself have publicly interacted with these contacts (follow, comment or likes on public posts).',
96                                         htmlentities($contact['name'], ENT_COMPAT, 'UTF-8')
97                                 );
98                                 break;
99                         default:
100                                 $friends = Model\Contact\Relation::listAll($cid, $condition, $pager->getItemsPerPage(), $pager->getStart());
101                                 $title = DI::l10n()->tt('Contact (%s)', 'Contacts (%s)', $total);
102                 }
103
104                 $o = Module\Contact::getTabsHTML($contact, Module\Contact::TAB_CONTACTS);
105
106                 $tabs = self::getContactFilterTabs('contact/' . $cid, $type, true);
107
108                 $contacts = array_map([Module\Contact::class, 'getContactTemplateVars'], $friends);
109
110                 $tpl = Renderer::getMarkupTemplate('profile/contacts.tpl');
111                 $o .= Renderer::replaceMacros($tpl, [
112                         '$title'    => $title,
113                         '$desc'     => $desc,
114                         '$tabs'     => $tabs,
115
116                         '$noresult_label'  => $noresult_label,
117
118                         '$contacts' => $contacts,
119                         '$paginate' => $pager->renderFull($total),
120                 ]);
121
122                 return $o;
123         }
124 }