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// must be run within Dokuwiki
10if(!defined('DOKU_INC')) die();
11
12/**
13 * Class action_plugin_starred
14 */
15class action_plugin_starred extends DokuWiki_Action_Plugin {
16    /** @var helper_plugin_starred */
17    protected $helper;
18
19    /**
20     * action_plugin_starred constructor.
21     */
22    public function __construct() {
23        $this->helper = plugin_load('helper', 'starred');
24    }
25
26    /**
27     * Registers a callback function for a given event
28     *
29     * @param Doku_Event_Handler $controller
30     */
31    function register(Doku_Event_Handler $controller) {
32
33        $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handle_ajax_call_unknown');
34        $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'handle_action_act_preprocess');
35
36    }
37
38    /**
39     * Handle the ajax call
40     *
41     * @param Doku_Event $event
42     * @param $param
43     */
44    function handle_ajax_call_unknown(Doku_Event $event, $param) {
45        if($event->data != 'startoggle') return;
46        global $ID;
47        $ID = cleanID($_REQUEST['id']);
48
49        $this->helper->toggleStar();
50        $this->tpl_starred(true);
51        $event->preventDefault();
52        $event->stopPropagation();
53    }
54
55    /**
56     * Handle the non-ajax call
57     *
58     * @param Doku_Event $event
59     * @param $param
60     */
61    function handle_action_act_preprocess(Doku_Event $event, $param) {
62        if(substr(act_clean($event->data), 0, 10) != 'startoggle') return;
63        $id = substr($event->data, 11);
64        $this->helper->toggleStar(null, $id);
65        $event->data = 'show';
66    }
67
68    /**
69     * Print the current star state for the current page
70     * @param bool $inneronly TBD
71     * @param bool $print Should the HTML be printed or returned?
72     * @return bool|string
73     */
74    function tpl_starred($inneronly = false, $print = true) {
75        global $ID;
76        if(!isset($_SERVER['REMOTE_USER'])) return false;
77        $star_html = $this->helper->starHtml($ID, $ID, $inneronly, true);
78        if($print) {
79            echo $star_html;
80        }
81        return $star_html;
82    }
83
84}
85
86// vim:ts=4:sw=4:et:enc=utf-8:
87