3 * @file mod/community.php
7 use Friendica\Content\Nav;
8 use Friendica\Content\Pager;
9 use Friendica\Core\ACL;
10 use Friendica\Core\Config;
11 use Friendica\Core\L10n;
12 use Friendica\Core\PConfig;
13 use Friendica\Core\Renderer;
14 use Friendica\Database\DBA;
15 use Friendica\Model\Contact;
16 use Friendica\Model\Item;
17 use Friendica\Model\User;
19 function community_init(App $a)
22 unset($_SESSION['theme']);
23 unset($_SESSION['mobile-theme']);
27 function community_content(App $a, $update = 0)
31 if (Config::get('system', 'block_public') && !local_user() && !remote_user()) {
32 notice(L10n::t('Public access denied.') . EOL);
36 $page_style = Config::get('system', 'community_page_style');
38 if ($page_style == CP_NO_INTERNAL_COMMUNITY) {
39 notice(L10n::t('Access denied.') . EOL);
46 switch ($a->argv[2]) {
48 $accounttype = User::ACCOUNT_TYPE_PERSON;
51 $accounttype = User::ACCOUNT_TYPE_ORGANISATION;
54 $accounttype = User::ACCOUNT_TYPE_NEWS;
57 $accounttype = User::ACCOUNT_TYPE_COMMUNITY;
63 $content = $a->argv[1];
65 if (!empty(Config::get('system', 'singleuser'))) {
66 // On single user systems only the global page does make sense
69 // When only the global community is allowed, we use this as default
70 $content = $page_style == CP_GLOBAL_COMMUNITY ? 'global' : 'local';
74 if (!in_array($content, ['local', 'global'])) {
75 notice(L10n::t('Community option not available.') . EOL);
79 // Check if we are allowed to display the content to visitors
81 $available = $page_style == CP_USERS_AND_GLOBAL;
84 $available = ($page_style == CP_USERS_ON_SERVER) && ($content == 'local');
88 $available = ($page_style == CP_GLOBAL_COMMUNITY) && ($content == 'global');
92 notice(L10n::t('Not available.') . EOL);
100 if ((local_user() || in_array($page_style, [CP_USERS_AND_GLOBAL, CP_USERS_ON_SERVER])) && empty(Config::get('system', 'singleuser'))) {
102 'label' => L10n::t('Local Community'),
103 'url' => 'community/local',
104 'sel' => $content == 'local' ? 'active' : '',
105 'title' => L10n::t('Posts from local users on this server'),
106 'id' => 'community-local-tab',
111 if (local_user() || in_array($page_style, [CP_USERS_AND_GLOBAL, CP_GLOBAL_COMMUNITY])) {
113 'label' => L10n::t('Global Community'),
114 'url' => 'community/global',
115 'sel' => $content == 'global' ? 'active' : '',
116 'title' => L10n::t('Posts from users of the whole federated network'),
117 'id' => 'community-global-tab',
122 $tab_tpl = Renderer::getMarkupTemplate('common_tabs.tpl');
123 $o .= Renderer::replaceMacros($tab_tpl, ['$tabs' => $tabs]);
125 Nav::setSelected('community');
127 // We need the editor here to be able to reshare an item.
131 'allow_location' => $a->user['allow_location'],
132 'default_location' => $a->user['default-location'],
133 'nickname' => $a->user['nickname'],
134 'lockstate' => (is_array($a->user) && (strlen($a->user['allow_cid']) || strlen($a->user['allow_gid']) || strlen($a->user['deny_cid']) || strlen($a->user['deny_gid'])) ? 'lock' : 'unlock'),
135 'acl' => ACL::getFullSelectorHTML($a->user, true),
137 'visitor' => 'block',
138 'profile_uid' => local_user(),
140 $o .= status_editor($a, $x, 0, true);
144 // check if we serve a mobile device and get the user settings accordingly
146 $itemspage_network = PConfig::get(local_user(), 'system', 'itemspage_mobile_network', 20);
148 $itemspage_network = PConfig::get(local_user(), 'system', 'itemspage_network', 40);
151 // now that we have the user settings, see if the theme forces
152 // a maximum item number which is lower then the user choice
153 if (($a->force_max_items > 0) && ($a->force_max_items < $itemspage_network)) {
154 $itemspage_network = $a->force_max_items;
157 $pager = new Pager($a->query_string, $itemspage_network);
159 $r = community_getitems($pager->getStart(), $pager->getItemsPerPage(), $content, $accounttype);
161 if (!DBA::isResult($r)) {
162 info(L10n::t('No results.') . EOL);
166 $maxpostperauthor = (int) Config::get('system', 'max_author_posts_community_page');
168 if (($maxpostperauthor != 0) && ($content == 'local')) {
170 $previousauthor = "";
175 foreach ($r as $item) {
176 if ($previousauthor == $item["author-link"]) {
181 $previousauthor = $item["author-link"];
183 if (($numposts < $maxpostperauthor) && (count($s) < $pager->getItemsPerPage())) {
187 if (count($s) < $pager->getItemsPerPage()) {
188 $r = community_getitems($pager->getStart() + ($count * $pager->getItemsPerPage()), $pager->getItemsPerPage(), $content, $accounttype);
190 } while ((count($s) < $pager->getItemsPerPage()) && ( ++$count < 50) && (count($r) > 0));
195 $o .= conversation($a, $s, $pager, 'community', $update, false, 'commented', local_user());
198 $o .= $pager->renderMinimal(count($r));
201 $t = Renderer::getMarkupTemplate("community.tpl");
202 return Renderer::replaceMacros($t, [
205 '$show_global_community_hint' => ($content == 'global') && Config::get('system', 'show_global_community_hint'),
206 '$global_community_hint' => L10n::t("This community stream shows all public posts received by this node. They may not reflect the opinions of this node’s users.")
210 function community_getitems($start, $itemspage, $content, $accounttype)
212 if ($content == 'local') {
213 if (!is_null($accounttype)) {
214 $sql_accounttype = " AND `user`.`account-type` = ?";
215 $values = [$accounttype, $start, $itemspage];
217 $sql_accounttype = "";
218 $values = [$start, $itemspage];
221 $r = DBA::p("SELECT `item`.`uri`, `author`.`url` AS `author-link` FROM `thread`
222 STRAIGHT_JOIN `user` ON `user`.`uid` = `thread`.`uid` AND NOT `user`.`hidewall`
223 STRAIGHT_JOIN `item` ON `item`.`id` = `thread`.`iid`
224 STRAIGHT_JOIN `contact` AS `author` ON `author`.`id`=`item`.`author-id`
225 WHERE `thread`.`visible` AND NOT `thread`.`deleted` AND NOT `thread`.`moderated`
226 AND NOT `thread`.`private` AND `thread`.`wall` AND `thread`.`origin` $sql_accounttype
227 ORDER BY `thread`.`commented` DESC LIMIT ?, ?", $values);
228 return DBA::toArray($r);
229 } elseif ($content == 'global') {
230 if (!is_null($accounttype)) {
231 $condition = ["`uid` = ? AND `owner`.`contact-type` = ?", 0, $accounttype];
233 $condition = ['uid' => 0];
236 $r = Item::selectThreadForUser(0, ['uri'], $condition, ['order' => ['commented' => true], 'limit' => [$start, $itemspage]]);
237 return DBA::toArray($r);
240 // Should never happen