xref: /plugin/fontello/admin.php (revision 1776c5c5befa8de3cc97e981444f6918b129ab85)
1<?php
2
3use dokuwiki\Extension\AdminPlugin;
4
5/**
6 * Admin component for importing Fontello ZIP packages.
7 */
8class admin_plugin_fontello extends AdminPlugin
9{
10    /** @var helper_plugin_fontello */
11    protected $helper;
12
13    public function __construct()
14    {
15        $this->helper = $this->loadHelper('fontello');
16    }
17
18    /**
19     * return sort order for position in admin menu
20     *
21     * @return int
22     */
23    public function getMenuSort()
24    {
25        return 300;
26    }
27
28    /**
29     * Handle ZIP uploads.
30     *
31     * @return void
32     */
33    public function handle()
34    {
35        global $ID;
36
37        if (($_POST['fontello_action'] ?? '') === 'save_enabled') {
38            if (!checkSecurityToken()) {
39                msg($this->getLang('err_bad_token'), -1);
40                return;
41            }
42
43            $enabledIcons = $_POST['enabled_icons'] ?? [];
44            if (!is_array($enabledIcons)) $enabledIcons = [];
45
46            try {
47                $this->helper->saveEnabledIconNames($enabledIcons);
48                msg($this->getLang('activation_ok'), 1);
49                send_redirect(wl($ID, ['do' => 'admin', 'page' => 'fontello'], true, '&'));
50            } catch (RuntimeException $exception) {
51                msg(hsc($exception->getMessage()), -1);
52            }
53
54            return;
55        }
56
57        if (!isset($_FILES['fontellozip']) || ($_FILES['fontellozip']['error'] ?? UPLOAD_ERR_NO_FILE) === UPLOAD_ERR_NO_FILE) {
58            return;
59        }
60
61        if (!checkSecurityToken()) {
62            msg($this->getLang('err_bad_token'), -1);
63            return;
64        }
65
66        try {
67            $this->helper->importPackage($_FILES['fontellozip']);
68            msg($this->getLang('upload_ok'), 1);
69            send_redirect(wl($ID, ['do' => 'admin', 'page' => 'fontello'], true, '&'));
70        } catch (RuntimeException $exception) {
71            msg(hsc($exception->getMessage()), -1);
72        }
73    }
74
75    /**
76     * Output the admin page.
77     *
78     * @return void
79     */
80    public function html()
81    {
82        global $lang;
83
84        $package = $this->helper->getPackageInfo();
85
86        echo '<h1>' . hsc($this->getLang('menu')) . '</h1>';
87        echo $this->locale_xhtml('intro');
88
89        echo '<form action="" method="post" enctype="multipart/form-data"><div class="no">';
90        formSecurityToken();
91        echo '<p>';
92        echo '<label for="fontellozip">' . hsc($this->getLang('label_zip')) . '</label><br />';
93        echo '<input type="file" name="fontellozip" id="fontellozip" accept=".zip,application/zip" /> ';
94        echo '<button type="submit">' . hsc($lang['btn_upload']) . '</button>';
95        echo '</p>';
96        echo '</div></form>';
97
98        if ($package === null) {
99            echo '<p>' . hsc($this->getLang('no_package')) . '</p>';
100            return;
101        }
102
103        echo '<div class="table"><table class="inline"><tbody>';
104        echo '<tr><th>' . hsc($this->getLang('current_zip')) . '</th><td>' . hsc($package['zip_name']) . '</td></tr>';
105        echo '<tr><th>' . hsc($this->getLang('current_prefix')) . '</th><td>' . hsc($package['prefix']) . '</td></tr>';
106        echo '<tr><th>' . hsc($this->getLang('current_count')) . '</th><td>' . (int) $package['icon_count'] . '</td></tr>';
107        echo '<tr><th>' . hsc($this->getLang('current_enabled')) . '</th><td>' . (int) $package['enabled_count'] . '</td></tr>';
108        echo '<tr><th>' . hsc($this->getLang('current_imported')) . '</th><td>' .
109            hsc($package['imported_at'] ? $package['imported_at'] : $this->getLang('unknown')) . '</td></tr>';
110        echo '</tbody></table></div>';
111
112        echo '<h2>' . hsc($this->getLang('icons_heading')) . '</h2>';
113        echo '<form action="" method="post"><div class="no">';
114        formSecurityToken();
115        echo '<input type="hidden" name="fontello_action" value="save_enabled" />';
116        echo '<div class="table"><table class="inline"><thead><tr>';
117        echo '<th>' . hsc($this->getLang('icon_enabled')) . '</th>';
118        echo '<th>' . hsc($this->getLang('icon_name')) . '</th>';
119        echo '<th>' . hsc($this->getLang('icon_class')) . '</th>';
120        echo '<th>' . hsc($this->getLang('icon_preview')) . '</th>';
121        echo '</tr></thead><tbody>';
122
123        foreach ($package['icons'] as $icon) {
124            $fieldId = 'fontello-enabled-' . preg_replace('/[^A-Za-z0-9_-]/', '-', $icon['name']);
125            echo '<tr>';
126            echo '<td><input type="checkbox" name="enabled_icons[]" id="' . hsc($fieldId) . '" value="' .
127                hsc($icon['name']) . '"' . (!empty($icon['enabled']) ? ' checked="checked"' : '') . ' /></td>';
128            echo '<td><label for="' . hsc($fieldId) . '"><code>' . hsc($icon['name']) . '</code></label></td>';
129            echo '<td><code>' . hsc($icon['class']) . '</code></td>';
130            echo '<td><span class="fontello-icon ' . hsc($icon['class']) . '" aria-hidden="true"></span></td>';
131            echo '</tr>';
132        }
133
134        echo '</tbody></table></div>';
135        echo '<p><button type="submit">' . hsc($this->getLang('save_activation')) . '</button></p>';
136        echo '</div></form>';
137    }
138}
139