]> git.mxchange.org Git - friendica-addons.git/blob - forumdirectory/forumdirectory.php
97908f8d72c339e0fcbd7145af3e035987e2c8c7
[friendica-addons.git] / forumdirectory / forumdirectory.php
1 <?php
2
3 /**
4  * Name: Forum Directory
5  * Description: Add a directory of forums hosted on your server, with verbose descriptions.
6  * Version: 1.0
7  * Author: Thomas Willingham <https://beardyunixer.com/profile/beardyunixer>
8  */
9
10 use Friendica\Content\Nav;
11 use Friendica\Content\Widget;
12 use Friendica\Core\Addon;
13 use Friendica\Core\Config;
14 use Friendica\Database\DBM;
15 use Friendica\Model\Profile;
16
17 require_once 'boot.php';
18 require_once 'include/dba.php';
19 require_once 'include/text.php';
20
21 function forumdirectory_install() {
22         Addon::registerHook('app_menu', 'addon/forumdirectory/forumdirectory.php', 'forumdirectory_app_menu');
23 }
24
25 function forumdirectory_uninstall() {
26         Addon::unregisterHook('app_menu', 'addon/forumdirectory/forumdirectory.php', 'forumdirectory_app_menu');
27 }
28
29 function forumdirectory_module()
30 {
31         return;
32 }
33
34 function forumdirectory_app_menu($a, &$b)
35 {
36         $b['app_menu'][] = '<div class="app-title"><a href="forumdirectory">' . t('Forum Directory') . '</a></div>';
37 }
38
39 function forumdirectory_init(&$a)
40 {
41         $a->page['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . $a->get_baseurl() . '/addon/forumdirectory/forumdirectory.css" media="all" />';
42
43         $a->set_pager_itemspage(60);
44
45         if (local_user()) {
46                 $a->page['aside'] .= Widget::findPeople();
47         } else {
48                 unset($_SESSION['theme']);
49         }
50 }
51
52 function forumdirectory_post(&$a)
53 {
54         if (x($_POST, 'search')) {
55                 $a->data['search'] = $_POST['search'];
56         }
57 }
58
59 function forumdirectory_content(&$a)
60 {
61         if ((Config::get('system', 'block_public')) && (!local_user()) && (!remote_user())) {
62                 notice(t('Public access denied.') . EOL);
63                 return;
64         }
65
66         $o = '';
67         Nav::setSelected('directory');
68
69         if (x($a->data, 'search')) {
70                 $search = notags(trim($a->data['search']));
71         } else {
72                 $search = ((x($_GET, 'search')) ? notags(trim(rawurldecode($_GET['search']))) : '');
73         }
74
75         $tpl = get_markup_template('directory_header.tpl');
76
77         $globaldir = '';
78         $gdirpath = Config::get('system', 'directory');
79         if (strlen($gdirpath)) {
80                 $globaldir = '<ul><li><div id="global-directory-link"><a href="'
81                         . Profile::zrl($gdirpath, true) . '">' . t('Global Directory') . '</a></div></li></ul>';
82         }
83
84         $admin = '';
85
86         $o .= replace_macros($tpl, [
87                 '$search'    => $search,
88                 '$globaldir' => $globaldir,
89                 '$desc'      => t('Find on this site'),
90                 '$admin'     => $admin,
91                 '$finding'   => (strlen($search) ? '<h4>' . t('Finding: ') . "'" . $search . "'" . '</h4>' : ""),
92                 '$sitedir'   => t('Site Directory'),
93                 '$submit'    => t('Find')
94         ]);
95
96         $sql_extra = '';
97         if (strlen($search)) {
98                 $sql_extra = " AND MATCH (`profile`.`name`, `user`.`nickname`, `pdesc`, `locality`,`region`,`country-name`,"
99                         . "`gender`,`marital`,`sexual`,`about`,`romance`,`work`,`education`,`pub_keywords`,`prv_keywords` )"
100                         . " AGAINST ('" . dbesc($search) . "' IN BOOLEAN MODE) ";
101         }
102
103         $publish = Config::get('system', 'publish_all') ? '' : " AND `publish` = 1 ";
104
105         $r = q("SELECT COUNT(*) AS `total` FROM `profile` LEFT JOIN `user` ON `user`.`uid` = `profile`.`uid`"
106                 . " WHERE `is-default` = 1 $publish AND `user`.`blocked` = 0 AND `page-flags` = 2 $sql_extra ");
107         if (DBM::is_result($r)) {
108                 $a->set_pager_total($r[0]['total']);
109         }
110
111         $order = " ORDER BY `name` ASC ";
112
113         $r = q("SELECT `profile`.*, `profile`.`uid` AS `profile_uid`, `user`.`nickname`, `user`.`timezone` , `user`.`page-flags`"
114                 . " FROM `profile` LEFT JOIN `user` ON `user`.`uid` = `profile`.`uid` WHERE `is-default` = 1 $publish"
115                 . " AND `user`.`blocked` = 0 AND `page-flags` = 2 $sql_extra $order LIMIT %d , %d ",
116                 intval($a->pager['start']),
117                 intval($a->pager['itemspage'])
118         );
119
120         if (DBM::is_result($r)) {
121                 if (in_array('small', $a->argv)) {
122                         $photo = 'thumb';
123                 } else {
124                         $photo = 'photo';
125                 }
126
127                 foreach ($r as $rr) {
128                         $profile_link = $a->get_baseurl() . '/profile/' . ((strlen($rr['nickname'])) ? $rr['nickname'] : $rr['profile_uid']);
129
130                         $pdesc = (($rr['pdesc']) ? $rr['pdesc'] . '<br />' : '');
131
132                         $details = '';
133                         if (strlen($rr['locality'])) {
134                                 $details .= $rr['locality'];
135                         }
136
137                         if (strlen($rr['region'])) {
138                                 if (strlen($rr['locality'])) {
139                                         $details .= ', ';
140                                 }
141                                 $details .= $rr['region'];
142                         }
143                         if (strlen($rr['country-name'])) {
144                                 if (strlen($details)) {
145                                         $details .= ', ';
146                                 }
147                                 $details .= $rr['country-name'];
148                         }
149
150                         if (strlen($rr['dob']) && ($years = age($rr['dob'], $rr['timezone'], '')) != 0) {
151                                 $details .= '<br />' . t('Age: ') . $years;
152                         }
153
154                         if (strlen($rr['gender'])) {
155                                 $details .= '<br />' . t('Gender: ') . $rr['gender'];
156                         }
157
158                         switch ($rr['page-flags']) {
159                                 case PAGE_NORMAL   : $page_type = "Personal Profile"; break;
160                                 case PAGE_SOAPBOX  : $page_type = "Fan Page"        ; break;
161                                 case PAGE_COMMUNITY: $page_type = "Community Forum" ; break;
162                                 case PAGE_FREELOVE : $page_type = "Open Forum"      ; break;
163                                 case PAGE_PRVGROUP : $page_type = "Private Group"   ; break;
164                         }
165
166                         $profile = $rr;
167
168                         $location = '';
169                         if (x($profile, 'address') == 1
170                                 || x($profile, 'locality') == 1
171                                 || x($profile, 'region') == 1
172                                 || x($profile, 'postal-code') == 1
173                                 || x($profile, 'country-name') == 1
174                         ) {
175                                 $location = t('Location:');
176                         }
177
178                         $gender   = x($profile, 'gender')   == 1 ? t('Gender:')   : false;
179                         $marital  = x($profile, 'marital')  == 1 ? t('Status:')   : false;
180                         $homepage = x($profile, 'homepage') == 1 ? t('Homepage:') : false;
181                         $about    = x($profile, 'about')    == 1 ? t('About:')    : false;
182
183                         $tpl = get_markup_template('forumdirectory_item.tpl', 'addon/forumdirectory/');
184
185                         $entry = replace_macros($tpl, [
186                                 '$id'           => $rr['id'],
187                                 '$profile_link' => $profile_link,
188                                 '$photo'        => $rr[$photo],
189                                 '$alt_text'     => $rr['name'],
190                                 '$name'         => $rr['name'],
191                                 '$details'      => $pdesc . $details,
192                                 '$page_type'    => $page_type,
193                                 '$profile'      => $profile,
194                                 '$location'     => $location,
195                                 '$gender'       => $gender,
196                                 '$pdesc'        => $pdesc,
197                                 '$marital'      => $marital,
198                                 '$homepage'     => $homepage,
199                                 '$about'        => $about,
200                         ]);
201
202                         $o .= $entry;
203                 }
204
205                 $o .= "<div class=\"directory-end\" ></div>\r\n";
206                 $o .= paginate($a);
207         } else {
208                 info(t("No entries \x28some entries may be hidden\x29.") . EOL);
209         }
210
211         return $o;
212 }