xref: /plugin/structpublish/action/show.php (revision 6d5e23d337b220c300f99c3da9b4ba6e2514c2c1)
1<?php
2
3use dokuwiki\plugin\structpublish\meta\Constants;
4use dokuwiki\plugin\structpublish\meta\Revision;
5
6class action_plugin_structpublish_show extends DokuWiki_Action_Plugin
7{
8    /** @inheritDoc */
9    public function register(Doku_Event_Handler $controller)
10    {
11        $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'handleShow');
12    }
13
14    /**
15     * Decide which revision to show based on role assignments
16     *
17     * @param Doku_Event $event
18     * @return void
19     */
20    public function handleShow(Doku_Event $event)
21    {
22        if ($event->data != 'show') {
23            return;
24        }
25
26        global $ID;
27        global $REV;
28        global $INFO;
29
30        /** @var helper_plugin_structpublish_db $dbHelper */
31        $dbHelper = plugin_load('helper', 'structpublish_db');
32
33        if (!$dbHelper->isPublishable()) {
34            return;
35        }
36
37        $currentRevision = new Revision($dbHelper->getDB(), $ID, $REV ?: $INFO['currentrev']);
38
39        /** @var action_plugin_structpublish_sqlitefunction $functions */
40        $functions = plugin_load('action', 'structpublish_sqlitefunction');
41        if (
42            $currentRevision->getStatus() !== Constants::STATUS_PUBLISHED
43            && !$functions->IS_PUBLISHER($ID)
44        ) {
45            $latestPublishedRev = $currentRevision->getLatestPublishedRevision()->getRev();
46            if (!$latestPublishedRev) {
47                $event->data = 'denied';
48                // FIXME we could add our own action to display a custom message instead of standard denied action
49            }
50
51            $REV = $latestPublishedRev;
52            $INFO['rev'] = $latestPublishedRev;
53        }
54    }
55}
56