1<?php
2
3/**
4 * Handle deleting of icons for config managers with uploadable content (implementation of ConfigManagerUploadable)
5 */
6class action_plugin_confmanager_deleteicon extends DokuWiki_Action_Plugin {
7
8    /**
9     * @var helper_plugin_confmanager
10     */
11    var $helper;
12
13    /**
14     * Registers a callback function for a given event
15     *
16     * @param Doku_Event_Handler $controller
17     */
18    public function register(Doku_Event_Handler $controller) {
19        $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE',  $this, 'upload', []);
20        $this->helper = plugin_load('helper', 'confmanager');
21    }
22
23    /**
24     * Tries to delete an icon
25     *
26     * @param Doku_Event $event
27     * @param $param
28     */
29    public function upload(Doku_Event $event, $param) {
30        if ($event->data !== 'confmanager_deleteIcon') {
31            return;
32        }
33
34        $event->preventDefault();
35        $event->stopPropagation();
36        if (!auth_isadmin()) {
37            header('HTTP/1.1 403 Forbidden');
38            header('Content-Type: text/plain');
39            echo $this->getLang('upload_errNoAdmin');
40            return;
41        }
42
43        $config = $this->getConfig();
44        if ($config === false) {
45            header('HTTP/1.1 405 Method Not Allowed');
46            header('Content-Type: text/plain');
47            echo $this->getLang('upload_errNoConfig');
48            return;
49        }
50
51        if (!$config->deleteIcon()) {
52            header('HTTP/1.1 400 Bad Request');
53            return;
54        }
55        echo '1';
56    }
57
58    /**
59     * Get an config manager in case of uploadable content
60     *
61     * @return bool|ConfigManagerUploadable
62     */
63    private function getConfig() {
64        global $INPUT;
65        $configId = $INPUT->str('configId', null, true);
66        if ($configId === null) {
67            return false;
68        }
69
70        $config = $this->helper->getConfigById($configId);
71        if (!$config) {
72            return false;
73        }
74
75        if (!($config instanceof ConfigManagerUploadable)) {
76            return false;
77        }
78        return $config;
79    }
80}
81