]> git.mxchange.org Git - friendica.git/blob - mod/community.php
add missing frio template
[friendica.git] / mod / community.php
1 <?php
2 /**
3  * @file mod/community.php
4  */
5
6 use Friendica\App;
7 use Friendica\Content\Feature;
8 use Friendica\Content\Nav;
9 use Friendica\Content\Pager;
10 use Friendica\Content\Widget\TrendingTags;
11 use Friendica\Core\ACL;
12 use Friendica\Core\Config;
13 use Friendica\Core\L10n;
14 use Friendica\Core\PConfig;
15 use Friendica\Core\Renderer;
16 use Friendica\Database\DBA;
17 use Friendica\Model\Item;
18 use Friendica\Model\User;
19
20 function community_content(App $a, $update = 0)
21 {
22         $o = '';
23
24         if (Config::get('system', 'block_public') && !Session::isAuthenticated()) {
25                 notice(L10n::t('Public access denied.') . EOL);
26                 return;
27         }
28
29         $page_style = Config::get('system', 'community_page_style');
30
31         if ($page_style == CP_NO_INTERNAL_COMMUNITY) {
32                 notice(L10n::t('Access denied.') . EOL);
33                 return;
34         }
35
36         $accounttype = null;
37
38         if ($a->argc > 2) {
39                 switch ($a->argv[2]) {
40                         case 'person':
41                                 $accounttype = User::ACCOUNT_TYPE_PERSON;
42                                 break;
43                         case 'organisation':
44                                 $accounttype = User::ACCOUNT_TYPE_ORGANISATION;
45                                 break;
46                         case 'news':
47                                 $accounttype = User::ACCOUNT_TYPE_NEWS;
48                                 break;
49                         case 'community':
50                                 $accounttype = User::ACCOUNT_TYPE_COMMUNITY;
51                                 break;
52                 }
53         }
54
55         if ($a->argc > 1) {
56                 $content = $a->argv[1];
57         } else {
58                 if (!empty(Config::get('system', 'singleuser'))) {
59                         // On single user systems only the global page does make sense
60                         $content = 'global';
61                 } else {
62                         // When only the global community is allowed, we use this as default
63                         $content = $page_style == CP_GLOBAL_COMMUNITY ? 'global' : 'local';
64                 }
65         }
66
67         if (!in_array($content, ['local', 'global'])) {
68                 notice(L10n::t('Community option not available.') . EOL);
69                 return;
70         }
71
72         // Check if we are allowed to display the content to visitors
73         if (!local_user()) {
74                 $available = $page_style == CP_USERS_AND_GLOBAL;
75
76                 if (!$available) {
77                         $available = ($page_style == CP_USERS_ON_SERVER) && ($content == 'local');
78                 }
79
80                 if (!$available) {
81                         $available = ($page_style == CP_GLOBAL_COMMUNITY) && ($content == 'global');
82                 }
83
84                 if (!$available) {
85                         notice(L10n::t('Not available.') . EOL);
86                         return;
87                 }
88         }
89
90         if (!$update) {
91                 $tabs = [];
92
93                 if ((local_user() || in_array($page_style, [CP_USERS_AND_GLOBAL, CP_USERS_ON_SERVER])) && empty(Config::get('system', 'singleuser'))) {
94                         $tabs[] = [
95                                 'label' => L10n::t('Local Community'),
96                                 'url' => 'community/local',
97                                 'sel' => $content == 'local' ? 'active' : '',
98                                 'title' => L10n::t('Posts from local users on this server'),
99                                 'id' => 'community-local-tab',
100                                 'accesskey' => 'l'
101                         ];
102                 }
103
104                 if (local_user() || in_array($page_style, [CP_USERS_AND_GLOBAL, CP_GLOBAL_COMMUNITY])) {
105                         $tabs[] = [
106                                 'label' => L10n::t('Global Community'),
107                                 'url' => 'community/global',
108                                 'sel' => $content == 'global' ? 'active' : '',
109                                 'title' => L10n::t('Posts from users of the whole federated network'),
110                                 'id' => 'community-global-tab',
111                                 'accesskey' => 'g'
112                         ];
113                 }
114
115                 $tab_tpl = Renderer::getMarkupTemplate('common_tabs.tpl');
116                 $o .= Renderer::replaceMacros($tab_tpl, ['$tabs' => $tabs]);
117
118                 Nav::setSelected('community');
119
120                 // We need the editor here to be able to reshare an item.
121                 if (local_user()) {
122                         $x = [
123                                 'is_owner' => true,
124                                 'allow_location' => $a->user['allow_location'],
125                                 'default_location' => $a->user['default-location'],
126                                 'nickname' => $a->user['nickname'],
127                                 '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'),
128                                 'acl' => ACL::getFullSelectorHTML($a->user, true),
129                                 'bang' => '',
130                                 'visitor' => 'block',
131                                 'profile_uid' => local_user(),
132                         ];
133                         $o .= status_editor($a, $x, 0, true);
134                 }
135         }
136
137         // check if we serve a mobile device and get the user settings accordingly
138         if ($a->is_mobile) {
139                 $itemspage_network = PConfig::get(local_user(), 'system', 'itemspage_mobile_network', 20);
140         } else {
141                 $itemspage_network = PConfig::get(local_user(), 'system', 'itemspage_network', 40);
142         }
143
144         // now that we have the user settings, see if the theme forces
145         // a maximum item number which is lower then the user choice
146         if (($a->force_max_items > 0) && ($a->force_max_items < $itemspage_network)) {
147                 $itemspage_network = $a->force_max_items;
148         }
149
150         $pager = new Pager($a->query_string, $itemspage_network);
151
152         $r = community_getitems($pager->getStart(), $pager->getItemsPerPage(), $content, $accounttype);
153
154         if (!DBA::isResult($r)) {
155                 info(L10n::t('No results.') . EOL);
156                 return $o;
157         }
158
159         $maxpostperauthor = (int) Config::get('system', 'max_author_posts_community_page');
160
161         if (($maxpostperauthor != 0) && ($content == 'local')) {
162                 $count = 1;
163                 $previousauthor = "";
164                 $numposts = 0;
165                 $s = [];
166
167                 do {
168                         foreach ($r as $item) {
169                                 if ($previousauthor == $item["author-link"]) {
170                                         ++$numposts;
171                                 } else {
172                                         $numposts = 0;
173                                 }
174                                 $previousauthor = $item["author-link"];
175
176                                 if (($numposts < $maxpostperauthor) && (count($s) < $pager->getItemsPerPage())) {
177                                         $s[] = $item;
178                                 }
179                         }
180                         if (count($s) < $pager->getItemsPerPage()) {
181                                 $r = community_getitems($pager->getStart() + ($count * $pager->getItemsPerPage()), $pager->getItemsPerPage(), $content, $accounttype);
182                         }
183                 } while ((count($s) < $pager->getItemsPerPage()) && ( ++$count < 50) && (count($r) > 0));
184         } else {
185                 $s = $r;
186         }
187
188         $o .= conversation($a, $s, $pager, 'community', $update, false, 'commented', local_user());
189
190         if (!$update) {
191                 $o .= $pager->renderMinimal(count($r));
192         }
193
194         if (empty($a->page['aside'])) {
195                 $a->page['aside'] = '';
196         }
197
198         if (Feature::isEnabled(local_user(), 'trending_tags')) {
199                 $a->page['aside'] .= TrendingTags::getHTML($content);
200         }
201
202         $t = Renderer::getMarkupTemplate("community.tpl");
203         return Renderer::replaceMacros($t, [
204                 '$content' => $o,
205                 '$header' => '',
206                 '$show_global_community_hint' => ($content == 'global') && Config::get('system', 'show_global_community_hint'),
207                 '$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.")
208         ]);
209 }
210
211 function community_getitems($start, $itemspage, $content, $accounttype)
212 {
213         if ($content == 'local') {
214                 if (!is_null($accounttype)) {
215                         $sql_accounttype = " AND `user`.`account-type` = ?";
216                         $values = [$accounttype, $start, $itemspage];
217                 } else {
218                         $sql_accounttype = "";
219                         $values = [$start, $itemspage];
220                 }
221
222                 /// @todo Use "unsearchable" here as well (instead of "hidewall")
223                 $r = DBA::p("SELECT `item`.`uri`, `author`.`url` AS `author-link` FROM `thread`
224                         STRAIGHT_JOIN `user` ON `user`.`uid` = `thread`.`uid` AND NOT `user`.`hidewall`
225                         STRAIGHT_JOIN `item` ON `item`.`id` = `thread`.`iid`
226                         STRAIGHT_JOIN `contact` AS `author` ON `author`.`id`=`item`.`author-id`
227                         WHERE `thread`.`visible` AND NOT `thread`.`deleted` AND NOT `thread`.`moderated`
228                         AND NOT `thread`.`private` AND `thread`.`wall` AND `thread`.`origin` $sql_accounttype
229                         ORDER BY `thread`.`commented` DESC LIMIT ?, ?", $values);
230                 return DBA::toArray($r);
231         } elseif ($content == 'global') {
232                 if (!is_null($accounttype)) {
233                         $condition = ["`uid` = ? AND NOT `author`.`unsearchable` AND NOT `owner`.`unsearchable` AND `owner`.`contact-type` = ?", 0, $accounttype];
234                 } else {
235                         $condition = ["`uid` = ? AND NOT `author`.`unsearchable` AND NOT `owner`.`unsearchable`", 0];
236                 }
237
238                 $r = Item::selectThreadForUser(0, ['uri'], $condition, ['order' => ['commented' => true], 'limit' => [$start, $itemspage]]);
239                 return DBA::toArray($r);
240         }
241
242         // Should never happen
243         return [];
244 }