xref: /plugin/structpublish/action/banner.php (revision 5c2215e890cc9516db9bc28cfa976283c0e72b1e)
187106851SAnna Dabrowska<?php
287106851SAnna Dabrowska
3e31c94d7SAndreas Gohruse dokuwiki\plugin\structpublish\meta\Constants;
4c2f8a3c4SAnna Dabrowskause dokuwiki\plugin\structpublish\meta\Revision;
5c2f8a3c4SAnna Dabrowska
687106851SAnna Dabrowska/**
787106851SAnna Dabrowska * Action component responsible for the publish banner
887106851SAnna Dabrowska * attached to struct data of a page
987106851SAnna Dabrowska */
1087106851SAnna Dabrowskaclass action_plugin_structpublish_banner extends DokuWiki_Action_Plugin
1187106851SAnna Dabrowska{
12e394901aSAnna Dabrowska    /** @var \helper_plugin_structpublish_db */
13e394901aSAnna Dabrowska    protected $dbHelper;
14c2f8a3c4SAnna Dabrowska
1587106851SAnna Dabrowska    /**
1687106851SAnna Dabrowska     * @inheritDoc
1787106851SAnna Dabrowska     */
1887106851SAnna Dabrowska    public function register(Doku_Event_Handler $controller)
1987106851SAnna Dabrowska    {
20e394901aSAnna Dabrowska        $controller->register_hook('TPL_ACT_RENDER', 'BEFORE', $this, 'renderBanner');
2187106851SAnna Dabrowska    }
2287106851SAnna Dabrowska
2387106851SAnna Dabrowska    /**
24e394901aSAnna Dabrowska     * Add banner to pages under structpublish control
2587106851SAnna Dabrowska     */
2687106851SAnna Dabrowska    public function renderBanner(Doku_Event $event)
2787106851SAnna Dabrowska    {
28c2f8a3c4SAnna Dabrowska        global $ID;
298d2065f7SAnna Dabrowska        global $INFO;
3040f4519bSAnna Dabrowska        global $REV;
3187106851SAnna Dabrowska
32*5c2215e8SAndreas Gohr        if ($event->data !== 'show') {
33*5c2215e8SAndreas Gohr            return;
34*5c2215e8SAndreas Gohr        }
353449f9ceSAnna Dabrowska
368d2065f7SAnna Dabrowska        $this->dbHelper = plugin_load('helper', 'structpublish_db');
37939e6e3cSAnna Dabrowska
38*5c2215e8SAndreas Gohr        if (!$this->dbHelper->isPublishable()) {
39*5c2215e8SAndreas Gohr            return;
40*5c2215e8SAndreas Gohr        }
41939e6e3cSAnna Dabrowska
42*5c2215e8SAndreas Gohr        // get the possible revisions needed in the banner
43*5c2215e8SAndreas Gohr        $newestRevision = new Revision($this->dbHelper->getDB(), $ID, $INFO['currentrev']);
44*5c2215e8SAndreas Gohr        if ($REV) {
45*5c2215e8SAndreas Gohr            $shownRevision = new Revision($this->dbHelper->getDB(), $ID, $REV);
46*5c2215e8SAndreas Gohr        } else {
47*5c2215e8SAndreas Gohr            $shownRevision = $newestRevision;
48*5c2215e8SAndreas Gohr        }
49*5c2215e8SAndreas Gohr        $latestpubRevision = $newestRevision->getLatestPublishedRevision();
50*5c2215e8SAndreas Gohr        $prevpubRevision = $shownRevision->getLatestPublishedRevision();
51c2f8a3c4SAnna Dabrowska
52*5c2215e8SAndreas Gohr        $banner = '<div class="plugin-structpublish-banner ' . $shownRevision->getStatus() . '">';
53*5c2215e8SAndreas Gohr
54*5c2215e8SAndreas Gohr        // status of the shown revision
55*5c2215e8SAndreas Gohr        $banner .= inlineSVG(__DIR__ . '/../ico/' . $shownRevision->getStatus() . '.svg');
56*5c2215e8SAndreas Gohr        $banner .= $this->getBannerText('status_' . $shownRevision->getStatus(), $shownRevision);
57*5c2215e8SAndreas Gohr
58*5c2215e8SAndreas Gohr        // link to previous or newest published version
59*5c2215e8SAndreas Gohr        if ($latestpubRevision !== null && $shownRevision->getRev() < $latestpubRevision->getRev()) {
60*5c2215e8SAndreas Gohr            $banner .= $this->getBannerText('latest_publish', $latestpubRevision);
61*5c2215e8SAndreas Gohr        } else {
62*5c2215e8SAndreas Gohr            $banner .= $this->getBannerText('previous_publish', $prevpubRevision);
63*5c2215e8SAndreas Gohr        }
64*5c2215e8SAndreas Gohr
65*5c2215e8SAndreas Gohr        // link to newest draft, if exists, is not shown already and user has a role
66*5c2215e8SAndreas Gohr        if (
67*5c2215e8SAndreas Gohr            $newestRevision->getRev() != $shownRevision->getRev() &&
68*5c2215e8SAndreas Gohr            $newestRevision->getStatus() != Constants::STATUS_PUBLISHED &&
69*5c2215e8SAndreas Gohr            $this->dbHelper->checkAccess($ID)
70*5c2215e8SAndreas Gohr        ) {
71*5c2215e8SAndreas Gohr            $banner .= $this->getBannerText('latest_draft', $newestRevision);
72*5c2215e8SAndreas Gohr        }
73*5c2215e8SAndreas Gohr
74*5c2215e8SAndreas Gohr        // action buttons
75*5c2215e8SAndreas Gohr        if ($shownRevision->getRev() == $newestRevision->getRev()) {
76*5c2215e8SAndreas Gohr            $banner .= $this->actionButtons(
77*5c2215e8SAndreas Gohr                $shownRevision->getStatus(),
78*5c2215e8SAndreas Gohr                $latestpubRevision ? $latestpubRevision->getVersion() : ''
79*5c2215e8SAndreas Gohr            );
80*5c2215e8SAndreas Gohr        }
81*5c2215e8SAndreas Gohr
82*5c2215e8SAndreas Gohr        $banner .= '</div>';
83*5c2215e8SAndreas Gohr        echo $banner;
8487106851SAnna Dabrowska    }
8587106851SAnna Dabrowska
8687106851SAnna Dabrowska    /**
87*5c2215e8SAndreas Gohr     * Fills place holder texts with data from the given Revision
88*5c2215e8SAndreas Gohr     *
89*5c2215e8SAndreas Gohr     * @param string $text
90*5c2215e8SAndreas Gohr     * @param Revision $rev
9187106851SAnna Dabrowska     * @return string
9287106851SAnna Dabrowska     */
93*5c2215e8SAndreas Gohr    protected function getBannerText($text, $rev)
94*5c2215e8SAndreas Gohr    {
95*5c2215e8SAndreas Gohr        if ($rev === null) {
96*5c2215e8SAndreas Gohr            return '';
97*5c2215e8SAndreas Gohr        }
98*5c2215e8SAndreas Gohr
99*5c2215e8SAndreas Gohr        $replace = [
100*5c2215e8SAndreas Gohr            '{user}' => userlink($rev->getUser()),
101*5c2215e8SAndreas Gohr            '{revision}' => $this->makeLink($rev->getId(), $rev->getRev(), dformat($rev->getRev())),
102*5c2215e8SAndreas Gohr            '{datetime}' => $this->makeLink($rev->getId(), $rev->getRev(), dformat($rev->getTimestamp())),
103*5c2215e8SAndreas Gohr            '{version}' => hsc($rev->getVersion()),
104*5c2215e8SAndreas Gohr        ];
105*5c2215e8SAndreas Gohr
106*5c2215e8SAndreas Gohr        $text = $this->getLang("banner_$text");
107*5c2215e8SAndreas Gohr        $text = strtr($text, $replace);
108*5c2215e8SAndreas Gohr
109*5c2215e8SAndreas Gohr        return "<p>$text</p>";
110*5c2215e8SAndreas Gohr    }
111*5c2215e8SAndreas Gohr
112*5c2215e8SAndreas Gohr    /**
113*5c2215e8SAndreas Gohr     * Create a HTML link to a specific revision
114*5c2215e8SAndreas Gohr     *
115*5c2215e8SAndreas Gohr     * @param string $id page id
116*5c2215e8SAndreas Gohr     * @param int $rev revision to link to
117*5c2215e8SAndreas Gohr     * @param int $text the link text to use
118*5c2215e8SAndreas Gohr     * @return string
119*5c2215e8SAndreas Gohr     */
120*5c2215e8SAndreas Gohr    protected function makeLink($id, $rev, $text)
121*5c2215e8SAndreas Gohr    {
122*5c2215e8SAndreas Gohr        $url = wl($id, ['rev' => $rev]);
123*5c2215e8SAndreas Gohr        return '<a href="' . $url . '">' . hsc($text) . '</a>';
124*5c2215e8SAndreas Gohr    }
125*5c2215e8SAndreas Gohr
126*5c2215e8SAndreas Gohr    /**
127*5c2215e8SAndreas Gohr     * Create the form for approval and publishing
128*5c2215e8SAndreas Gohr     *
129*5c2215e8SAndreas Gohr     * @param string $status current status
130*5c2215e8SAndreas Gohr     * @param string $newVersion suggested new Version
131*5c2215e8SAndreas Gohr     * @return string
132*5c2215e8SAndreas Gohr     */
133*5c2215e8SAndreas Gohr    protected function actionButtons($status, $newVersion)
13487106851SAnna Dabrowska    {
13587106851SAnna Dabrowska        global $ID;
136e31c94d7SAndreas Gohr        if ($status === Constants::STATUS_PUBLISHED) {
137*5c2215e8SAndreas Gohr            return '';
1388d2065f7SAnna Dabrowska        }
139e7259784SAnna Dabrowska
140e394901aSAnna Dabrowska        $form = new dokuwiki\Form\Form();
1411b063be2SAnna Dabrowska
142*5c2215e8SAndreas Gohr        if (
143*5c2215e8SAndreas Gohr            $status !== Constants::STATUS_APPROVED &&
144*5c2215e8SAndreas Gohr            $this->dbHelper->checkAccess($ID, [Constants::ACTION_APPROVE])
145*5c2215e8SAndreas Gohr        ) {
146*5c2215e8SAndreas Gohr            $form->addButton(
147*5c2215e8SAndreas Gohr                'structpublish[approve]',
148*5c2215e8SAndreas Gohr                $this->getLang('action_' . Constants::ACTION_APPROVE)
149*5c2215e8SAndreas Gohr            )->attr('type', 'submit');
1501b063be2SAnna Dabrowska        }
151e31c94d7SAndreas Gohr
152*5c2215e8SAndreas Gohr        if ($this->dbHelper->checkAccess($ID, [Constants::ACTION_PUBLISH])) {
153*5c2215e8SAndreas Gohr            $form->addTextInput('version', $this->getLang('newversion'))->val($newVersion);
154*5c2215e8SAndreas Gohr            $form->addButton(
155*5c2215e8SAndreas Gohr                'structpublish[publish]',
156*5c2215e8SAndreas Gohr                $this->getLang('action_' . Constants::ACTION_PUBLISH)
157*5c2215e8SAndreas Gohr            )->attr('type', 'submit');
158*5c2215e8SAndreas Gohr        }
159e394901aSAnna Dabrowska
160e394901aSAnna Dabrowska        return $form->toHTML();
161c2f8a3c4SAnna Dabrowska    }
16287106851SAnna Dabrowska}
163