xref: /plugin/fontello/admin.php (revision 95357802f68767bcd0367676ca97b384fa2109c6)
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 (
58            !isset($_FILES['fontellozip']) ||
59            ($_FILES['fontellozip']['error'] ?? UPLOAD_ERR_NO_FILE) === UPLOAD_ERR_NO_FILE
60        ) {
61            return;
62        }
63
64        if (!checkSecurityToken()) {
65            msg($this->getLang('err_bad_token'), -1);
66            return;
67        }
68
69        try {
70            $this->helper->importPackage($_FILES['fontellozip']);
71            msg($this->getLang('upload_ok'), 1);
72            send_redirect(wl($ID, ['do' => 'admin', 'page' => 'fontello'], true, '&'));
73        } catch (RuntimeException $exception) {
74            msg(hsc($exception->getMessage()), -1);
75        }
76    }
77
78    /**
79     * Output the admin page.
80     *
81     * @return void
82     */
83    public function html()
84    {
85        global $lang;
86
87        $package = $this->helper->getPackageInfo();
88
89        echo '<h1>' . hsc($this->getLang('menu')) . '</h1>';
90        echo $this->locale_xhtml('intro');
91
92        echo '<form action="" method="post" enctype="multipart/form-data"><div class="no">';
93        formSecurityToken();
94        echo '<p>';
95        echo '<label for="fontellozip">' . hsc($this->getLang('label_zip')) . '</label><br />';
96        echo '<input type="file" name="fontellozip" id="fontellozip" accept=".zip,application/zip" /> ';
97        echo '<button type="submit">' . hsc($lang['btn_upload']) . '</button>';
98        echo '</p>';
99        echo '</div></form>';
100
101        if ($package === null) {
102            echo '<p>' . hsc($this->getLang('no_package')) . '</p>';
103            return;
104        }
105
106        echo '<div class="table"><table class="inline"><tbody>';
107        echo '<tr><th>' . hsc($this->getLang('current_zip')) . '</th><td>' . hsc($package['zip_name']) . '</td></tr>';
108        echo '<tr><th>' . hsc($this->getLang('current_prefix')) . '</th><td>' . hsc($package['prefix']) . '</td></tr>';
109        echo '<tr><th>' . hsc($this->getLang('current_count')) . '</th><td>' .
110            (int) $package['icon_count'] . '</td></tr>';
111        echo '<tr><th>' . hsc($this->getLang('current_enabled')) . '</th><td>' .
112            (int) $package['enabled_count'] . '</td></tr>';
113        echo '<tr><th>' . hsc($this->getLang('current_imported')) . '</th><td>' .
114            hsc($package['imported_at'] ? $package['imported_at'] : $this->getLang('unknown')) . '</td></tr>';
115        echo '</tbody></table></div>';
116
117        echo '<h2>' . hsc($this->getLang('icons_heading')) . '</h2>';
118        echo '<form action="" method="post"><div class="no">';
119        formSecurityToken();
120        echo '<input type="hidden" name="fontello_action" value="save_enabled" />';
121        echo '<div class="table"><table class="inline"><thead><tr>';
122        echo '<th>' . hsc($this->getLang('icon_enabled')) . '</th>';
123        echo '<th>' . hsc($this->getLang('icon_name')) . '</th>';
124        echo '<th>' . hsc($this->getLang('icon_class')) . '</th>';
125        echo '<th>' . hsc($this->getLang('icon_preview')) . '</th>';
126        echo '</tr></thead><tbody>';
127
128        foreach ($package['icons'] as $icon) {
129            $fieldId = 'fontello-enabled-' . preg_replace('/[^A-Za-z0-9_-]/', '-', $icon['name']);
130            echo '<tr>';
131            echo '<td><input type="checkbox" name="enabled_icons[]" id="' . hsc($fieldId) . '" value="' .
132                hsc($icon['name']) . '"' . (!empty($icon['enabled']) ? ' checked="checked"' : '') . ' /></td>';
133            echo '<td><label for="' . hsc($fieldId) . '"><code>' . hsc($icon['name']) . '</code></label></td>';
134            echo '<td><code>' . hsc($icon['class']) . '</code></td>';
135            echo '<td><span class="fontello-icon ' . hsc($icon['class']) . '" aria-hidden="true"></span></td>';
136            echo '</tr>';
137        }
138
139        echo '</tbody></table></div>';
140        echo '<p><button type="submit">' . hsc($this->getLang('save_activation')) . '</button></p>';
141        echo '</div></form>';
142    }
143}
144