1<?php
2
3use dokuwiki\Extension\ActionPlugin;
4use dokuwiki\Extension\Event;
5use dokuwiki\Extension\EventHandler;
6use dokuwiki\Form\Form;
7use dokuwiki\Form\InputElement;
8use dokuwiki\plugin\wordimport\docx\DocX;
9
10/**
11 * DokuWiki Plugin wordimport (Action Component)
12 *
13 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
14 * @author Andreas Gohr <dokuwiki@cosmocode.de>
15 */
16class action_plugin_wordimport_ui extends ActionPlugin
17{
18    /** @inheritDoc */
19    public function register(EventHandler $controller)
20    {
21        $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'handleActionActPreprocess');
22        $controller->register_hook('TPL_ACT_UNKNOWN', 'BEFORE', $this, 'handleTplActUnknown');
23    }
24
25    /**
26     * Event handler for ACTION_ACT_PREPROCESS
27     *
28     * @see https://www.dokuwiki.org/devel:events:ACTION_ACT_PREPROCESS
29     * @param Event $event Event object
30     * @param mixed $param optional parameter passed when event was registered
31     * @return void
32     */
33    public function handleActionActPreprocess(Event $event, $param)
34    {
35        $act = act_clean($event->data);
36        if ($act !== 'wordimport') return;
37
38
39        global $ID;
40
41        if (
42            isset($_FILES['file']['tmp_name']) &&
43            is_uploaded_file($_FILES['file']['tmp_name']) &&
44            checkSecurityToken()
45        ) {
46            try {
47                $this->import($_FILES['file']['tmp_name'], $ID);
48                $event->data = 'show'; // change back to normal display
49                msg($this->getLang('imported'), 1);
50                return;
51            } catch (Exception $e) {
52                msg(hsc($e->getMessage()), -1);
53            }
54        }
55
56        $event->preventDefault();
57    }
58
59    /**
60     * Event handler for TPL_ACT_UNKNOWN
61     *
62     * @see https://www.dokuwiki.org/devel:events:tpl_act_unknown
63     * @param Event $event Event object
64     * @param mixed $param optional parameter passed when event was registered
65     * @return void
66     */
67    public function handleTplActUnknown(Event $event, $param)
68    {
69        if ($event->data !== 'wordimport') return;
70        $event->preventDefault();
71        $this->html();
72    }
73
74    /**
75     * Import a .docx file into a DokuWiki page
76     *
77     * @param string $file Path to the .docx file
78     * @param string $page The page to import to
79     * @return void
80     * @throws Exception
81     */
82    public function import($file, $page)
83    {
84        $this->loadConfig();
85        $docx = new DocX($file, $this->conf);
86        $docx->import($page);
87    }
88
89    /**
90     * Output the upload form
91     *
92     * @return void
93     */
94    public function html()
95    {
96        global $ID;
97        $form = new Form(['class' => 'plugin_wordimport', 'enctype' => 'multipart/form-data', 'method' => 'post']);
98        $form->addHTML($this->locale_xhtml('intro'));
99
100        $form->addFieldsetOpen();
101
102        $upload = new InputElement('file', 'file', $this->getLang('uploadfield'));
103        $upload->attr('accept', '.docx');
104
105        $form->addElement($upload);
106
107        $form->addTagOpen('p')->addClass('buttons');
108        $form->addButton('do[wordimport]', sprintf($this->getLang('btn_import'), $ID));
109        $form->addButton('do[show]', $this->getLang('btn_cancel'));
110        $form->addTagClose('p');
111        $form->addFieldsetClose();
112        echo $form->toHTML();
113    }
114}
115