1<?php
2/**
3 * Text Config (conf/*.conf) Manager admin plugin
4 * http://wiki.splitbrain.org/plugin:txtconf
5 *
6 * Thanks to everyone who provided so many examples of plugin or
7 * contributed to the plugin tutorials... This was of great help!
8 * Special thanks to Christopher Smith for his config plugin on
9 * which this one is based.
10 * If you see part of your code here and I forgot to give credits
11 * for your work, please let me know so I could add your name.
12 *
13 * TODO:
14 * - Remove cache for pages affected by conf modif
15 * - Add option to download smileys and interwiki icons
16 * - Add option to allow edition of "non local files"
17 * - ?Add option to edit plugin:explain as well?
18 *
19 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
20 * @author     Stephane Chamberland <stephane.chamberland@ec.gc.ca>
21 * @date       2006-06-28
22 */
23
24if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
25if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
26require_once(DOKU_PLUGIN.'admin.php');
27
28if(!defined('PLUGIN_NAME')) define('PLUGIN_NAME','txtconf');
29
30if(!defined('DOKU_CONF')) define('DOKU_CONF',DOKU_INC.'conf/');
31require_once(DOKU_INC.'inc/confutils.php');
32
33define('PLUGIN_TXTCONF',dirname(__FILE__).'/');
34require_once(PLUGIN_TXTCONF.'config0.class.php'); // base configuration class and generic settings classes from Chris Smith
35require_once(PLUGIN_TXTCONF.'txtconfig.class.php'); // Text config class
36
37/**
38 * All DokuWiki plugins to extend the admin function
39 * need to inherit from this class
40 */
41class admin_plugin_txtconf extends DokuWiki_Admin_Plugin {
42
43    var $_myconf = null;
44    var $_conftype = null;
45    var $_input =  null;
46    var $_changed = false;
47
48    var $_conf_type_list = array('mime','acronyms','smileys','entities','interwiki');
49
50    /**
51     * return some info
52     */
53    function getInfo(){
54        return array(
55            'author' => 'Stephane Chamberland ',
56            'email'  => 'stephane.chamberland@ec.gc.ca',
57            'date'   => '2006-06-09',
58            'name'   => $this->getLang('menu'),
59            'desc'   => $this->getLang('desc'),
60            'url'    => 'http://wiki.splitbrain.org/plugin:'.PLUGIN_NAME,
61        );
62    }
63
64    /**
65     * return sort order for position in admin menu
66     */
67    function getMenuSort() {
68        return 101;
69    }
70
71    /**
72     * handle user request
73     */
74    function handle() {
75        global $ID;
76        global $lang;
77
78        if (isset($_POST['conftype'])) {
79            $this->_myconf = new txtconfiguration(PLUGIN_TXTCONF.'conf/'.$_POST['conftype'].'.metadata.php');
80            $this->_conftype = $_POST['conftype'];
81            //if (!isset($_POST['txtconf'])) {
82            //    ptnl('Ask for confirmation');
83            //}
84            // don't go any further if the configuration is locked
85            if ($this->_myconf->_locked) return True;
86        }
87
88        if (isset($_POST['submit'])) {
89            if ($_POST['submit'] == $lang['btn_save']) {
90                $this->_input = $_REQUEST['config'];
91                while (list($key) = each($this->_myconf->setting)) {
92                    $input = isset($this->_input[$key]) ? $this->_input[$key] : NULL;
93                    if ($input == '') $input = null;
94                    if ($this->_myconf->setting[$key]->update($input))
95                        $this->_changed = true;
96                    //if ($this->_myconf->setting[$key]->error())
97                    //    $this->_error = true;
98                }
99            }
100
101            if ($_POST['submit'] == $this->getLang('btn_addnew')) {
102                //TODO: check if it already exists
103                if( $this->_myconf->add_local_setting($_POST['newkey'],$_POST['newval'])) $this->_changed = true;
104            }
105
106            if ($this->_changed  && !$this->_error) {
107                if ($this->_myconf->save_settings(PLUGIN_NAME)) {
108                    msg('Successfully Saved '.$this->_conftype,1);
109                } else {
110                    msg('Problem Saving '.$this->_conftype,-1);
111                }
112            }
113        }
114        return True;
115    }
116
117    /**
118     * output appropriate html
119     */
120
121    function _print_open_form() {
122        global $ID;
123        ptln('<div id="'.PLUGIN_NAME.'__manager">');
124        ptln('<form action="'.wl($ID).'" method="post">');
125        ptln('  <input type="hidden" name="do"     value="admin" />');
126        ptln('  <input type="hidden" name="page"   value="'.PLUGIN_NAME.'" />');
127        ptln('  <input type="hidden" name="id"     value="'.$ID.'" />');
128    }
129
130    function _print_close_form() {
131        ptln('</form>');
132        ptln('</div>');
133    }
134
135    function _print_choose_conf_form($msg='') {
136        global $ID;
137        global $lang;
138
139        if ($msg) {
140            ptln('<p>');
141            if ($msg) ptln($msg);
142            ptln('</p>');
143        }
144        ptln('<p>');
145
146        ptln('<select name="conftype">');
147        foreach ($this->_conf_type_list as $mytype) {
148            if ($this->_conftype && $this->_conftype == $mytype) {
149                ptln('<option value="'.$mytype.'" selected="selected">'.$mytype.'</option>');
150            } else {
151                ptln('<option value="'.$mytype.'">'.$mytype.'</option>');
152            }
153        }
154        ptln('</select>');
155
156        ptln('<input type="submit" name="submit" class="button" value="'.$this->getLang('btn_editThisConf').'" />');
157        ptln('</p>');
158    }
159
160    function _html_choose_conf() {
161        global $ID;
162        global $lang;
163
164        print $this->locale_xhtml('intro');
165
166        $this->_print_open_form();
167
168        $this->_print_choose_conf_form($this->getLang('msg_chooseConf'));
169
170        $this->_print_close_form();
171    }
172
173    function _html_edit_conf() {
174        global $ID;
175        global $lang;
176
177        //print $this->locale_xhtml('intro_'.$this->myconf->type);
178        print $this->locale_xhtml($this->_myconf->_intro);
179
180        ptln('<hr size=80%/>');
181        ptln('<p>'.$this->getLang('msg_howto').'</p>');
182
183        //TODO: Once form is updated/saved, print success/error msg
184
185        $this->_print_open_form();
186
187        ptln('  <input type="hidden" name="txtconf"     value="1" />');
188        ptln('  <input type="hidden" name="conftype"    value="'.$this->_conftype.'" />');
189
190        ptln('<table>');
191        foreach ($this->_myconf->setting as $mysetting) {
192            list($label,$value) = $mysetting->html($this);
193            ptln('<tr>');
194            ptln('<td>'.$label.'</td>');
195            ptln('<td>'.$value.'</td>');
196            ptln('</tr>');
197        }
198        ptln('</table>');
199
200        if (!$this->_myconf->locked) {
201        ptln('<p>');
202        ptln('  <input type="submit" name="submit" class="button" value="'.$lang['btn_save'].'" accesskey="s" />'."&nbsp;\n");
203        ptln('  <input type="reset" class="button" value="'.$lang['btn_reset'].'" />'."\n");
204        ptln('</p>');
205
206        ptln('<hr size=80%/>');
207        ptln('<p>'.$this->getLang('msg_howtoadd').'</p>');
208
209        ptln('<p>');
210        ptln('<input id="newkey" name="newkey" type="text" class="edit" size="10" value="" />');
211        ptln('<input id="newval" name="newval" type="text" class="edit" size="70" value="" /><br />');
212        ptln('  <input type="submit" name="submit" class="button" value="'.$this->getLang('btn_addnew').'" />'."\n");
213        ptln('</p>');
214        } else {
215            ptln('<p>'.$this->getLang('filelocked').'</p>');
216        }
217
218        ptln('<hr width=80%/>');
219        ptln('<p>');
220        $this->_print_choose_conf_form($this->getLang('msg_editThisConf'));
221        ptln('</p>');
222
223        $this->_print_close_form();
224    }
225
226    function html() {
227        if ($this->_myconf) {
228            $this->_html_edit_conf();
229        } else {
230            $this->_html_choose_conf();
231        }
232    }
233}
234