Remove deprecated App::registerStylesheet()/FooterScript() - replace with DI::page...
[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\Config;
12 use Friendica\Core\Hook;
13 use Friendica\Core\L10n;
14 use Friendica\Core\Renderer;
15 use Friendica\DI;
16
17 /**
18  * cookienotice_install
19  * registers hooks
20  *
21  * @return void
22  */
23 function cookienotice_install()
24 {
25         Hook::register('page_content_top', __FILE__, 'cookienotice_page_content_top');
26         Hook::register('page_end', __FILE__, 'cookienotice_page_end');
27 }
28
29 /**
30  * cookienotice_addon_admin
31  * creates the admins config panel
32  *
33  * @param App    $a
34  * @param string $s The existing config panel html so far
35  *
36  * @return void
37  */
38 function cookienotice_addon_admin(App $a, &$s)
39 {
40         if (!is_site_admin()) {
41                 return;
42         }
43
44         $text = Config::get('cookienotice', 'text', L10n::t('This website uses cookies. If you continue browsing this website, you agree to the usage of cookies.'));
45         $oktext = Config::get('cookienotice', 'oktext', L10n::t('OK'));
46
47         $t = Renderer::getMarkupTemplate('admin.tpl', __DIR__);
48         $s .= Renderer::replaceMacros($t, [
49                 '$description' => 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.'),
50                 '$text' => ['cookienotice-text', L10n::t('Cookie Usage Notice'), $text],
51                 '$oktext' => ['cookienotice-oktext', L10n::t('OK Button Text'), $oktext],
52                 '$submit' => L10n::t('Save Settings')
53         ]);
54
55         return;
56 }
57
58 /**
59  * cookienotice_addon_admin_post
60  * handles the post request from the admin panel
61  *
62  * @param App    $a
63  *
64  * @return void
65  */
66 function cookienotice_addon_admin_post(App $a)
67 {
68         if (!is_site_admin()) {
69                 return;
70         }
71
72         if ($_POST['cookienotice-submit']) {
73                 Config::set('cookienotice', 'text', trim(strip_tags($_POST['cookienotice-text'])));
74                 Config::set('cookienotice', 'oktext', trim(strip_tags($_POST['cookienotice-oktext'])));
75                 info(L10n::t('cookienotice Settings saved.'));
76         }
77 }
78
79 /**
80  * cookienotice_page_content_top
81  * page_content_top hook
82  * adds css and scripts to the <head> section of the html
83  *
84  * @param App    $a
85  * @param string $b unused - the header html incl. nav
86  *
87  * @return void
88  */
89 function cookienotice_page_content_top(App $a, &$b)
90 {
91         $stylesheetPath = __DIR__ . '/cookienotice.css';
92         $footerscriptPath = __DIR__ . '/cookienotice.js';
93
94         DI::page()->registerStylesheet($stylesheetPath);
95         DI::page()->registerFooterScript($footerscriptPath);
96 }
97
98 /**
99  * cookienotice_page_end
100  * page_end hook
101  * ads our cookienotice box to the end of the html
102  *
103  * @param App    $a
104  * @param string $b the page html
105  *
106  * @return void
107  */
108 function cookienotice_page_end(App $a, &$b)
109 {
110         $text = (string)Config::get('cookienotice', 'text', 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.'));
111         $oktext = (string)Config::get('cookienotice', 'oktext', L10n::t('OK'));
112
113         $page_end_tpl = Renderer::getMarkupTemplate('cookienotice.tpl', __DIR__);
114
115         $page_end = Renderer::replaceMacros($page_end_tpl, [
116                 '$text' => $text,
117                 '$oktext' => $oktext,
118         ]);
119
120         $b .= $page_end;
121 }