1<?php
2/**
3 * Move Plugin AJAX handler to step through a move plan
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Andreas Gohr <gohr@cosmocode.de>
7 */
8// must be run within Dokuwiki
9if(!defined('DOKU_INC')) die();
10
11/**
12 * Class action_plugin_move_progress
13 */
14class action_plugin_move_progress extends DokuWiki_Action_Plugin {
15
16    /**
17     * Register event handlers.
18     *
19     * @param Doku_Event_Handler $controller The plugin controller
20     */
21    public function register(Doku_Event_Handler $controller) {
22        $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handle_ajax');
23    }
24
25    /**
26     * Step up
27     *
28     * @param Doku_Event $event
29     */
30    public function handle_ajax(Doku_Event $event) {
31        if($event->data != 'plugin_move_progress') return;
32        $event->preventDefault();
33        $event->stopPropagation();
34
35        global $INPUT;
36        global $USERINFO;
37
38        if(!auth_ismanager($_SERVER['REMOTE_USER'], $USERINFO['grps'])) {
39            http_status(403);
40            exit;
41        }
42
43        $return = array(
44            'error'    => '',
45            'complete' => false,
46            'progress' => 0
47        );
48
49        /** @var helper_plugin_move_plan $plan */
50        $plan = plugin_load('helper', 'move_plan');
51
52        if(!$plan->isCommited()) {
53            // There is no plan. Something went wrong
54            $return['complete'] = true;
55        } else {
56            $todo               = $plan->nextStep($INPUT->bool('skip'));
57            $return['progress'] = $plan->getProgress();
58            $return['error']    = $plan->getLastError();
59            if($todo === 0) $return['complete'] = true;
60        }
61
62        $json = new JSON();
63        header('Content-Type: application/json');
64        echo $json->encode($return);
65    }
66}