ff3215d36aa00aaf64fe203ddd8de8847d0d7aef
[friendica-addons.git] / communityhome / communityhome.php
1 <?php
2 /**
3  * Name: Community home
4  * Description: Show last community activity in homepage
5  * Version: 2.0
6  * Author: Fabio Comuni <http://kirgroup.com/profile/fabrixxm>
7  */
8
9
10 require_once('mod/community.php');
11
12
13 function communityhome_install() {
14         register_hook('home_content', 'addon/communityhome/communityhome.php', 'communityhome_home');
15         logger("installed communityhome");
16 }
17
18 function communityhome_uninstall() {
19         unregister_hook('home_content', 'addon/communityhome/communityhome.php', 'communityhome_home');
20         logger("removed communityhome");
21 }
22
23 function communityhome_home(&$a, &$o){
24         // custom css
25         $a->page['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="'.$a->get_baseurl().'/addon/communityhome/communityhome.css" media="all" />';
26
27         if (!get_config('communityhome','hidelogin')){
28                 $aside = array(
29                         '$tab_1' => t('Login'),
30                         '$tab_2' => t('OpenID'),
31                         '$noOid' => get_config('system','no_openid'),
32                 );
33
34                 // login form
35                 $aside['$login_title'] =  t('Login');
36                 $aside['$login_form'] = login(($a->config['register_policy'] == REGISTER_CLOSED) ? false : true);
37         } else
38                 $aside = array(
39                         //'$tab_1' => t('Login'),
40                         //'$tab_2' => t('OpenID'),
41                         //'$noOid' => get_config('system','no_openid'),
42                 );
43
44         // last 12 users
45         if (get_config('communityhome','showlastusers')===true){
46                 $aside['$lastusers_title'] = t('Latest users');
47                 $aside['$lastusers_items'] = array();
48                 $sql_extra = "";
49                 $publish = (get_config('system','publish_all') ? '' : " AND `publish` = 1 " );
50                 $order = " ORDER BY `register_date` DESC ";
51
52                 $r = q("SELECT `profile`.*, `profile`.`uid` AS `profile_uid`, `user`.`nickname`
53                                 FROM `profile` LEFT JOIN `user` ON `user`.`uid` = `profile`.`uid` 
54                                 WHERE `is-default` = 1 $publish AND `user`.`blocked` = 0 $sql_extra $order LIMIT %d , %d ",
55                         0,
56                         12
57                 );
58         #       $tpl = file_get_contents( dirname(__file__).'/directory_item.tpl');
59                 $tpl = get_markup_template( 'directory_item.tpl', 'addon/communityhome/' );
60                 if(count($r)) {
61                         $photo = 'thumb';
62                         foreach($r as $rr) {
63                                 $profile_link = $a->get_baseurl() . '/profile/' . ((strlen($rr['nickname'])) ? $rr['nickname'] : $rr['profile_uid']);
64                                 $entry = replace_macros($tpl,array(
65                                         '$id' => $rr['id'],
66                                         '$profile_link' => $profile_link,
67                                         '$photo' => $a->get_cached_avatar_image($rr[$photo]),
68                                         '$alt_text' => $rr['name'],
69                                 ));
70                                 $aside['$lastusers_items'][] = $entry;
71                         }
72                 }
73         }
74         // 12 most active users (by posts and contacts)
75         // this query don't work on some mysql versions
76         if (get_config('communityhome','showactiveusers')===true){
77                 $r = q("SELECT `uni`.`contacts`,`uni`.`items`, `profile`.*, `profile`.`uid` AS `profile_uid`, `user`.`nickname`  FROM
78                                 (SELECT COUNT(*) as `contacts`, `uid` FROM `contact` WHERE `self`=0 GROUP BY `uid`) AS `con`,
79                                 (SELECT COUNT(*) as `items`, `uid` FROM `item` WHERE `item`.`changed` > DATE(NOW() - INTERVAL 1 MONTH) AND `item`.`wall` = 1 GROUP BY `uid`) AS `ite`,
80                                 (
81                                 SELECT `contacts`,`items`,`ite`.`uid` FROM `con` RIGHT OUTER JOIN `ite` ON `con`.`uid`=`ite`.`uid` 
82                                 UNION ALL 
83                                 SELECT `contacts`,`items`,`con`.`uid` FROM `con` LEFT OUTER JOIN `ite` ON `con`.`uid`=`ite`.`uid`
84                                 ) AS `uni`, `user`, `profile`
85                                 WHERE `uni`.`uid`=`user`.`uid`
86                                 AND `uni`.`uid`=`profile`.`uid` AND `profile`.`publish`=1
87                                 GROUP BY `uid`
88                                 ORDER BY `items` DESC,`contacts` DESC
89                                 LIMIT 0,10");
90                 if($r && count($r)) {
91                         $aside['$activeusers_title']  = t('Most active users');
92                         $aside['$activeusers_items']  = array();
93
94                         $photo = 'thumb';
95                         foreach($r as $rr) {
96                                 $profile_link = $a->get_baseurl() . '/profile/' . ((strlen($rr['nickname'])) ? $rr['nickname'] : $rr['profile_uid']);
97                                 $entry = replace_macros($tpl,array(
98                                         '$id' => $rr['id'],
99                                         '$profile_link' => $profile_link,
100                                         '$photo' => $rr[$photo],
101                                         '$alt_text' => sprintf("%s (%s posts, %s contacts)",$rr['name'], ($rr['items']?$rr['items']:'0'), ($rr['contacts']?$rr['contacts']:'0'))
102                                 ));
103                                 $aside['$activeusers_items'][] = $entry;
104                         }
105                 }
106         }
107         // last 12 photos
108         if (get_config('communityhome','showlastphotos')===true){
109                 $aside['$photos_title'] = t('Latest photos');
110                 $aside['$photos_items'] = array();
111                 $r = q("SELECT `photo`.`id`, `photo`.`resource-id`, `photo`.`scale`, `photo`.`desc`, `user`.`nickname`, `user`.`username` FROM 
112                                         (SELECT `resource-id`, MAX(`scale`) as maxscale FROM `photo` 
113                                                 WHERE `profile`=0 AND `contact-id`=0 AND `album` NOT IN ('Contact Photos', '%s', 'Profile Photos', '%s')
114                                                         AND `allow_cid`='' AND `allow_gid`='' AND `deny_cid`='' AND `deny_gid`='' GROUP BY `resource-id`) AS `t1`
115                                         INNER JOIN `photo` ON `photo`.`resource-id`=`t1`.`resource-id` AND `photo`.`scale` = `t1`.`maxscale`,
116                                         `user` 
117                                         WHERE `user`.`uid` = `photo`.`uid`
118                                         AND `user`.`blockwall`=0
119                                         AND `user`.`hidewall` = 0
120                                         ORDER BY `photo`.`edited` DESC
121                                         LIMIT 0, 12",
122                                         dbesc(t('Contact Photos')),
123                                         dbesc(t('Profile Photos'))
124                                         );
125
126
127                 if(count($r)) {
128         #               $tpl = file_get_contents( dirname(__file__).'/directory_item.tpl');
129                         $tpl = get_markup_template( 'directory_item.tpl', 'addon/communityhome/' );
130                         foreach($r as $rr) {
131                                 $photo_page = $a->get_baseurl() . '/photos/' . $rr['nickname'] . '/image/' . $rr['resource-id'];
132                                 $photo_url = $a->get_baseurl() . '/photo/' .  $rr['resource-id'] . '-' . $rr['scale'] .'.jpg';
133
134                                 $entry = replace_macros($tpl,array(
135                                         '$id' => $rr['id'],
136                                         '$profile_link' => $photo_page,
137                                         '$photo' => $photo_url,
138                                         '$alt_text' => $rr['username']." : ".$rr['desc'],
139                                 ));
140
141                                 $aside['$photos_items'][] = $entry;
142                         }
143                 }
144         }
145
146         // last 10 liked items
147         if (get_config('communityhome','showlastlike')===true){
148                 $aside['$like_title'] = t('Latest likes');
149                 $aside['$like_items'] = array();
150                 $r = q("SELECT `T1`.`created`, `T1`.`liker`, `T1`.`liker-link`, `item`.* FROM 
151                                 (SELECT `parent-uri`, `created`, `author-name` AS `liker`,`author-link` AS `liker-link` 
152                                         FROM `item` WHERE `verb`='http://activitystrea.ms/schema/1.0/like' GROUP BY `parent-uri` ORDER BY `created` DESC) AS T1
153                                 INNER JOIN `item` ON `item`.`uri`=`T1`.`parent-uri` 
154                                 WHERE `T1`.`liker-link` LIKE '%s%%' OR `item`.`author-link` LIKE '%s%%'
155                                 GROUP BY `uri`
156                                 ORDER BY `T1`.`created` DESC
157                                 LIMIT 0,10",
158                                 $a->get_baseurl(),$a->get_baseurl()
159                                 );
160
161                 foreach ($r as $rr) {
162                         $author  = '<a href="' . $rr['liker-link'] . '">' . $rr['liker'] . '</a>';
163                         $objauthor =  '<a href="' . $rr['author-link'] . '">' . $rr['author-name'] . '</a>';
164
165                         //var_dump($rr['verb'],$rr['object-type']); killme();
166                         switch($rr['verb']){
167                                 case 'http://activitystrea.ms/schema/1.0/post':
168                                         switch ($rr['object-type']){
169                                                 case 'http://activitystrea.ms/schema/1.0/event':
170                                                         $post_type = t('event');
171                                                         break;
172                                                 default:
173                                                         $post_type = t('status');
174                                         }
175                                         break;
176                                 default:
177                                         if ($rr['resource-id']){
178                                                 $post_type = t('photo');
179                                                 $m=array();     preg_match("/\[url=([^]]*)\]/", $rr['body'], $m);
180                                                 $rr['plink'] = $m[1];
181                                         } else {
182                                                 $post_type = t('status');
183                                         }
184                         }
185                         $plink = '<a href="' . $rr['plink'] . '">' . $post_type . '</a>';
186
187                         $aside['$like_items'][] = sprintf( t('%1$s likes %2$s\'s %3$s'), $author, $objauthor, $plink);
188
189                 }
190         }
191
192 #       $tpl = file_get_contents(dirname(__file__).'/communityhome.tpl');
193         $tpl = get_markup_template('communityhome.tpl', 'addon/communityhome/');
194         $a->page['aside'] = replace_macros($tpl, $aside);
195
196         $o = '<h1>' . ((x($a->config,'sitename')) ? sprintf( t("Welcome to %s") ,$a->config['sitename']) : "" ) . '</h1>';
197
198         if(file_exists('home.html'))
199                 $o = file_get_contents('home.html');
200
201         if (get_config('communityhome','showcommunitystream')===true){
202                 $oldset = get_config('system','community_page_style');
203                 if ($oldset == CP_NO_COMMUNITY_PAGE)
204                         set_config('system','community_page_style', CP_USERS_ON_SERVER);
205
206                 $o .= community_content($a,1);
207
208                 if ($oldset == CP_NO_COMMUNITY_PAGE)
209                         set_config('system','community_page_style', $oldset);
210         }
211 }