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