]> git.mxchange.org Git - friendica-addons.git/blob - communityhome/communityhome.php
Merge branch '3.6-rc'
[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' => $a->get_cached_avatar_image($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                                         '$alt_text' => $rr['username']." : ".$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 }