xref: /plugin/farmer/admin/config.php (revision 65dd3ddf74b90693d531a41925726ca7cd194a7b)
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('label');
71        $form->addTagOpen('span');
72        $form->addHTML($this->getLang('conf_notfound'));
73        $form->addTagClose('span');
74        $form->addTagOpen('select')->attr('name', 'farmconf[notfound][show]');
75        foreach(array('farmer', '404', 'list', 'redirect') as $key) {
76            $opt = $form->addTagOpen('option')->attr('value', $key);
77            if($farmconf['notfound']['show'] == $key) $opt->attr('selected', 'selected');
78            $form->addHTML($this->getLang('conf_notfound_' . $key));
79            $form->addTagClose('option');
80        }
81        $form->addTagClose('select');
82        $form->addTagClose('label');
83
84        $form->addTextInput('farmconf[notfound][url]', $this->getLang('conf_notfound_url'))->val($farmconf['notfound']['url']);
85        $form->addFieldsetClose();
86
87        $form->addButton('save', $this->getLang('save'));
88        echo $form->toHTML();
89    }
90
91    /**
92     * Simple function to create an ini file
93     *
94     * Does no escaping, but should suffice for our use case
95     *
96     * @link http://stackoverflow.com/a/5695202/172068
97     * @param array $data The data to transform
98     * @return string
99     */
100    public function createIni($data) {
101        $res = array();
102        foreach($data as $key => $val) {
103            if(is_array($val)) {
104                $res[] = '';
105                $res[] = "[$key]";
106                foreach($val as $skey => $sval) {
107                    $res[] = "$skey = " . (is_numeric($sval) ? $sval : '"' . $sval . '"');
108                }
109            } else {
110                $res[] = "$key = " . (is_numeric($val) ? $val : '"' . $val . '"');
111            }
112        }
113        $res[] = '';
114        return join("\n", $res);
115    }
116}
117
118// vim:ts=4:sw=4:et:
119