<?php 
// Check if this file is included in osConcert
defined('_FEXEC') or die();

/*
    AJAX Multiuser Seat Reservations
    for osConcert, Online Seat Booking
    by Martin Zeitler, Germany
*/

// Cart state
$cart_count = (int)$cart->count_contents();
$cart_qty   = (int)$cart->count_contents_gt();
$cart_total = $currencies->format($cart->show_total());

$has_cart = ($cart_count > 0);

// Signup style
$sign_up_style = $has_cart
    ? 'style="padding-top:12px; display:block; z-index:100;"'
    : 'style="display:none;"';

// Legend
require_once(DIR_WS_TEMPLATES . TEMPLATE_NAME . '/content/legend.php');
?>
<?php if ($parent_id > 0): ?>
    <div class="text-center">
        <h3><?php echo $category['categories_name']; ?></h3>
        <h3><?php echo $formatted_date . ' ' . $formatted_time; ?></h3>
    </div>
<?php endif; ?>

<?php
// Box office refund mode
if (isset($_SESSION['box_office_refund'])) {

    require_once(DIR_WS_MODULES . 'triple_boxes.php');

} else {

    // Optional how-to guide
    if (BOX_WIDTH_LEFT == 'yes' && HOW_TO_GUIDE == 'true') {
        require_once(DIR_WS_TEMPLATES . TEMPLATE_NAME . '/content/howto.php');
    }
    ?>

    <div id="btnCheckOut" style="display:none;">
        <h2 class="m-0">
            <a class="btn btn-outline-light btn-sm" href="<?php echo tep_href_link('shopping_cart.php'); ?>">
                <?php echo sprintf(
                    MODULE_NAVBAR_SHOPPING_CART_HAS_CONTENTS,
                    $cart_qty,
                    $cart_total
                ); ?>
            </a>

            <a class="btn btn-success btn-sm" href="<?php echo tep_href_link(FILENAME_CHECKOUT_SHIPPING, '', 'SSL'); ?>">
                <?php echo defined('IMAGE_BUTTON_CHECKOUT') ? IMAGE_BUTTON_CHECKOUT : 'Checkout'; ?>
            </a>
        </h2>
    </div>

<?php } ?>
<?php
// Check caching
$caching = false;
if (defined('SEAT_PLAN_CACHE') && SEAT_PLAN_CACHE === 'true') {
    $caching = true;
}

$expiry = 240 * 60; // Cache expiry: 240 minutes
//$timeout = true;

// Handle different scenarios
if (isset($_SESSION['box_office_refund']) && $_SESSION['box_office_refund'] == 'yes') {
    if(DESIGN_MODE == 'yes') {
        echo $sp->tep_renderSeatplanCSS($cPath);
        echo $sp->tep_renderSeatplanRefundDesign($cPath);
    } else {
        echo $sp->tep_renderSeatplanRefund($cPath);
    }
} elseif (isset($_SESSION['box_office_reservation']) && $_SESSION['box_office_reservation'] == 'yes') {
    if(DESIGN_MODE == 'yes') {
	echo $sp->tep_renderSeatplanCSS($cPath);
	echo $sp->tep_renderSeatplanReservationDesign($cPath);
	} else {
	echo $sp->tep_renderSeatplanReservation($cPath);
	}
} else {
    if ($caching) {
        $path = DIR_FS_CATALOG.'/cache/';
        $cache = $path.'section'.$cPath.'.html';

        if(!file_exists($path)) {
            mkdir($path, 0755);
        }

        if (file_exists($cache) && (time() - filemtime($cache) < $expiry)) {
            echo "\n<!-- Cached on ".date('jS F Y @ H:i', filemtime($cache))." -->\n";
            include($cache);
        } else {
            if(file_exists($cache)) {
                unlink($cache);
            }

            ob_start();
            if ((DESIGN_MODE == 'yes') && ($manufacturers_id > 4 && $manufacturers_id < 9)) {
                echo $sp->tep_renderSeatplanCSS($cPath);
                echo $sp->tep_renderSeatplanDesign($cPath);
            } else {
                if ($plan_id < 5) {
                    echo $sp->tep_renderSeatplan($cPath);
                }
            }
            $fp = fopen($cache,'w');
            fwrite($fp,ob_get_contents());
            fclose($fp);
            ob_end_flush();
        }
    } else {
        echo "\n<!-- Rendered on ".date('jS F Y @ H:i', time())." -->\n";
        if ((DESIGN_MODE == 'yes') && ($manufacturers_id > 4 && $manufacturers_id < 9)) {
            echo $sp->tep_renderSeatplanCSS($cPath);
            echo $sp->tep_renderSeatplanDesign($cPath);
        } else {
            if ($plan_id < 5) {
                echo $sp->tep_renderSeatplan($cPath);
            }
        }
    }
}

// Display how to guide at bottom
if (BOX_WIDTH_LEFT == 'no' && HOW_TO_GUIDE == 'true') {
    require_once(DIR_WS_TEMPLATES.TEMPLATE_NAME.'/content/howto.php');
}

// Enforce login if necessary
if (SEATPLAN_LOGIN_ENFORCED == 'true' && !$FSESSION->is_registered('customer_id')) {
    require_once(DIR_WS_INCLUDES.'login_enforced.php');
}
?>

