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