]> git.mxchange.org Git - friendica-addons.git/blob - communityhome/communityhome.php
add "most active users"
[friendica-addons.git] / communityhome / communityhome.php
1 <?php
2 /**
3  * Name: Community home
4  * Description: Show last community activity in homepage
5  * Version: 1.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         // login form
28         $aside .= "<h3>". t('Login'). "</h3>";
29         $aside .= login(($a->config['register_policy'] == REGISTER_CLOSED) ? false : true);
30         
31         // last 12 users
32         $aside .= "<h3>". t('Last users'). "</h3>";
33         $sql_extra = "";
34         $publish = (get_config('system','publish_all') ? '' : " AND `publish` = 1 " );
35         $order = " ORDER BY `register_date` DESC ";
36
37         $r = q("SELECT `profile`.*, `profile`.`uid` AS `profile_uid`, `user`.`nickname`
38                         FROM `profile` LEFT JOIN `user` ON `user`.`uid` = `profile`.`uid` 
39                         WHERE `is-default` = 1 $publish AND `user`.`blocked` = 0 $sql_extra $order LIMIT %d , %d ",
40                 0,
41                 12
42         );
43         $aside .= "<div class='items-wrapper'>";
44         $tpl = file_get_contents( dirname(__file__).'/directory_item.tpl');
45         if(count($r)) {
46                 $photo = 'thumb';
47                 foreach($r as $rr) {
48                         $profile_link = $a->get_baseurl() . '/profile/' . ((strlen($rr['nickname'])) ? $rr['nickname'] : $rr['profile_uid']);
49                         $entry = replace_macros($tpl,array(
50                                 '$id' => $rr['id'],
51                                 '$profile-link' => $profile_link,
52                                 '$photo' => $rr[$photo],
53                                 '$alt-text' => $rr['name'],
54                         ));
55                         $aside .= $entry;
56                 }
57         }
58         $aside .= "</div>";
59         
60         // 12 most active users (by posts and contacts)
61         // this query don't work on some mysql versions
62         $r = q("SELECT `uni`.`contacts`,`uni`.`items`, `profile`.*, `profile`.`uid` AS `profile_uid`, `user`.`nickname`  FROM
63                         (SELECT COUNT(`id`) as `contacts`, `uid` FROM `contact` WHERE `self`=0 GROUP BY `uid`) AS `con`,
64                         (SELECT COUNT(`id`) as `items`, `uid` FROM `item` WHERE `item`.`changed` > DATE(NOW() - INTERVAL 1 MONTH) AND `item`.`wall` = 1 GROUP BY `uid`) AS `ite`,
65                         (
66                         SELECT `contacts`,`items`,`ite`.`uid` FROM `con` RIGHT OUTER JOIN `ite` ON `con`.`uid`=`ite`.`uid` 
67                         UNION ALL 
68                         SELECT `contacts`,`items`,`con`.`uid` FROM `con` LEFT OUTER JOIN `ite` ON `con`.`uid`=`ite`.`uid`
69                         ) AS `uni`, `user`, `profile`
70                         WHERE `uni`.`uid`=`user`.`uid`
71                         AND `uni`.`uid`=`profile`.`uid` AND `profile`.`publish`=1
72                         GROUP BY `uid`
73                         ORDER BY `items` DESC,`contacts` DESC
74                         LIMIT 0,10");
75         if($r && count($r)) {
76                 $aside .= "<h3>". t('Most active users'). "</h3>";
77                 $aside .= "<div class='items-wrapper'>";
78                 
79                 $photo = 'thumb';
80                 foreach($r as $rr) {
81                         $profile_link = $a->get_baseurl() . '/profile/' . ((strlen($rr['nickname'])) ? $rr['nickname'] : $rr['profile_uid']);
82                         $entry = replace_macros($tpl,array(
83                                 '$id' => $rr['id'],
84                                 '$profile-link' => $profile_link,
85                                 '$photo' => $rr[$photo],
86                                 '$alt-text' => sprintf("%s (%s posts, %s contacts)",$rr['name'], ($rr['items']?$rr['items']:'0'), ($rr['contacts']?$rr['contacts']:'0'))
87                         ));
88                         $aside .= $entry;
89                 }
90                 $aside .= "</div>";
91         }
92         
93         // last 12 photos
94         $aside .= "<h3>". t('Last photos'). "</h3>";
95         $r = q("SELECT `photo`.`id`, `photo`.`resource-id`, `photo`.`scale`, `photo`.`desc`, `user`.`nickname`, `user`.`username` FROM 
96                                 (SELECT `resource-id`, MAX(`scale`) as maxscale FROM `photo` 
97                                         WHERE `profile`=0 AND `contact-id`=0 AND `album` NOT IN ('Contact Photos', '%s', 'Profile Photos', '%s')
98                                                 AND `allow_cid`='' AND `allow_gid`='' AND `deny_cid`='' AND `deny_gid`='' GROUP BY `resource-id`) AS `t1`
99                                 INNER JOIN `photo` ON `photo`.`resource-id`=`t1`.`resource-id` AND `photo`.`scale` = `t1`.`maxscale`,
100                                 `user` 
101                                 WHERE `user`.`uid` = `photo`.`uid`
102                                 AND `user`.`blockwall`=0
103                                 ORDER BY `photo`.`edited` DESC
104                                 LIMIT 0, 12",
105                                 dbesc(t('Contact Photos')),
106                                 dbesc(t('Profile Photos'))
107                                 );
108
109         $aside .= "<div class='items-wrapper'>";                                
110         if(count($r)) {
111                 $tpl = file_get_contents( dirname(__file__).'/directory_item.tpl');
112                 foreach($r as $rr) {
113                         $photo_page = $a->get_baseurl() . '/photos/' . $rr['nickname'] . '/image/' . $rr['resource-id'];
114                         $photo_url = $a->get_baseurl() . '/photo/' .  $rr['resource-id'] . '-' . $rr['scale'] .'.jpg';
115                 
116                         $entry = replace_macros($tpl,array(
117                                 '$id' => $rr['id'],
118                                 '$profile-link' => $photo_page,
119                                 '$photo' => $photo_url,
120                                 '$alt-text' => $rr['username']." : ".$rr['desc'],
121                         ));
122
123                         $aside .= $entry;
124                 }
125         }
126         $aside .= "</div>";
127         
128         // last 10 liked items
129         $aside .= "<h3>". t('Last likes'). "</h3>";
130         $r = q("SELECT `T1`.`created`, `T1`.`liker`, `T1`.`liker-link`, `item`.* FROM 
131                         (SELECT `parent-uri`, `created`, `author-name` AS `liker`,`author-link` AS `liker-link` 
132                                 FROM `item` WHERE `verb`='http://activitystrea.ms/schema/1.0/like' GROUP BY `parent-uri` ORDER BY `created` DESC) AS T1
133                         INNER JOIN `item` ON `item`.`uri`=`T1`.`parent-uri` 
134                         WHERE `T1`.`liker-link` LIKE '%s%%' OR `item`.`author-link` LIKE '%s%%'
135                         GROUP BY `uri`
136                         ORDER BY `T1`.`created` DESC
137                         LIMIT 0,10",
138                         $a->get_baseurl(),$a->get_baseurl()
139                         );
140
141         $aside .= "<ul id='likes'>";
142         foreach ($r as $rr) {
143                 $author  = '<a href="' . $rr['liker-link'] . '">' . $rr['liker'] . '</a>';
144                 $objauthor =  '<a href="' . $rr['author-link'] . '">' . $rr['author-name'] . '</a>';
145                 
146                 //var_dump($rr['verb'],$rr['object-type']); killme();
147                 switch($rr['verb']){
148                         case 'http://activitystrea.ms/schema/1.0/post':
149                                 switch ($rr['object-type']){
150                                         case 'http://activitystrea.ms/schema/1.0/event':
151                                                 $post_type = t('event');
152                                                 break;
153                                         default:
154                                                 $post_type = t('status');
155                                 }
156                                 break;
157                         default:
158                                 if ($rr['resource-id']){
159                                         $post_type = t('photo');
160                                         $m=array();     preg_match("/\[url=([^]]*)\]/", $rr['body'], $m);
161                                         $rr['plink'] = $m[1];
162                                 } else {
163                                         $post_type = t('status');
164                                 }
165                 }
166                 $plink = '<a href="' . $rr['plink'] . '">' . $post_type . '</a>';
167
168                 $aside .= "<li>". sprintf( t('%1$s likes %2$s\'s %3$s'), $author, $objauthor, $plink) ."</li>";
169                 
170         }
171         $aside .= "</ul>";
172                 
173         
174         $a->page['aside'] = $aside;
175         
176         $o = '<h1>' . ((x($a->config,'sitename')) ? sprintf( t("Welcome to %s") ,$a->config['sitename']) : "" ) . '</h1>';
177         
178         $oldset = get_config('system','no_community_page');
179         set_config('system','no_community_page', false);
180         $o .= community_content($a,1);
181         set_config('system','no_community_page', $oldset);
182 }