1<?php
2/**
3 * DokuWiki Plugin dataau (Admin Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Andreas Gohr <gohr@cosmocode.de>
7 */
8
9// must be run within Dokuwiki
10if (!defined('DOKU_INC')) die();
11
12/**
13 * Administration form for configuring the type aliases
14 */
15class admin_plugin_dataau_aliases extends DokuWiki_Admin_Plugin {
16
17    /**
18     * will hold the dataau helper plugin
19     * @var helper_plugin_data
20     */
21    protected $dthlp = null;
22
23    /**
24     * Constructor. Load helper plugin
25     */
26    public function __construct(){
27        $this->dthlp = plugin_load('helper', 'dataau');
28    }
29
30    /**
31     * Determine position in list in admin window
32     * Lower values are sorted up
33     *
34     * @return int
35     */
36    public function getMenuSort() {
37        return 501;
38    }
39
40    /**
41     * Return true for access only by admins (config:superuser) or false if managers are allowed as well
42     *
43     * @return bool
44     */
45    public function forAdminOnly() {
46        return true;
47    }
48
49    /**
50     * Return the text that is displayed at the main admin menu
51     *
52     * @param string $language lang code
53     * @return string menu string
54     */
55    public function getMenuText($language) {
56        return $this->getLang('menu_alias');
57    }
58
59    /**
60     * Carry out required processing
61     */
62    public function handle() {
63        if(!is_array($_REQUEST['d']) || !checkSecurityToken()) return;
64
65        $sqlite = $this->dthlp->_getDB();
66        if(!$sqlite) return;
67
68        $sqlite->query("BEGIN TRANSACTION");
69        if (!$sqlite->query("DELETE FROM aliases")) {
70            $sqlite->query('ROLLBACK TRANSACTION');
71            return;
72        }
73        foreach($_REQUEST['d'] as $row){
74            $row = array_map('trim',$row);
75            $row['name'] = utf8_strtolower($row['name']);
76            $row['name'] = rtrim($row['name'],'s');
77            if(!$row['name']) continue;
78
79            // Clean enum
80            $arr = preg_split('/\s*,\s*/', $row['enum']);
81            $arr = array_unique($arr);
82            $row['enum'] = implode(', ', $arr);
83
84            if (!$sqlite->query("INSERT INTO aliases (name, type, prefix, postfix, enum)
85                                 VALUES (?,?,?,?,?)",$row)) {
86                $sqlite->query('ROLLBACK TRANSACTION');
87                return;
88            }
89        }
90        $sqlite->query("COMMIT TRANSACTION");
91    }
92
93    /**
94     * Output html of the admin page
95     */
96    public function html() {
97        $sqlite = $this->dthlp->_getDB();
98        if(!$sqlite) return;
99
100        echo $this->locale_xhtml('admin_intro');
101
102        $sql = "SELECT * FROM aliases ORDER BY name";
103        $res = $sqlite->query($sql);
104        $rows = $sqlite->res2arr($res);
105
106        $form = new Doku_Form(array('method'=>'post'));
107        $form->addHidden('page','dataau_aliases');
108        $form->addElement(
109            '<table class="inline">'.
110            '<tr>'.
111            '<th>'.$this->getLang('name').'</th>'.
112            '<th>'.$this->getLang('type').'</th>'.
113            '<th>'.$this->getLang('prefix').'</th>'.
114            '<th>'.$this->getLang('postfix').'</th>'.
115            '<th>'.$this->getLang('enum').'</th>'.
116            '</tr>'
117        );
118
119        // add empty row for adding a new entry
120        $rows[] = array('name'=>'','type'=>'','prefix'=>'','postfix'=>'','enum'=>'');
121
122        $cur = 0;
123        foreach($rows as $row){
124            $form->addElement('<tr>');
125
126            $form->addElement('<td>');
127            $form->addElement(form_makeTextField('d['.$cur.'][name]',$row['name'],''));
128            $form->addElement('</td>');
129
130            $form->addElement('<td>');
131            $form->addElement(form_makeMenuField(
132                                  'd['.$cur.'][type]',
133                                  array('','page','title','mail','url', 'dt', 'wiki','tag', 'hidden', 'img'),//'nspage' don't support post/prefixes
134                                  $row['type'],''
135                              ));
136            $form->addElement('</td>');
137
138            $form->addElement('<td>');
139            $form->addElement(form_makeTextField('d['.$cur.'][prefix]',$row['prefix'],''));
140            $form->addElement('</td>');
141
142            $form->addElement('<td>');
143            $form->addElement(form_makeTextField('d['.$cur.'][postfix]',$row['postfix'],''));
144            $form->addElement('</td>');
145
146            $form->addElement('<td>');
147            $form->addElement(form_makeTextField('d['.$cur.'][enum]',$row['enum'],''));
148            $form->addElement('</td>');
149
150            $form->addElement('</tr>');
151
152            $cur++;
153        }
154
155        $form->addElement('</table>');
156        $form->addElement(form_makeButton('submit','admin',$this->getLang('submit')));
157        $form->printForm();
158    }
159
160}
161
162// vim:ts=4:sw=4:et:enc=utf-8:
163