1<?php
2/**
3 * DokuWiki Plugin starred (Action Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Andreas Gohr <andi@splitbrain.org>
7 */
8
9/**
10 * Class action_plugin_starred
11 */
12class action_plugin_starred extends DokuWiki_Action_Plugin
13{
14    /** @var helper_plugin_starred */
15    protected $helper;
16
17    /**
18     * action_plugin_starred constructor.
19     */
20    public function __construct()
21    {
22        $this->helper = plugin_load('helper', 'starred');
23    }
24
25    /** @inheritdoc */
26    public function register(Doku_Event_Handler $controller)
27    {
28
29        $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handle_ajax_call_unknown');
30        $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'handle_action_act_preprocess');
31
32    }
33
34    /**
35     * Handle the ajax call
36     *
37     * @param Doku_Event $event AJAX_CALL_UNKNOWN
38     * @param $param
39     */
40    public function handle_ajax_call_unknown(Doku_Event $event, $param)
41    {
42        if ($event->data != 'startoggle') return;
43        global $ID;
44        global $INPUT;
45        $ID = cleanID($INPUT->str('id'));
46
47        $this->helper->toggleStar();
48        $this->tpl_starred(true);
49        $event->preventDefault();
50        $event->stopPropagation();
51    }
52
53    /**
54     * Handle the non-ajax call
55     *
56     * @param Doku_Event $event ACTION_ACT_PREPROCESS
57     * @param $param
58     */
59    public function handle_action_act_preprocess(Doku_Event $event, $param)
60    {
61        if (substr(act_clean($event->data), 0, 10) != 'startoggle') return;
62        $id = substr($event->data, 11);
63        $this->helper->toggleStar(null, $id);
64        $event->data = 'show';
65    }
66
67    /**
68     * Print the current star state for the current page
69     * @param bool $inneronly TBD
70     * @param bool $print Should the HTML be printed or returned?
71     * @return bool|string
72     */
73    public function tpl_starred($inneronly = false, $print = true)
74    {
75        global $ID;
76        global $INPUT;
77
78        if (!$INPUT->server->has('REMOTE_USER')) return false;
79        $star_html = $this->helper->starHtml($ID, $ID, $inneronly, true);
80        if ($print) {
81            echo $star_html;
82        }
83        return $star_html;
84    }
85
86}
87