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