<?php

// Wiki plugin for the integration of a page outline
// By Daniel Kasmeroglu, Copyright (C) 2011. All rights reserved.
// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE (LGPL).

// this script may only be included - so it's better to die if called directly.
if (strpos($_SERVER["SCRIPT_NAME"],basename(__FILE__)) !== false) {
    header("location: index.php");
    die;
}

function wikiplugin_externaltoc_help() {
    return tra("Displays an outline of a wikipage.").":<br />~np~{EXTERNALTOC(wikipage='foo')}~/np~";
}

function wikiplugin_externaltoc_info() {
    return array(
        'name' => tra('Externaltoc'),
        'description' => tra('Display page content in a set of tabs'),
        'validate' => 'all',
        'params' => array(
            'page' => array(
                'required' => false,
                'name' => tra('Wikipage'),
                'description' => tra('The wikipage which outline shall be displayed.'),
            ),
            'title' => array(
                'required' => false,
                'name' => tra('Title'),
                'description' => tra('A title to be used'),
            ),
            'type' => array(
                'required' => false,
                'name' => tra('Type'),
                'description' => tra('Create a box layout if desired. Allowed values: n|box. Default: n.'),
            ),
            'maxdepth' => array(
                'required' => false,
                'name' => tra('Maximum depth'),
                'description' => tra('The maximum depth to be used.'),
                'filter' => 'int',
            ),
            'levels' => array(
                'required' => false,
                'name' => tra('Levels'),
                'description' => tra('The levels which have to be displayed.'),
                'filter' => 'int',
            ),
            'nums' => array(
                'required' => false,
                'name' => tra('Numbers'),
                'description' => tra('Display numbers. Allowed values: y|n. Default: y.'),
            ),
        ),
    );
}

function wikiplugin_externaltoc($data, $params) {

    global $tikilib;

    extract ($params,EXTR_SKIP);

    $originalpage = '';
    foreach ( $_SERVER as $entry ) {
        if (strpos($entry,"page=") === 0) {
            $originalpage = trim(substr($entry,5));
        }
    }

    if (!isset($page)) {
        $page = $originalpage;
        if (strlen($page) === 0) {
            // there's no wiki page, so we don't have an outline yet
            return "";
        }
    }

    if (!isset($type)) {
        $type = 'n';
    }
    if (!isset($maxdepth)) {
        $maxdepth = 0;
    }
    if (!isset($nums)) {
        $nums = 'y';
    }

    // we're on the same page, so we can use the original function 'maketoc' instead
    if ($page === $originalpage) {
        $args  = "{maketoc ";
        if ($type === "box") {
            $args .= "type=box ";
        }
        $args .= "nums=".$nums." ";
        if (isset($title)) {
            $args .= "title=".$title." ";
        } else {
            $args .= "title=notitle ";
        }
        $args .= "maxdepth=".$maxdepth." ";
        if (isset($levels)) {
            $args .= "levels=".$levels." ";
        }
        $args .= "}";
        return $args;
    }

    $anch = $tikilib->get_page_toc_array( $page );

    $externaltoc = '';

    if ( $type === "box" ) {
        $externaltoc = "<table id='toc' class='toc'>\n<tr><td>";
        if (isset($title) && (!empty($title))) {
            $externaltoc .= $title;
        }
        $externaltoc .= "<ul>";
        $externaltoc_header = '';
        $externaltoc_footer = "</ul></td></tr></table>\n";
        $link_class = 'toclink';
    } else {
        $externaltoc = '';
        $externaltoc_header = "<div id='toc'>";
        if (isset($title) && (!empty($title))) {
            $externaltoc_header .= $title;
        }
        $externaltoc_footer = '</div>';
        $link_class = 'link';
    }

    // render the outline
    if ( count($anch) ) {
    
        foreach ( $anch as $tocentry ) {
            if ( $maxdepth > 0 && $tocentry['hdrlevel'] > $maxdepth ) {
                continue;
            }
            if (!empty($levels) && !in_array($tocentry['hdrlevel'], $levels)) {
                continue;
            }
            // Generate the toc entry title (with nums)
            if ( $nums == 'n' ) {
                $tocentry_title = '';
            } else {
                $tocentry_title = $tocentry['title_displayed_num'];
            }
            $tocentry_title .= $tocentry['title'];
            // Generate the toc entry link
            if ( $tocentry['pagenum'] > 1 ) {
                $tocentry_link = $_SERVER['PHP_SELF'].'?page='.$page.'&pagenum='.$tocentry['pagenum'].'#'.$tocentry['id'];
            } else {
                $tocentry_link = $_SERVER['PHP_SELF'].'?page='.$page.'#'.$tocentry['id'];
            }
            $tocentry_title = "<a href='$tocentry_link' class='link'>".$tocentry_title.'</a>';
            if ( $externaltoc != '' ) {
                 $externaltoc.= "\n";
            }
            $shift = $tocentry['hdrlevel'];
            if (!empty($levels)) {
                for ($i = 1; $i <= $tocentry['hdrlevel']; ++$i) {
                    if (!in_array($i, $levels)) {
                        --$shift;
                    }
                }
            }
            if ( $type === "box" ) {
                $externaltoc .= "<li class='toclevel-".$shift."'>".$tocentry_title."</li>";
            } else {
                $externaltoc .= str_repeat('*', $shift).$tocentry_title;
            }
        }
        $externaltoc = $tikilib->parse_data($externaltoc );
        if (preg_match("/^<ul>/", $externaltoc)) {
            $externaltoc = preg_replace("/^<ul>/", '<ul class="toc">', $externaltoc);
            $externaltoc .= '<!--toc-->';
        }
        if ( $link_class != 'link' ) {
            $externaltoc = preg_replace("/'link'/", "'$link_class'", $externaltoc);
        }
    }
    return $externaltoc_header.$externaltoc.$externaltoc_footer;

}
