xref: /plugin/farmer/admin/config.php (revision 55aab17c30a2c3054d4a303e2c7deeaaa7496538)
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();
43c4c8e953SAndreas Gohr        $farmdir = $farmconf['base']['farmdir'];
44a646d519SAndreas Gohr        $farmconf = array_merge($farmconf, $INPUT->arr('farmconf'));
45c4c8e953SAndreas Gohr        $farmconf['base']['farmdir'] = $farmdir;
46c4c8e953SAndreas Gohr
47c4c8e953SAndreas Gohr        $farmconf['base']['basedomain'] = trim(trim($farmconf['base']['basedomain'],'.'));
48a646d519SAndreas Gohr
49da0ae2c0SAndreas Gohr        $ini = DOKU_INC . 'conf/farm.ini';
50da0ae2c0SAndreas Gohr        $data = "; Farm config created by the farmer plugin\n";
51a646d519SAndreas Gohr        $data .= $this->createIni($farmconf);
52da0ae2c0SAndreas Gohr        io_saveFile($ini, $data);
53da0ae2c0SAndreas Gohr
54da0ae2c0SAndreas Gohr        $self = wl($ID, array('do' => 'admin', 'page' => 'farmer', 'sub' => 'config'), true, '&');
55da0ae2c0SAndreas Gohr        send_redirect($self);
56da0ae2c0SAndreas Gohr    }
57da0ae2c0SAndreas Gohr
58da0ae2c0SAndreas Gohr    /**
59da0ae2c0SAndreas Gohr     * Render HTML output, e.g. helpful text and a form
60da0ae2c0SAndreas Gohr     */
61da0ae2c0SAndreas Gohr    public function html() {
62b96c66ccSAndreas Gohr        $farmconf = $this->helper->getConfig();
63da0ae2c0SAndreas Gohr
64da0ae2c0SAndreas Gohr        $form = new Form(array('method' => 'post'));
65c4c8e953SAndreas Gohr
66c4c8e953SAndreas Gohr        $form->addFieldsetOpen($this->getLang('base'));
67c4c8e953SAndreas Gohr        $form->addHTML('<label><span>'.$this->getLang('farm dir').'</span>'.DOKU_FARMDIR);
68c4c8e953SAndreas Gohr        $form->addTextInput('farmconf[base][farmhost]', $this->getLang('farm host'))->val($farmconf['base']['farmhost']);
69c4c8e953SAndreas Gohr        $form->addTextInput('farmconf[base][basedomain]', $this->getLang('base domain'))->val($farmconf['base']['basedomain']);
70c4c8e953SAndreas Gohr        $form->addFieldsetClose();
71c4c8e953SAndreas Gohr
72c4c8e953SAndreas Gohr
73da0ae2c0SAndreas Gohr        $form->addFieldsetOpen($this->getLang('conf_inherit'));
74da0ae2c0SAndreas Gohr        foreach($farmconf['inherit'] as $key => $val) {
75da0ae2c0SAndreas Gohr            $form->setHiddenField("farmconf[inherit][$key]", 0);
76da0ae2c0SAndreas Gohr            $chk = $form->addCheckbox("farmconf[inherit][$key]", $this->getLang('conf_inherit_' . $key))->useInput(false);
77da0ae2c0SAndreas Gohr            if($val) $chk->attr('checked', 'checked');
78da0ae2c0SAndreas Gohr        }
79da0ae2c0SAndreas Gohr        $form->addFieldsetClose();
80da0ae2c0SAndreas Gohr
81bd48c676SAndreas Gohr
82*55aab17cSAndreas Gohr        $options = array(
83*55aab17cSAndreas Gohr            'farmer' => $this->getLang('conf_notfound_farmer'),
84*55aab17cSAndreas Gohr            '404' => $this->getLang('conf_notfound_404'),
85*55aab17cSAndreas Gohr            'list' => $this->getLang('conf_notfound_list'),
86*55aab17cSAndreas Gohr            'redirect'  => $this->getLang('conf_notfound_redirect')
87*55aab17cSAndreas Gohr        );
88*55aab17cSAndreas Gohr
89*55aab17cSAndreas Gohr        $form->addFieldsetOpen($this->getLang('conf_notfound'));
90*55aab17cSAndreas Gohr        $form->addDropdown('farmconf[notfound][show]', $options, $this->getLang('conf_notfound'))->val($farmconf['notfound']['show']);
91da0ae2c0SAndreas Gohr        $form->addTextInput('farmconf[notfound][url]', $this->getLang('conf_notfound_url'))->val($farmconf['notfound']['url']);
92da0ae2c0SAndreas Gohr        $form->addFieldsetClose();
93da0ae2c0SAndreas Gohr
94da0ae2c0SAndreas Gohr        $form->addButton('save', $this->getLang('save'));
95da0ae2c0SAndreas Gohr        echo $form->toHTML();
96da0ae2c0SAndreas Gohr    }
97da0ae2c0SAndreas Gohr
98da0ae2c0SAndreas Gohr    /**
99da0ae2c0SAndreas Gohr     * Simple function to create an ini file
100da0ae2c0SAndreas Gohr     *
101da0ae2c0SAndreas Gohr     * Does no escaping, but should suffice for our use case
102da0ae2c0SAndreas Gohr     *
103da0ae2c0SAndreas Gohr     * @link http://stackoverflow.com/a/5695202/172068
104da0ae2c0SAndreas Gohr     * @param array $data The data to transform
105da0ae2c0SAndreas Gohr     * @return string
106da0ae2c0SAndreas Gohr     */
107da0ae2c0SAndreas Gohr    public function createIni($data) {
108da0ae2c0SAndreas Gohr        $res = array();
109da0ae2c0SAndreas Gohr        foreach($data as $key => $val) {
110da0ae2c0SAndreas Gohr            if(is_array($val)) {
111da0ae2c0SAndreas Gohr                $res[] = '';
112da0ae2c0SAndreas Gohr                $res[] = "[$key]";
113da0ae2c0SAndreas Gohr                foreach($val as $skey => $sval) {
114da0ae2c0SAndreas Gohr                    $res[] = "$skey = " . (is_numeric($sval) ? $sval : '"' . $sval . '"');
115da0ae2c0SAndreas Gohr                }
116da0ae2c0SAndreas Gohr            } else {
117da0ae2c0SAndreas Gohr                $res[] = "$key = " . (is_numeric($val) ? $val : '"' . $val . '"');
118da0ae2c0SAndreas Gohr            }
119da0ae2c0SAndreas Gohr        }
120da0ae2c0SAndreas Gohr        $res[] = '';
121da0ae2c0SAndreas Gohr        return join("\n", $res);
122da0ae2c0SAndreas Gohr    }
123da0ae2c0SAndreas Gohr}
124da0ae2c0SAndreas Gohr
125da0ae2c0SAndreas Gohr// vim:ts=4:sw=4:et:
126