xref: /plugin/quicksubscribe/action.php (revision 5b25dd0f4d88f26fb77c359a19e2c388ea1168cf)
1346619c6SAndreas Gohr<?php
2*5b25dd0fSAnna Dabrowska
3*5b25dd0fSAnna Dabrowskause dokuwiki\Extension\ActionPlugin;
4*5b25dd0fSAnna Dabrowskause dokuwiki\Extension\EventHandler;
5*5b25dd0fSAnna Dabrowskause dokuwiki\Extension\Event;
6*5b25dd0fSAnna Dabrowskause dokuwiki\Subscriptions\SubscriberManager;
7*5b25dd0fSAnna Dabrowska
8346619c6SAndreas Gohr/**
9346619c6SAndreas Gohr * DokuWiki Plugin quicksubscribe (Action Component)
10346619c6SAndreas Gohr *
11346619c6SAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
12346619c6SAndreas Gohr * @author  Andreas Gohr <gohr@cosmocode.de>
13346619c6SAndreas Gohr */
14*5b25dd0fSAnna Dabrowskaclass action_plugin_quicksubscribe extends ActionPlugin
15e02bd35eSAndreas Gohr{
16e02bd35eSAndreas Gohr    /** @inheritdoc */
17*5b25dd0fSAnna Dabrowska    public function register(EventHandler $controller)
18e02bd35eSAndreas Gohr    {
19346619c6SAndreas Gohr        $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handle_ajax_call_unknown');
20346619c6SAndreas Gohr    }
21346619c6SAndreas Gohr
22e02bd35eSAndreas Gohr    /**
23e02bd35eSAndreas Gohr     * Handle subscription/unsubscription AJAX events
24e02bd35eSAndreas Gohr     *
25*5b25dd0fSAnna Dabrowska     * @param Event $event
26e02bd35eSAndreas Gohr     * @param $param
27e02bd35eSAndreas Gohr     */
28*5b25dd0fSAnna Dabrowska    public function handle_ajax_call_unknown(Event $event, $param)
29e02bd35eSAndreas Gohr    {
30346619c6SAndreas Gohr        if ($event->data != 'plugin_quicksubscribe') return;
31346619c6SAndreas Gohr        $event->preventDefault();
32346619c6SAndreas Gohr        $event->stopPropagation();
33346619c6SAndreas Gohr
34346619c6SAndreas Gohr        global $INPUT;
35346619c6SAndreas Gohr
36346619c6SAndreas Gohr        $ns = cleanID($INPUT->str('ns')) . ':'; // we only handle namespaces
37346619c6SAndreas Gohr        $do = $INPUT->str('do');
38346619c6SAndreas Gohr
39346619c6SAndreas Gohr        $ok = false;
40346619c6SAndreas Gohr        $msg = '';
41346619c6SAndreas Gohr
42*5b25dd0fSAnna Dabrowska        $sub = new SubscriberManager();
43346619c6SAndreas Gohr
44346619c6SAndreas Gohr        if ($do == 'subscribe') {
45346619c6SAndreas Gohr            // new subscriptions
46e02bd35eSAndreas Gohr            try {
47346619c6SAndreas Gohr                $ok = $sub->add($ns, $_SERVER['REMOTE_USER'], 'list');
48e02bd35eSAndreas Gohr            } catch (\Exception $ignored) {
49e02bd35eSAndreas Gohr                $ok = false;
50e02bd35eSAndreas Gohr            }
51346619c6SAndreas Gohr            if ($ok) {
52e02bd35eSAndreas Gohr                $msg = sprintf($this->getLang('sub_succ'), prettyprint_id($ns));
53346619c6SAndreas Gohr            } else {
54e02bd35eSAndreas Gohr                $msg = sprintf($this->getLang('sub_fail'), prettyprint_id($ns));
55346619c6SAndreas Gohr            }
56346619c6SAndreas Gohr        } elseif ($do == 'unsubscribe') {
57346619c6SAndreas Gohr            // subscription removal
58346619c6SAndreas Gohr            $ok = $sub->remove($ns, $_SERVER['REMOTE_USER']);
59346619c6SAndreas Gohr            if ($ok) {
60e02bd35eSAndreas Gohr                $msg = sprintf($this->getLang('unsub_succ'), prettyprint_id($ns));
61346619c6SAndreas Gohr            } else {
62e02bd35eSAndreas Gohr                $msg = sprintf($this->getLang('unsub_fail'), prettyprint_id($ns));
63346619c6SAndreas Gohr            }
64346619c6SAndreas Gohr        }
65346619c6SAndreas Gohr
66346619c6SAndreas Gohr        if (!$ok) http_status(400);
67346619c6SAndreas Gohr        echo '<p>' . $msg . '</p>';
68346619c6SAndreas Gohr    }
69346619c6SAndreas Gohr}
70