xref: /plugin/farmer/admin/config.php (revision da0ae2c0252db3e5c2ed377b5e8c5f4d93cc81bb)
1<?php
2/**
3 * DokuWiki Plugin farmer (Admin Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Michael Große <grosse@cosmocode.de>
7 */
8
9// must be run within Dokuwiki
10use dokuwiki\Form\Form;
11
12if(!defined('DOKU_INC')) die();
13
14class admin_plugin_farmer_config extends DokuWiki_Admin_Plugin {
15
16    /**
17     * @return bool admin only!
18     */
19    public function forAdminOnly() {
20        return false;
21    }
22
23    /**
24     * Should carry out any processing required by the plugin.
25     */
26    public function handle() {
27        global $INPUT;
28        global $ID;
29        if(!$INPUT->has('farmconf')) return;
30        if(!checkSecurityToken()) return;
31
32        $ini = DOKU_INC . 'conf/farm.ini';
33        $data = "; Farm config created by the farmer plugin\n";
34        $data .= $this->createIni($INPUT->arr('farmconf'));
35        io_saveFile($ini, $data);
36
37        $self = wl($ID, array('do' => 'admin', 'page' => 'farmer', 'sub' => 'config'), true, '&');
38        send_redirect($self);
39    }
40
41    /**
42     * Render HTML output, e.g. helpful text and a form
43     */
44    public function html() {
45        global $FARMCORE;
46        $farmconf = $FARMCORE->getConfig();
47
48        $form = new Form(array('method' => 'post'));
49
50        $form->addFieldsetOpen($this->getLang('conf_inherit'));
51        foreach($farmconf['inherit'] as $key => $val) {
52            $form->setHiddenField("farmconf[inherit][$key]", 0);
53            $chk = $form->addCheckbox("farmconf[inherit][$key]", $this->getLang('conf_inherit_' . $key))->useInput(false);
54            if($val) $chk->attr('checked', 'checked');
55        }
56        $form->addFieldsetClose();
57
58        $form->addFieldsetOpen($this->getLang('conf_notfound'));
59        $form->addTagOpen('select')->attr('name', 'farmconf[notfound][show]');
60        foreach(array('farmer', '404', 'list', 'redirect') as $key) {
61            $opt = $form->addTagOpen('option')->attr('value', $key);
62            if($farmconf['notfound']['show'] == $key) $opt->attr('selected', 'selected');
63            $form->addHTML($this->getLang('conf_notfound_' . $key));
64            $form->addTagClose('option');
65        }
66        $form->addTagClose('select');
67        $form->addTextInput('farmconf[notfound][url]', $this->getLang('conf_notfound_url'))->val($farmconf['notfound']['url']);
68        $form->addFieldsetClose();
69
70        $form->addButton('save', $this->getLang('save'));
71        echo $form->toHTML();
72    }
73
74    /**
75     * Simple function to create an ini file
76     *
77     * Does no escaping, but should suffice for our use case
78     *
79     * @link http://stackoverflow.com/a/5695202/172068
80     * @param array $data The data to transform
81     * @return string
82     */
83    public function createIni($data) {
84        $res = array();
85        foreach($data as $key => $val) {
86            if(is_array($val)) {
87                $res[] = '';
88                $res[] = "[$key]";
89                foreach($val as $skey => $sval) {
90                    $res[] = "$skey = " . (is_numeric($sval) ? $sval : '"' . $sval . '"');
91                }
92            } else {
93                $res[] = "$key = " . (is_numeric($val) ? $val : '"' . $val . '"');
94            }
95        }
96        $res[] = '';
97        return join("\n", $res);
98    }
99}
100
101// vim:ts=4:sw=4:et:
102