1<?php
2
3/**
4 * Every line is a config option. The config values are basically an array.
5 * I.e. the scheme or wordblock config.
6 */
7class ConfigManagerSingleLineCoreConfig extends ConfigManagerAbstractCascadeConfig {
8
9    /**
10     * Load file
11     *
12     * @param string $fileName
13     * @return array
14     */
15    protected  function loadFile($fileName) {
16        if (@!file_exists($fileName)) {
17            return [];
18        }
19        $config = file($fileName);
20        $config = array_map('trim', $config);
21        $config = preg_replace('/^#.*/', '', $config);
22        $config = str_replace('\\#', '#', $config);
23        return array_filter($config);
24    }
25
26    public function display() {
27        $configs = $this->readConfig();
28        $default = $configs['default'];
29        $local = $configs['local'];
30        $configs = array_merge($default, $local);
31
32        usort($configs, [$this->helper, '_sortHuman']);
33        include DOKU_PLUGIN . 'confmanager/tpl/showConfigSingleLine.php';
34    }
35
36    public function save() {
37        global $INPUT;
38        $lines = $INPUT->arr('line');
39        $config = $this->readConfig();
40        $custom = $this->getCustomEntries($lines, $config['default']);
41
42        $this->saveToFile($custom);
43    }
44
45    /**
46     * Get the custom entries from the input
47     *
48     * @param array $input
49     * @param array $default
50     * @return array
51     */
52    private function getCustomEntries($input, $default) {
53        $save = [];
54        foreach ($input as $line) {
55            if (in_array($line, $default)) {
56                continue;
57            }
58            $line = $this->prepareEntity($line);
59            if ($line === '') {
60                continue;
61            }
62
63            $save[] = $line;
64        }
65
66        return $save;
67    }
68
69    /**
70     * Save config
71     *
72     * @param array $config
73     */
74    private function saveToFile($config) {
75        global $config_cascade;
76        if (!isset($config_cascade[$this->internalName]['local'])
77            || count($config_cascade[$this->internalName]['local']) === 0) {
78            msg($this->helper->getLang('no local file given'),-1);
79            return;
80        }
81
82        $file = $config_cascade[$this->internalName]['local'][0];
83
84        if (empty($config)) {
85            if (!@unlink($file)) {
86                msg($this->helper->getLang('cannot apply changes'), -1);
87                return;
88            }
89            msg($this->helper->getLang('changes applied'), 1);
90            return;
91        }
92
93        uksort($config, [$this->helper, '_sortConf']);
94        $content = $this->helper->getCoreConfigHeader();
95        foreach ($config as $item) {
96            $content .= "$item\n";
97        }
98
99        $this->helper->saveFile($file, $content);
100    }
101
102
103}
104