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