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