]> git.mxchange.org Git - friendica.git/blob - mod/community.php
We now have a single menu item again
[friendica.git] / mod / community.php
1 <?php
2
3 use Friendica\App;
4 use Friendica\Core\Config;
5 use Friendica\Core\PConfig;
6 use Friendica\Database\DBM;
7
8 function community_init(App $a) {
9         if (!local_user()) {
10                 unset($_SESSION['theme']);
11                 unset($_SESSION['mobile-theme']);
12         }
13 }
14
15 function community_content(App $a, $update = 0) {
16         $o = '';
17
18         if (Config::get('system','block_public') && !local_user() && !remote_user()) {
19                 notice(t('Public access denied.') . EOL);
20                 return;
21         }
22
23         $page_style = Config::get('system','community_page_style');
24
25         if ($a->argc > 1) {
26                 $content = $a->argv[1];
27         } else {
28                 // When only the global community is allowed, we use this as default
29                 $content = $page_style == CP_GLOBAL_COMMUNITY ? 'global' : 'local';
30         }
31
32         if (!in_array($content, ['local', 'global'])) {
33                 notice(t('Community option not available.') . EOL);
34                 return;
35         }
36
37         // Check if we are allowed to display the content to visitors
38         if (!local_user()) {
39                 $available = $page_style == CP_USERS_AND_GLOBAL;
40
41                 if (!$available) {
42                         $available = ($page_style == CP_USERS_ON_SERVER) && ($content == 'local');
43                 }
44
45                 if (!$available) {
46                         $available = ($page_style == CP_GLOBAL_COMMUNITY) && ($content == 'global');
47                 }
48
49                 if (!$available) {
50                         notice(t('Not available.') . EOL);
51                         return;
52                 }
53         }
54
55         require_once 'include/bbcode.php';
56         require_once 'include/security.php';
57         require_once 'include/conversation.php';
58
59         if (!$update) {
60                 $tabs = [];
61
62                 $tabs[] = array('label'=>t('Community'),
63                                 'url' => 'community/local',
64                                 'sel' => $content == 'local' ? 'active' : '',
65                                 'title' => t('Posts from local users on this server'),
66                                 'id' => 'community-local-tab',
67                                 'accesskey' => 'l');
68
69                 $tabs[] = array('label' => t('Global Timeline'),
70                                 'url' => 'community/global',
71                                 'sel' => $content == 'global' ? 'active' : '',
72                                 'title' => t('Posts from users of the federated network'),
73                                 'id'    => 'community-global-tab',
74                                 'accesskey' => 'g');
75
76                 $tab_tpl = get_markup_template('common_tabs.tpl');
77                 $o .= replace_macros($tab_tpl, array('$tabs' => $tabs));
78
79                 nav_set_selected('community');
80         }
81
82         if (Config::get('system', 'comment_public')) {
83                 // check if we serve a mobile device and get the user settings
84                 // accordingly
85                 if ($a->is_mobile) {
86                         $itemspage_network = PConfig::get(local_user(),'system','itemspage_mobile_network', 20);
87                 } else {
88                         $itemspage_network = PConfig::get(local_user(),'system','itemspage_network', 40);
89                 }
90
91                 // now that we have the user settings, see if the theme forces
92                 // a maximum item number which is lower then the user choice
93                 if (($a->force_max_items > 0) && ($a->force_max_items < $itemspage_network)) {
94                         $itemspage_network = $a->force_max_items;
95                 }
96
97                 $a->set_pager_itemspage($itemspage_network);
98         }
99
100         $r = community_getitems($a->pager['start'], $a->pager['itemspage'], $content);
101
102         if (!DBM::is_result($r)) {
103                 info(t('No results.') . EOL);
104                 return $o;
105         }
106
107         $maxpostperauthor = Config::get('system','max_author_posts_community_page');
108
109         if (($maxpostperauthor != 0) && ($content == 'local')) {
110                 $count = 1;
111                 $previousauthor = "";
112                 $numposts = 0;
113                 $s = array();
114
115                 do {
116                         foreach ($r AS $row=>$item) {
117                                 if ($previousauthor == $item["author-link"]) {
118                                         ++$numposts;
119                                 } else {
120                                         $numposts = 0;
121                                 }
122                                 $previousauthor = $item["author-link"];
123
124                                 if (($numposts < $maxpostperauthor) && (sizeof($s) < $a->pager['itemspage'])) {
125                                         $s[] = $item;
126                                 }
127                         }
128                         if (sizeof($s) < $a->pager['itemspage']) {
129                                 $r = community_getitems($a->pager['start'] + ($count * $a->pager['itemspage']), $a->pager['itemspage'], $content);
130                         }
131                 } while ((sizeof($s) < $a->pager['itemspage']) && (++$count < 50) && (sizeof($r) > 0));
132         } else {
133                 $s = $r;
134         }
135
136         $o .= conversation($a, $s, 'community', $update);
137
138         $o .= alt_pager($a, count($r));
139
140         $t = get_markup_template("community.tpl");
141         return replace_macros($t, array(
142                 '$content' => $o,
143                 '$header' => '',
144                 '$show_global_community_hint' => ($content == 'global') && Config::get('system', 'show_global_community_hint'),
145                 '$global_community_hint' => t("This community stream shows all public posts received by this node. They may not reflect the opinions of this node’s users.")
146         ));
147 }
148
149 function community_getitems($start, $itemspage, $content) {
150         if ($content == 'local') {
151                 $r = dba::p("SELECT ".item_fieldlists()." FROM `thread`
152                         INNER JOIN `user` ON `user`.`uid` = `thread`.`uid` AND NOT `user`.`hidewall`
153                         INNER JOIN `item` ON `item`.`id` = `thread`.`iid`
154                         AND `item`.`allow_cid` = ''  AND `item`.`allow_gid` = ''
155                         AND `item`.`deny_cid`  = '' AND `item`.`deny_gid`  = ''".
156                         item_joins()." AND `contact`.`self`
157                         WHERE `thread`.`visible` AND NOT `thread`.`deleted` AND NOT `thread`.`moderated`
158                         AND NOT `thread`.`private` AND `thread`.`wall`
159                         ORDER BY `thread`.`received` DESC LIMIT ".intval($start).", ".intval($itemspage)
160                 );
161                 return dba::inArray($r);
162         } elseif ($content == 'global') {
163                 $r = dba::p("SELECT ".item_fieldlists()." FROM `thread`
164                         INNER JOIN `item` ON `item`.`id` = `thread`.`iid` ".item_joins().
165                         "WHERE `thread`.`uid` = 0 AND `verb` = ?
166                         ORDER BY `thread`.`created` DESC LIMIT ".intval($start).", ".intval($itemspage),
167                         ACTIVITY_POST
168                 );
169                 return dba::inArray($r);
170         }
171
172         // Should never happen
173         return array();
174 }