xref: /plugin/quicksubscribe/helper.php (revision 5b25dd0f4d88f26fb77c359a19e2c388ea1168cf)
1b2d51c84SAdrian Lang<?php
2b2d51c84SAdrian Lang
3*5b25dd0fSAnna Dabrowskause dokuwiki\Extension\Plugin;
4*5b25dd0fSAnna Dabrowskause dokuwiki\Menu\Item\Subscribe;
5*5b25dd0fSAnna Dabrowska
6*5b25dd0fSAnna Dabrowskaclass helper_plugin_quicksubscribe extends Plugin
76bd6f51fSAndreas Gohr{
86bd6f51fSAndreas Gohr    /**
96bd6f51fSAndreas Gohr     * Returns the quick subscribe icon using an inline SVG
106bd6f51fSAndreas Gohr     *
116bd6f51fSAndreas Gohr     * @return string
126bd6f51fSAndreas Gohr     */
13*5b25dd0fSAnna Dabrowska    public function tpl_subscribe()
146bd6f51fSAndreas Gohr    {
15b2d51c84SAdrian Lang        global $INFO;
166bd6f51fSAndreas Gohr        global $ACT;
176bd6f51fSAndreas Gohr
186bd6f51fSAndreas Gohr        // only on show
196bd6f51fSAndreas Gohr        if ($ACT !== 'show') return '';
206bd6f51fSAndreas Gohr
216bd6f51fSAndreas Gohr        // check if subscription is available
226bd6f51fSAndreas Gohr        try {
23*5b25dd0fSAnna Dabrowska            $submgr = new Subscribe();
246bd6f51fSAndreas Gohr        } catch (\RuntimeException $ignored) {
256bd6f51fSAndreas Gohr            return '';
26b2d51c84SAdrian Lang        }
276bd6f51fSAndreas Gohr
28337272e3SAndreas Gohr        // we need to access the javascript translations
29337272e3SAndreas Gohr        $this->setupLocale();
30337272e3SAndreas Gohr
316bd6f51fSAndreas Gohr        if ($INFO['subscribed']) {
32337272e3SAndreas Gohr            $title = $this->lang['js']['unsubscribe'];
336bd6f51fSAndreas Gohr            $target = $INFO['subscribed'][0]['target'];
346bd6f51fSAndreas Gohr            $class = 'plugin_qsub_subscribed';
356bd6f51fSAndreas Gohr        } else {
36337272e3SAndreas Gohr            $title = $this->lang['js']['subscribe'];
376bd6f51fSAndreas Gohr            $target = '';
386bd6f51fSAndreas Gohr            $class = 'plugin_qsub_notsubscribed';
396bd6f51fSAndreas Gohr        }
406bd6f51fSAndreas Gohr
416bd6f51fSAndreas Gohr        // we hide one of the SVGs based on the outer class
426bd6f51fSAndreas Gohr        $svg = inlineSVG(__DIR__ . '/images/bell-ring.svg') . inlineSVG(__DIR__ . '/images/bell-outline.svg');
436bd6f51fSAndreas Gohr
446bd6f51fSAndreas Gohr        $link = $submgr->getLinkAttributes('plugin_qsub_');
456bd6f51fSAndreas Gohr        $link['data-target'] = $target;
466bd6f51fSAndreas Gohr        $link['title'] = $title;
476bd6f51fSAndreas Gohr        $link['class'] .= ' ' . $class;
486bd6f51fSAndreas Gohr        $attr = buildAttributes($link);
496bd6f51fSAndreas Gohr
506bd6f51fSAndreas Gohr        return "<a $attr>$svg</a>";
51b2d51c84SAdrian Lang    }
52b2d51c84SAdrian Lang}
53