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