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