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