xref: /plugin/quicksubscribe/action.php (revision 6bd6f51f0107da5737b734181560cb6f363639f3)
1<?php
2/**
3 * DokuWiki Plugin quicksubscribe (Action Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Andreas Gohr <gohr@cosmocode.de>
7 */
8
9// must be run within Dokuwiki
10if(!defined('DOKU_INC')) die();
11
12class action_plugin_quicksubscribe extends DokuWiki_Action_Plugin {
13
14    function register(Doku_Event_Handler $controller) {
15        $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handle_ajax_call_unknown');
16    }
17
18    function handle_ajax_call_unknown(&$event, $param) {
19        if($event->data != 'plugin_quicksubscribe') return;
20        $event->preventDefault();
21        $event->stopPropagation();
22
23        global $INPUT;
24
25        $ns = cleanID($INPUT->str('ns')) . ':'; // we only handle namespaces
26        $do = $INPUT->str('do');
27
28        $ok = false;
29        $msg = '';
30
31        $sub = new Subscription();
32
33        if($do == 'subscribe') {
34            // new subscriptions
35            $ok = $sub->add($ns, $_SERVER['REMOTE_USER'], 'list');
36            if($ok) {
37                $msg = sprintf($this->getLang('sub_succ'), $this->prettyid($ns));
38            } else {
39                $msg = sprintf($this->getLang('sub_fail'), $this->prettyid($ns));
40            }
41        } elseif($do == 'unsubscribe') {
42            // subscription removal
43            $ok = $sub->remove($ns, $_SERVER['REMOTE_USER']);
44            if($ok) {
45                $msg = sprintf($this->getLang('unsub_succ'), $this->prettyid($ns));
46            } else {
47                $msg = sprintf($this->getLang('unsub_fail'), $this->prettyid($ns));
48            }
49        }
50
51        if(!$ok) http_status(400);
52        echo '<p>' . $msg . '</p>';
53    }
54
55    private function prettyid($ns) {
56        $ns = cleanID($ns);
57        return $ns ? ($ns . ':*') : '*';
58    }
59}
60
61// vim:ts=4:sw=4:et:enc=utf-8:
62