xref: /plugin/structpublish/action/banner.php (revision 3231ebae4b1306b9f8a4137b83017a39a16e79eb)
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_permissions');
36        if (!$this->permissionsHelper->isPublishable()) 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->permissionsHelper->isPublisher($ID, $user)) {
54
55            $status = $revision->getStatus() ?: Revision::STATUS_DRAFT;
56            $version = $revision->getVersion() ?: '';
57            $actionForm = $status !== Revision::STATUS_PUBLISHED ? $this->formHtml() : '';
58            $html = sprintf(
59                $this->getBannerTemplate(),
60                $status,
61                $version,
62                $status,
63                $actionForm
64            );
65        }
66
67        return $html;
68    }
69
70    protected function formHtml()
71    {
72        $form = new dokuwiki\Form\Form();
73        $form->addButton('structpublish[approve]', 'APPROVE')->attr('type', 'submit');
74        $form->addButton('structpublish[publish]', 'PUBLISH')->attr('type', 'submit');
75
76        return $form->toHTML();
77    }
78
79    protected function getBannerTemplate()
80    {
81        $template = '<div class="plugin-structpublish-banner banner-%s">';
82        $template .= '<div class="plugin-structpublish-version">' . $this->getLang('version') . ': %s</div>';
83        $template .= '<div class="plugin-structpublish-status">' . $this->getLang('status') . ': %s</div>';
84        $template .= '<div class="plugin-structpublish-actions">%s</div>';
85        $template .= '</div>';
86
87        return $template;
88    }
89}
90