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