]> git.mxchange.org Git - friendica.git/blob - mod/community.php
Many t() calls
[friendica.git] / mod / community.php
1 <?php
2 /**
3  * @file mod/community.php
4  */
5 use Friendica\App;
6 use Friendica\Content\Nav;
7 use Friendica\Core\Config;
8 use Friendica\Core\L10n;
9 use Friendica\Core\PConfig;
10 use Friendica\Database\DBM;
11
12 function community_init(App $a)
13 {
14         if (!local_user()) {
15                 unset($_SESSION['theme']);
16                 unset($_SESSION['mobile-theme']);
17         }
18 }
19
20 function community_content(App $a, $update = 0)
21 {
22         $o = '';
23
24         if (Config::get('system', 'block_public') && !local_user() && !remote_user()) {
25                 notice(L10n::t('Public access denied.') . EOL);
26                 return;
27         }
28
29         $page_style = Config::get('system', 'community_page_style');
30
31         if ($a->argc > 1) {
32                 $content = $a->argv[1];
33         } else {
34                 if (!empty(Config::get('system', 'singleuser'))) {
35                         // On single user systems only the global page does make sense
36                         $content = 'global';
37                 } else {
38                         // When only the global community is allowed, we use this as default
39                         $content = $page_style == CP_GLOBAL_COMMUNITY ? 'global' : 'local';
40                 }
41         }
42
43         if (!in_array($content, ['local', 'global'])) {
44                 notice(L10n::t('Community option not available.') . EOL);
45                 return;
46         }
47
48         // Check if we are allowed to display the content to visitors
49         if (!local_user()) {
50                 $available = $page_style == CP_USERS_AND_GLOBAL;
51
52                 if (!$available) {
53                         $available = ($page_style == CP_USERS_ON_SERVER) && ($content == 'local');
54                 }
55
56                 if (!$available) {
57                         $available = ($page_style == CP_GLOBAL_COMMUNITY) && ($content == 'global');
58                 }
59
60                 if (!$available) {
61                         notice(L10n::t('Not available.') . EOL);
62                         return;
63                 }
64         }
65
66         require_once 'include/bbcode.php';
67         require_once 'include/security.php';
68         require_once 'include/conversation.php';
69
70         if (!$update) {
71                 $tabs = [];
72
73                 if ((local_user() || in_array($page_style, [CP_USERS_AND_GLOBAL, CP_USERS_ON_SERVER])) && empty(Config::get('system', 'singleuser'))) {
74                         $tabs[] = [
75                                 'label' => L10n::t('Community'),
76                                 'url' => 'community/local',
77                                 'sel' => $content == 'local' ? 'active' : '',
78                                 'title' => L10n::t('Posts from local users on this server'),
79                                 'id' => 'community-local-tab',
80                                 'accesskey' => 'l'
81                         ];
82                 }
83
84                 if (local_user() || in_array($page_style, [CP_USERS_AND_GLOBAL, CP_GLOBAL_COMMUNITY])) {
85                         $tabs[] = [
86                                 'label' => L10n::t('Global Timeline'),
87                                 'url' => 'community/global',
88                                 'sel' => $content == 'global' ? 'active' : '',
89                                 'title' => L10n::t('Posts from users of the federated network'),
90                                 'id' => 'community-global-tab',
91                                 'accesskey' => 'g'
92                         ];
93                 }
94
95                 $tab_tpl = get_markup_template('common_tabs.tpl');
96                 $o .= replace_macros($tab_tpl, ['$tabs' => $tabs]);
97
98                 Nav::setSelected('community');
99
100                 // We need the editor here to be able to reshare an item.
101                 if (local_user()) {
102                         $x = [
103                                 'is_owner' => true,
104                                 'allow_location' => $a->user['allow_location'],
105                                 'default_location' => $a->user['default-location'],
106                                 'nickname' => $a->user['nickname'],
107                                 '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'),
108                                 'acl' => populate_acl($a->user, true),
109                                 'bang' => '',
110                                 'visitor' => 'block',
111                                 'profile_uid' => local_user(),
112                         ];
113                         $o .= status_editor($a, $x, 0, true);
114                 }
115         }
116
117         if (Config::get('system', 'comment_public')) {
118                 // check if we serve a mobile device and get the user settings
119                 // accordingly
120                 if ($a->is_mobile) {
121                         $itemspage_network = PConfig::get(local_user(), 'system', 'itemspage_mobile_network', 20);
122                 } else {
123                         $itemspage_network = PConfig::get(local_user(), 'system', 'itemspage_network', 40);
124                 }
125
126                 // now that we have the user settings, see if the theme forces
127                 // a maximum item number which is lower then the user choice
128                 if (($a->force_max_items > 0) && ($a->force_max_items < $itemspage_network)) {
129                         $itemspage_network = $a->force_max_items;
130                 }
131
132                 $a->set_pager_itemspage($itemspage_network);
133         }
134
135         $r = community_getitems($a->pager['start'], $a->pager['itemspage'], $content);
136
137         if (!DBM::is_result($r)) {
138                 info(L10n::t('No results.') . EOL);
139                 return $o;
140         }
141
142         $maxpostperauthor = (int) Config::get('system', 'max_author_posts_community_page');
143
144         if (($maxpostperauthor != 0) && ($content == 'local')) {
145                 $count = 1;
146                 $previousauthor = "";
147                 $numposts = 0;
148                 $s = [];
149
150                 do {
151                         foreach ($r as $item) {
152                                 if ($previousauthor == $item["author-link"]) {
153                                         ++$numposts;
154                                 } else {
155                                         $numposts = 0;
156                                 }
157                                 $previousauthor = $item["author-link"];
158
159                                 if (($numposts < $maxpostperauthor) && (count($s) < $a->pager['itemspage'])) {
160                                         $s[] = $item;
161                                 }
162                         }
163                         if (count($s) < $a->pager['itemspage']) {
164                                 $r = community_getitems($a->pager['start'] + ($count * $a->pager['itemspage']), $a->pager['itemspage'], $content);
165                         }
166                 } while ((count($s) < $a->pager['itemspage']) && ( ++$count < 50) && (count($r) > 0));
167         } else {
168                 $s = $r;
169         }
170
171         $o .= conversation($a, $s, 'community', $update);
172
173         if (!$update) {
174                 $o .= alt_pager($a, count($r));
175         }
176
177         $t = get_markup_template("community.tpl");
178         return replace_macros($t, [
179                 '$content' => $o,
180                 '$header' => '',
181                 '$show_global_community_hint' => ($content == 'global') && Config::get('system', 'show_global_community_hint'),
182                 '$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.")
183         ]);
184 }
185
186 function community_getitems($start, $itemspage, $content)
187 {
188         if ($content == 'local') {
189                 $r = dba::p("SELECT " . item_fieldlists() . " FROM `thread`
190                         INNER JOIN `user` ON `user`.`uid` = `thread`.`uid` AND NOT `user`.`hidewall`
191                         INNER JOIN `item` ON `item`.`id` = `thread`.`iid`
192                         AND `item`.`allow_cid` = ''  AND `item`.`allow_gid` = ''
193                         AND `item`.`deny_cid`  = '' AND `item`.`deny_gid`  = ''" .
194                         item_joins() . " AND `contact`.`self`
195                         WHERE `thread`.`visible` AND NOT `thread`.`deleted` AND NOT `thread`.`moderated`
196                         AND NOT `thread`.`private` AND `thread`.`wall`
197                         ORDER BY `thread`.`commented` DESC LIMIT " . intval($start) . ", " . intval($itemspage)
198                 );
199                 return dba::inArray($r);
200         } elseif ($content == 'global') {
201                 $r = dba::p("SELECT " . item_fieldlists() . " FROM `thread`
202                         INNER JOIN `item` ON `item`.`id` = `thread`.`iid` " . item_joins() .
203                                 "WHERE `thread`.`uid` = 0 AND `verb` = ?
204                         ORDER BY `thread`.`commented` DESC LIMIT " . intval($start) . ", " . intval($itemspage),
205                         ACTIVITY_POST
206                 );
207                 return dba::inArray($r);
208         }
209
210         // Should never happen
211         return [];
212 }