1<?php
2
3use dokuwiki\Extension\ActionPlugin;
4use dokuwiki\Extension\EventHandler;
5use dokuwiki\Extension\Event;
6use dokuwiki\plugin\structpublish\meta\Constants;
7
8class action_plugin_structpublish_publish extends ActionPlugin
9{
10    /** @inheritDoc */
11    public function register(EventHandler $controller)
12    {
13        $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'changeStatus');
14    }
15
16    /**
17     * Handle the publish button and version field
18     *
19     * @param Event $event
20     * @return void
21     * @throws Exception
22     */
23    public function changeStatus(Event $event)
24    {
25        if ($event->data != 'show') {
26            return;
27        }
28
29        global $INPUT;
30
31        $in = $INPUT->arr('structpublish');
32        $action = key($in);
33        if (!$action || !in_array($action, [Constants::ACTION_PUBLISH, Constants::ACTION_APPROVE])) {
34            return;
35        }
36
37        if (!checkSecurityToken()) return;
38
39        /** @var helper_plugin_structpublish_publish $helper */
40        $helper = plugin_load('helper', 'structpublish_publish');
41        $newRevision = $helper->saveRevision(key($in), $INPUT->str('version'));
42
43        /** @var helper_plugin_structpublish_notify $notifyHelper */
44        $notifyHelper  = plugin_load('helper', 'structpublish_notify');
45        $notifyHelper->sendEmails($action, $newRevision);
46    }
47}
48