xref: /plugin/structpublish/action/banner.php (revision 8d2065f7b2b1f3b8d189ad26587e4daa79a85afd)
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        global $REV;
32
33        if ($event->data !== 'show') return;
34
35        $this->permissionsHelper = plugin_load('helper', 'structpublish_permissions');
36        if (!$this->permissionsHelper->isPublishable()) return;
37
38        $this->dbHelper = plugin_load('helper', 'structpublish_db');
39        $revision = new Revision($this->dbHelper->getDB(), $ID, $REV ?: $INFO['currentrev']);
40
41        echo $this->getBannerHtml($revision);
42    }
43
44    /**
45     * @param Revision $revision latest publish data
46     * @return string
47     */
48    protected function getBannerHtml($revision)
49    {
50        global $ID;
51
52        $status = $revision->getStatus() ?: Revision::STATUS_DRAFT;
53        if ($status === Revision::STATUS_PUBLISHED) {
54            $publisher = userlink($revision->getUser(), true);
55            $publishDate = $revision->getDate();
56        } else {
57            $publisher = userlink($revision->getLatestPublished('user'), true);
58            $publishDate = $revision->getLatestPublished('date');
59        }
60
61        $version =  '';
62        if ($revision->getVersion()) {
63            $version = $revision->getVersion() . " ($publishDate, $publisher)";
64
65            if ($status !== Revision::STATUS_PUBLISHED) {
66                $version = sprintf(
67                    '<a href="'. wl($ID, ['rev' => $revision->getLatestPublished('revision')]) . ' ">%s</a>',
68                    $version
69                );
70            }
71        }
72
73        $actionForm = $this->formHtml($status);
74
75        $html = sprintf(
76            $this->getBannerTemplate(),
77            $status,
78            $status,
79            $version,
80            $actionForm
81        );
82
83        return $html;
84    }
85
86    protected function formHtml($status)
87    {
88        if ($status === Revision::STATUS_PUBLISHED) return '';
89
90        $form = new dokuwiki\Form\Form();
91
92        if ($status !== Revision::STATUS_APPROVED) {
93            $form->addButton('structpublish[approve]', 'APPROVE')->attr('type', 'submit');
94        }
95        $form->addButton('structpublish[publish]', 'PUBLISH')->attr('type', 'submit');
96
97        return $form->toHTML();
98    }
99
100    protected function getBannerTemplate()
101    {
102        $template = '<div class="plugin-structpublish-banner banner-%s">';
103        $template .= '<div class="plugin-structpublish-status">' . $this->getLang('status') . ': %s</div>';
104        $template .= '<div class="plugin-structpublish-version">' . $this->getLang('version') . ': %s</div>';
105        $template .= '<div class="plugin-structpublish-actions">%s</div>';
106        $template .= '</div>';
107
108        return $template;
109    }
110}
111