xref: /plugin/structpublish/action/save.php (revision 97a87056b0ba063a39712885fe4846032d10ff1a)
1<?php
2
3use dokuwiki\plugin\struct\meta\StructException;
4use dokuwiki\plugin\structpublish\meta\Constants;
5use dokuwiki\plugin\structpublish\meta\Revision;
6
7/**
8 * Action component to handle page save
9 */
10class action_plugin_structpublish_save extends DokuWiki_Action_Plugin
11{
12    /** @inheritDoc */
13    public function register(Doku_Event_Handler $controller)
14    {
15        $controller->register_hook('COMMON_WIKIPAGE_SAVE', 'AFTER', $this, 'handleSave');
16    }
17
18    /**
19     * Handle the page save event to store revision meta data
20     *
21     * @param Doku_Event $event
22     * @return void
23     */
24    public function handleSave(Doku_Event $event)
25    {
26        /** @var helper_plugin_structpublish_db $dbHelper */
27        $dbHelper = plugin_load('helper', 'structpublish_db');
28
29        $id = $event->data['id'];
30
31        if (!$dbHelper->isPublishable()) {
32            return;
33        }
34
35        $revision = new Revision($dbHelper->getDB(), $id, $event->data['newRevision']);
36        $revision->setStatus(Constants::STATUS_DRAFT);
37
38        try {
39            $revision->save();
40        } catch(StructException $e) {
41            msg($e->getMessage(), -1);
42        }
43    }
44}
45