xref: /dokuwiki/lib/plugins/styling/action.php (revision 123bc813fd93ab5d8dab3cc4a66a09e613a10aa2)
1<?php
2/**
3 * DokuWiki Plugin styling (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_styling
14 *
15 * This handles all the save actions and loading the interface
16 *
17 * All this usually would be done within an admin plugin, but we want to have this available outside
18 * the admin interface using our floating dialog.
19 */
20class action_plugin_styling extends DokuWiki_Action_Plugin {
21
22    /**
23     * Registers a callback functions
24     *
25     * @param Doku_Event_Handler $controller DokuWiki's event controller object
26     * @return void
27     */
28    public function register(Doku_Event_Handler $controller) {
29        $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handle_ajax');
30        $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'handle_action');
31        $controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'handle_header');
32    }
33
34    /**
35     * Adds the preview parameter to the stylesheet loading in non-js mode
36     *
37     * @param Doku_Event $event  event object by reference
38     * @param mixed      $param  [the parameters passed as fifth argument to register_hook() when this
39     *                           handler was registered]
40     * @return void
41     */
42    public function handle_header(Doku_Event &$event, $param) {
43        global $ACT;
44        global $INPUT;
45        if($ACT != 'admin' || $INPUT->str('page') != 'styling') return;
46        if(!auth_isadmin()) return;
47
48        // set preview
49        $len = count($event->data['link']);
50        for($i = 0; $i < $len; $i++) {
51            if(
52                $event->data['link'][$i]['rel'] == 'stylesheet' &&
53                strpos($event->data['link'][$i]['href'], 'lib/exe/css.php') !== false
54            ) {
55                $event->data['link'][$i]['href'] .= '&preview=1&tseed='.time();
56            }
57        }
58    }
59
60    /**
61     * Updates the style.ini settings by passing it on to handle() of the admin component
62     *
63     * @param Doku_Event $event  event object by reference
64     * @param mixed      $param  [the parameters passed as fifth argument to register_hook() when this
65     *                           handler was registered]
66     * @return void
67     */
68    public function handle_action(Doku_Event &$event, $param) {
69        if($event->data != 'styling_plugin') return;
70        if(!auth_isadmin()) return;
71        $event->data = 'show';
72
73        /** @var admin_plugin_styling $hlp */
74        $hlp = plugin_load('admin', 'styling');
75        $hlp->handle();
76    }
77
78    /**
79     * Create the style form in the floating Dialog
80     *
81     * @param Doku_Event $event  event object by reference
82     * @param mixed      $param  [the parameters passed as fifth argument to register_hook() when this
83     *                           handler was registered]
84     * @return void
85     */
86
87    public function handle_ajax(Doku_Event &$event, $param) {
88        if($event->data != 'plugin_styling') return;
89        if(!auth_isadmin()) return;
90        $event->preventDefault();
91        $event->stopPropagation();
92
93        global $ID;
94        global $INPUT;
95        $ID = getID();
96
97        /** @var admin_plugin_styling $hlp */
98        $hlp = plugin_load('admin', 'styling');
99        if($INPUT->str('run') == 'preview') {
100            $hlp->run_preview();
101        } else {
102            $hlp->form(true);
103        }
104    }
105
106}
107
108// vim:ts=4:sw=4:et:
109