1<?php
2/**
3 * Copyright (c) 2020. ComboStrap, Inc. and its affiliates. All Rights Reserved.
4 *
5 * This source code is licensed under the GPL license found in the
6 * COPYING  file in the root directory of this source tree.
7 *
8 * @license  GPL 3 (https://www.gnu.org/licenses/gpl-3.0.en.html)
9 * @author   ComboStrap <support@combostrap.com>
10 *
11 */
12
13namespace ComboStrap;
14
15
16use Doku_Renderer;
17use DokuWiki_Admin_Plugin;
18
19class TocUtility
20{
21
22    public static function renderToc($renderer)
23    {
24        global $conf;
25        global $TOC;
26
27        // If the TOC is null (The toc may be initialized by a plugin)
28        if (!is_array($TOC) or count($TOC) == 0) {
29            $TOC = $renderer->toc;
30        }
31
32        if (count($TOC) > $conf['tocminheads']) {
33            return tpl_toc($return = true);
34        } else {
35            return "";
36        }
37    }
38
39    /**
40     * @param Doku_Renderer $renderer
41     * @return bool if the toc need to be shown
42     *
43     * From {@link Doku_Renderer::notoc()}
44     * $this->info['toc'] = false;
45     * when
46     * ~~NOTOC~~
47     */
48    public static function showToc($renderer)
49    {
50
51        global $ACT;
52
53
54        /**
55         * Search page, no toc
56         */
57        if ($ACT == 'search') {
58
59            return false;
60
61        }
62
63
64        /**
65         * If this is another template such as Dokuwiki, we get two TOC.
66         */
67        if (!Site::isStrapTemplate()){
68            return false;
69        }
70
71        /**
72         * On the admin page
73         */
74        if ($ACT == 'admin') {
75
76            global $INPUT;
77            $plugin = null;
78            $class = $INPUT->str('page');
79            if (!empty($class)) {
80
81                $pluginList = plugin_list('admin');
82
83                if (in_array($class, $pluginList)) {
84                    // attempt to load the plugin
85                    /** @var $plugin DokuWiki_Admin_Plugin */
86                    $plugin = plugin_load('admin', $class);
87                }
88
89                if ($plugin !== null) {
90                    global $TOC;
91                    if (!is_array($TOC)) $TOC = $plugin->getTOC(); //if TOC wasn't requested yet
92                    if (!is_array($TOC)) {
93                        return false;
94                    } else {
95                        return true;
96                    }
97
98                }
99
100            }
101
102        }
103
104
105        if (isset($renderer->info['toc'])){
106            return $renderer->info['toc'];
107        } else {
108            return true;
109        }
110
111    }
112}
113