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