1<?php
2
3// must be run within Dokuwiki
4use dokuwiki\plugin\stale\StaleMenuItem;
5
6if (!defined('DOKU_INC')) die();
7
8require_once(__DIR__ . '/Menuitem.php');
9
10/**
11 * Class action_plugin_move_rewrite
12 */
13class action_plugin_stale extends DokuWiki_Action_Plugin
14{
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    {
23        $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handle_ajax_call');
24
25        /**
26         * Add a icon in the page tools menu
27         * https://www.dokuwiki.org/devel:event:menu_items_assembly
28         */
29        $controller->register_hook('MENU_ITEMS_ASSEMBLY', 'AFTER', $this, 'add_menu_item');
30    }
31
32    /**
33     * Render a subtree
34     *
35     * @param Doku_Event $event
36     * @param            $params
37     */
38    public function handle_ajax_call(Doku_Event $event, $params)
39    {
40        if ($event->data != 'plugin_stale') return;
41        $event->preventDefault();
42        $event->stopPropagation();
43
44        /** @var helper_plugin_stale $stale */
45        $stale = plugin_load('helper', 'stale');
46
47        if ($stale->canTouch() !== true) {
48            http_status(403);
49            $message = "You don't have the right to touch";
50        } else {
51
52            $message = $stale->stale();
53            http_status(200);
54
55        }
56        $jsonArray = array("message" => $message);
57        header('Content-Type: application/json');
58        echo json_encode($jsonArray);
59
60
61    }
62
63    public function add_menu_item(Doku_Event $event, $param)
64    {
65
66        /**
67         * The `view` property defines the menu that is currently built
68         * https://www.dokuwiki.org/devel:menus
69         * If this is not the page menu, return
70         */
71        if ($event->data['view'] != 'site') return;
72
73        /** @var helper_plugin_stale $stale */
74        $stale = plugin_load('helper', 'stale');
75        if ($stale->canTouch()===true){
76
77            array_splice($event->data['items'], -1, 0, array(new StaleMenuItem()));
78
79        }
80
81
82
83    }
84
85
86}
87