4 * Description: Show last community activity in homepage
6 * Author: Fabio Comuni <http://kirgroup.com/profile/fabrixxm>
8 use Friendica\Core\Addon;
9 use Friendica\Core\Config;
10 use Friendica\Module\Login;
12 require_once('mod/community.php');
15 function communityhome_install() {
16 Addon::registerHook('home_content', 'addon/communityhome/communityhome.php', 'communityhome_home');
17 logger("installed communityhome");
20 function communityhome_uninstall() {
21 Addon::unregisterHook('home_content', 'addon/communityhome/communityhome.php', 'communityhome_home');
22 logger("removed communityhome");
25 function communityhome_getopts() {
27 'hidelogin'=>t('Hide login form'),
28 'showlastusers'=>t('Show last new users'),
29 'showactiveusers'=>t('Show last active users'),
30 'showlastphotos'=>t('Show last photos'),
31 'showlastlike'=>t('Show last liked items'),
32 'showcommunitystream'=>t('Show community stream')
36 function communityhome_addon_admin(&$a, &$o)
38 $tpl = get_markup_template('settings.tpl', 'addon/communityhome/');
40 $opts = communityhome_getopts();
42 '$submit' => t("Submit"),
46 foreach ($opts as $k => $v) {
47 $ctx['fields'][] = ['communityhome_'.$k, $v, Config::get('communityhome', $k)];
49 $o = replace_macros($tpl, $ctx);
52 function communityhome_addon_admin_post(&$a, &$b)
54 if (x($_POST, 'communityhome-submit')) {
55 $opts = communityhome_getopts();
56 foreach ($opts as $k => $v) {
57 Config::set('communityhome', $k, x($_POST, 'communityhome_'.$k));
63 function communityhome_home(&$a, &$o){
65 $a->page['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="'.$a->get_baseurl().'/addon/communityhome/communityhome.css" media="all" />';
67 if (!Config::get('communityhome','hidelogin')){
69 '$tab_1' => t('Login'),
70 '$tab_2' => t('OpenID'),
71 '$noOid' => Config::get('system','no_openid'),
75 $aside['$login_title'] = t('Login');
76 $aside['$login_form'] = Login::form($a->query_string, $a->config['register_policy'] == REGISTER_CLOSED ? false : true);
79 //'$tab_1' => t('Login'),
80 //'$tab_2' => t('OpenID'),
81 //'$noOid' => Config::get('system','no_openid'),
86 if (Config::get('communityhome','showlastusers')){
87 $aside['$lastusers_title'] = t('Latest users');
88 $aside['$lastusers_items'] = [];
90 $publish = (Config::get('system','publish_all') ? '' : " AND `publish` = 1 " );
91 $order = " ORDER BY `register_date` DESC ";
93 $r = q("SELECT `profile`.*, `profile`.`uid` AS `profile_uid`, `user`.`nickname`
94 FROM `profile` LEFT JOIN `user` ON `user`.`uid` = `profile`.`uid`
95 WHERE `is-default` = 1 $publish AND `user`.`blocked` = 0 $sql_extra $order LIMIT %d , %d ",
99 # $tpl = file_get_contents( dirname(__file__).'/directory_item.tpl');
100 $tpl = get_markup_template( 'directory_item.tpl', 'addon/communityhome/' );
104 $profile_link = $a->get_baseurl() . '/profile/' . ((strlen($rr['nickname'])) ? $rr['nickname'] : $rr['profile_uid']);
105 $entry = replace_macros($tpl,[
107 '$profile_link' => $profile_link,
108 '$photo' => $rr[$photo],
109 '$alt_text' => $rr['name'],
111 $aside['$lastusers_items'][] = $entry;
115 // 12 most active users (by posts and contacts)
116 // this query don't work on some mysql versions
117 if (Config::get('communityhome','showactiveusers')){
118 $r = q("SELECT `uni`.`contacts`,`uni`.`items`, `profile`.*, `profile`.`uid` AS `profile_uid`, `user`.`nickname` FROM
119 (SELECT COUNT(*) as `contacts`, `uid` FROM `contact` WHERE `self`=0 GROUP BY `uid`) AS `con`,
120 (SELECT COUNT(*) as `items`, `uid` FROM `item` WHERE `item`.`changed` > DATE(NOW() - INTERVAL 1 MONTH) AND `item`.`wall` = 1 GROUP BY `uid`) AS `ite`,
122 SELECT `contacts`,`items`,`ite`.`uid` FROM `con` RIGHT OUTER JOIN `ite` ON `con`.`uid`=`ite`.`uid`
124 SELECT `contacts`,`items`,`con`.`uid` FROM `con` LEFT OUTER JOIN `ite` ON `con`.`uid`=`ite`.`uid`
125 ) AS `uni`, `user`, `profile`
126 WHERE `uni`.`uid`=`user`.`uid`
127 AND `uni`.`uid`=`profile`.`uid` AND `profile`.`publish`=1
129 ORDER BY `items` DESC,`contacts` DESC
131 if($r && count($r)) {
132 $aside['$activeusers_title'] = t('Most active users');
133 $aside['$activeusers_items'] = [];
137 $profile_link = $a->get_baseurl() . '/profile/' . ((strlen($rr['nickname'])) ? $rr['nickname'] : $rr['profile_uid']);
138 $entry = replace_macros($tpl,[
140 '$profile_link' => $profile_link,
141 '$photo' => $rr[$photo],
142 '$photo_user' => sprintf("%s (%s posts, %s contacts)",$rr['name'], ($rr['items']?$rr['items']:'0'), ($rr['contacts']?$rr['contacts']:'0'))
144 $aside['$activeusers_items'][] = $entry;
149 if (Config::get('communityhome','showlastphotos')){
150 $aside['$photos_title'] = t('Latest photos');
151 $aside['$photos_items'] = [];
152 $r = q("SELECT `photo`.`id`, `photo`.`resource-id`, `photo`.`scale`, `photo`.`desc`, `user`.`nickname`, `user`.`username` FROM
153 (SELECT `resource-id`, MAX(`scale`) as maxscale FROM `photo`
154 WHERE `profile`=0 AND `contact-id`=0 AND `album` NOT IN ('Contact Photos', '%s', 'Profile Photos', '%s')
155 AND `allow_cid`='' AND `allow_gid`='' AND `deny_cid`='' AND `deny_gid`='' GROUP BY `resource-id`) AS `t1`
156 INNER JOIN `photo` ON `photo`.`resource-id`=`t1`.`resource-id` AND `photo`.`scale` = `t1`.`maxscale`,
158 WHERE `user`.`uid` = `photo`.`uid`
159 AND `user`.`blockwall`=0
160 AND `user`.`hidewall` = 0
161 ORDER BY `photo`.`edited` DESC
163 dbesc(t('Contact Photos')),
164 dbesc(t('Profile Photos'))
169 # $tpl = file_get_contents( dirname(__file__).'/directory_item.tpl');
170 $tpl = get_markup_template( 'directory_item.tpl', 'addon/communityhome/' );
172 $photo_page = $a->get_baseurl() . '/photos/' . $rr['nickname'] . '/image/' . $rr['resource-id'];
173 $photo_url = $a->get_baseurl() . '/photo/' . $rr['resource-id'] . '-' . $rr['scale'] .'.jpg';
175 $entry = replace_macros($tpl,[
177 '$profile_link' => $photo_page,
178 '$photo' => $photo_url,
179 '$photo_user' => $rr['username'],
180 '$photo_title' => $rr['desc']
183 $aside['$photos_items'][] = $entry;
188 // last 10 liked items
189 if (Config::get('communityhome','showlastlike')){
190 $aside['$like_title'] = t('Latest likes');
191 $aside['$like_items'] = [];
192 $r = q("SELECT `T1`.`created`, `T1`.`liker`, `T1`.`liker-link`, `item`.* FROM
193 (SELECT `parent-uri`, `created`, `author-name` AS `liker`,`author-link` AS `liker-link`
194 FROM `item` WHERE `verb`='http://activitystrea.ms/schema/1.0/like' GROUP BY `parent-uri` ORDER BY `created` DESC) AS T1
195 INNER JOIN `item` ON `item`.`uri`=`T1`.`parent-uri`
196 WHERE `T1`.`liker-link` LIKE '%s%%' OR `item`.`author-link` LIKE '%s%%'
198 ORDER BY `T1`.`created` DESC
200 $a->get_baseurl(),$a->get_baseurl()
203 foreach ($r as $rr) {
204 $author = '<a href="' . $rr['liker-link'] . '">' . $rr['liker'] . '</a>';
205 $objauthor = '<a href="' . $rr['author-link'] . '">' . $rr['author-name'] . '</a>';
207 //var_dump($rr['verb'],$rr['object-type']); killme();
209 case 'http://activitystrea.ms/schema/1.0/post':
210 switch ($rr['object-type']){
211 case 'http://activitystrea.ms/schema/1.0/event':
212 $post_type = t('event');
215 $post_type = t('status');
219 if ($rr['resource-id']){
220 $post_type = t('photo');
221 $m=[]; preg_match("/\[url=([^]]*)\]/", $rr['body'], $m);
222 $rr['plink'] = $m[1];
224 $post_type = t('status');
227 $plink = '<a href="' . $rr['plink'] . '">' . $post_type . '</a>';
229 $aside['$like_items'][] = sprintf( t('%1$s likes %2$s\'s %3$s'), $author, $objauthor, $plink);
234 # $tpl = file_get_contents(dirname(__file__).'/communityhome.tpl');
235 $tpl = get_markup_template('communityhome.tpl', 'addon/communityhome/');
236 $a->page['aside'] = replace_macros($tpl, $aside);
238 $o = '<h1>' . ((x($a->config,'sitename')) ? sprintf( t("Welcome to %s") ,$a->config['sitename']) : "" ) . '</h1>';
240 if(file_exists('home.html'))
241 $o = file_get_contents('home.html');
243 if (Config::get('communityhome','showcommunitystream')){
244 $oldset = Config::get('system','community_page_style');
245 if ($oldset == CP_NO_COMMUNITY_PAGE)
246 Config::set('system','community_page_style', CP_USERS_ON_SERVER);
248 $o .= community_content($a,1);
250 if ($oldset == CP_NO_COMMUNITY_PAGE)
251 Config::set('system','community_page_style', $oldset);