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