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