xref: /plugin/structpublish/action/banner.php (revision c2f8a3c436c4e9b483f84c9901319c1d150026a8)
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        $data = $event->data;
31        if (!$data['hasdata'] || $data['format'] !== 'xhtml') return true;
32
33        $this->permissionsHelper = plugin_load('helper', 'structpublish_permissions');
34        if (!$this->permissionsHelper->isPublishable()) return true;
35
36        $revision = new Revision($this->permissionsHelper->getDb(), $ID);
37
38        $renderer = $data['renderer'];
39        $html = $this->getBannerHtml($revision);
40        $renderer->doc .= $html;
41
42        return true;
43    }
44
45    /**
46     * @param Revision $revision
47     * @return string
48     */
49    protected function getBannerHtml($revision)
50    {
51        global $ID;
52        // FIXME use $INFO?
53        $user = $_SERVER['REMOTE_USER'];
54        $html = '';
55
56        if ($this->permissionsHelper->isPublisher($ID, $user)) {
57            $html = sprintf(
58                $this->getBannerTemplate(),
59                $revision->getStatus(),
60                $revision->getVersion(),
61                $revision->getStatus(),
62                $this->linksToHtml($this->permissionsHelper->getActionLinks())
63            );
64        }
65
66        return $html;
67    }
68
69    protected function linksToHtml($links)
70    {
71        $html = '';
72        if (empty($links)) return $html;
73        foreach ($links as $action => $link) {
74            $html .= '<a href="' . $link . '">'. $action .'</a>';
75        }
76        return $html;
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">' . $this->getLang('actions') . ': %s</div>';
85        $template .= '</div>';
86
87        return $template;
88    }
89}
90