xref: /plugin/combo/action/backlinkmenuitem.php (revision 4cadd4f8c541149bdda95f080e38a6d4e3a640ca)
1<?php
2
3use ComboStrap\BacklinkMenuItem;
4use ComboStrap\Event;
5use ComboStrap\FileSystems;
6use ComboStrap\Identity;
7use ComboStrap\MarkupRef;
8use ComboStrap\MetadataDokuWikiStore;
9use ComboStrap\Mime;
10use ComboStrap\Page;
11use ComboStrap\PagePath;
12use ComboStrap\PluginUtility;
13use ComboStrap\Reference;
14use ComboStrap\References;
15
16
17require_once(__DIR__ . '/../ComboStrap/PluginUtility.php');
18
19/**
20 * Handle the backlink menu item
21 */
22class action_plugin_combo_backlinkmenuitem extends DokuWiki_Action_Plugin
23{
24
25
26    const CALL_ID = "combo-backlink";
27    const CANONICAL = "backlink";
28    const WHREF = "whref";
29
30    public function register(Doku_Event_Handler $controller)
31    {
32
33
34        /**
35         * Add a icon in the page tools menu
36         * https://www.dokuwiki.org/devel:event:menu_items_assembly
37         */
38        $controller->register_hook('MENU_ITEMS_ASSEMBLY', 'AFTER', $this, 'addMenuItem');
39
40
41        /**
42         * The ajax api to return data
43         */
44        $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'ajaxCall');
45
46
47    }
48
49    function addMenuItem(Doku_Event $event, $param)
50    {
51
52
53        /**
54         * The `view` property defines the menu that is currently built
55         * https://www.dokuwiki.org/devel:menus
56         * If this is not the page menu, return
57         */
58        if ($event->data['view'] != 'page') return;
59
60        global $INFO;
61        if (!$INFO['exists']) {
62            return;
63        }
64        $menuItems = &$event->data["items"];
65        foreach ($menuItems as $key => $menuItem) {
66            if ($menuItem instanceof \dokuwiki\Menu\Item\Backlink) {
67                $menuItems[$key] = new BacklinkMenuItem();
68                break;
69            }
70        }
71        /**
72         * Add the wl to build the link to the backlinks actions
73         */
74        $id = PluginUtility::getRequestedWikiId();
75        global $JSINFO;
76        $JSINFO[self::WHREF] = wl($id);
77
78    }
79
80    /**
81     * Main function; dispatches the visual comment actions
82     * @param   $event Doku_Event
83     */
84    function ajaxCall(&$event, $param): void
85    {
86        $call = $event->data;
87        if ($call != self::CALL_ID) {
88            return;
89        }
90        //no other ajax call handlers needed
91        $event->stopPropagation();
92        $event->preventDefault();
93
94        /**
95         * Shared check between post and get HTTP method
96         */
97        $id = $_GET["id"];
98        if ($id === null) {
99            /**
100             * With {@link TestRequest}
101             * for instance
102             */
103            $id = $_REQUEST["id"];
104        }
105
106        if (empty($id)) {
107            \ComboStrap\HttpResponse::create(\ComboStrap\HttpResponse::STATUS_BAD_REQUEST)
108                ->setEvent($event)
109                ->setCanonical(self::CANONICAL)
110                ->send("The page id should not be empty", Mime::HTML);
111            return;
112        }
113
114
115        $backlinkPages = Page::createPageFromId($id);
116        $html = syntax_plugin_combo_related::getHtmlRelated($backlinkPages);
117
118
119        \ComboStrap\HttpResponse::create(\ComboStrap\HttpResponse::STATUS_ALL_GOOD)
120            ->setEvent($event)
121            ->setCanonical(self::CANONICAL)
122            ->send($html, Mime::HTML);
123
124    }
125
126
127}
128
129
130
131