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

// delete this line as $t must have been inherited
//$t = new Movie();

if(isset($t) && $t instanceof Movie) {
    $genres = array_map('trim', explode(",", $t->genres));
    $related = [];

    $or = [];
    foreach($genres as $gen) {
        $or [] = "m.genres like '%$gen%'";
    }
    $or_sql = implode(" OR ", $or);


    $q = "SELECT DISTINCT m.* FROM im_movies m, im_showtimes s WHERE m.movie_code=s.movie_code AND m.movie_code!='".$t->movie_code."' AND ($or_sql) ORDER BY m.title ASC LIMIT 0,9;";
    //echo $q;
    $r = @mysql_query($q);
    if($r && mysql_num_rows($r) > 0) {
        while($row = mysql_fetch_assoc($r)) {
            $related [$row['movie_code']] = $row;
        }
    }

    if(count($related) > 0) {

        // Sort related by number of matches in genres
        foreach($related as $rm_code => $rm) {
            $rgenres = array_map('trim', explode(",", $rm['genres']));
            $related[$rm_code]['matches'] = count(array_intersect($genres, $rgenres));
        }

        $matches = [];
        foreach($related as $rm_code => $rm) {
            $matches[$rm_code] = $rm['matches'];
        }

        array_multisort($matches, SORT_DESC, $related);

        ?>
        <section class="container-fluid no-padding">
            <h3 class="related-title">Related Movies Playing This Week</h3>
            <div class="row">
                <?php
                $cnt = 1;
                foreach($related as $rm_code => $rm) {
                    if($cnt < 7) {
                        $img_path = "$include_www/imCinemaAdmin/movies/" . $rm_code . "/";
                        $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'], '-');
                        ?>
                        <div class="col-sm-33">
                            <div class="related-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
                    }
                    $cnt++;
                }
                ?>
            </div>
        </section>
        <?php
    }

    //echo '<pre>', var_export($related, true), '</pre>';
}