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