1<?php
2
3/**
4 * Google Ads for DokuWiki
5 *
6 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
7 * @author     Terence J. Grant<tjgrant@tatewake.com>
8 */
9
10if (!defined('DOKU_INC')) {
11    define('DOKU_INC', realpath(dirname(__FILE__) . '/../../') . '/');
12}
13if (!defined('DOKU_PLUGIN')) {
14    define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/');
15}
16require_once(DOKU_PLUGIN . 'admin.php');
17
18include_once(DOKU_PLUGIN . 'googleads/code.php');
19
20/**
21 * All DokuWiki plugins to extend the admin function
22 * need to inherit from this class
23 */
24class admin_plugin_googleads extends DokuWiki_Admin_Plugin
25{
26    public $state = 0;
27    public $googleads = '';
28
29    /**
30     * Constructor
31     */
32    public function admin_plugin_googleads()
33    {
34        $this->setupLocale();
35    }
36
37    /**
38     * handle user request
39     */
40    public function handle()
41    {
42        $this->state = 0;
43
44        if (!isset($_REQUEST['cmd']) || !is_array($_REQUEST['cmd'])) {
45            return;
46        }
47
48        $this->googleads = $_REQUEST['googleads'];
49
50        if (is_array($this->googleads)) {
51            $this->state = 1;
52        }
53    }
54
55    /**
56     * output appropriate html
57     */
58    public function html()
59    {
60        global $conf;
61        global $gads_loaded, $gads_settings;
62
63        if ($this->state != 0) {
64            $gads_settings['code'] = $this->googleads != null && array_key_exists('code', $this->googleads) ? addslashes($this->googleads['code']) : '';
65            $gads_settings['dontcountadmin'] = $this->getIsValueOn($this->googleads, 'dontcountadmin') ? 1 : 0;
66            $gads_settings['dontcountmanager'] = $this->getIsValueOn($this->googleads, 'dontcountmanager') ? 1 : 0;
67            $gads_settings['dontcountusers'] = $this->getIsValueOn($this->googleads, 'dontcountusers') ? 1 : 0;
68
69            gads_save();
70        }
71
72        print $this->locale_xhtml('intro');
73        print $this->getForm();
74        print '<br/><br/>';
75        print $this->locale_xhtml('outtro');
76    }
77
78    protected function getIsValueOn($map, $key)
79    {
80        $result = false;
81
82        if ($map != null && array_key_exists($key, $map)) {
83            $result = $map[$key] == 1 || $map[$key] === 'on' || $map[$key] === 'checked';
84        }
85
86        return $result;
87    }
88
89    /**
90     * Create the preference form
91     *
92     * @return string
93     */
94    protected function getForm()
95    {
96        global $ID;
97        global $gads_settings;
98
99        $form = new \dokuwiki\Form\Form([
100            'method' => 'POST',
101            'action' => wl($ID, ['do' => 'admin', 'page' => $this->getPluginName(), 'cmd[googleads]' => 'true'], false, '&')
102        ]);
103        $form->addFieldsetOpen($this->getLang('components'));
104
105        $ta = $form->addTextarea('googleads[code]', $this->getLang('gads_googleads_code'));
106
107        if ($gads_settings != null && array_key_exists('code', $gads_settings)) {
108            $ta->val(stripslashes($gads_settings['code']));
109        }
110
111        $cb = $form->addCheckbox("googleads[dontcountadmin]", $this->getLang('gads_dont_count_admin'))->useInput(false)->addClass('block');
112        if ($this->getIsValueOn($gads_settings, 'dontcountadmin')) {
113            $cb->attr('checked', 'checked');
114        }
115
116        $cb = $form->addCheckbox("googleads[dontcountmanager]", $this->getLang('gads_dont_count_manager'))->useInput(false)->addClass('block');
117        if ($this->getIsValueOn($gads_settings, 'dontcountmanager')) {
118            $cb->attr('checked', 'checked');
119        }
120
121        $cb = $form->addCheckbox("googleads[dontcountusers]", $this->getLang('gads_dont_count_users'))->useInput(false)->addClass('block');
122        if ($this->getIsValueOn($gads_settings, 'dontcountusers')) {
123            $cb->attr('checked', 'checked');
124        }
125
126        $form->addButton('save', $this->getLang('gads_save'));
127        return $form->toHTML();
128    }
129}
130