1<?php
2
3/**
4 * Class ConfigManagerTwoLine
5 */
6class ConfigManagerTwoLine implements ConfigManagerConfigType {
7
8    private $name;
9    private $description;
10    private $configFile;
11
12    /**
13     * @var helper_plugin_confmanager
14     */
15    private $helper;
16
17    /**
18     * @param string $name Display name
19     * @param string $description Config file description
20     * @param string $configFile config files
21     */
22    function __construct($name, $description = '', $configFile) {
23        $this->configFile = $configFile;
24        $this->description = $description;
25        $this->name = $name;
26        $this->helper = plugin_load('helper', 'confmanager');
27    }
28
29    /**
30     * Get the name of the config file. Will be displayed in the admin menu.
31     *
32     * @return string single line
33     */
34    function getName() {
35        return $this->name;
36    }
37
38    /**
39     * Get a short description of the config file to show in the admin menu.
40     * wiki text is allowed.
41     *
42     * @return string multi line
43     */
44    function getDescription() {
45        return $this->description;
46    }
47
48    /**
49     * get all paths to config file (local or protected).
50     * this is used to generate the config id and warnings if the files are not writeable.
51     *
52     * @return array
53     */
54    function getPaths() {
55        return [$this->configFile];
56    }
57
58    /**
59     * Display the config file in some html view.
60     * You have to provide input elements for values.
61     * They will be embedded in a form to save changes.
62     *
63     * @return void
64     */
65    function display() {
66        $local = confToHash($this->configFile);
67        $default = [];
68        $configs = $local;
69        uksort($configs, [$this->helper, '_sortHuman']);
70        if(!is_writable($this->configFile)) msg($this->helper->getLang('warning:not writable'),-1);
71
72        include DOKU_PLUGIN . 'confmanager/tpl/showConfigTwoLine.php';
73    }
74
75    /**
76     * this method can fetch the information from the fields generated in display().
77     * it has to handle the correct writing process.
78     *
79     * @return void
80     */
81    function save() {
82        global $INPUT;
83        $keys = $INPUT->arr('keys');
84        $values = $INPUT->arr('values');
85        $newKey = $INPUT->arr('newKey');
86        $newValue = $INPUT->arr('newValue');
87
88        if (count($keys) !== count($values) || count($newKey) !== count($newValue)) {
89            msg($this->helper->getLang('invalid save arguments'), -1);
90            return;
91        }
92
93        $lines = [];
94        if (!empty($keys)) {
95            $lines = array_combine($keys, $values);
96        }
97        if (!empty($newKey)) {
98            $lines = array_merge($lines, array_combine($newKey, $newValue));
99        }
100
101        uksort($lines, [$this->helper, '_sortConf']);
102
103        $content = '';
104        foreach ($lines as $key => $value) {
105            $key = $this->helper->prepareEntity($key);
106            $value = $this->helper->prepareEntity($value);
107
108            $content .= "$key\t$value\n";
109        }
110
111        $this->helper->saveFile($this->configFile, $content);
112    }
113}
114