xref: /plugin/structpublish/action/banner.php (revision 9dc573e720ed80530c53eee4dc7d123385ff85f9)
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
158b0ba635SAndreas Gohr    /** @inheritDoc */
1687106851SAnna Dabrowska    public function register(Doku_Event_Handler $controller)
1787106851SAnna Dabrowska    {
18e394901aSAnna Dabrowska        $controller->register_hook('TPL_ACT_RENDER', 'BEFORE', $this, 'renderBanner');
1987106851SAnna Dabrowska    }
2087106851SAnna Dabrowska
2187106851SAnna Dabrowska    /**
22e394901aSAnna Dabrowska     * Add banner to pages under structpublish control
2387106851SAnna Dabrowska     */
2487106851SAnna Dabrowska    public function renderBanner(Doku_Event $event)
2587106851SAnna Dabrowska    {
26c2f8a3c4SAnna Dabrowska        global $ID;
278d2065f7SAnna Dabrowska        global $INFO;
2840f4519bSAnna Dabrowska        global $REV;
2987106851SAnna Dabrowska
305c2215e8SAndreas Gohr        if ($event->data !== 'show') {
315c2215e8SAndreas Gohr            return;
325c2215e8SAndreas Gohr        }
333449f9ceSAnna Dabrowska
348d2065f7SAnna Dabrowska        $this->dbHelper = plugin_load('helper', 'structpublish_db');
35939e6e3cSAnna Dabrowska
365c2215e8SAndreas Gohr        if (!$this->dbHelper->isPublishable()) {
375c2215e8SAndreas Gohr            return;
385c2215e8SAndreas Gohr        }
39939e6e3cSAnna Dabrowska
405c2215e8SAndreas Gohr        // get the possible revisions needed in the banner
415c2215e8SAndreas Gohr        $newestRevision = new Revision($this->dbHelper->getDB(), $ID, $INFO['currentrev']);
425c2215e8SAndreas Gohr        if ($REV) {
435c2215e8SAndreas Gohr            $shownRevision = new Revision($this->dbHelper->getDB(), $ID, $REV);
445c2215e8SAndreas Gohr        } else {
455c2215e8SAndreas Gohr            $shownRevision = $newestRevision;
465c2215e8SAndreas Gohr        }
475c2215e8SAndreas Gohr        $latestpubRevision = $newestRevision->getLatestPublishedRevision();
485c2215e8SAndreas Gohr        $prevpubRevision = $shownRevision->getLatestPublishedRevision();
49c2f8a3c4SAnna Dabrowska
505c2215e8SAndreas Gohr        $banner = '<div class="plugin-structpublish-banner ' . $shownRevision->getStatus() . '">';
515c2215e8SAndreas Gohr
525c2215e8SAndreas Gohr        // status of the shown revision
535c2215e8SAndreas Gohr        $banner .= inlineSVG(__DIR__ . '/../ico/' . $shownRevision->getStatus() . '.svg');
545c2215e8SAndreas Gohr        $banner .= $this->getBannerText('status_' . $shownRevision->getStatus(), $shownRevision);
555c2215e8SAndreas Gohr
565c2215e8SAndreas Gohr        // link to previous or newest published version
575c2215e8SAndreas Gohr        if ($latestpubRevision !== null && $shownRevision->getRev() < $latestpubRevision->getRev()) {
585c2215e8SAndreas Gohr            $banner .= $this->getBannerText('latest_publish', $latestpubRevision);
595c2215e8SAndreas Gohr        } else {
605c2215e8SAndreas Gohr            $banner .= $this->getBannerText('previous_publish', $prevpubRevision);
615c2215e8SAndreas Gohr        }
625c2215e8SAndreas Gohr
635c2215e8SAndreas Gohr        // link to newest draft, if exists, is not shown already and user has a role
645c2215e8SAndreas Gohr        if (
655c2215e8SAndreas Gohr            $newestRevision->getRev() != $shownRevision->getRev() &&
665c2215e8SAndreas Gohr            $newestRevision->getStatus() != Constants::STATUS_PUBLISHED &&
675c2215e8SAndreas Gohr            $this->dbHelper->checkAccess($ID)
685c2215e8SAndreas Gohr        ) {
695c2215e8SAndreas Gohr            $banner .= $this->getBannerText('latest_draft', $newestRevision);
705c2215e8SAndreas Gohr        }
715c2215e8SAndreas Gohr
725c2215e8SAndreas Gohr        // action buttons
735c2215e8SAndreas Gohr        if ($shownRevision->getRev() == $newestRevision->getRev()) {
745c2215e8SAndreas Gohr            $banner .= $this->actionButtons(
755c2215e8SAndreas Gohr                $shownRevision->getStatus(),
76*9dc573e7SAndreas Gohr                $latestpubRevision ? $this->increaseVersion($latestpubRevision->getVersion()) : '1'
775c2215e8SAndreas Gohr            );
785c2215e8SAndreas Gohr        }
795c2215e8SAndreas Gohr
805c2215e8SAndreas Gohr        $banner .= '</div>';
815c2215e8SAndreas Gohr        echo $banner;
8287106851SAnna Dabrowska    }
8387106851SAnna Dabrowska
8487106851SAnna Dabrowska    /**
855c2215e8SAndreas Gohr     * Fills place holder texts with data from the given Revision
865c2215e8SAndreas Gohr     *
875c2215e8SAndreas Gohr     * @param string $text
885c2215e8SAndreas Gohr     * @param Revision $rev
8987106851SAnna Dabrowska     * @return string
9087106851SAnna Dabrowska     */
915c2215e8SAndreas Gohr    protected function getBannerText($text, $rev)
925c2215e8SAndreas Gohr    {
935c2215e8SAndreas Gohr        if ($rev === null) {
945c2215e8SAndreas Gohr            return '';
955c2215e8SAndreas Gohr        }
965c2215e8SAndreas Gohr
975c2215e8SAndreas Gohr        $replace = [
985c2215e8SAndreas Gohr            '{user}' => userlink($rev->getUser()),
995c2215e8SAndreas Gohr            '{revision}' => $this->makeLink($rev->getId(), $rev->getRev(), dformat($rev->getRev())),
1005c2215e8SAndreas Gohr            '{datetime}' => $this->makeLink($rev->getId(), $rev->getRev(), dformat($rev->getTimestamp())),
1015c2215e8SAndreas Gohr            '{version}' => hsc($rev->getVersion()),
1025c2215e8SAndreas Gohr        ];
1035c2215e8SAndreas Gohr
1045c2215e8SAndreas Gohr        $text = $this->getLang("banner_$text");
1055c2215e8SAndreas Gohr        $text = strtr($text, $replace);
1065c2215e8SAndreas Gohr
1075c2215e8SAndreas Gohr        return "<p>$text</p>";
1085c2215e8SAndreas Gohr    }
1095c2215e8SAndreas Gohr
1105c2215e8SAndreas Gohr    /**
1115c2215e8SAndreas Gohr     * Create a HTML link to a specific revision
1125c2215e8SAndreas Gohr     *
1135c2215e8SAndreas Gohr     * @param string $id page id
1145c2215e8SAndreas Gohr     * @param int $rev revision to link to
1155c2215e8SAndreas Gohr     * @param int $text the link text to use
1165c2215e8SAndreas Gohr     * @return string
1175c2215e8SAndreas Gohr     */
1185c2215e8SAndreas Gohr    protected function makeLink($id, $rev, $text)
1195c2215e8SAndreas Gohr    {
1205c2215e8SAndreas Gohr        $url = wl($id, ['rev' => $rev]);
1215c2215e8SAndreas Gohr        return '<a href="' . $url . '">' . hsc($text) . '</a>';
1225c2215e8SAndreas Gohr    }
1235c2215e8SAndreas Gohr
1245c2215e8SAndreas Gohr    /**
1255c2215e8SAndreas Gohr     * Create the form for approval and publishing
1265c2215e8SAndreas Gohr     *
1275c2215e8SAndreas Gohr     * @param string $status current status
1285c2215e8SAndreas Gohr     * @param string $newVersion suggested new Version
1295c2215e8SAndreas Gohr     * @return string
1305c2215e8SAndreas Gohr     */
1315c2215e8SAndreas Gohr    protected function actionButtons($status, $newVersion)
13287106851SAnna Dabrowska    {
13387106851SAnna Dabrowska        global $ID;
134e31c94d7SAndreas Gohr        if ($status === Constants::STATUS_PUBLISHED) {
1355c2215e8SAndreas Gohr            return '';
1368d2065f7SAnna Dabrowska        }
137e7259784SAnna Dabrowska
138e394901aSAnna Dabrowska        $form = new dokuwiki\Form\Form();
1391b063be2SAnna Dabrowska
1405c2215e8SAndreas Gohr        if (
1415c2215e8SAndreas Gohr            $status !== Constants::STATUS_APPROVED &&
1425c2215e8SAndreas Gohr            $this->dbHelper->checkAccess($ID, [Constants::ACTION_APPROVE])
1435c2215e8SAndreas Gohr        ) {
1445c2215e8SAndreas Gohr            $form->addButton(
1455c2215e8SAndreas Gohr                'structpublish[approve]',
1465c2215e8SAndreas Gohr                $this->getLang('action_' . Constants::ACTION_APPROVE)
1475c2215e8SAndreas Gohr            )->attr('type', 'submit');
1481b063be2SAnna Dabrowska        }
149e31c94d7SAndreas Gohr
1505c2215e8SAndreas Gohr        if ($this->dbHelper->checkAccess($ID, [Constants::ACTION_PUBLISH])) {
1515c2215e8SAndreas Gohr            $form->addTextInput('version', $this->getLang('newversion'))->val($newVersion);
1525c2215e8SAndreas Gohr            $form->addButton(
1535c2215e8SAndreas Gohr                'structpublish[publish]',
1545c2215e8SAndreas Gohr                $this->getLang('action_' . Constants::ACTION_PUBLISH)
1555c2215e8SAndreas Gohr            )->attr('type', 'submit');
1565c2215e8SAndreas Gohr        }
157e394901aSAnna Dabrowska
158e394901aSAnna Dabrowska        return $form->toHTML();
159c2f8a3c4SAnna Dabrowska    }
160*9dc573e7SAndreas Gohr
161*9dc573e7SAndreas Gohr    /**
162*9dc573e7SAndreas Gohr     * Tries to increase a given version
163*9dc573e7SAndreas Gohr     *
164*9dc573e7SAndreas Gohr     * @param string $version
165*9dc573e7SAndreas Gohr     * @return string
166*9dc573e7SAndreas Gohr     */
167*9dc573e7SAndreas Gohr    protected function increaseVersion($version)
168*9dc573e7SAndreas Gohr    {
169*9dc573e7SAndreas Gohr        $parts = explode('.', $version);
170*9dc573e7SAndreas Gohr        $last = array_pop($parts);
171*9dc573e7SAndreas Gohr
172*9dc573e7SAndreas Gohr        if (is_numeric($last)) {
173*9dc573e7SAndreas Gohr            $last++;
174*9dc573e7SAndreas Gohr        }
175*9dc573e7SAndreas Gohr        $parts[] = $last;
176*9dc573e7SAndreas Gohr
177*9dc573e7SAndreas Gohr        return join('.', $parts);
178*9dc573e7SAndreas Gohr    }
17987106851SAnna Dabrowska}
180