xref: /plugin/farmer/admin/config.php (revision b96c66cc018b7a6d9630acddc46fe47fbaff0136)
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    /** @var  helper_plugin_farmer */
17    protected $helper;
18
19    /**
20     * @return bool admin only!
21     */
22    public function forAdminOnly() {
23        return false;
24    }
25
26    /**
27     * admin_plugin_farmer_config constructor.
28     */
29    public function __construct() {
30        $this->helper = plugin_load('helper', 'farmer');
31    }
32
33    /**
34     * Should carry out any processing required by the plugin.
35     */
36    public function handle() {
37        global $INPUT;
38        global $ID;
39        if(!$INPUT->has('farmconf')) return;
40        if(!checkSecurityToken()) return;
41
42        $farmconf = $this->helper->getConfig();
43        $farmconf = array_merge($farmconf, $INPUT->arr('farmconf'));
44
45        $ini = DOKU_INC . 'conf/farm.ini';
46        $data = "; Farm config created by the farmer plugin\n";
47        $data .= $this->createIni($farmconf);
48        io_saveFile($ini, $data);
49
50        $self = wl($ID, array('do' => 'admin', 'page' => 'farmer', 'sub' => 'config'), true, '&');
51        send_redirect($self);
52    }
53
54    /**
55     * Render HTML output, e.g. helpful text and a form
56     */
57    public function html() {
58        $farmconf = $this->helper->getConfig();
59
60        $form = new Form(array('method' => 'post'));
61        $form->addFieldsetOpen($this->getLang('conf_inherit'));
62        foreach($farmconf['inherit'] as $key => $val) {
63            $form->setHiddenField("farmconf[inherit][$key]", 0);
64            $chk = $form->addCheckbox("farmconf[inherit][$key]", $this->getLang('conf_inherit_' . $key))->useInput(false);
65            if($val) $chk->attr('checked', 'checked');
66        }
67        $form->addFieldsetClose();
68
69        $form->addFieldsetOpen($this->getLang('conf_notfound'));
70        $form->addTagOpen('select')->attr('name', 'farmconf[notfound][show]');
71        foreach(array('farmer', '404', 'list', 'redirect') as $key) {
72            $opt = $form->addTagOpen('option')->attr('value', $key);
73            if($farmconf['notfound']['show'] == $key) $opt->attr('selected', 'selected');
74            $form->addHTML($this->getLang('conf_notfound_' . $key));
75            $form->addTagClose('option');
76        }
77        $form->addTagClose('select');
78        $form->addTextInput('farmconf[notfound][url]', $this->getLang('conf_notfound_url'))->val($farmconf['notfound']['url']);
79        $form->addFieldsetClose();
80
81        $form->addButton('save', $this->getLang('save'));
82        echo $form->toHTML();
83    }
84
85    /**
86     * Simple function to create an ini file
87     *
88     * Does no escaping, but should suffice for our use case
89     *
90     * @link http://stackoverflow.com/a/5695202/172068
91     * @param array $data The data to transform
92     * @return string
93     */
94    public function createIni($data) {
95        $res = array();
96        foreach($data as $key => $val) {
97            if(is_array($val)) {
98                $res[] = '';
99                $res[] = "[$key]";
100                foreach($val as $skey => $sval) {
101                    $res[] = "$skey = " . (is_numeric($sval) ? $sval : '"' . $sval . '"');
102                }
103            } else {
104                $res[] = "$key = " . (is_numeric($val) ? $val : '"' . $val . '"');
105            }
106        }
107        $res[] = '';
108        return join("\n", $res);
109    }
110}
111
112// vim:ts=4:sw=4:et:
113