1<?php
2
3/**
4 * Class helper_plugin_confmanager
5 */
6class helper_plugin_confmanager extends DokuWiki_Plugin {
7
8    /**
9     * Get all registered config managers
10     *
11     * @return ConfigManagerConfigType[]
12     */
13    public function getConfigFiles() {
14        static $configs = null;
15        if ($configs === null) {
16            $configs = array();
17            trigger_event('CONFMANAGER_CONFIGFILES_REGISTER', $configs, null, false);
18            usort($configs, array($this, '_sortByConfigName'));
19        }
20        return $configs;
21    }
22
23    /**
24     * Get a specific config manager
25     *
26     * @param string $id Config ID
27     * @return ConfigManagerConfigType
28     */
29    public function getConfigById($id) {
30        foreach ($this->getConfigFiles() as $config) {
31            if ($this->getConfigId($config) === $id) {
32                return $config;
33            }
34        }
35        return false;
36    }
37
38    /**
39     * Get the id of a config manager
40     *
41     * @param ConfigManagerConfigType $config
42     * @return string
43     */
44    public function getConfigId(ConfigManagerConfigType $config) {
45        $hash = '';
46        $paths = $config->getPaths();
47        if (count($paths) == 0) return '';
48
49        $hash .= realpath($paths[0]);
50        return md5($hash);
51    }
52
53    /**
54     * @param string[] $files
55     * @return bool
56     */
57    public function areWriteable($files) {
58        foreach ($files as $file) {
59            if (!is_writable($file)) {
60                return false;
61            }
62        }
63        return true;
64    }
65
66    /**
67     * @param string $file      filename path to file
68     * @param string $content
69     */
70    public function saveFile($file, $content) {
71        $success = io_saveFile($file, $content);
72        if ($success !== false ) {
73            msg($this->getLang('changes applied'), 1);
74        } elseif (!is_writable($file)) {
75            msg($this->getLang('error:saving failed not writable'), -1);
76        } else {
77            msg($this->getLang('error:saving failed'), -1);
78        }
79
80    }
81
82    /**
83     * Get header for config files created by confmanager
84     *
85     * @return string
86     */
87    public function getCoreConfigHeader() {
88        return "# This config was generated by the confmanager plugin.\n"
89             . "# Changes made to this file may be overwritten by the plugin.\n"
90             . "# Please use the confmanager in the Dokuwiki admin interface.\n";
91    }
92
93    /**
94     * @param string $k1
95     * @param string $k2
96     * @return int
97     */
98    public function _sortConf( $k1 , $k2 ) {
99        return strlen( $k2 ) - strlen( $k1 );
100    }
101
102    /**
103     * @param string $k1
104     * @param string $k2
105     * @return int
106     */
107    public function _sortHuman( $k1 , $k2 ) {
108        $k1 = strtolower($k1);
109        $k2 = strtolower($k2);
110        return strnatcmp($k1,$k2);
111    }
112
113    /**
114     * Compare config managers by name
115     *
116     * @param ConfigManagerConfigType $left
117     * @param ConfigManagerConfigType $right
118     * @return int
119     */
120    public function _sortByConfigName(ConfigManagerConfigType $left, ConfigManagerConfigType $right) {
121        return strnatcasecmp($left->getName(), $right->getName());
122    }
123
124    public function tplSaveButton() {
125        static $called = false;
126        if ($called) {
127            return;
128        }
129        include DOKU_PLUGIN . 'confmanager/tpl/formControls.php';
130        $called = true;
131    }
132
133    /**
134     * Prepare entity for saving
135     *
136     * @param string $str
137     * @return string
138     */
139    public function prepareEntity($str) {
140        $str = trim($str);
141        $str = str_replace("\n", '', $str);
142        $str = str_replace("\r", '', $str);
143        $str = str_replace('#', '\\#', $str);
144        return $str;
145    }
146}
147
148