5 * Description: Show last community activity in homepage
7 * Author: Fabio Comuni <http://kirgroup.com/profile/fabrixxm>
11 use Friendica\Core\Addon;
12 use Friendica\Core\Config;
13 use Friendica\Core\L10n;
14 use Friendica\Module\Login;
16 require_once 'mod/community.php';
18 function communityhome_install()
20 Addon::registerHook('home_content', 'addon/communityhome/communityhome.php', 'communityhome_home');
21 logger("installed communityhome");
24 function communityhome_uninstall()
26 Addon::unregisterHook('home_content', 'addon/communityhome/communityhome.php', 'communityhome_home');
27 logger("removed communityhome");
30 function communityhome_getopts()
33 'hidelogin' => L10n::t('Hide login form'),
34 'showlastusers' => L10n::t('Show last new users'),
35 'showactiveusers' => L10n::t('Show last active users'),
36 'showlastphotos' => L10n::t('Show last photos'),
37 'showlastlike' => L10n::t('Show last liked items'),
38 'showcommunitystream' => L10n::t('Show community stream')
42 function communityhome_addon_admin(App $a, &$o)
44 $tpl = get_markup_template('settings.tpl', 'addon/communityhome/');
46 $opts = communityhome_getopts();
48 '$submit' => L10n::t("Submit"),
52 foreach ($opts as $k => $v) {
53 $ctx['fields'][] = ['communityhome_' . $k, $v, Config::get('communityhome', $k)];
55 $o = replace_macros($tpl, $ctx);
58 function communityhome_addon_admin_post(App $a)
60 if (x($_POST, 'communityhome-submit')) {
61 $opts = communityhome_getopts();
62 foreach ($opts as $k => $v) {
63 Config::set('communityhome', $k, x($_POST, 'communityhome_' . $k));
68 function communityhome_home(App $a, &$o)
71 $a->page['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . $a->get_baseurl() . '/addon/communityhome/communityhome.css" media="all" />';
73 if (!Config::get('communityhome', 'hidelogin')) {
75 '$tab_1' => L10n::t('Login'),
76 '$tab_2' => L10n::t('OpenID'),
77 '$noOid' => Config::get('system', 'no_openid'),
81 $aside['$login_title'] = L10n::t('Login');
82 $aside['$login_form'] = Login::form($a->query_string, $a->config['register_policy'] == REGISTER_CLOSED ? false : true);
85 //'$tab_1' => L10n::t('Login'),
86 //'$tab_2' => L10n::t('OpenID'),
87 //'$noOid' => Config::get('system','no_openid'),
92 if (Config::get('communityhome', 'showlastusers')) {
93 $aside['$lastusers_title'] = L10n::t('Latest users');
94 $aside['$lastusers_items'] = [];
96 $publish = (Config::get('system', 'publish_all') ? '' : " AND `publish` = 1 " );
97 $order = " ORDER BY `register_date` DESC ";
99 $r = q("SELECT `profile`.*, `profile`.`uid` AS `profile_uid`, `user`.`nickname`
100 FROM `profile` LEFT JOIN `user` ON `user`.`uid` = `profile`.`uid`
101 WHERE `is-default` = 1 $publish AND `user`.`blocked` = 0 $sql_extra $order LIMIT %d, %d ",
105 # $tpl = file_get_contents( dirname(__file__).'/directory_item.tpl');
106 $tpl = get_markup_template('directory_item.tpl', 'addon/communityhome/');
109 foreach ($r as $rr) {
110 $profile_link = $a->get_baseurl() . '/profile/' . ((strlen($rr['nickname'])) ? $rr['nickname'] : $rr['profile_uid']);
111 $entry = replace_macros($tpl, [
113 '$profile_link' => $profile_link,
114 '$photo' => $rr[$photo],
115 '$alt_text' => $rr['name'],
117 $aside['$lastusers_items'][] = $entry;
121 // 12 most active users (by posts and contacts)
122 // this query don't work on some mysql versions
123 if (Config::get('communityhome', 'showactiveusers')) {
124 $r = q("SELECT `uni`.`contacts`,`uni`.`items`, `profile`.*, `profile`.`uid` AS `profile_uid`, `user`.`nickname` FROM
125 (SELECT COUNT(*) as `contacts`, `uid` FROM `contact` WHERE `self`=0 GROUP BY `uid`) AS `con`,
126 (SELECT COUNT(*) as `items`, `uid` FROM `item` WHERE `item`.`changed` > DATE(NOW() - INTERVAL 1 MONTH) AND `item`.`wall` = 1 GROUP BY `uid`) AS `ite`,
128 SELECT `contacts`,`items`,`ite`.`uid` FROM `con` RIGHT OUTER JOIN `ite` ON `con`.`uid`=`ite`.`uid`
130 SELECT `contacts`,`items`,`con`.`uid` FROM `con` LEFT OUTER JOIN `ite` ON `con`.`uid`=`ite`.`uid`
131 ) AS `uni`, `user`, `profile`
132 WHERE `uni`.`uid`=`user`.`uid`
133 AND `uni`.`uid`=`profile`.`uid` AND `profile`.`publish`=1
135 ORDER BY `items` DESC,`contacts` DESC
137 if ($r && count($r)) {
138 $aside['$activeusers_title'] = L10n::t('Most active users');
139 $aside['$activeusers_items'] = [];
142 foreach ($r as $rr) {
143 $profile_link = $a->get_baseurl() . '/profile/' . ((strlen($rr['nickname'])) ? $rr['nickname'] : $rr['profile_uid']);
144 $entry = replace_macros($tpl, [
146 '$profile_link' => $profile_link,
147 '$photo' => $rr[$photo],
148 '$photo_user' => sprintf("%s (%s posts, %s contacts)", $rr['name'], ($rr['items'] ? $rr['items'] : '0'),
149 ($rr['contacts'] ? $rr['contacts'] : '0'))
151 $aside['$activeusers_items'][] = $entry;
156 if (Config::get('communityhome', 'showlastphotos')) {
157 $aside['$photos_title'] = L10n::t('Latest photos');
158 $aside['$photos_items'] = [];
159 $r = q("SELECT `photo`.`id`, `photo`.`resource-id`, `photo`.`scale`, `photo`.`desc`, `user`.`nickname`, `user`.`username` FROM
160 (SELECT `resource-id`, MAX(`scale`) as maxscale FROM `photo`
161 WHERE `profile`=0 AND `contact-id`=0 AND `album` NOT IN ('Contact Photos', '%s', 'Profile Photos', '%s')
162 AND `allow_cid`='' AND `allow_gid`='' AND `deny_cid`='' AND `deny_gid`='' GROUP BY `resource-id`) AS `t1`
163 INNER JOIN `photo` ON `photo`.`resource-id`=`t1`.`resource-id` AND `photo`.`scale` = `t1`.`maxscale`,
165 WHERE `user`.`uid` = `photo`.`uid`
166 AND `user`.`blockwall`=0
167 AND `user`.`hidewall` = 0
168 ORDER BY `photo`.`edited` DESC
170 dbesc(L10n::t('Contact Photos')),
171 dbesc(L10n::t('Profile Photos'))
176 # $tpl = file_get_contents( dirname(__file__).'/directory_item.tpl');
177 $tpl = get_markup_template('directory_item.tpl', 'addon/communityhome/');
178 foreach ($r as $rr) {
179 $photo_page = $a->get_baseurl() . '/photos/' . $rr['nickname'] . '/image/' . $rr['resource-id'];
180 $photo_url = $a->get_baseurl() . '/photo/' . $rr['resource-id'] . '-' . $rr['scale'] . '.jpg';
182 $entry = replace_macros($tpl, [
184 '$profile_link' => $photo_page,
185 '$photo' => $photo_url,
186 '$photo_user' => $rr['username'],
187 '$photo_title' => $rr['desc']
190 $aside['$photos_items'][] = $entry;
195 // last 10 liked items
196 if (Config::get('communityhome', 'showlastlike')) {
197 $aside['$like_title'] = L10n::t('Latest likes');
198 $aside['$like_items'] = [];
199 $r = q("SELECT `T1`.`created`, `T1`.`liker`, `T1`.`liker-link`, `item`.* FROM
200 (SELECT `parent-uri`, `created`, `author-name` AS `liker`,`author-link` AS `liker-link`
201 FROM `item` WHERE `verb`='http://activitystrea.ms/schema/1.0/like' GROUP BY `parent-uri` ORDER BY `created` DESC) AS T1
202 INNER JOIN `item` ON `item`.`uri`=`T1`.`parent-uri`
203 WHERE `T1`.`liker-link` LIKE '%s%%' OR `item`.`author-link` LIKE '%s%%'
205 ORDER BY `T1`.`created` DESC
211 foreach ($r as $rr) {
212 $author = '<a href="' . $rr['liker-link'] . '">' . $rr['liker'] . '</a>';
213 $objauthor = '<a href="' . $rr['author-link'] . '">' . $rr['author-name'] . '</a>';
215 //var_dump($rr['verb'],$rr['object-type']); killme();
216 switch ($rr['verb']) {
217 case 'http://activitystrea.ms/schema/1.0/post':
218 switch ($rr['object-type']) {
219 case 'http://activitystrea.ms/schema/1.0/event':
220 $post_type = L10n::t('event');
223 $post_type = L10n::t('status');
227 if ($rr['resource-id']) {
228 $post_type = L10n::t('photo');
230 preg_match("/\[url=([^]]*)\]/", $rr['body'], $m);
231 $rr['plink'] = $m[1];
233 $post_type = L10n::t('status');
236 $plink = '<a href="' . $rr['plink'] . '">' . $post_type . '</a>';
238 $aside['$like_items'][] = L10n::t('%1$s likes %2$s\'s %3$s', $author, $objauthor, $plink);
242 # $tpl = file_get_contents(dirname(__file__).'/communityhome.tpl');
243 $tpl = get_markup_template('communityhome.tpl', 'addon/communityhome/');
244 $a->page['aside'] = replace_macros($tpl, $aside);
246 $o = '<h1>' . ((x($a->config, 'sitename')) ? L10n::t("Welcome to %s", $a->config['sitename']) : "" ) . '</h1>';
248 if (file_exists('home.html')) $o = file_get_contents('home.html');
250 if (Config::get('communityhome', 'showcommunitystream')) {
251 $oldset = Config::get('system', 'community_page_style');
252 if ($oldset == CP_NO_COMMUNITY_PAGE) Config::set('system', 'community_page_style', CP_USERS_ON_SERVER);
254 $o .= community_content($a, 1);
256 if ($oldset == CP_NO_COMMUNITY_PAGE) Config::set('system', 'community_page_style', $oldset);