1<?php
2/**
3 * Breadcrumb (Deluxe) Plugin: a replacement for the internal breadcumb
4 *
5 * @license  GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author   Bjoern Ellebrecht <dokuwiki.development@c0de8.com>
7 *
8 * @noinspection PhpUnused,
9 *               PhpMissingParamTypeInspection, PhpMissingReturnTypeInspection
10 */
11
12// must be run within Dokuwiki
13use dokuwiki\File\PageResolver;
14
15if(!defined('DOKU_INC')) {
16    die();
17}
18
19class syntax_plugin_breadcrumbdeluxe extends DokuWiki_Syntax_Plugin
20{
21    final public function getType(): string
22    {
23        return 'substition';
24    }
25
26    final public function getPType(): string {
27        return 'normal';
28    }
29
30    final public function getSort(): string {
31        return 1;
32    }
33
34    final public function handle($match, $state, $pos, Doku_Handler $handler): bool
35    {
36        return true;
37    }
38
39    final public function render($format, Doku_Renderer $renderer, $data): void
40    {
41    }
42
43    /**
44     * Hierarchical breadcrumbs (Deluxe))  {copied from original dokuwiki and modified}
45     *
46     * This code was suggested as replacement for the usual breadcrumbs.
47     * It only makes sense with a deep site structure.
48     *
49     * @param ?string $separator Separator between entries
50     * @param bool $return return or print
51     * @return bool|string
52     */
53    final public function tpl_youarehere(?string $separator = null, bool $return = false) {
54        global $conf;
55        global $ID;
56        global $lang;
57
58        // check if enabled
59        if ($this->isBreadcrumbDisabled()) {
60            return false;
61        }
62
63        // set default
64        if ($separator === null) {
65            $separator = ' » ';
66        }
67
68        $out = '';
69
70        $parts = explode(':', $ID);
71        $count = count($parts);
72
73        // display label (you are here) for breadcrumb
74        $out .= '<span class="bchead">' . $lang['youarehere'] . ' </span>';
75
76        // always print the startpage
77        $out .= '<span class="home">' . tpl_pagelink(':' . ucfirst($conf['start']), null, true) . '</span>';
78
79        // print intermediate namespace links
80        $part = '';
81        for ($i = 0; $i < $count - 1; $i++) {
82            $part .= $parts[$i] . ':';
83            $page = $part;
84            if ($page == $conf['start']) continue; // Skip startpage
85
86            // output
87            $out .= $separator . tpl_pagelink($page, p_get_first_heading($parts[$i]), true);
88        }
89
90        // print current page, skipping start page, skipping for namespace index
91        if (isset($page)) {
92            $page = (new PageResolver('root'))->resolveId($page);
93            if ($page == $part . $parts[$i]) {
94                if ($return) return $out;
95                echo $out;
96                return true;
97            }
98        }
99        $page = $part . $parts[$i];
100        if ($page == $conf['start']) {
101            if ($return) return $out;
102            echo $out;
103            return true;
104        }
105
106        $out .= $separator;
107        $out .= tpl_pagelink($page, p_get_first_heading($page), true);
108
109        if ($return) {
110            return $out;
111        }
112
113        echo $out;
114
115        return (bool)$out;
116    }
117
118    private function isBreadcrumbDisabled(): bool
119    {
120        global $conf;
121
122        return (bool) !$conf['youarehere'];
123    }
124}
125