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