4 * Description: Show last community activity in homepage
6 * Author: Fabio Comuni <http://kirgroup.com/profile/fabrixxm>
9 use Friendica\Core\Config;
10 use Friendica\Module\Login;
12 require_once('mod/community.php');
15 function communityhome_install() {
16 register_hook('home_content', 'addon/communityhome/communityhome.php', 'communityhome_home');
17 logger("installed communityhome");
20 function communityhome_uninstall() {
21 unregister_hook('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_plugin_admin(&$a, &$o) {
37 $tpl = get_markup_template( 'settings.tpl', 'addon/communityhome/' );
39 $opts = communityhome_getopts();
41 '$submit' => t("Submit"),
44 foreach($opts as $k=>$v) {
45 $ctx['fields'][] = ['communityhome_'.$k, $v, Config::get('communityhome', $k)];
47 $o = replace_macros($tpl, $ctx);
50 function communityhome_plugin_admin_post(&$a,&$b) {
51 if(x($_POST,'communityhome-submit')) {
52 $opts = communityhome_getopts();
53 foreach($opts as $k=>$v) {
54 Config::set('communityhome', $k, x($_POST,'communityhome_'.$k));
60 function communityhome_home(&$a, &$o){
62 $a->page['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="'.$a->get_baseurl().'/addon/communityhome/communityhome.css" media="all" />';
64 if (!Config::get('communityhome','hidelogin')){
66 '$tab_1' => t('Login'),
67 '$tab_2' => t('OpenID'),
68 '$noOid' => Config::get('system','no_openid'),
72 $aside['$login_title'] = t('Login');
73 $aside['$login_form'] = Login::form($a->query_string, $a->config['register_policy'] == REGISTER_CLOSED ? false : true);
76 //'$tab_1' => t('Login'),
77 //'$tab_2' => t('OpenID'),
78 //'$noOid' => Config::get('system','no_openid'),
83 if (Config::get('communityhome','showlastusers')){
84 $aside['$lastusers_title'] = t('Latest users');
85 $aside['$lastusers_items'] = [];
87 $publish = (Config::get('system','publish_all') ? '' : " AND `publish` = 1 " );
88 $order = " ORDER BY `register_date` DESC ";
90 $r = q("SELECT `profile`.*, `profile`.`uid` AS `profile_uid`, `user`.`nickname`
91 FROM `profile` LEFT JOIN `user` ON `user`.`uid` = `profile`.`uid`
92 WHERE `is-default` = 1 $publish AND `user`.`blocked` = 0 $sql_extra $order LIMIT %d , %d ",
96 # $tpl = file_get_contents( dirname(__file__).'/directory_item.tpl');
97 $tpl = get_markup_template( 'directory_item.tpl', 'addon/communityhome/' );
101 $profile_link = $a->get_baseurl() . '/profile/' . ((strlen($rr['nickname'])) ? $rr['nickname'] : $rr['profile_uid']);
102 $entry = replace_macros($tpl,[
104 '$profile_link' => $profile_link,
105 '$photo' => $rr[$photo],
106 '$alt_text' => $rr['name'],
108 $aside['$lastusers_items'][] = $entry;
112 // 12 most active users (by posts and contacts)
113 // this query don't work on some mysql versions
114 if (Config::get('communityhome','showactiveusers')){
115 $r = q("SELECT `uni`.`contacts`,`uni`.`items`, `profile`.*, `profile`.`uid` AS `profile_uid`, `user`.`nickname` FROM
116 (SELECT COUNT(*) as `contacts`, `uid` FROM `contact` WHERE `self`=0 GROUP BY `uid`) AS `con`,
117 (SELECT COUNT(*) as `items`, `uid` FROM `item` WHERE `item`.`changed` > DATE(NOW() - INTERVAL 1 MONTH) AND `item`.`wall` = 1 GROUP BY `uid`) AS `ite`,
119 SELECT `contacts`,`items`,`ite`.`uid` FROM `con` RIGHT OUTER JOIN `ite` ON `con`.`uid`=`ite`.`uid`
121 SELECT `contacts`,`items`,`con`.`uid` FROM `con` LEFT OUTER JOIN `ite` ON `con`.`uid`=`ite`.`uid`
122 ) AS `uni`, `user`, `profile`
123 WHERE `uni`.`uid`=`user`.`uid`
124 AND `uni`.`uid`=`profile`.`uid` AND `profile`.`publish`=1
126 ORDER BY `items` DESC,`contacts` DESC
128 if($r && count($r)) {
129 $aside['$activeusers_title'] = t('Most active users');
130 $aside['$activeusers_items'] = [];
134 $profile_link = $a->get_baseurl() . '/profile/' . ((strlen($rr['nickname'])) ? $rr['nickname'] : $rr['profile_uid']);
135 $entry = replace_macros($tpl,[
137 '$profile_link' => $profile_link,
138 '$photo' => $rr[$photo],
139 '$photo_user' => sprintf("%s (%s posts, %s contacts)",$rr['name'], ($rr['items']?$rr['items']:'0'), ($rr['contacts']?$rr['contacts']:'0'))
141 $aside['$activeusers_items'][] = $entry;
146 if (Config::get('communityhome','showlastphotos')){
147 $aside['$photos_title'] = t('Latest photos');
148 $aside['$photos_items'] = [];
149 $r = q("SELECT `photo`.`id`, `photo`.`resource-id`, `photo`.`scale`, `photo`.`desc`, `user`.`nickname`, `user`.`username` FROM
150 (SELECT `resource-id`, MAX(`scale`) as maxscale FROM `photo`
151 WHERE `profile`=0 AND `contact-id`=0 AND `album` NOT IN ('Contact Photos', '%s', 'Profile Photos', '%s')
152 AND `allow_cid`='' AND `allow_gid`='' AND `deny_cid`='' AND `deny_gid`='' GROUP BY `resource-id`) AS `t1`
153 INNER JOIN `photo` ON `photo`.`resource-id`=`t1`.`resource-id` AND `photo`.`scale` = `t1`.`maxscale`,
155 WHERE `user`.`uid` = `photo`.`uid`
156 AND `user`.`blockwall`=0
157 AND `user`.`hidewall` = 0
158 ORDER BY `photo`.`edited` DESC
160 dbesc(t('Contact Photos')),
161 dbesc(t('Profile Photos'))
166 # $tpl = file_get_contents( dirname(__file__).'/directory_item.tpl');
167 $tpl = get_markup_template( 'directory_item.tpl', 'addon/communityhome/' );
169 $photo_page = $a->get_baseurl() . '/photos/' . $rr['nickname'] . '/image/' . $rr['resource-id'];
170 $photo_url = $a->get_baseurl() . '/photo/' . $rr['resource-id'] . '-' . $rr['scale'] .'.jpg';
172 $entry = replace_macros($tpl,[
174 '$profile_link' => $photo_page,
175 '$photo' => $photo_url,
176 '$photo_user' => $rr['username'],
177 '$photo_title' => $rr['desc']
180 $aside['$photos_items'][] = $entry;
185 // last 10 liked items
186 if (Config::get('communityhome','showlastlike')){
187 $aside['$like_title'] = t('Latest likes');
188 $aside['$like_items'] = [];
189 $r = q("SELECT `T1`.`created`, `T1`.`liker`, `T1`.`liker-link`, `item`.* FROM
190 (SELECT `parent-uri`, `created`, `author-name` AS `liker`,`author-link` AS `liker-link`
191 FROM `item` WHERE `verb`='http://activitystrea.ms/schema/1.0/like' GROUP BY `parent-uri` ORDER BY `created` DESC) AS T1
192 INNER JOIN `item` ON `item`.`uri`=`T1`.`parent-uri`
193 WHERE `T1`.`liker-link` LIKE '%s%%' OR `item`.`author-link` LIKE '%s%%'
195 ORDER BY `T1`.`created` DESC
197 $a->get_baseurl(),$a->get_baseurl()
200 foreach ($r as $rr) {
201 $author = '<a href="' . $rr['liker-link'] . '">' . $rr['liker'] . '</a>';
202 $objauthor = '<a href="' . $rr['author-link'] . '">' . $rr['author-name'] . '</a>';
204 //var_dump($rr['verb'],$rr['object-type']); killme();
206 case 'http://activitystrea.ms/schema/1.0/post':
207 switch ($rr['object-type']){
208 case 'http://activitystrea.ms/schema/1.0/event':
209 $post_type = t('event');
212 $post_type = t('status');
216 if ($rr['resource-id']){
217 $post_type = t('photo');
218 $m=[]; preg_match("/\[url=([^]]*)\]/", $rr['body'], $m);
219 $rr['plink'] = $m[1];
221 $post_type = t('status');
224 $plink = '<a href="' . $rr['plink'] . '">' . $post_type . '</a>';
226 $aside['$like_items'][] = sprintf( t('%1$s likes %2$s\'s %3$s'), $author, $objauthor, $plink);
231 # $tpl = file_get_contents(dirname(__file__).'/communityhome.tpl');
232 $tpl = get_markup_template('communityhome.tpl', 'addon/communityhome/');
233 $a->page['aside'] = replace_macros($tpl, $aside);
235 $o = '<h1>' . ((x($a->config,'sitename')) ? sprintf( t("Welcome to %s") ,$a->config['sitename']) : "" ) . '</h1>';
237 if(file_exists('home.html'))
238 $o = file_get_contents('home.html');
240 if (Config::get('communityhome','showcommunitystream')){
241 $oldset = Config::get('system','community_page_style');
242 if ($oldset == CP_NO_COMMUNITY_PAGE)
243 Config::set('system','community_page_style', CP_USERS_ON_SERVER);
245 $o .= community_content($a,1);
247 if ($oldset == CP_NO_COMMUNITY_PAGE)
248 Config::set('system','community_page_style', $oldset);