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