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