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