]> git.mxchange.org Git - friendica.git/blob - mod/directory.php
6f6348103d71847dc89fa6f4ead0b25718644a7a
[friendica.git] / mod / directory.php
1 <?php
2 /**
3  * @file mod/directory.php
4  */
5
6 use Friendica\App;
7 use Friendica\Content\Nav;
8 use Friendica\Content\Pager;
9 use Friendica\Content\Widget;
10 use Friendica\Core\Addon;
11 use Friendica\Core\Config;
12 use Friendica\Core\L10n;
13 use Friendica\Core\Renderer;
14 use Friendica\Database\DBA;
15 use Friendica\Model\Contact;
16 use Friendica\Model\Profile;
17 use Friendica\Util\Proxy as ProxyUtils;
18
19 function directory_init(App $a)
20 {
21         if (local_user()) {
22                 $a->page['aside'] .= Widget::findPeople();
23                 $a->page['aside'] .= Widget::follow();
24         } else {
25                 unset($_SESSION['theme']);
26                 unset($_SESSION['mobile-theme']);
27         }
28 }
29
30 function directory_post(App $a)
31 {
32         if (x($_POST, 'search')) {
33                 $a->data['search'] = $_POST['search'];
34         }
35 }
36
37 function directory_content(App $a)
38 {
39         if ((Config::get('system', 'block_public') && !local_user() && !remote_user())
40                 || (Config::get('system', 'block_local_dir') && !local_user() && !remote_user())
41         ) {
42                 notice(L10n::t('Public access denied.') . EOL);
43                 return;
44         }
45
46         $o = '';
47         Nav::setSelected('directory');
48
49         if (x($a->data, 'search')) {
50                 $search = notags(trim($a->data['search']));
51         } else {
52                 $search = ((x($_GET, 'search')) ? notags(trim(rawurldecode($_GET['search']))) : '');
53         }
54
55         $gdirpath = '';
56         $dirurl = Config::get('system', 'directory');
57         if (strlen($dirurl)) {
58                 $gdirpath = Profile::zrl($dirurl, true);
59         }
60
61         if ($search) {
62                 $search = DBA::escape($search);
63
64                 $sql_extra = " AND ((`profile`.`name` LIKE '%$search%') OR
65                                 (`user`.`nickname` LIKE '%$search%') OR
66                                 (`profile`.`pdesc` LIKE '%$search%') OR
67                                 (`profile`.`locality` LIKE '%$search%') OR
68                                 (`profile`.`region` LIKE '%$search%') OR
69                                 (`profile`.`country-name` LIKE '%$search%') OR
70                                 (`profile`.`gender` LIKE '%$search%') OR
71                                 (`profile`.`marital` LIKE '%$search%') OR
72                                 (`profile`.`sexual` LIKE '%$search%') OR
73                                 (`profile`.`about` LIKE '%$search%') OR
74                                 (`profile`.`romance` LIKE '%$search%') OR
75                                 (`profile`.`work` LIKE '%$search%') OR
76                                 (`profile`.`education` LIKE '%$search%') OR
77                                 (`profile`.`pub_keywords` LIKE '%$search%') OR
78                                 (`profile`.`prv_keywords` LIKE '%$search%'))";
79         } else {
80                 $sql_extra = '';
81         }
82
83         $publish = (Config::get('system', 'publish_all') ? '' : " AND `publish` = 1 " );
84
85
86         $total = 0;
87         $cnt = DBA::fetchFirst("SELECT COUNT(*) AS `total` FROM `profile`
88                                 LEFT JOIN `user` ON `user`.`uid` = `profile`.`uid`
89                                 WHERE `is-default` $publish AND NOT `user`.`blocked` AND NOT `user`.`account_removed` $sql_extra");
90         if (DBA::isResult($cnt)) {
91                 $total = $cnt['total'];
92         }
93         $pager = new Pager($a->query_string, 60);
94
95         $order = " ORDER BY `name` ASC ";
96
97         $limit = $pager->getStart()."," . $pager->getItemsPerPage();
98
99         $r = DBA::p("SELECT `profile`.*, `profile`.`uid` AS `profile_uid`, `user`.`nickname`, `user`.`timezone` , `user`.`page-flags`,
100                         `contact`.`addr`, `contact`.`url` AS profile_url FROM `profile`
101                         LEFT JOIN `user` ON `user`.`uid` = `profile`.`uid`
102                         LEFT JOIN `contact` ON `contact`.`uid` = `user`.`uid`
103                         WHERE `is-default` $publish AND NOT `user`.`blocked` AND NOT `user`.`account_removed` AND `contact`.`self`
104                         $sql_extra $order LIMIT $limit"
105         );
106         if (DBA::isResult($r)) {
107                 if (in_array('small', $a->argv)) {
108                         $photo = 'thumb';
109                 } else {
110                         $photo = 'photo';
111                 }
112
113                 while ($rr = DBA::fetch($r)) {
114                         $itemurl= '';
115
116                         $itemurl = (($rr['addr'] != "") ? $rr['addr'] : $rr['profile_url']);
117
118                         $profile_link = 'profile/' . ((strlen($rr['nickname'])) ? $rr['nickname'] : $rr['profile_uid']);
119
120                         $pdesc = (($rr['pdesc']) ? $rr['pdesc'] . '<br />' : '');
121
122                         $details = '';
123                         if (strlen($rr['locality'])) {
124                                 $details .= $rr['locality'];
125                         }
126                         if (strlen($rr['region'])) {
127                                 if (strlen($rr['locality'])) {
128                                         $details .= ', ';
129                                 }
130                                 $details .= $rr['region'];
131                         }
132                         if (strlen($rr['country-name'])) {
133                                 if (strlen($details)) {
134                                         $details .= ', ';
135                                 }
136                                 $details .= $rr['country-name'];
137                         }
138 //                      if(strlen($rr['dob'])) {
139 //                              if(($years = age($rr['dob'],$rr['timezone'],'')) != 0)
140 //                                      $details .= '<br />' . L10n::t('Age: ') . $years ;
141 //                      }
142 //                      if(strlen($rr['gender']))
143 //                              $details .= '<br />' . L10n::t('Gender: ') . $rr['gender'];
144
145                         $profile = $rr;
146
147                         if ((x($profile, 'address')             == 1)
148                                 || (x($profile, 'locality')     == 1)
149                                 || (x($profile, 'region')       == 1)
150                                 || (x($profile, 'postal-code')  == 1)
151                                 || (x($profile, 'country-name') == 1)
152                         ) {
153                                 $location = L10n::t('Location:');
154                         } else {
155                                 $location = '';
156                         }
157
158                         $gender   = ((x($profile, 'gender')   == 1) ? L10n::t('Gender:')   : false);
159                         $marital  = ((x($profile, 'marital')  == 1) ? L10n::t('Status:')   : false);
160                         $homepage = ((x($profile, 'homepage') == 1) ? L10n::t('Homepage:') : false);
161                         $about    = ((x($profile, 'about')    == 1) ? L10n::t('About:')    : false);
162
163                         $location_e = $location;
164
165                         $photo_menu = [
166                                 'profile' => [L10n::t("View Profile"), Contact::magicLink($profile_link)]
167                         ];
168
169                         $entry = [
170                                 'id'           => $rr['id'],
171                                 'url'          => $profile_link,
172                                 'itemurl'      => $itemurl,
173                                 'thumb'        => ProxyUtils::proxifyUrl($rr[$photo], false, ProxyUtils::SIZE_THUMB),
174                                 'img_hover'    => $rr['name'],
175                                 'name'         => $rr['name'],
176                                 'details'      => $details,
177                                 'account_type' => Contact::getAccountType($rr),
178                                 'profile'      => $profile,
179                                 'location'     => $location_e,
180                                 'tags'         => $rr['pub_keywords'],
181                                 'gender'       => $gender,
182                                 'pdesc'        => $pdesc,
183                                 'marital'      => $marital,
184                                 'homepage'     => $homepage,
185                                 'about'        => $about,
186                                 'photo_menu'   => $photo_menu,
187
188                         ];
189
190                         $arr = ['contact' => $rr, 'entry' => $entry];
191
192                         Addon::callHooks('directory_item', $arr);
193
194                         unset($profile);
195                         unset($location);
196
197                         if (!$arr['entry']) {
198                                 continue;
199                         }
200
201                         $entries[] = $arr['entry'];
202                 }
203                 DBA::close($r);
204
205                 $tpl = get_markup_template('directory_header.tpl');
206
207                 $o .= Renderer::replaceMacros($tpl, [
208                         '$search'    => $search,
209                         '$globaldir' => L10n::t('Global Directory'),
210                         '$gdirpath'  => $gdirpath,
211                         '$desc'      => L10n::t('Find on this site'),
212                         '$contacts'  => $entries,
213                         '$finding'   => L10n::t('Results for:'),
214                         '$findterm'  => (strlen($search) ? $search : ""),
215                         '$title'     => L10n::t('Site Directory'),
216                         '$submit'    => L10n::t('Find'),
217                         '$paginate'  => $pager->renderFull($total),
218                 ]);
219         } else {
220                 info(L10n::t("No entries \x28some entries may be hidden\x29.") . EOL);
221         }
222
223         return $o;
224 }