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