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