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