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