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