<?php
/*
  osCommerce / osConcert

  Footer "Information" links box
*/

// Check to ensure this file is included in osConcert!
defined('_FEXEC') or die();

// ---------------------------------------------------------------------
// Config / template for each link
// ---------------------------------------------------------------------
	$MASTER_BOX_HTML='<li><i class="bi bi-chevron-right"></i><a href="{{LINK_1}}">{{NAME}}</a></li>'."\n";

$template_html   = $MASTER_BOX_HTML;

if (!defined('BOX_HEADING_INFORMATION')) {
    define('BOX_HEADING_INFORMATION', 'Information');
}

// ---------------------------------------------------------------------
// Recursive renderer for the footer category tree
// ---------------------------------------------------------------------
function tep_show_footer_category($counter, $depth = 0)
{
    global $static_foo,
           $page_categories_string,
           $page_display_id,
           $template_html,
           $BOX_CONTENT_ICON,
           $INITIAL_WIDTH;

    // Safety guard: avoid infinite recursion if data ever gets corrupted
    if ($depth > 1000) {
        // You could log here if you like.
        return;
    }

    if (!isset($static_foo[$counter])) {
        return;
    }

    // Build stcPath for this node
    if ((int)$static_foo[$counter]['parent'] === 0) {
        $stcPath_new = 'stcPath=' . $counter;
    } else {
        $stcPath_new = 'stcPath=' . $static_foo[$counter]['path'];
    }

    // Template replacement values
    $replace_array               = [];
    $replace_array['SPACE_WIDTH']  = ($static_foo[$counter]['level'] + 1) * 0;
    $replace_array['SPACER']       = $replace_array['SPACE_WIDTH'];
    $replace_array['NAME']         = $static_foo[$counter]['name'];
    $replace_array['ICON']         = isset($BOX_CONTENT_ICON) ? $BOX_CONTENT_ICON : '';
    $replace_array['LINK_1']       = tep_href_link(FILENAME_DEFAULT, $stcPath_new);
    $replace_array['INITIAL_WIDTH'] = isset($INITIAL_WIDTH) ? $INITIAL_WIDTH : 0;

    // Determine which ID is "selected" (last element of stcPath, if any)
    $selected = [];
    if (!empty($page_display_id) && is_array($page_display_id)) {
        $lastIndex = count($page_display_id) - 1;
        if ($lastIndex >= 0) {
            $selected[] = $page_display_id[$lastIndex];
        }
    }

    // Active / normal class
    if (!empty($selected) && in_array($counter, $selected, true)) {
        $replace_array['CLASS'] = 'list-group-item list-group-item-action list-group-item-primary active';
    } else {
        // Note: using $static_foo here, not $foo
        if (!empty($static_foo[$counter]['parent'])) {
            $replace_array['CLASS'] = 'list-group-item list-group-item-action';
        } else {
            $replace_array['CLASS'] = 'list-group-item list-group-item-action';
        }
    }

    // Apply replacements to template
    $replace_string = $template_html;
    foreach ($replace_array as $key => $value) {
        $replace_string = str_replace('{{' . $key . '}}', $value, $replace_string);
    }

    // Append to global string
    $page_categories_string .= $replace_string;

    // Recurse into linked sibling
    if (!empty($static_foo[$counter]['next_id'])) {
        tep_show_footer_category($static_foo[$counter]['next_id'], $depth + 1);
    }
}

// ---------------------------------------------------------------------
// Build the tree from the database
// ---------------------------------------------------------------------

$page_categories_string = '';
$static_foo             = [];
$static_first_element   = null;

// Root (parent_id = 0) visible pages for current language
$static_sql = "
  SELECT
    md.page_name,
    md.description,
    md.page_id,
    m.parent_id
  FROM " . TABLE_MAINPAGE . " m
  JOIN " . TABLE_MAINPAGE_DESCRIPTIONS . " md
    ON m.page_id = md.page_id
  WHERE m.parent_id = '0'
    AND m.page_status = 1
    AND md.language_id = '" . (int)$FSESSION->languages_id . "'
  ORDER BY m.sort_order, md.page_name
";

$static_query = tep_db_query($static_sql);
$static_prev_id = null;

while ($static_pages = tep_db_fetch_array($static_query)) {
    $id = (int)$static_pages['page_id'];

    $static_foo[$id] = [
        'name'    => $static_pages['page_name'],
        'parent'  => (int)$static_pages['parent_id'],
        'level'   => 0,
        'path'    => (string)$id,
        'next_id' => false,
    ];

    if ($static_prev_id !== null) {
        $static_foo[$static_prev_id]['next_id'] = $id;
    }

    $static_prev_id = $id;

    if ($static_first_element === null) {
        $static_first_element = $id;
    }
}

// ---------------------------------------------------------------------
// Handle deeper paths if $stcPath is present (sub-pages)
// ---------------------------------------------------------------------
if (!empty($stcPath)) {
    $static_new_path  = '';
    $page_display_id  = preg_split('/_/', $stcPath);
    $page_display_id  = array_filter($page_display_id, 'strlen'); // remove empty bits
    $page_display_id  = array_map('intval', $page_display_id);

    foreach ($page_display_id as $static_key => $static_value) {
        unset($static_prev_id, $static_first_id, $static_last_id);

        $static_query = tep_db_query("
          SELECT
            md.page_id,
            md.page_name,
            m.parent_id
          FROM " . TABLE_MAINPAGE . " m
          JOIN " . TABLE_MAINPAGE_DESCRIPTIONS . " md
            ON m.page_id = md.page_id
          WHERE m.parent_id = '" . (int)$static_value . "'
            AND m.page_status = 1
            AND md.language_id = '" . (int)$FSESSION->languages_id . "'
          ORDER BY m.sort_order, md.page_name
        ");

        $static_category_check = tep_db_num_rows($static_query);

        if ($static_category_check > 0) {
            $static_new_path .= $static_value;

            while ($row = tep_db_fetch_array($static_query)) {
                $child_id = (int)$row['page_id'];

                $static_foo[$child_id] = [
                    'name'    => $row['page_name'],
                    'parent'  => (int)$row['parent_id'],
                    'level'   => $static_key + 1,
                    'path'    => $static_new_path . '_' . $child_id,
                    'next_id' => false,
                ];

                if (isset($static_prev_id)) {
                    $static_foo[$static_prev_id]['next_id'] = $child_id;
                }

                $static_prev_id = $child_id;

                if (!isset($static_first_id)) {
                    $static_first_id = $child_id;
                }

                $static_last_id = $child_id;
            }

            // Splice this sub-list into the main chain
            if (isset($static_last_id, $static_first_id, $static_foo[$static_value])) {
                $static_foo[$static_last_id]['next_id']   = $static_foo[$static_value]['next_id'];
                $static_foo[$static_value]['next_id']     = $static_first_id;
            }

            $static_new_path .= '_';
        } else {
            // No children at this level; stop descending
            break;
        }
    }
}

// ---------------------------------------------------------------------
// Render the chain (if there is anything to show)
// ---------------------------------------------------------------------
if (!empty($static_first_element) && isset($static_foo[$static_first_element])) {
    tep_show_footer_category($static_first_element);
}

echo $page_categories_string;
?>

<!-- information_eof //-->
