4 * Description: Show last community activity in homepage
6 * Author: Fabio Comuni <http://kirgroup.com/profile/fabrixxm>
9 use Friendica\Core\Config;
11 require_once('mod/community.php');
14 function communityhome_install() {
15 register_hook('home_content', 'addon/communityhome/communityhome.php', 'communityhome_home');
16 logger("installed communityhome");
19 function communityhome_uninstall() {
20 unregister_hook('home_content', 'addon/communityhome/communityhome.php', 'communityhome_home');
21 logger("removed communityhome");
24 function communityhome_getopts() {
26 'hidelogin'=>t('Hide login form'),
27 'showlastusers'=>t('Show last new users'),
28 'showactiveusers'=>t('Show last active users'),
29 'showlastphotos'=>t('Show last photos'),
30 'showlastlike'=>t('Show last liked items'),
31 'showcommunitystream'=>t('Show community stream')
35 function communityhome_plugin_admin(&$a, &$o) {
36 $tpl = get_markup_template( 'settings.tpl', 'addon/communityhome/' );
38 $opts = communityhome_getopts();
40 '$submit' => t("Submit"),
43 foreach($opts as $k=>$v) {
44 $ctx['fields'][] = ['communityhome_'.$k, $v, Config::get('communityhome', $k)];
46 $o = replace_macros($tpl, $ctx);
49 function communityhome_plugin_admin_post(&$a,&$b) {
50 if(x($_POST,'communityhome-submit')) {
51 $opts = communityhome_getopts();
52 foreach($opts as $k=>$v) {
53 Config::set('communityhome', $k, x($_POST,'communityhome_'.$k));
59 function communityhome_home(&$a, &$o){
61 $a->page['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="'.$a->get_baseurl().'/addon/communityhome/communityhome.css" media="all" />';
63 if (!Config::get('communityhome','hidelogin')){
65 '$tab_1' => t('Login'),
66 '$tab_2' => t('OpenID'),
67 '$noOid' => Config::get('system','no_openid'),
71 $aside['$login_title'] = t('Login');
72 $aside['$login_form'] = login(($a->config['register_policy'] == REGISTER_CLOSED) ? false : true);
75 //'$tab_1' => t('Login'),
76 //'$tab_2' => t('OpenID'),
77 //'$noOid' => Config::get('system','no_openid'),
82 if (Config::get('communityhome','showlastusers')){
83 $aside['$lastusers_title'] = t('Latest users');
84 $aside['$lastusers_items'] = array();
86 $publish = (Config::get('system','publish_all') ? '' : " AND `publish` = 1 " );
87 $order = " ORDER BY `register_date` DESC ";
89 $r = q("SELECT `profile`.*, `profile`.`uid` AS `profile_uid`, `user`.`nickname`
90 FROM `profile` LEFT JOIN `user` ON `user`.`uid` = `profile`.`uid`
91 WHERE `is-default` = 1 $publish AND `user`.`blocked` = 0 $sql_extra $order LIMIT %d , %d ",
95 # $tpl = file_get_contents( dirname(__file__).'/directory_item.tpl');
96 $tpl = get_markup_template( 'directory_item.tpl', 'addon/communityhome/' );
100 $profile_link = $a->get_baseurl() . '/profile/' . ((strlen($rr['nickname'])) ? $rr['nickname'] : $rr['profile_uid']);
101 $entry = replace_macros($tpl,array(
103 '$profile_link' => $profile_link,
104 '$photo' => $a->get_cached_avatar_image($rr[$photo]),
105 '$alt_text' => $rr['name'],
107 $aside['$lastusers_items'][] = $entry;
111 // 12 most active users (by posts and contacts)
112 // this query don't work on some mysql versions
113 if (Config::get('communityhome','showactiveusers')){
114 $r = q("SELECT `uni`.`contacts`,`uni`.`items`, `profile`.*, `profile`.`uid` AS `profile_uid`, `user`.`nickname` FROM
115 (SELECT COUNT(*) as `contacts`, `uid` FROM `contact` WHERE `self`=0 GROUP BY `uid`) AS `con`,
116 (SELECT COUNT(*) as `items`, `uid` FROM `item` WHERE `item`.`changed` > DATE(NOW() - INTERVAL 1 MONTH) AND `item`.`wall` = 1 GROUP BY `uid`) AS `ite`,
118 SELECT `contacts`,`items`,`ite`.`uid` FROM `con` RIGHT OUTER JOIN `ite` ON `con`.`uid`=`ite`.`uid`
120 SELECT `contacts`,`items`,`con`.`uid` FROM `con` LEFT OUTER JOIN `ite` ON `con`.`uid`=`ite`.`uid`
121 ) AS `uni`, `user`, `profile`
122 WHERE `uni`.`uid`=`user`.`uid`
123 AND `uni`.`uid`=`profile`.`uid` AND `profile`.`publish`=1
125 ORDER BY `items` DESC,`contacts` DESC
127 if($r && count($r)) {
128 $aside['$activeusers_title'] = t('Most active users');
129 $aside['$activeusers_items'] = array();
133 $profile_link = $a->get_baseurl() . '/profile/' . ((strlen($rr['nickname'])) ? $rr['nickname'] : $rr['profile_uid']);
134 $entry = replace_macros($tpl,array(
136 '$profile_link' => $profile_link,
137 '$photo' => $rr[$photo],
138 '$photo_user' => sprintf("%s (%s posts, %s contacts)",$rr['name'], ($rr['items']?$rr['items']:'0'), ($rr['contacts']?$rr['contacts']:'0'))
140 $aside['$activeusers_items'][] = $entry;
145 if (Config::get('communityhome','showlastphotos')){
146 $aside['$photos_title'] = t('Latest photos');
147 $aside['$photos_items'] = array();
148 $r = q("SELECT `photo`.`id`, `photo`.`resource-id`, `photo`.`scale`, `photo`.`desc`, `user`.`nickname`, `user`.`username` FROM
149 (SELECT `resource-id`, MAX(`scale`) as maxscale FROM `photo`
150 WHERE `profile`=0 AND `contact-id`=0 AND `album` NOT IN ('Contact Photos', '%s', 'Profile Photos', '%s')
151 AND `allow_cid`='' AND `allow_gid`='' AND `deny_cid`='' AND `deny_gid`='' GROUP BY `resource-id`) AS `t1`
152 INNER JOIN `photo` ON `photo`.`resource-id`=`t1`.`resource-id` AND `photo`.`scale` = `t1`.`maxscale`,
154 WHERE `user`.`uid` = `photo`.`uid`
155 AND `user`.`blockwall`=0
156 AND `user`.`hidewall` = 0
157 ORDER BY `photo`.`edited` DESC
159 dbesc(t('Contact Photos')),
160 dbesc(t('Profile Photos'))
165 # $tpl = file_get_contents( dirname(__file__).'/directory_item.tpl');
166 $tpl = get_markup_template( 'directory_item.tpl', 'addon/communityhome/' );
168 $photo_page = $a->get_baseurl() . '/photos/' . $rr['nickname'] . '/image/' . $rr['resource-id'];
169 $photo_url = $a->get_baseurl() . '/photo/' . $rr['resource-id'] . '-' . $rr['scale'] .'.jpg';
171 $entry = replace_macros($tpl,array(
173 '$profile_link' => $photo_page,
174 '$photo' => $photo_url,
175 '$photo_user' => $rr['username'],
176 '$photo_title' => $rr['desc']
179 $aside['$photos_items'][] = $entry;
184 // last 10 liked items
185 if (Config::get('communityhome','showlastlike')){
186 $aside['$like_title'] = t('Latest likes');
187 $aside['$like_items'] = array();
188 $r = q("SELECT `T1`.`created`, `T1`.`liker`, `T1`.`liker-link`, `item`.* FROM
189 (SELECT `parent-uri`, `created`, `author-name` AS `liker`,`author-link` AS `liker-link`
190 FROM `item` WHERE `verb`='http://activitystrea.ms/schema/1.0/like' GROUP BY `parent-uri` ORDER BY `created` DESC) AS T1
191 INNER JOIN `item` ON `item`.`uri`=`T1`.`parent-uri`
192 WHERE `T1`.`liker-link` LIKE '%s%%' OR `item`.`author-link` LIKE '%s%%'
194 ORDER BY `T1`.`created` DESC
196 $a->get_baseurl(),$a->get_baseurl()
199 foreach ($r as $rr) {
200 $author = '<a href="' . $rr['liker-link'] . '">' . $rr['liker'] . '</a>';
201 $objauthor = '<a href="' . $rr['author-link'] . '">' . $rr['author-name'] . '</a>';
203 //var_dump($rr['verb'],$rr['object-type']); killme();
205 case 'http://activitystrea.ms/schema/1.0/post':
206 switch ($rr['object-type']){
207 case 'http://activitystrea.ms/schema/1.0/event':
208 $post_type = t('event');
211 $post_type = t('status');
215 if ($rr['resource-id']){
216 $post_type = t('photo');
217 $m=array(); preg_match("/\[url=([^]]*)\]/", $rr['body'], $m);
218 $rr['plink'] = $m[1];
220 $post_type = t('status');
223 $plink = '<a href="' . $rr['plink'] . '">' . $post_type . '</a>';
225 $aside['$like_items'][] = sprintf( t('%1$s likes %2$s\'s %3$s'), $author, $objauthor, $plink);
230 # $tpl = file_get_contents(dirname(__file__).'/communityhome.tpl');
231 $tpl = get_markup_template('communityhome.tpl', 'addon/communityhome/');
232 $a->page['aside'] = replace_macros($tpl, $aside);
234 $o = '<h1>' . ((x($a->config,'sitename')) ? sprintf( t("Welcome to %s") ,$a->config['sitename']) : "" ) . '</h1>';
236 if(file_exists('home.html'))
237 $o = file_get_contents('home.html');
239 if (Config::get('communityhome','showcommunitystream')){
240 $oldset = Config::get('system','community_page_style');
241 if ($oldset == CP_NO_COMMUNITY_PAGE)
242 Config::set('system','community_page_style', CP_USERS_ON_SERVER);
244 $o .= community_content($a,1);
246 if ($oldset == CP_NO_COMMUNITY_PAGE)
247 Config::set('system','community_page_style', $oldset);