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