diaspora addon EN GB translation updated THX AndyH§
[friendica-addons.git/.git] / cookienotice / cookienotice.php
1 <?php
2
3 /**
4  * Name: Cookie Notice
5  * Description: Configure, show and handle a simple cookie notice
6  * Version: 1.0
7  * Author: Peter Liebetrau <https://socivitas/profile/peerteer>
8  */
9
10 use Friendica\App;
11 use Friendica\Core\Hook;
12 use Friendica\Core\Renderer;
13 use Friendica\DI;
14
15 /**
16  * cookienotice_install
17  * registers hooks
18  *
19  * @return void
20  */
21 function cookienotice_install()
22 {
23         Hook::register('page_content_top', __FILE__, 'cookienotice_page_content_top');
24         Hook::register('page_end', __FILE__, 'cookienotice_page_end');
25 }
26
27 /**
28  * cookienotice_addon_admin
29  * creates the admins config panel
30  *
31  * @param App    $a
32  * @param string $s The existing config panel html so far
33  *
34  * @return void
35  */
36 function cookienotice_addon_admin(App $a, &$s)
37 {
38         if (!is_site_admin()) {
39                 return;
40         }
41
42         $text = DI::config()->get('cookienotice', 'text', DI::l10n()->t('This website uses cookies. If you continue browsing this website, you agree to the usage of cookies.'));
43         $oktext = DI::config()->get('cookienotice', 'oktext', DI::l10n()->t('OK'));
44
45         $t = Renderer::getMarkupTemplate('admin.tpl', 'addon/cookienotice/');
46         $s .= Renderer::replaceMacros($t, [
47                 '$description' => DI::l10n()->t('<b>Configure your cookie usage notice.</b> It should just be a notice, saying that the website uses cookies. It is shown as long as a user didnt confirm clicking the OK button.'),
48                 '$text' => ['cookienotice-text', DI::l10n()->t('Cookie Usage Notice'), $text],
49                 '$oktext' => ['cookienotice-oktext', DI::l10n()->t('OK Button Text'), $oktext],
50                 '$submit' => DI::l10n()->t('Save Settings')
51         ]);
52
53         return;
54 }
55
56 /**
57  * cookienotice_addon_admin_post
58  * handles the post request from the admin panel
59  *
60  * @param App    $a
61  *
62  * @return void
63  */
64 function cookienotice_addon_admin_post(App $a)
65 {
66         if (!is_site_admin()) {
67                 return;
68         }
69
70         if ($_POST['cookienotice-submit']) {
71                 DI::config()->set('cookienotice', 'text', trim(strip_tags($_POST['cookienotice-text'])));
72                 DI::config()->set('cookienotice', 'oktext', trim(strip_tags($_POST['cookienotice-oktext'])));
73                 info(DI::l10n()->t('cookienotice Settings saved.'));
74         }
75 }
76
77 /**
78  * cookienotice_page_content_top
79  * page_content_top hook
80  * adds css and scripts to the <head> section of the html
81  *
82  * @param App    $a
83  * @param string $b unused - the header html incl. nav
84  *
85  * @return void
86  */
87 function cookienotice_page_content_top(App $a, &$b)
88 {
89         $stylesheetPath = __DIR__ . '/cookienotice.css';
90         $footerscriptPath = __DIR__ . '/cookienotice.js';
91
92         DI::page()->registerStylesheet($stylesheetPath);
93         DI::page()->registerFooterScript($footerscriptPath);
94 }
95
96 /**
97  * cookienotice_page_end
98  * page_end hook
99  * ads our cookienotice box to the end of the html
100  *
101  * @param App    $a
102  * @param string $b the page html
103  *
104  * @return void
105  */
106 function cookienotice_page_end(App $a, &$b)
107 {
108         $text = (string)DI::config()->get('cookienotice', 'text', DI::l10n()->t('This website uses cookies to recognize revisiting and logged in users. You accept the usage of these cookies by continue browsing this website.'));
109         $oktext = (string)DI::config()->get('cookienotice', 'oktext', DI::l10n()->t('OK'));
110
111         $page_end_tpl = Renderer::getMarkupTemplate('cookienotice.tpl', 'addon/cookienotice/');
112
113         $page_end = Renderer::replaceMacros($page_end_tpl, [
114                 '$text' => $text,
115                 '$oktext' => $oktext,
116         ]);
117
118         $b .= $page_end;
119 }