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