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