xref: /plugin/structpublish/action/banner.php (revision 1b063be2bba157e194f2f6e6f6eef66137461ea8)
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 $INFO;
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->dbHelper->IS_PUBLISHER($ID)) return;
37
38        $revision = new Revision($this->permissionsHelper->getDb(), $ID, $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        $user = $_SERVER['REMOTE_USER'];
51        $html = '';
52
53        if ($this->dbHelper->IS_PUBLISHER($ID, $user)) {
54
55            $status = $revision->getStatus() ?: Revision::STATUS_DRAFT;
56            $publisher = userlink($revision->getUser(), true);
57            $publishDate = $revision->getDate();
58
59            $version =  '';
60            if ($revision->getVersion()) {
61                $version = '<a href="'. wl($ID, ['rev' => $revision->getLatestPublishedRev()]) . ' ">';
62                $version .= $revision->getVersion() . " ($publishDate, $publisher)";
63                $version .= '</a>';
64            }
65
66            $actionForm = $this->formHtml($status);
67
68            $html = sprintf(
69                $this->getBannerTemplate(),
70                $status,
71                $status,
72                $version,
73                $actionForm
74            );
75        }
76
77        return $html;
78    }
79
80    protected function formHtml($status)
81    {
82        if ($status === Revision::STATUS_PUBLISHED) return '';
83
84        $form = new dokuwiki\Form\Form();
85
86        if ($status !== Revision::STATUS_APPROVED) {
87            $form->addButton('structpublish[approve]', 'APPROVE')->attr('type', 'submit');
88        }
89        $form->addButton('structpublish[publish]', 'PUBLISH')->attr('type', 'submit');
90
91        return $form->toHTML();
92    }
93
94    protected function getBannerTemplate()
95    {
96        $template = '<div class="plugin-structpublish-banner banner-%s">';
97        $template .= '<div class="plugin-structpublish-status">' . $this->getLang('status') . ': %s</div>';
98        $template .= '<div class="plugin-structpublish-version">' . $this->getLang('version') . ': %s</div>';
99        $template .= '<div class="plugin-structpublish-actions">%s</div>';
100        $template .= '</div>';
101
102        return $template;
103    }
104}
105