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