xref: /plugin/structpublish/action/banner.php (revision 0dcf68caffd50649254218c13bee06c6ca23bcf8)
1<?php
2
3use dokuwiki\plugin\structpublish\meta\Constants;
4use dokuwiki\plugin\structpublish\meta\Revision;
5
6/**
7 * Action component responsible for the publish banner
8 * attached to struct data of a page
9 */
10class action_plugin_structpublish_banner extends DokuWiki_Action_Plugin
11{
12    /** @var \helper_plugin_structpublish_db */
13    protected $dbHelper;
14
15    /**
16     * @inheritDoc
17     */
18    public function register(Doku_Event_Handler $controller)
19    {
20        $controller->register_hook('TPL_ACT_RENDER', 'BEFORE', $this, 'renderBanner');
21    }
22
23    /**
24     * Add banner to pages under structpublish control
25     */
26    public function renderBanner(Doku_Event $event)
27    {
28        global $ID;
29        global $INFO;
30        global $REV;
31
32        if ($event->data !== 'show') return;
33
34        $this->dbHelper = plugin_load('helper', 'structpublish_db');
35
36        if (!$this->dbHelper->isPublishable()) return;
37
38        $revision = new Revision($this->dbHelper->getDB(), $ID, $REV ?: $INFO['currentrev']);
39
40        echo $this->getBannerHtml($revision);
41    }
42
43    /**
44     * @param Revision $revision latest publish data
45     * @return string
46     */
47    protected function getBannerHtml($revision)
48    {
49        global $ID;
50
51        $status = $revision->getStatus() ?: Constants::STATUS_DRAFT;
52        if ($status === Constants::STATUS_PUBLISHED) {
53            $publisher = userlink($revision->getUser(), true);
54            $publishDate = $revision->getDate();
55        } else {
56            $publisher = userlink($revision->getLatestPublishedRevision()->getUser(), true);
57            $publishDate = $revision->getLatestPublishedRevision()->getDate();
58        }
59
60        $version =  '';
61        if ($revision->getVersion()) {
62            $version = $revision->getVersion() . " ($publishDate, $publisher)";
63
64            if ($status !== Constants::STATUS_PUBLISHED) {
65                $version = sprintf(
66                    '<a href="'. wl($ID, ['rev' => $revision->getLatestPublishedRevision()->getRev()]) . ' ">%s</a>',
67                    $version
68                );
69            }
70        }
71
72        $actionForm = $this->formHtml($status);
73
74        $html = sprintf(
75            $this->getBannerTemplate(),
76            $status,
77            $status,
78            $version,
79            $actionForm
80        );
81
82        return $html;
83    }
84
85    protected function formHtml($status)
86    {
87        global $ID;
88        if ($status === Constants::STATUS_PUBLISHED) return '';
89
90        $form = new dokuwiki\Form\Form();
91
92        if ($status !== Constants::STATUS_APPROVED) {
93            $form->addButton('structpublish[approve]', 'APPROVE')->attr('type', 'submit');
94        }
95
96
97        $form->addButton('structpublish[publish]', 'PUBLISH')->attr('type', 'submit');
98
99        return $form->toHTML();
100    }
101
102    protected function getBannerTemplate()
103    {
104        $template = '<div class="plugin-structpublish-banner banner-%s">';
105        $template .= '<div class="plugin-structpublish-status">' . $this->getLang('status') . ': %s</div>';
106        $template .= '<div class="plugin-structpublish-version">' . $this->getLang('version') . ': %s</div>';
107        $template .= '<div class="plugin-structpublish-actions">%s</div>';
108        $template .= '</div>';
109
110        return $template;
111    }
112}
113