]> git.mxchange.org Git - friendica-addons.git/blob - forumdirectory/forumdirectory.php
[various] Remove App dependency from hook functions
[friendica-addons.git] / forumdirectory / forumdirectory.php
1 <?php
2 /**
3  * Name: Forum Directory
4  * Description: Add a directory of forums hosted on your server, with verbose descriptions.
5  * Version: 1.1
6  * Author: Thomas Willingham <https://beardyunixer.com/profile/beardyunixer>
7  */
8
9 use Friendica\App;
10 use Friendica\Content\Nav;
11 use Friendica\Content\Pager;
12 use Friendica\Content\Widget;
13 use Friendica\Core\Hook;
14 use Friendica\Core\Renderer;
15 use Friendica\Database\DBA;
16 use Friendica\DI;
17 use Friendica\Model\Profile;
18 use Friendica\Model\User;
19
20 global $forumdirectory_search;
21
22 function forumdirectory_install()
23 {
24         Hook::register('app_menu', 'addon/forumdirectory/forumdirectory.php', 'forumdirectory_app_menu');
25 }
26
27 /**
28  * This is a statement rather than an actual function definition. The simple
29  * existence of this method is checked to figure out if the addon offers a
30  * module.
31  */
32 /**
33  * This is a statement rather than an actual function definition. The simple
34  * existence of this method is checked to figure out if the addon offers a
35  * module.
36  */
37 function forumdirectory_module() {}
38
39 function forumdirectory_app_menu(array &$b)
40 {
41         $b['app_menu'][] = '<div class="app-title"><a href="forumdirectory">' . DI::l10n()->t('Forum Directory') . '</a></div>';
42 }
43
44 function forumdirectory_init()
45 {
46         if (DI::userSession()->getLocalUserId()) {
47                 DI::page()['aside'] .= Widget::findPeople();
48         }
49 }
50
51 function forumdirectory_post()
52 {
53         global $forumdirectory_search;
54
55         if (!empty($_POST['search'])) {
56                 $forumdirectory_search = $_POST['search'];
57         }
58 }
59
60 function forumdirectory_content()
61 {
62         global $forumdirectory_search;
63
64         if (DI::config()->get('system', 'block_public') && !DI::userSession()->getLocalUserId() && !DI::userSession()->getRemoteUserId()) {
65                 DI::sysmsg()->addNotice(DI::l10n()->t('Public access denied.'));
66                 return;
67         }
68
69         $o = '';
70         $entries = [];
71
72         Nav::setSelected('directory');
73
74         if (!empty($forumdirectory_search)) {
75                 $search = trim($forumdirectory_search);
76         } else {
77                 $search = (!empty($_GET['search']) ? trim(rawurldecode($_GET['search'])) : '');
78         }
79
80         $gdirpath = '';
81         $dirurl = DI::config()->get('system', 'directory');
82         if (strlen($dirurl)) {
83                 $gdirpath = Profile::zrl($dirurl, true);
84         }
85
86         $sql_extra = '';
87         if (strlen($search)) {
88                 $search = DBA::escape($search);
89
90                 $sql_extra = " AND ((`profile`.`name` LIKE '%$search%') OR
91                                 (`user`.`nickname` LIKE '%$search%') OR
92                                 (`profile`.`about` LIKE '%$search%') OR
93                                 (`profile`.`locality` LIKE '%$search%') OR
94                                 (`profile`.`region` LIKE '%$search%') OR
95                                 (`profile`.`country-name` LIKE '%$search%') OR
96                                 (`profile`.`pub_keywords` LIKE '%$search%') OR
97                                 (`profile`.`prv_keywords` LIKE '%$search%'))";
98         }
99
100         $publish = DI::config()->get('system', 'publish_all') ? '' : "`publish` = 1";
101
102         $total = 0;
103         $cnt = DBA::fetchFirst("SELECT COUNT(*) AS `total` FROM `profile`
104                                 INNER JOIN `user` ON `user`.`uid` = `profile`.`uid`
105                                 WHERE $publish AND NOT `user`.`blocked` AND NOT `user`.`account_removed` AND `user`.`page-flags` = ? $sql_extra",
106                                 User::PAGE_FLAGS_COMMUNITY);
107         if (DBA::isResult($cnt)) {
108                 $total = $cnt['total'];
109         }
110
111         $pager = new Pager(DI::l10n(), DI::args()->getQueryString(), 60);
112
113         $order = " ORDER BY `name` ASC ";
114
115         $limit = $pager->getStart()."," . $pager->getItemsPerPage();
116
117         $r = DBA::p("SELECT `profile`.*, `user`.`nickname`, `user`.`timezone` , `user`.`page-flags`,
118                         `contact`.`addr`, `contact`.`url` FROM `profile`
119                         INNER JOIN `user` ON `user`.`uid` = `profile`.`uid`
120                         INNER JOIN `contact` ON `contact`.`uid` = `user`.`uid`
121                         WHERE $publish AND NOT `user`.`blocked` AND NOT `user`.`account_removed` AND `user`.`page-flags` = ? AND `contact`.`self`
122                         $sql_extra $order LIMIT $limit", User::PAGE_FLAGS_COMMUNITY
123         );
124
125         if (DBA::isResult($r)) {
126                 if (in_array('small', DI::args()->getArgv())) {
127                         $photo = 'thumb';
128                 } else {
129                         $photo = 'photo';
130                 }
131
132                 while ($rr = DBA::fetch($r)) {
133                         $entries[] = Friendica\Module\Directory::formatEntry($rr, $photo);
134                 }
135                 DBA::close($r);
136         } else {
137                 DI::sysmsg()->addNotice(DI::l10n()->t("No entries \x28some entries may be hidden\x29."));
138         }
139
140         $tpl = Renderer::getMarkupTemplate('directory_header.tpl');
141         $o .= Renderer::replaceMacros($tpl, [
142                 '$search'     => $search,
143                 '$globaldir'  => DI::l10n()->t('Global Directory'),
144                 '$gdirpath'   => $gdirpath,
145                 '$desc'       => DI::l10n()->t('Find on this site'),
146                 '$contacts'   => $entries,
147                 '$finding'    => DI::l10n()->t('Results for:'),
148                 '$findterm'   => (strlen($search) ? $search : ""),
149                 '$title'      => DI::l10n()->t('Forum Directory'),
150                 '$search_mod' => 'forumdirectory',
151                 '$submit'     => DI::l10n()->t('Find'),
152                 '$paginate'   => $pager->renderFull($total),
153         ]);
154
155         return $o;
156 }