xref: /plugin/structpublish/action/banner.php (revision 40f4519b316f8d5316baab41bc31c713611b3179)
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_permissions */
12    protected $permissionsHelper;
13    /** @var \helper_plugin_structpublish_db */
14    protected $dbHelper;
15
16    /**
17     * @inheritDoc
18     */
19    public function register(Doku_Event_Handler $controller)
20    {
21        $controller->register_hook('TPL_ACT_RENDER', 'BEFORE', $this, 'renderBanner');
22    }
23
24    /**
25     * Add banner to pages under structpublish control
26     */
27    public function renderBanner(Doku_Event $event)
28    {
29        global $ID;
30        global $REV;
31
32        if ($event->data !== 'show') return;
33
34        $this->permissionsHelper = plugin_load('helper', 'structpublish_permissions');
35        $this->dbHelper = plugin_load('helper', 'structpublish_db');
36        if (!$this->permissionsHelper->isPublishable()) return;
37
38        $revision = new Revision($this->dbHelper->getDB(), $ID, $REV);
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() ?: Revision::STATUS_DRAFT;
52        $publisher = userlink($revision->getUser(), true);
53        $publishDate = $revision->getDate();
54
55        $version =  '';
56        if ($revision->getVersion()) {
57            $version = '<a href="'. wl($ID, ['rev' => $revision->getLatestPublishedRev()]) . ' ">';
58            $version .= $revision->getVersion() . " ($publishDate, $publisher)";
59            $version .= '</a>';
60        }
61
62        $actionForm = $this->formHtml($status);
63
64        $html = sprintf(
65            $this->getBannerTemplate(),
66            $status,
67            $status,
68            $version,
69            $actionForm
70        );
71
72        return $html;
73    }
74
75    protected function formHtml($status)
76    {
77        if ($status === Revision::STATUS_PUBLISHED) return '';
78
79        $form = new dokuwiki\Form\Form();
80
81        if ($status !== Revision::STATUS_APPROVED) {
82            $form->addButton('structpublish[approve]', 'APPROVE')->attr('type', 'submit');
83        }
84        $form->addButton('structpublish[publish]', 'PUBLISH')->attr('type', 'submit');
85
86        return $form->toHTML();
87    }
88
89    protected function getBannerTemplate()
90    {
91        $template = '<div class="plugin-structpublish-banner banner-%s">';
92        $template .= '<div class="plugin-structpublish-status">' . $this->getLang('status') . ': %s</div>';
93        $template .= '<div class="plugin-structpublish-version">' . $this->getLang('version') . ': %s</div>';
94        $template .= '<div class="plugin-structpublish-actions">%s</div>';
95        $template .= '</div>';
96
97        return $template;
98    }
99}
100