5 * Description: Show last community activity in homepage
7 * Author: Fabio Comuni <http://kirgroup.com/profile/fabrixxm>
9 use Friendica\Core\Addon;
10 use Friendica\Core\Config;
11 use Friendica\Core\L10n;
12 use Friendica\Module\Login;
14 require_once 'mod/community.php';
16 function communityhome_install()
18 Addon::registerHook('home_content', 'addon/communityhome/communityhome.php', 'communityhome_home');
19 logger("installed communityhome");
22 function communityhome_uninstall()
24 Addon::unregisterHook('home_content', 'addon/communityhome/communityhome.php', 'communityhome_home');
25 logger("removed communityhome");
28 function communityhome_getopts()
31 'hidelogin' => L10n::t('Hide login form'),
32 'showlastusers' => L10n::t('Show last new users'),
33 'showactiveusers' => L10n::t('Show last active users'),
34 'showlastphotos' => L10n::t('Show last photos'),
35 'showlastlike' => L10n::t('Show last liked items'),
36 'showcommunitystream' => L10n::t('Show community stream')
40 function communityhome_addon_admin(&$a, &$o)
42 $tpl = get_markup_template('settings.tpl', 'addon/communityhome/');
44 $opts = communityhome_getopts();
46 '$submit' => L10n::t("Submit"),
50 foreach ($opts as $k => $v) {
51 $ctx['fields'][] = ['communityhome_' . $k, $v, Config::get('communityhome', $k)];
53 $o = replace_macros($tpl, $ctx);
56 function communityhome_addon_admin_post(&$a, &$b)
58 if (x($_POST, 'communityhome-submit')) {
59 $opts = communityhome_getopts();
60 foreach ($opts as $k => $v) {
61 Config::set('communityhome', $k, x($_POST, 'communityhome_' . $k));
66 function communityhome_home(&$a, &$o)
69 $a->page['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . $a->get_baseurl() . '/addon/communityhome/communityhome.css" media="all" />';
71 if (!Config::get('communityhome', 'hidelogin')) {
73 '$tab_1' => L10n::t('Login'),
74 '$tab_2' => L10n::t('OpenID'),
75 '$noOid' => Config::get('system', 'no_openid'),
79 $aside['$login_title'] = L10n::t('Login');
80 $aside['$login_form'] = Login::form($a->query_string, $a->config['register_policy'] == REGISTER_CLOSED ? false : true);
83 //'$tab_1' => L10n::t('Login'),
84 //'$tab_2' => L10n::t('OpenID'),
85 //'$noOid' => Config::get('system','no_openid'),
90 if (Config::get('communityhome', 'showlastusers')) {
91 $aside['$lastusers_title'] = L10n::t('Latest users');
92 $aside['$lastusers_items'] = [];
94 $publish = (Config::get('system', 'publish_all') ? '' : " AND `publish` = 1 " );
95 $order = " ORDER BY `register_date` DESC ";
97 $r = q("SELECT `profile`.*, `profile`.`uid` AS `profile_uid`, `user`.`nickname`
98 FROM `profile` LEFT JOIN `user` ON `user`.`uid` = `profile`.`uid`
99 WHERE `is-default` = 1 $publish AND `user`.`blocked` = 0 $sql_extra $order LIMIT %d, %d ",
103 # $tpl = file_get_contents( dirname(__file__).'/directory_item.tpl');
104 $tpl = get_markup_template('directory_item.tpl', 'addon/communityhome/');
107 foreach ($r as $rr) {
108 $profile_link = $a->get_baseurl() . '/profile/' . ((strlen($rr['nickname'])) ? $rr['nickname'] : $rr['profile_uid']);
109 $entry = replace_macros($tpl, [
111 '$profile_link' => $profile_link,
112 '$photo' => $rr[$photo],
113 '$alt_text' => $rr['name'],
115 $aside['$lastusers_items'][] = $entry;
119 // 12 most active users (by posts and contacts)
120 // this query don't work on some mysql versions
121 if (Config::get('communityhome', 'showactiveusers')) {
122 $r = q("SELECT `uni`.`contacts`,`uni`.`items`, `profile`.*, `profile`.`uid` AS `profile_uid`, `user`.`nickname` FROM
123 (SELECT COUNT(*) as `contacts`, `uid` FROM `contact` WHERE `self`=0 GROUP BY `uid`) AS `con`,
124 (SELECT COUNT(*) as `items`, `uid` FROM `item` WHERE `item`.`changed` > DATE(NOW() - INTERVAL 1 MONTH) AND `item`.`wall` = 1 GROUP BY `uid`) AS `ite`,
126 SELECT `contacts`,`items`,`ite`.`uid` FROM `con` RIGHT OUTER JOIN `ite` ON `con`.`uid`=`ite`.`uid`
128 SELECT `contacts`,`items`,`con`.`uid` FROM `con` LEFT OUTER JOIN `ite` ON `con`.`uid`=`ite`.`uid`
129 ) AS `uni`, `user`, `profile`
130 WHERE `uni`.`uid`=`user`.`uid`
131 AND `uni`.`uid`=`profile`.`uid` AND `profile`.`publish`=1
133 ORDER BY `items` DESC,`contacts` DESC
135 if ($r && count($r)) {
136 $aside['$activeusers_title'] = L10n::t('Most active users');
137 $aside['$activeusers_items'] = [];
140 foreach ($r as $rr) {
141 $profile_link = $a->get_baseurl() . '/profile/' . ((strlen($rr['nickname'])) ? $rr['nickname'] : $rr['profile_uid']);
142 $entry = replace_macros($tpl, [
144 '$profile_link' => $profile_link,
145 '$photo' => $rr[$photo],
146 '$photo_user' => sprintf("%s (%s posts, %s contacts)", $rr['name'], ($rr['items'] ? $rr['items'] : '0'),
147 ($rr['contacts'] ? $rr['contacts'] : '0'))
149 $aside['$activeusers_items'][] = $entry;
154 if (Config::get('communityhome', 'showlastphotos')) {
155 $aside['$photos_title'] = L10n::t('Latest photos');
156 $aside['$photos_items'] = [];
157 $r = q("SELECT `photo`.`id`, `photo`.`resource-id`, `photo`.`scale`, `photo`.`desc`, `user`.`nickname`, `user`.`username` FROM
158 (SELECT `resource-id`, MAX(`scale`) as maxscale FROM `photo`
159 WHERE `profile`=0 AND `contact-id`=0 AND `album` NOT IN ('Contact Photos', '%s', 'Profile Photos', '%s')
160 AND `allow_cid`='' AND `allow_gid`='' AND `deny_cid`='' AND `deny_gid`='' GROUP BY `resource-id`) AS `t1`
161 INNER JOIN `photo` ON `photo`.`resource-id`=`t1`.`resource-id` AND `photo`.`scale` = `t1`.`maxscale`,
163 WHERE `user`.`uid` = `photo`.`uid`
164 AND `user`.`blockwall`=0
165 AND `user`.`hidewall` = 0
166 ORDER BY `photo`.`edited` DESC
168 dbesc(L10n::t('Contact Photos')),
169 dbesc(L10n::t('Profile Photos'))
174 # $tpl = file_get_contents( dirname(__file__).'/directory_item.tpl');
175 $tpl = get_markup_template('directory_item.tpl', 'addon/communityhome/');
176 foreach ($r as $rr) {
177 $photo_page = $a->get_baseurl() . '/photos/' . $rr['nickname'] . '/image/' . $rr['resource-id'];
178 $photo_url = $a->get_baseurl() . '/photo/' . $rr['resource-id'] . '-' . $rr['scale'] . '.jpg';
180 $entry = replace_macros($tpl, [
182 '$profile_link' => $photo_page,
183 '$photo' => $photo_url,
184 '$photo_user' => $rr['username'],
185 '$photo_title' => $rr['desc']
188 $aside['$photos_items'][] = $entry;
193 // last 10 liked items
194 if (Config::get('communityhome', 'showlastlike')) {
195 $aside['$like_title'] = L10n::t('Latest likes');
196 $aside['$like_items'] = [];
197 $r = q("SELECT `T1`.`created`, `T1`.`liker`, `T1`.`liker-link`, `item`.* FROM
198 (SELECT `parent-uri`, `created`, `author-name` AS `liker`,`author-link` AS `liker-link`
199 FROM `item` WHERE `verb`='http://activitystrea.ms/schema/1.0/like' GROUP BY `parent-uri` ORDER BY `created` DESC) AS T1
200 INNER JOIN `item` ON `item`.`uri`=`T1`.`parent-uri`
201 WHERE `T1`.`liker-link` LIKE '%s%%' OR `item`.`author-link` LIKE '%s%%'
203 ORDER BY `T1`.`created` DESC
209 foreach ($r as $rr) {
210 $author = '<a href="' . $rr['liker-link'] . '">' . $rr['liker'] . '</a>';
211 $objauthor = '<a href="' . $rr['author-link'] . '">' . $rr['author-name'] . '</a>';
213 //var_dump($rr['verb'],$rr['object-type']); killme();
214 switch ($rr['verb']) {
215 case 'http://activitystrea.ms/schema/1.0/post':
216 switch ($rr['object-type']) {
217 case 'http://activitystrea.ms/schema/1.0/event':
218 $post_type = L10n::t('event');
221 $post_type = L10n::t('status');
225 if ($rr['resource-id']) {
226 $post_type = L10n::t('photo');
228 preg_match("/\[url=([^]]*)\]/", $rr['body'], $m);
229 $rr['plink'] = $m[1];
231 $post_type = L10n::t('status');
234 $plink = '<a href="' . $rr['plink'] . '">' . $post_type . '</a>';
236 $aside['$like_items'][] = L10n::t('%1$s likes %2$s\'s %3$s', $author, $objauthor, $plink);
240 # $tpl = file_get_contents(dirname(__file__).'/communityhome.tpl');
241 $tpl = get_markup_template('communityhome.tpl', 'addon/communityhome/');
242 $a->page['aside'] = replace_macros($tpl, $aside);
244 $o = '<h1>' . ((x($a->config, 'sitename')) ? L10n::t("Welcome to %s", $a->config['sitename']) : "" ) . '</h1>';
246 if (file_exists('home.html')) $o = file_get_contents('home.html');
248 if (Config::get('communityhome', 'showcommunitystream')) {
249 $oldset = Config::get('system', 'community_page_style');
250 if ($oldset == CP_NO_COMMUNITY_PAGE) Config::set('system', 'community_page_style', CP_USERS_ON_SERVER);
252 $o .= community_content($a, 1);
254 if ($oldset == CP_NO_COMMUNITY_PAGE) Config::set('system', 'community_page_style', $oldset);