xref: /plugin/structpublish/action/banner.php (revision 3449f9ce9770752ac28fe0246797d80e125747d5)
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
14    /**
15     * @inheritDoc
16     */
17    public function register(Doku_Event_Handler $controller)
18    {
19        $controller->register_hook('PLUGIN_STRUCT_RENDER_SCHEMA_DATA', 'AFTER', $this, 'renderBanner');
20    }
21
22    /**
23     * Add banner to struct data of a page
24     *
25     * @return bool
26     */
27    public function renderBanner(Doku_Event $event)
28    {
29        global $ID;
30        global $INFO;
31        $data = $event->data;
32        if (!$data['hasdata'] || $data['format'] !== 'xhtml') return true;
33
34        /** @var Doku_Renderer_xhtml $renderer */
35        $renderer = $data['renderer'];
36        $renderer->nocache();
37
38        $this->permissionsHelper = plugin_load('helper', 'structpublish_permissions');
39        if (!$this->permissionsHelper->isPublishable()) return true;
40
41        $revision = new Revision($this->permissionsHelper->getDb(), $ID, $INFO['currentrev']);
42
43        $html = $this->getBannerHtml($revision);
44        $renderer->doc .= $html;
45
46        return true;
47    }
48
49    /**
50     * @param Revision $revision
51     * @return string
52     */
53    protected function getBannerHtml($revision)
54    {
55        global $ID;
56        // FIXME use $INFO?
57        $user = $_SERVER['REMOTE_USER'];
58        $html = '';
59
60        if ($this->permissionsHelper->isPublisher($ID, $user)) {
61
62            $actionLinks = $this->linksToHtml($this->permissionsHelper->getActionLinks($revision));
63
64            $html = sprintf(
65                $this->getBannerTemplate(),
66                $revision->getStatus(),
67                $revision->getVersion(),
68                $revision->getStatus(),
69                $actionLinks
70            );
71        }
72
73        return $html;
74    }
75
76    protected function linksToHtml($links)
77    {
78        $html = '';
79        if (empty($links)) return $html;
80        foreach ($links as $action => $link) {
81            $html .= '<a href="' . $link . '">'. $action .'</a>';
82        }
83        return $html;
84    }
85
86    protected function getBannerTemplate()
87    {
88        $template = '<div class="plugin-structpublish-banner banner-%s">';
89        $template .= '<div class="plugin-structpublish-version">' . $this->getLang('version') . ': %s</div>';
90        $template .= '<div class="plugin-structpublish-status">' . $this->getLang('status') . ': %s</div>';
91        $template .= '<div class="plugin-structpublish-actions">' . $this->getLang('actions') . ': %s</div>';
92        $template .= '</div>';
93
94        return $template;
95    }
96}
97