]> git.mxchange.org Git - friendica.git/blob - src/Module/Directory.php
parameters now are having a default value and are optional
[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\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 = self::getApp();
27                 $config = $app->getConfig();
28
29                 if (($config->get('system', 'block_public') && !Session::isAuthenticated()) ||
30                         ($config->get('system', 'block_local_dir') && !Session::isAuthenticated())) {
31                         throw new HTTPException\ForbiddenException(L10n::t('Public access denied.'));
32                 }
33
34                 if (local_user()) {
35                         $app->page['aside'] .= Widget::findPeople();
36                         $app->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($app->query_string, 60);
55
56                 $profiles = Profile::searchProfiles($pager->getStart(), $pager->getItemsPerPage(), $search);
57
58                 if ($profiles['total'] === 0) {
59                         info(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'  => L10n::t('Global Directory'),
77                         '$gDirPath'   => $gDirPath,
78                         '$desc'       => L10n::t('Find on this site'),
79                         '$contacts'   => $entries,
80                         '$finding'    => L10n::t('Results for:'),
81                         '$findterm'   => (strlen($search) ? $search : ""),
82                         '$title'      => L10n::t('Site Directory'),
83                         '$search_mod' => 'directory',
84                         '$submit'     => 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 = L10n::t('Location:');
136                 } else {
137                         $location = '';
138                 }
139
140                 $gender =   (!empty($profile['gender'])   ? L10n::t('Gender:')   : false);
141                 $marital =  (!empty($profile['marital'])  ? L10n::t('Status:')   : false);
142                 $homepage = (!empty($profile['homepage']) ? L10n::t('Homepage:') : false);
143                 $about =    (!empty($profile['about'])    ? L10n::t('About:')    : false);
144
145                 $location_e = $location;
146
147                 $photo_menu = [
148                         'profile' => [L10n::t("View Profile"), Contact::magicLink($profile_link)]
149                 ];
150
151                 $entry = [
152                         'id'           => $contact['id'],
153                         'url'          => Contact::magicLink($profile_link),
154                         'itemurl'      => $itemurl,
155                         'thumb'        => ProxyUtils::proxifyUrl($contact[$photo_size], false, ProxyUtils::SIZE_THUMB),
156                         'img_hover'    => $contact['name'],
157                         'name'         => $contact['name'],
158                         'details'      => $details,
159                         'account_type' => Contact::getAccountType($contact),
160                         'profile'      => $profile,
161                         'location'     => $location_e,
162                         'tags'         => $contact['pub_keywords'],
163                         'gender'       => $gender,
164                         'pdesc'        => $pdesc,
165                         'marital'      => $marital,
166                         'homepage'     => $homepage,
167                         'about'        => $about,
168                         'photo_menu'   => $photo_menu,
169
170                 ];
171
172                 $hook = ['contact' => $contact, 'entry' => $entry];
173
174                 Hook::callAll('directory_item', $hook);
175
176                 unset($profile);
177                 unset($location);
178
179                 return $hook['entry'];
180         }
181 }