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