1<?php
2/**
3 * DokuWiki Plugin rating (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_rating extends DokuWiki_Action_Plugin {
13
14    /**
15     * Registers a callback function for a given event
16     *
17     * @param Doku_Event_Handler $controller DokuWiki's event controller object
18     * @return void
19     */
20    public function register(Doku_Event_Handler $controller) {
21        $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handle_ajax_call_unknown');
22        $controller->register_hook('DOKUWIKI_STARTED', 'AFTER', $this, 'handle_vote');
23        $controller->register_hook('IO_WIKIPAGE_WRITE', 'AFTER', $this, 'handle_delete');
24    }
25
26
27
28    /**
29     * [Custom event handler which performs action]
30     *
31     * @param Doku_Event $event event object by reference
32     * @param mixed $param [the parameters passed as fifth argument to register_hook() when this
33     *                           handler was registered]
34     * @return void
35     */
36    public function handle_delete(Doku_Event &$event, $param) {
37        if($event->data[3]) return; // it's an old revision
38        if($event->data[0][1]) return; // there's still content
39        // still here? page was deleted
40
41        /** @var helper_plugin_rating $hlp */
42        $hlp = plugin_load('helper', 'rating');
43        $hlp->remove($event->data[2]);
44    }
45
46    /**
47     * [Custom event handler which performs action]
48     *
49     * @param Doku_Event $event event object by reference
50     * @param mixed $param [the parameters passed as fifth argument to register_hook() when this
51     *                           handler was registered]
52     * @return void
53     */
54    public function handle_ajax_call_unknown(Doku_Event &$event, $param) {
55        if($event->data != 'rating') return;
56        $event->preventDefault();
57        $event->stopPropagation();
58
59        global $ID;
60        $ID = getID();
61
62        // let the other handler do it
63        $this->handle_vote($event, $param);
64
65        /** @var helper_plugin_rating $hlp */
66        $hlp = plugin_load('helper', 'rating');
67        $hlp->tpl(true);
68    }
69
70    /**
71     * [Custom event handler which performs action]
72     *
73     * @param Doku_Event $event event object by reference
74     * @param mixed $param [the parameters passed as fifth argument to register_hook() when this
75     *                           handler was registered]
76     * @return void
77     */
78    public function handle_vote(Doku_Event &$event, $param) {
79        global $INPUT;
80        global $ID;
81        if(!$INPUT->has('rating')) return;
82
83        $rate = $INPUT->int('rating');
84
85        /** @var helper_plugin_rating $hlp */
86        $hlp = plugin_load('helper', 'rating');
87        $hlp->rate($rate, $ID);
88    }
89
90}
91
92// vim:ts=4:sw=4:et:
93