]> git.mxchange.org Git - friendica.git/blob - src/Module/Directory.php
Merge pull request #13599 from Raroun/Fix_for_Pull_Request_#13596_missing_a_hidden...
[friendica.git] / src / Module / Directory.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;
23
24 use Friendica\BaseModule;
25 use Friendica\Content\Nav;
26 use Friendica\Content\Pager;
27 use Friendica\Content\Widget;
28 use Friendica\Core\Hook;
29 use Friendica\Core\Renderer;
30 use Friendica\Core\Search;
31 use Friendica\DI;
32 use Friendica\Model;
33 use Friendica\Model\Profile;
34 use Friendica\Network\HTTPException;
35
36 /**
37  * Shows the local directory of this node
38  */
39 class Directory extends BaseModule
40 {
41         protected function content(array $request = []): string
42         {
43                 $app = DI::app();
44                 $config = DI::config();
45
46                 if (($config->get('system', 'block_public') && !DI::userSession()->isAuthenticated()) ||
47                         ($config->get('system', 'block_local_dir') && !DI::userSession()->isAuthenticated())) {
48                         throw new HTTPException\ForbiddenException(DI::l10n()->t('Public access denied.'));
49                 }
50
51                 if (DI::userSession()->getLocalUserId()) {
52                         DI::page()['aside'] .= Widget::findPeople();
53                         DI::page()['aside'] .= Widget::follow();
54                 }
55
56                 $output = '';
57                 $entries = [];
58
59                 Nav::setSelected('directory');
60
61                 $search = trim(rawurldecode($_REQUEST['search'] ?? ''));
62
63                 $gDirPath = '';
64                 $dirURL = Search::getGlobalDirectory();
65                 if (strlen($dirURL)) {
66                         $gDirPath = Profile::zrl($dirURL, true);
67                 }
68
69                 $pager = new Pager(DI::l10n(), DI::args()->getQueryString(), 60);
70
71                 $profiles = Profile::searchProfiles($pager->getStart(), $pager->getItemsPerPage(), $search);
72
73                 if ($profiles['total'] === 0) {
74                         DI::sysmsg()->addNotice(DI::l10n()->t('No entries (some entries may be hidden).'));
75                 } else {
76                         foreach ($profiles['entries'] as $entry) {
77                                 $contact = Model\Contact::getByURLForUser($entry['url'], DI::userSession()->getLocalUserId());
78                                 if (!empty($contact)) {
79                                         $entries[] = Contact::getContactTemplateVars($contact);
80                                 }
81                         }
82                 }
83
84                 $tpl = Renderer::getMarkupTemplate('directory_header.tpl');
85
86                 $output .= Renderer::replaceMacros($tpl, [
87                         '$search'     => $search,
88                         '$globaldir'  => DI::l10n()->t('Global Directory'),
89                         '$gDirPath'   => $gDirPath,
90                         '$desc'       => DI::l10n()->t('Find on this site'),
91                         '$contacts'   => $entries,
92                         '$finding'    => DI::l10n()->t('Results for:'),
93                         '$findterm'   => (strlen($search) ? $search : ""),
94                         '$title'      => DI::l10n()->t('Site Directory'),
95                         '$search_mod' => 'directory',
96                         '$submit'     => DI::l10n()->t('Find'),
97                         '$paginate'   => $pager->renderFull($profiles['total']),
98                 ]);
99
100                 return $output;
101         }
102
103         /**
104          * Format contact/profile/user data from the database into an usable
105          * array for displaying directory entries.
106          *
107          * @param array  $contact    The directory entry from the database.
108          * @param string $photo_size Avatar size (thumb, photo or micro).
109          *
110          * @return array
111          *
112          * @throws \Exception
113          */
114         public static function formatEntry(array $contact, string $photo_size = 'photo'): array
115         {
116                 $itemurl = (($contact['addr'] != "") ? $contact['addr'] : $contact['url']);
117
118                 $profile_link = $contact['url'];
119
120                 $about = (($contact['about']) ? $contact['about'] . '<br />' : '');
121
122                 $details = '';
123                 if (strlen($contact['locality'])) {
124                         $details .= $contact['locality'];
125                 }
126                 if (strlen($contact['region'])) {
127                         if (strlen($contact['locality'])) {
128                                 $details .= ', ';
129                         }
130                         $details .= $contact['region'];
131                 }
132                 if (strlen($contact['country-name'])) {
133                         if (strlen($details)) {
134                                 $details .= ', ';
135                         }
136                         $details .= $contact['country-name'];
137                 }
138
139                 $profile = $contact;
140
141                 if (!empty($profile['address'])
142                         || !empty($profile['locality'])
143                         || !empty($profile['region'])
144                         || !empty($profile['postal-code'])
145                         || !empty($profile['country-name'])
146                 ) {
147                         $location = DI::l10n()->t('Location:');
148                 } else {
149                         $location = '';
150                 }
151
152                 $homepage = (!empty($profile['homepage']) ? DI::l10n()->t('Homepage:') : false);
153
154                 $location_e = $location;
155
156                 $photo_menu = [
157                         'profile' => [DI::l10n()->t("View Profile"), Model\Contact::magicLink($profile_link)]
158                 ];
159
160                 $entry = [
161                         'id'           => $contact['id'],
162                         'url'          => Model\Contact::magicLink($profile_link),
163                         'itemurl'      => $itemurl,
164                         'thumb'        => Model\Contact::getThumb($contact),
165                         'img_hover'    => $contact['name'],
166                         'name'         => $contact['name'],
167                         'details'      => $details,
168                         'account_type' => Model\Contact::getAccountType($contact['contact-type'] ?? 0),
169                         'profile'      => $profile,
170                         'location'     => $location_e,
171                         'tags'         => $contact['pub_keywords'],
172                         'about'        => $about,
173                         'homepage'     => $homepage,
174                         'photo_menu'   => $photo_menu,
175
176                 ];
177
178                 $hook = ['contact' => $contact, 'entry' => $entry];
179
180                 Hook::callAll('directory_item', $hook);
181
182                 unset($profile);
183                 unset($location);
184
185                 return $hook['entry'];
186         }
187 }