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