Forums / Cotonti / Extensions / Support / Autotab - create tabs on my pages where I want them

Reworking the autotoc extension

foxhound
#37631 2013-06-25 18:51

Hey Luke, I made some more changes and made many different tries already but still unsucessfull.
Here is the full php:

<?php
/* ====================
[BEGIN_COT_EXT]
Hooks=page.tags
[END_COT_EXT]
==================== */

defined('COT_CODE') or die('Wrong URL');

require_once cot_langfile('autotab');

$elems = explode(',', $cfg['plugin']['autotab']['elements']);
$text = $t->vars['PAGE_TEXT'];
$chapters = array();
$chapters_elem = array();

foreach($elems as $level => $elem)
{
    $elem = trim($elem);
    $headings = array();
    preg_match_all("`<$elem>(.*?)</$elem>`is", $text, $headings, PREG_OFFSET_CAPTURE);
    $headings = $headings[1];
    if (!$headings) continue;
    foreach($headings as $heading)
    {
        $title = $heading[0];
        $chapters[$heading[1]] = array($title, $level);
        $chapters_elem[$title] = $elem;
    }
}
ksort($chapters);

$toc = array();
$parents = array();
foreach($chapters as $chapter)
{
    list($title, $level) = $chapter;
    switch($level)
    {
        case 0:
            $toc[$title] = array();
            $parents[$level] = $title;
            break;
        case 1:
            $toc[$parents[0]][$title] = array();
            $parents[$level] = $title;
            break;
        default:
            $toc[$parents[0]][$parents[1]][$title] = array();
            break;
    }
}

function buildTOC(&$text, $chapters, $parents = '')
{
    global $chapters_elem;
    $i=0;
    $toc = '<div class="tabs-1"><ul class="tabs-nav clearfix">';
    foreach($chapters as $chapter_raw => $subchapters)
    {
        $chapter = strip_tags(trim($chapter_raw));
        $i++;
        $elem = $chapters_elem[$chapter_raw];
        $level = $parents.$i;
        $url = $_SERVER["REQUEST_URI"] . "#tab$level";
        $toc .= "<li><a href=\"$url\" title=\"$chapter\">$chapter</a>";
        // $text = str_replace("<$elem>$chapter_raw</$elem>", "<div class=\"tab-content\" id=\"tab$level\"><div class=\"gamelist\"><$elem>$level. $chapter</$elem>", "");
        //if (count($subchapters) > 0)
        //{
        // $toc .= buildTOC($text, $subchapters, $level.'.');
        $text = str_replace("<$elem>$chapter_raw</$elem>", "<div class=\"tab-content\" id=\"tab$level\"><div class=\"gamelist\"><$elem>$level. $chapter</$elem>", $text);
        // $text .= '</div></div>'; this does not work cause now it creates xx number, equal to the number of tabs cause it loop, of ending divs after the last tab text.
        //}
        $toc .= '</li>';
        // $text .= '</div>'; this does not work cause now it creates xx number, equal to the number of tabs cause it loop, of ending divs after the last tab text.
    }
    $end .= '</div></div>';
    $toc .= '</ul><div class="tabs-container">';
    $text .= '</div></div>'; // this div closes the tabs-1 and the tabs-container div!
    return $toc;
}

$t->assign('PAGE_TOC', buildTOC($text, $toc));
$t->assign('PAGE_TEXT', $text);

?>

 

 

And here is the html output:

<div class="fake">
<div class="tabs-1"><ul class="tabs-nav clearfix"><li><a href="/new_armaholic/index.php?e=page&c=page&al=test-12#tab1" title="tab1">tab1</a></li><li><a href="/new_armaholic/index.php?e=page&c=page&al=test-12#tab2" title="tab2">tab2</a></li><li><a href="/new_armaholic/index.php?e=page&c=page&al=test-12#tab3" title="tab3">tab3</a></li></ul>
<div class="tabs-container">
<div class="tab-content" id="tab1"><div class="gamelist"><h2>1. tab1</h2>
<h3>text tab 1</h3>
<div class="tab-content" id="tab2"><div class="gamelist"><h2>2. tab2</h2>
<h3>text tab 2</h3>
<div class="tab-content" id="tab3"><div class="gamelist"><h2>3. tab3</h2>
<h3>text tab 3</h3></div></div>
</div><!-- end fake class -->


The thing is, I need to close 2 divs after each H3 tag and 4 after the last H3 tag.
I can not figure out where I need to place the code to close the 2 divs in the right position, I can make them close after the last H3 without issues but I need it after all H3 tags.

I hope you may know how I can manage to do this.

<img src="http://www.armaholic.com/datas/thumbs/green-sea-battalion-uniforms-version-03-preview_4.jpg" alt="green-sea-battalion-uniforms-version-03-" />