1<?php
2
3/**
4 * Every line is a config option. The first word in the line is a config key .
5 * The config is a associative array. I.e. the acronyms or entities config
6 */
7class ConfigManagerTwoLineCascadeConfig extends ConfigManagerAbstractCascadeConfig {
8
9    /**
10     * Load file
11     *
12     * @param string $fileName
13     * @return array
14     */
15    protected function loadFile($fileName) {
16        return confToHash($fileName);
17    }
18
19    public function display() {
20        $configs = $this->readConfig();
21        $default = $configs['default'];
22        $local = $configs['local'];
23        $configs = array_merge($default, $local);
24
25        uksort($configs, [$this->helper, '_sortHuman']);
26        include DOKU_PLUGIN . 'confmanager/tpl/showConfigTwoLine.php';
27    }
28
29    public function save() {
30        global $INPUT;
31        $config = $this->readConfig();
32        $keys = $INPUT->arr('keys');
33        $values = $INPUT->arr('values');
34        if (count($keys) !== count($values)) {
35            msg($this->helper->getLang('invalid save arguments'), -1);
36        }
37
38        if (empty($keys)) {
39            $lines = [];
40        } else {
41            $lines = array_combine($keys, $values);
42        }
43
44        $lines = array_merge($lines, $this->getNewValues());
45
46        $custom = $this->getCustomEntries($lines, $config['default']);
47
48        $this->saveToFile($custom);
49        $this->handleSave($config['default']);
50    }
51
52    protected function handleSave() {}
53
54    /**
55     * Get the custom entries from the input
56     *
57     * @param array $input
58     * @param array $default
59     * @return array
60     */
61    private function getCustomEntries($input, $default) {
62        $save = [];
63        foreach ($input as $key => $value) {
64
65            if (array_key_exists($key, $default)) {
66                if ($default[$key] === $value) {
67                    continue;
68                }
69            }
70
71            $key = $this->prepareEntity($key);
72            $value = $this->prepareEntity($value);
73            if ($key === '') {
74                continue;
75            }
76            $save[$key] = $value;
77        }
78
79        return $save;
80    }
81
82    /**
83     * @param $config
84     */
85    protected function saveToFile($config) {
86        global $config_cascade;
87        if (!isset($config_cascade[$this->internalName]['local'])
88            || count($config_cascade[$this->internalName]['local']) === 0) {
89            msg($this->helper->getLang('no local file given'),-1);
90            return;
91        }
92
93        $file = $config_cascade[$this->internalName]['local'][0];
94
95        if (empty($config)) {
96            if (!@unlink($file)) {
97                msg($this->helper->getLang('cannot apply changes'), -1);
98                return;
99            }
100            msg($this->helper->getLang('changes applied'), 1);
101            return;
102        }
103
104        uksort($config, [$this->helper, '_sortConf']);
105        $content = $this->helper->getCoreConfigHeader();
106        foreach ($config as $key => $value) {
107            $content .= "$key\t$value\n";
108        }
109
110        $this->helper->saveFile($file, $content);
111    }
112
113    /**
114     * Returns new values as associative array
115     *
116     * @return array
117     */
118    private function getNewValues() {
119        global $INPUT;
120        $newKey = $INPUT->arr('newKey');
121        $newValue = $INPUT->arr('newValue');
122        if (count($newKey) !== count($newValue)) {
123            return [];
124        }
125
126        return array_combine($newKey, $newValue);
127    }
128}
129