1<?php
2
3use dokuwiki\Extension\ActionPlugin;
4use dokuwiki\Extension\EventHandler;
5use dokuwiki\Extension\Event;
6use dokuwiki\Form\CheckableElement;
7use dokuwiki\Form\HTMLElement;
8use dokuwiki\plugin\structpublish\meta\Revision;
9use dokuwiki\plugin\structpublish\meta\Constants;
10
11class action_plugin_structpublish_revisions extends ActionPlugin
12{
13    public function register(EventHandler $controller)
14    {
15        $controller->register_hook('FORM_REVISIONS_OUTPUT', 'BEFORE', $this, 'handleRevisions');
16    }
17
18    /**
19     * Adds publish info to page revisions
20     *
21     * @param Event $event
22     * @return void
23     */
24    public function handleRevisions(Event $event)
25    {
26        global $INFO;
27
28        /** @var dokuwiki\Form\Form $form */
29        $form = $event->data;
30
31        /** @var helper_plugin_structpublish_db $helper */
32        $helper = plugin_load('helper', 'structpublish_db');
33
34        if (!$helper->isPublishable()) {
35            return;
36        }
37
38        $elCount = $form->elementCount();
39        $checkName = 'rev2[]';
40
41        for ($i = 0; $i < $elCount; $i++) {
42            $el = $form->getElementAt($i);
43
44            if (!$el instanceof CheckableElement && !$el instanceof HTMLElement) {
45                continue;
46            }
47
48            // extract rev from checkbox info
49            if (is_a($el, CheckableElement::class)) {
50                if ($el->attr('name') === $checkName) {
51                    $rev = $el->attr('value');
52                }
53            }
54
55            // get most recent status for rev
56            $revision = new Revision($INFO['id'], $rev);
57            $status = $revision->getStatus();
58            $version = $revision->getVersion();
59
60            // insert status for published revisions
61            if (
62                is_a($el, HTMLElement::class) &&
63                !empty(trim($el->val())) &&
64                $status === Constants::STATUS_PUBLISHED
65            ) {
66                $val = $el->val();
67                $label = '<span class="plugin-structpublish-version">' .
68                    $status . ' (' . $this->getLang('version') . ' ' . $version . ')</span>';
69                $el->val("$val $label");
70            }
71        }
72    }
73}
74