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