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