<?

global $dbconn;

require_once('include/bdmo.inc');
require_once('include/search.inc');
require_once('include/pageLayout.inc');
 
entete();

$stmt = $dbconn->query('SELECT id_maj, date_maj FROM maj ORDER BY id_maj DESC LIMIT 1');
$result = $stmt->fetchObject();

$majmax = $result->id_maj;
$lastupdate = $result->date_maj;

$newsStmt = $dbconn->query('SELECT * FROM news WHERE actif = 1 ORDER BY date DESC');

$js = '';
$nav = '';
?>

    <TABLE BORDER="0" CELLPADDING="5" CELLSPACING="0" WIDTH="100%">
        <TR>
            <TD WIDTH="40%" VALIGN="top" ROWSPAN="2">
                <TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0" WIDTH="100%">
                    <TR>
                        <TD>
                            <FONT CLASS="small"><I><?= $captLastupdate ?>: <?= strftime(
                                        '%d.%m.%Y',
                                        strtotime($lastupdate)
                                    ) ?></I></FONT><BR>
                            <BR>
                        </TD>
                    </TR>

                    <?php

                    while ($news = $newsStmt->fetchObject()) {
                        if ($news->libelle_en != null) {
                            $libelle = str_replace('&', '&amp;', $news->libelle_en);
                            $libelle = stripslashes($libelle);
                            $libelle = str_replace('BORDER="2">', 'ALT="" BORDER="2">', $libelle);

                            ?>
                            <TR>
                                <TD ALIGN="left"><B><?= strftime('%d.%m.%Y', strtotime($news->date)) ?></B><BR><BR></TD>
                            </TR>

                            <TR>
                                <TD ALIGN="left"><?= nl2br($libelle, false) ?><BR><BR><BR></TD>
                            </TR>
                            <?php
                        }
                    }
                    ?>
                </TABLE>
            </TD>
            <TD class="selections clearfix" VALIGN="TOP">
                <?php

                $featStmt = $dbconn->query(
                    'SELECT ref_disque, f.featured_category_id, category_name_en FROM featured f INNER JOIN featured_categories fc ON f.featured_category_id = fc.featured_category_id WHERE fc.active = 1 ORDER BY f.featured_category_id, f.numero ASC'
                );
                $currentCat = 0;

                $groupesStmt = $dbconn->prepare(
                    'SELECT nom, groupe.id_groupe AS id_groupe FROM disque_groupe LEFT JOIN groupe ON disque_groupe.id_groupe = groupe.id_groupe WHERE disque_groupe.ref_disque = :ref_disque'
                );
                $groupesStmt->bindParam(':ref_disque', $ref_disque, PDO::PARAM_INT);

                $labelsStmt = $dbconn->prepare(
                    'SELECT nom, label.id_label AS id_label FROM disque_label LEFT JOIN label ON disque_label.id_label = label.id_label WHERE disque_label.ref_disque = :ref_disque'
                );
                $labelsStmt->bindParam(':ref_disque', $ref_disque, PDO::PARAM_INT);

                $disquesStmt = $dbconn->prepare(
                    'SELECT commentaire, disc_qty, enstock, epuise, id_maj, pochette, prix, promo, release_date, support, titre FROM disque WHERE ref_disque = :ref_disque'
                );
                $disquesStmt->bindParam(':ref_disque', $ref_disque, PDO::PARAM_INT);

                while ($feat = $featStmt->fetchObject()) {
                $ref_disque = $feat->ref_disque;

                $groupesStmt->execute();
                $groupes = [];
                while ($resultGroupes = $groupesStmt->fetchObject()) {
                    if (in_array($resultGroupes->nom, ['VARIOUS', 'V/A'])) {
                        $groupes[] = $resultGroupes->nom;
                    } else {
                        $groupes[] = '<A HREF="/catalog.php?type=idg&amp;val=' . $resultGroupes->id_groupe . '">' . $resultGroupes->nom . '</A>';
                    }
                }
                $groupes = implode('/', $groupes);

                $labelsStmt->execute();
                $labels = [];
                while ($resultLabels = $labelsStmt->fetchObject()) {

                    if (!in_array($resultLabels->nom, ['SANSLABEL', 'DIVERS/MISC'])) {
                        $labels[] = '<A HREF="/catalog.php?type=idl&amp;val=' . $resultLabels->id_label . '">' . $resultLabels->nom . '</A> ';
                    }
                }
                $affichelabs = implode('/', $labels);

                $disquesStmt->execute();
                while ($disquesResult = $disquesStmt->fetchObject()) {

                $titre = stripSlashes($disquesResult->titre);

                if ($disquesResult->pochette != '') {
                    $pochette = '<IMG SRC="/images/pochettes/' . $disquesResult->pochette . '" BORDER="0" WIDTH="90px" ALT="' . htmlspecialchars(
                            $titre
                        ) . '">';
                } else {
                    $ItemNr = '00' . substr("00000000" . $ref_disque, -6);
                    $prodImagesDir = isset($_ENV['PROD_IMAGES_DIR']) ? $_ENV['PROD_IMAGES_DIR'] : '';

                    $imgName = "images/prod/I" . substr($ItemNr, 0, 2) . "/" . substr(
                            $ItemNr,
                            2,
                            3
                        ) . "/" . $ItemNr . ".jpg";
                    if (file_exists($prodImagesDir . $imgName)) {
                        $pochette = '<IMG SRC="/' . $imgName . '" BORDER="0" WIDTH="90px" ALT="' . htmlspecialchars(
                                $titre
                            ) . '">';
                    } else {
                        $pochette = '<BR>';
                    }
                }

                if ($disquesResult->id_maj == $majmax) {

                    $affichenew = ' <FONT CLASS="new"><B>' . $captNew . '</B></FONT>';
                } else {
                    $affichenew = '';
                }

                if ($disquesResult->promo == 1) {
                    $affichepromo = '<FONT CLASS="taux_prom small"><B>' . $captCataPromo . '</B></FONT>';
                } else {
                    $affichepromo = '';
                }

                if ($disquesResult->enstock == 1) {
                    $dispo = '<FONT CLASS="xsmall" style="color:#99CC99"><B>' . $captEnstock . '</B></FONT>';
                    $btn = '<IMG SRC="/images/menu/en/acheter.gif" BORDER="0" ALT="Buy Now">';
                } elseif ($disquesResult->epuise == 0) {
                    $dispo = '<FONT CLASS="xsmall" style="color:#6699FF"><B>' . $capt23sem . '</B></FONT>';
                    $btn = '<IMG SRC="/images/menu/en/reserver.gif" BORDER="0" ALT="Order">';
                } else {
                    $dispo = '';
                    $btn = '';
                }

                $commentaire = $disquesResult->commentaire;
                if (strlen($commentaire) < 350) {
                    $commentaire = htmlspecialchars(stripSlashes($commentaire)) . '<BR>';
                } else {
                    $commentaire = htmlspecialchars(substr(stripSlashes($commentaire), 0, 346))
                        . ' ... <A HREF="catalog.php?type=rd&amp;val=' . $ref_disque . '"><B>' . $captHomeSuite . '</B></A><BR>';

                    $nb_b = substr_count($commentaire, "<B>");
                    $nb_slash_b = substr_count($commentaire, "</B>");

                    if ($nb_b > $nb_slash_b) {
                        $commentaire .= '</B>';
                    }
                }

                if (isset($cookieSet)) {
                    $affichePrix = '<FONT CLASS="small">' . $captCataPrix . '&nbsp;:&nbsp;<B>' . formatPrix(
                            $disquesResult->prix
                        ) . '</B></FONT>';
                } else {
                    $affichePrix = '<a href="/client/index.php" title="' . $captLogin . '">[' . $captLogin . ']</a>';
                }


                if ($feat->featured_category_id != $currentCat) {
                    if ($currentCat > 0) {
                        echo '</TABLE>';
                        $js .= 'document.getElementById("selection-' . $feat->featured_category_id . '").style.display="none";' . PHP_EOL;
                    }

                    $theCategoryName = $feat->category_name_en;
                    echo '<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0" ID="selection-' . $feat->featured_category_id . '" CLASS="one-selection" width="100%">';

                    // create navigation to load record selections
                    $nav = $nav . '<li';

                    if ($currentCat == 0) {
                        $nav = $nav . ' class="active"';
                    }

                    $nav = $nav . '><a href="javascript:;" title="selection-' . $feat->featured_category_id . '">' . $theCategoryName . '</a></li>';

                    $currentCat = $feat->featured_category_id;
                }

                ?>

        <TR>
            <TD>
                <TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0" WIDTH="100%" CLASS="one-record">
                    <TR>
                        <TD width="100%">
                            <TABLE BORDER="0" CELLPADDING="3" CELLSPACING="0" WIDTH="100%" BGCOLOR="#4c4c7f">
                                <TR>
                                    <TD VALIGN="top" ROWSPAN="2" WIDTH="0%"><A
                                            HREF="catalog.php?type=rd&amp;val=<?= $ref_disque ?>"><?= $pochette ?></A>
                                    </TD>
                                    <TD COLSPAN="3" VALIGN="top" WIDTH="100%">
                                        <B><?= $groupes ?></B><BR>
                                        <FONT CLASS="xsmall">
                                            <B><?= $titre ?></B> <FONT CLASS="xsmall">(<?= afficheSupport(
                                                    $disquesResult->support
                                                ) ?>)</FONT><?= $affichenew . ' ' . $affichepromo ?><BR>
                                            <?= $affichelabs ?><BR>
                                        </FONT>
                                        <FONT CLASS="small"><br>
                                            <?= $commentaire ?>
                                        </FONT>
                                        <BR>
                                        <FONT CLASS="xsmall">
                                            <?= $captDiscQuantity . ' ' . $disquesResult->disc_qty ?><br>
                                            <?= $captReleaseDate . ' ' . $disquesResult->release_date ?><br>
                                            <?= $captCataRef . ' : ' . $ref_disque ?><BR>
                                            <?= $captCataDispo . ' : ' . $dispo ?><BR>
                                        </FONT>
                                    </TD>
                                </TR>
                                <TR>
                                    <TD width="100%"><FONT CLASS="small"><?= $captCataFormat ?> : </FONT><FONT
                                            CLASS="xsmall"><B><?= afficheSupport($disquesResult->support) ?></B></FONT>
                                    </TD>
                                    <TD align="right" width="0%"><?= $affichePrix ?></TD>
                                    <TD ALIGN="right" width="0%">
                                        <A HREF="/caddie/index.php?ref_disque=<?= $ref_disque ?>"><?= $btn ?></A>
                                    </TD>
                                </TR>
                            </TABLE>
                        </TD>

                    </TR>

                </TABLE>


            </TD>
        </TR>

        <?php

        }
        }
        ?>
    </TABLE>
    <ul id="navigation-selection"><?= $nav ?></ul>
<?php
if (strlen($js) > 0) {
    ?>
    <script type="text/javascript">/* <![CDATA[ */
        <?= $js ?>
        /* ]]> */
    </script>
    <?php
}

?>
    <BR><BR><BR><BR>

<?php
footer();