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\Core\L10n;
11 use Friendica\Module\Login;
13 require_once('mod/community.php');
16 function communityhome_install() {
17 Addon::registerHook('home_content', 'addon/communityhome/communityhome.php', 'communityhome_home');
18 logger("installed communityhome");
21 function communityhome_uninstall() {
22 Addon::unregisterHook('home_content', 'addon/communityhome/communityhome.php', 'communityhome_home');
23 logger("removed communityhome");
26 function communityhome_getopts() {
28 'hidelogin'=>L10n::t('Hide login form'),
29 'showlastusers'=>L10n::t('Show last new users'),
30 'showactiveusers'=>L10n::t('Show last active users'),
31 'showlastphotos'=>L10n::t('Show last photos'),
32 'showlastlike'=>L10n::t('Show last liked items'),
33 'showcommunitystream'=>L10n::t('Show community stream')
37 function communityhome_addon_admin(&$a, &$o)
39 $tpl = get_markup_template('settings.tpl', 'addon/communityhome/');
41 $opts = communityhome_getopts();
43 '$submit' => L10n::t("Submit"),
47 foreach ($opts as $k => $v) {
48 $ctx['fields'][] = ['communityhome_'.$k, $v, Config::get('communityhome', $k)];
50 $o = replace_macros($tpl, $ctx);
53 function communityhome_addon_admin_post(&$a, &$b)
55 if (x($_POST, 'communityhome-submit')) {
56 $opts = communityhome_getopts();
57 foreach ($opts as $k => $v) {
58 Config::set('communityhome', $k, x($_POST, 'communityhome_'.$k));
64 function communityhome_home(&$a, &$o){
66 $a->page['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="'.$a->get_baseurl().'/addon/communityhome/communityhome.css" media="all" />';
68 if (!Config::get('communityhome','hidelogin')){
70 '$tab_1' => L10n::t('Login'),
71 '$tab_2' => L10n::t('OpenID'),
72 '$noOid' => Config::get('system','no_openid'),
76 $aside['$login_title'] = L10n::t('Login');
77 $aside['$login_form'] = Login::form($a->query_string, $a->config['register_policy'] == REGISTER_CLOSED ? false : true);
80 //'$tab_1' => L10n::t('Login'),
81 //'$tab_2' => L10n::t('OpenID'),
82 //'$noOid' => Config::get('system','no_openid'),
87 if (Config::get('communityhome','showlastusers')){
88 $aside['$lastusers_title'] = L10n::t('Latest users');
89 $aside['$lastusers_items'] = [];
91 $publish = (Config::get('system','publish_all') ? '' : " AND `publish` = 1 " );
92 $order = " ORDER BY `register_date` DESC ";
94 $r = q("SELECT `profile`.*, `profile`.`uid` AS `profile_uid`, `user`.`nickname`
95 FROM `profile` LEFT JOIN `user` ON `user`.`uid` = `profile`.`uid`
96 WHERE `is-default` = 1 $publish AND `user`.`blocked` = 0 $sql_extra $order LIMIT %d , %d ",
100 # $tpl = file_get_contents( dirname(__file__).'/directory_item.tpl');
101 $tpl = get_markup_template( 'directory_item.tpl', 'addon/communityhome/' );
105 $profile_link = $a->get_baseurl() . '/profile/' . ((strlen($rr['nickname'])) ? $rr['nickname'] : $rr['profile_uid']);
106 $entry = replace_macros($tpl,[
108 '$profile_link' => $profile_link,
109 '$photo' => $rr[$photo],
110 '$alt_text' => $rr['name'],
112 $aside['$lastusers_items'][] = $entry;
116 // 12 most active users (by posts and contacts)
117 // this query don't work on some mysql versions
118 if (Config::get('communityhome','showactiveusers')){
119 $r = q("SELECT `uni`.`contacts`,`uni`.`items`, `profile`.*, `profile`.`uid` AS `profile_uid`, `user`.`nickname` FROM
120 (SELECT COUNT(*) as `contacts`, `uid` FROM `contact` WHERE `self`=0 GROUP BY `uid`) AS `con`,
121 (SELECT COUNT(*) as `items`, `uid` FROM `item` WHERE `item`.`changed` > DATE(NOW() - INTERVAL 1 MONTH) AND `item`.`wall` = 1 GROUP BY `uid`) AS `ite`,
123 SELECT `contacts`,`items`,`ite`.`uid` FROM `con` RIGHT OUTER JOIN `ite` ON `con`.`uid`=`ite`.`uid`
125 SELECT `contacts`,`items`,`con`.`uid` FROM `con` LEFT OUTER JOIN `ite` ON `con`.`uid`=`ite`.`uid`
126 ) AS `uni`, `user`, `profile`
127 WHERE `uni`.`uid`=`user`.`uid`
128 AND `uni`.`uid`=`profile`.`uid` AND `profile`.`publish`=1
130 ORDER BY `items` DESC,`contacts` DESC
132 if($r && count($r)) {
133 $aside['$activeusers_title'] = L10n::t('Most active users');
134 $aside['$activeusers_items'] = [];
138 $profile_link = $a->get_baseurl() . '/profile/' . ((strlen($rr['nickname'])) ? $rr['nickname'] : $rr['profile_uid']);
139 $entry = replace_macros($tpl,[
141 '$profile_link' => $profile_link,
142 '$photo' => $rr[$photo],
143 '$photo_user' => sprintf("%s (%s posts, %s contacts)",$rr['name'], ($rr['items']?$rr['items']:'0'), ($rr['contacts']?$rr['contacts']:'0'))
145 $aside['$activeusers_items'][] = $entry;
150 if (Config::get('communityhome','showlastphotos')){
151 $aside['$photos_title'] = L10n::t('Latest photos');
152 $aside['$photos_items'] = [];
153 $r = q("SELECT `photo`.`id`, `photo`.`resource-id`, `photo`.`scale`, `photo`.`desc`, `user`.`nickname`, `user`.`username` FROM
154 (SELECT `resource-id`, MAX(`scale`) as maxscale FROM `photo`
155 WHERE `profile`=0 AND `contact-id`=0 AND `album` NOT IN ('Contact Photos', '%s', 'Profile Photos', '%s')
156 AND `allow_cid`='' AND `allow_gid`='' AND `deny_cid`='' AND `deny_gid`='' GROUP BY `resource-id`) AS `t1`
157 INNER JOIN `photo` ON `photo`.`resource-id`=`t1`.`resource-id` AND `photo`.`scale` = `t1`.`maxscale`,
159 WHERE `user`.`uid` = `photo`.`uid`
160 AND `user`.`blockwall`=0
161 AND `user`.`hidewall` = 0
162 ORDER BY `photo`.`edited` DESC
164 dbesc(L10n::t('Contact Photos')),
165 dbesc(L10n::t('Profile Photos'))
170 # $tpl = file_get_contents( dirname(__file__).'/directory_item.tpl');
171 $tpl = get_markup_template( 'directory_item.tpl', 'addon/communityhome/' );
173 $photo_page = $a->get_baseurl() . '/photos/' . $rr['nickname'] . '/image/' . $rr['resource-id'];
174 $photo_url = $a->get_baseurl() . '/photo/' . $rr['resource-id'] . '-' . $rr['scale'] .'.jpg';
176 $entry = replace_macros($tpl,[
178 '$profile_link' => $photo_page,
179 '$photo' => $photo_url,
180 '$photo_user' => $rr['username'],
181 '$photo_title' => $rr['desc']
184 $aside['$photos_items'][] = $entry;
189 // last 10 liked items
190 if (Config::get('communityhome','showlastlike')){
191 $aside['$like_title'] = L10n::t('Latest likes');
192 $aside['$like_items'] = [];
193 $r = q("SELECT `T1`.`created`, `T1`.`liker`, `T1`.`liker-link`, `item`.* FROM
194 (SELECT `parent-uri`, `created`, `author-name` AS `liker`,`author-link` AS `liker-link`
195 FROM `item` WHERE `verb`='http://activitystrea.ms/schema/1.0/like' GROUP BY `parent-uri` ORDER BY `created` DESC) AS T1
196 INNER JOIN `item` ON `item`.`uri`=`T1`.`parent-uri`
197 WHERE `T1`.`liker-link` LIKE '%s%%' OR `item`.`author-link` LIKE '%s%%'
199 ORDER BY `T1`.`created` DESC
201 $a->get_baseurl(),$a->get_baseurl()
204 foreach ($r as $rr) {
205 $author = '<a href="' . $rr['liker-link'] . '">' . $rr['liker'] . '</a>';
206 $objauthor = '<a href="' . $rr['author-link'] . '">' . $rr['author-name'] . '</a>';
208 //var_dump($rr['verb'],$rr['object-type']); killme();
210 case 'http://activitystrea.ms/schema/1.0/post':
211 switch ($rr['object-type']){
212 case 'http://activitystrea.ms/schema/1.0/event':
213 $post_type = L10n::t('event');
216 $post_type = L10n::t('status');
220 if ($rr['resource-id']){
221 $post_type = L10n::t('photo');
222 $m=[]; preg_match("/\[url=([^]]*)\]/", $rr['body'], $m);
223 $rr['plink'] = $m[1];
225 $post_type = L10n::t('status');
228 $plink = '<a href="' . $rr['plink'] . '">' . $post_type . '</a>';
230 $aside['$like_items'][] = L10n::t('%1$s likes %2$s\'s %3$s', $author, $objauthor, $plink);
235 # $tpl = file_get_contents(dirname(__file__).'/communityhome.tpl');
236 $tpl = get_markup_template('communityhome.tpl', 'addon/communityhome/');
237 $a->page['aside'] = replace_macros($tpl, $aside);
239 $o = '<h1>' . ((x($a->config,'sitename')) ? L10n::t("Welcome to %s", $a->config['sitename']) : "" ) . '</h1>';
241 if(file_exists('home.html'))
242 $o = file_get_contents('home.html');
244 if (Config::get('communityhome','showcommunitystream')){
245 $oldset = Config::get('system','community_page_style');
246 if ($oldset == CP_NO_COMMUNITY_PAGE)
247 Config::set('system','community_page_style', CP_USERS_ON_SERVER);
249 $o .= community_content($a,1);
251 if ($oldset == CP_NO_COMMUNITY_PAGE)
252 Config::set('system','community_page_style', $oldset);