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