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