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