<?php
require_once(dirname(__FILE__) . "/../../imCinemaAdmin/classes/Str.php");
require_once(dirname(__FILE__) . "/../../imCinemaAdmin/classes/movie.class.php");

if(!function_exists("makeFriendlyUrl")) {
    function makeFriendlyUrl($string, $separator = '-')
    {
        $friendly_url = trim(preg_replace('#[^\p{L}\p{N}]+#u', ' ', trim($string)));
        $frp = explode(" ", $friendly_url);
        if (count($frp)) {
            $glue = array();
            foreach ($frp as $fpart) {
                if (trim($fpart) != "") $glue[] = trim($fpart);
            }

            $friendly_url = implode($separator, $glue);
        }
        //$friendly_url .= "-" . date("Hs");

        return strtolower($friendly_url);
    }
}

$optres ['css'][] = $module_css_path . "amodules/im_cinemas/css/im_cinemas.css";
$optres['js'][] = $path . "assets/plugins/isotope/imagesloaded.pkgd.js";
$optres['js'][] = $path . "assets/plugins/isotope/isotope.pkgd.min.js";
$optres['css'][] = $module_css_path . "amodules/im_cinemas/css/im_playing.css";
$optres['js'][] = $path . "amodules/im_cinemas/js/im_playing.js";

$genres = [];
$playing = [];

$start_date_t = strtotime("previous thursday", strtotime("next thursday"));
$end_date_t = strtotime("previous wednesday", strtotime("next wednesday"));

if($end_date_t < $start_date_t) $end_date_t = strtotime("next wednesday");

$start_date = date("Y-m-d", $start_date_t);
$end_date = date("Y-m-d", $end_date_t);
$today = date("Y-m-d", time());

function nonEmptyValue($value) {
    return trim($value) != "";
}

// Get an array of now playing (np) movies
$np = [];

$q = "SELECT DISTINCT m.*, s.movie_3d, s.movie_gr FROM im_movies m, im_nowplaying s
WHERE
m.movie_code=s.movie_code
AND '$today' >= s.start_date
AND '$today' <= s.end_date
ORDER BY m.title ASC;";
//echo $q;
$r = @mysql_query($q);
if($r && mysql_num_rows($r) > 0) {
    while($row = mysql_fetch_assoc($r)) {
        $np [] = $row['movie_code'];
        $data = $row;
        $mgenres = array_map('trim', explode(",", $row['genres']));
        $mgenres = array_map('ucwords', $mgenres);
        $mgenres = array_filter($mgenres, "nonEmptyValue");
        $genres = array_merge($genres, $mgenres);
        $data['class_filter'] = implode(" ", array_map("strtolower", $mgenres));
        $playing [$row['movie_code']] = $data;
    }

    $genres = array_unique($genres);
}

$have_gr = false;
$have_3d = false;
$nps = "'" . implode("','", $np) . "'";
$q = "SELECT s.movie_code, s.movie_3d, s.movie_gr FROM im_nowplaying s WHERE '$today' >= s.start_date AND '$today' <= s.end_date";
$r = @mysql_query($q);
if($r && mysql_num_rows($r) > 0) {
    while ($row = mysql_fetch_assoc($r)) {
        $movie_code = $row['movie_code'];
        $movie_3d = intval($row['movie_3d']);
        $movie_gr = intval($row['movie_gr']);

        if($movie_gr == 1) {
            $playing[$movie_code]['class_filter'] .= " greek";
            $have_gr = true;
            //echo "<br>$movie_code in greek";
        }
        if($movie_3d == 1) {
            $playing[$movie_code]['class_filter'] .= " 3d";
            $have_3d = true;
        }
    }
}

if($have_gr) $genres [] = "Greek";
if($have_3d) $genres [] = "3D";

?>


<div class="button-group filter-button-group movies-filter">
    <a href="#" class="btn-filter active" data-filter=".all">All Genres</a>
    <?php
    foreach($genres as $gen) {
        $filter_class = trim(strtolower($gen));
        ?>
        <a href="#<?=$filter_class?>" class="btn-filter" data-filter=".<?=$filter_class?>"><?=$gen;?></a>
        <?php
    }
    ?>
</div>

<div class="grid">
        <div class="grid-sizer"></div>
        <?php
        foreach($playing as $rm_code=>$rm) {
            $poster = ($rm['poster_large'] != "") ? $rm['poster_large'] : $rm['poster'];
            $poster_file = dirname(__FILE__) . "/../../imCinemaAdmin/movies/" . $rm['movie_code'] . "/" . $poster;
            $no_poster_file = dirname(__FILE__) . "/../../imCinemaAdmin/movies/no-poster.png";
            $poster_path = file_exists($poster_file) ? $poster_file : $no_poster_file;

            $poster_path = str_replace(dirname(__FILE__) . "/../../", $full_uri, $poster_path);

            //$rurl = $homepage_url . "movie/" . Str::slug($rm['title'] . ' ' . $rm['year'], '-');
            $rurl = $homepage_url . "movie/" . makeFriendlyUrl($rm['title'] . ' ' . $rm['year'], '-');
            ?>
            <div class="grid-item all <?=$rm['class_filter'];?>">
                <div class="now-playing-movie">
                    <a href="<?= $rurl ?>">
                        <img src="<?= $poster_path; ?>" class="img-responsive"
                             rel="<?php echo $rm_code; ?>" alt="<?php echo $rm['title'] . ' (' . $rm['year'] . ')'; ?>">
                        <span class="rm-title"><?php echo $rm['title'] . ' (' . $rm['year'] . ')'; ?></span>
                    </a>
                </div>
            </div>
            <?php
        }
        ?>
</div>
<?php

if($gads_show && $def_ads == "yes" && $def_ad_id != "") {
    $adsense_container_class = "no-margin";
    echo '<div style="margin-bottom: 6px;">';
    include(dirname(__FILE__) . "/../gads_content/gads_content_default.php");
    echo '</div>';
    unset($adsense_container_class);
}

if(isset($_REQUEST['adsense_debug'])) {
    echo '<pre>';
    echo '$gads_show = ' . var_export($gads_show, true);
    echo '$def_ads = ' . var_export($def_ads, true);
    echo '$def_ad_id = ' . var_export($def_ad_id, true);
    echo '</pre>';
}