<?php
use eftec\bladeone\BladeOne;

$views = __DIR__ . '/views';
$cache = __DIR__ . '/cache';
if(!defined("BLADEONE_MODE")) {
    define("BLADEONE_MODE", 1); // (optional) 1=forced (test),2=run fast (production), 0=automatic, default value.
}
$blade = new BladeOne($views, $cache);

$rppg = [
    "theme" => "default",
    "promo_cat_id" => null,
    "show_left_side" => 1,
    "show_right_side" => 1
];

if(isset($module_params['rppg']) ) {
    $rppg = $module_params['rppg'];
};
$fara = 0;

if(intval($rppg['show_left_side']) == 0) {
    // hide left sidebar
    $cf_show_left_side = false;
    $fara += 1;
}

if(intval($rppg['show_right_side']) == 0) {
    // hide banners sidebar
    $cf_show_right_side = false;
    $fara += 1;
}

// Promos list
$promos = [];

//echo '<pre>', var_export($module_params, true), '</pre>';
//echo '<pre>', var_export($ribbon_promo_settings, true), '</pre>';


require_once(__DIR__ . "/themes/{$rppg['theme']}/theme_includes.php");
$optres['css'][] = $module_css_path . "amodules/ribbon_promo_pages_gallery/css/{$rppg['theme']}-ribbon_promo_pages_gallery.css";
$optres['js'][] = $path . "assets/plugins/shuffle/jquery-shuffle.min.js";
$optres['js'][] = $path . "amodules/ribbon_promo_pages_gallery/js/{$rppg['theme']}-ribbon_promo_pages_gallery.js";



if(is_array($rppg) && $rppg['promo_cat_id'] > 0 ) {
    $q = "SELECT * FROM promos WHERE promo_cat_id='{$rppg['promo_cat_id']}' AND status='1' ORDER BY strength ASC, RAND();";
    //echo $q;
    $r = @mysql_query($q);
    if ($r && mysql_num_rows($r) > 0) {
        while ($row = mysql_fetch_assoc($r)) {
            $promos[$row['id']] = array_map('stripslashes', $row);
        }
    }
}
//echo '<pre>', var_export($ribbon_promo_buttons, true), '</pre>';
if (is_array($promos) && count($promos) > 0) {
    $num_elements = count($promos) + 0;
    $ribbon_promo_cnt = 1;
    $data_merge = 1;
    $images_folder = __DIR__ . "/../../images/promos/$domain_id/";
    foreach ($promos as $p_id => $p_data) {
        $rel = [];
        if($p_data['follow'] != 1) {
            $rel [] = 'nofollow';
        }

        if($p_data['url_target'] == "_blank") {
            $rel [] = "noopener noreferrer";
        }

        $target_attr = $p_data['url_target'];

        $promo_advert = null;
        $promo_advert_id = $p_data['advertisment_id'] > 0 ? $p_data['advertisment_id'] : null;
        if($promo_advert_id > 0) {
            $qpa = "SELECT * FROM advertisment WHERE advertisment_id='$promo_advert_id';";
            $rqa = @mysql_query($qpa);
            if($rqa && mysql_num_rows($rqa) > 0) {
                $padv = mysql_fetch_assoc($rqa);

                $promo_advert = array_map('stripslashes', $padv);

                $advert_local_url = $homepage_url . $promo_advert['title_tr'];
                $advert_website = $promo_advert['website'];

                if(trim($p_data['url']) == "") {
                    $promos[$p_id]['url'] = $advert_website;
                    $rel = [];
                    if($promo_advert['show_website'] == 1) {
                        if($promo_advert['nofollow'] == 1) $rel [] = 'nofollow';
                        $rel [] = "noopener noreferrer";
                        $target_attr = "_blank";
                    }
                    else {
                        $promos[$p_id]['url'] = $advert_local_url;
                        $rel [] = "noopener noreferrer";
                        $target_attr = "_blank";
                    }
                }

            }
        }




        $rel_attr = count($rel) > 0 ? 'rel="'.(implode(" ", $rel)).'"' : '';


        $promos[$p_id]['rel_attr'] = $rel_attr;
        $promos[$p_id]['target_attr'] = $target_attr;


        $image_path = $images_folder . "{$p_data['promo_cat_id']}/" .  $p_data['id'] . "/{$p_data['image']}";
        if(file_exists($image_path)) {
            $image_url = ($path == "" ? ($protocol . $ultra_domainname . "/") : $path) . "images/promos/$domain_id/{$p_data['promo_cat_id']}/{$p_data['id']}/{$p_data['image']}";
            $image_url_th = ($path == "" ? ($protocol . $ultra_domainname . "/") : $path)  . "images/promos/$domain_id/{$p_data['promo_cat_id']}/{$p_data['id']}/thumbs/{$p_data['image']}";
        }

        $promos[$p_id]['image_url'] = $image_url;
        $promos[$p_id]['image_url_th'] = $image_url_th;
    }

    // Show the click-shout text
    $optres['css'][] = $module_css_path  . 'amodules/adverts/css/click_shout.css';
    $click_shout_replacement = '<div class="click-shout"><i class="fa fa-hand-pointer-o fa-rotate-180 fa-lg"></i> Click on photos to visit the websites</div>';

    $img_placeholder = $path . "images/advertisments/placeholder.png";
    $ribbon_promo_pages_gallery_view = $blade->run("{$rppg['theme']}/ribbon_promo_pages_gallery_default",
        [
            "promos" => $promos,
            "num_elements" => $num_elements,
            "settings"  => $rppg,
            "id"            => "rppg-" . $rppg['promo_cat_id'],
            "fara" => $fara > 0 ? 'fara-' . $fara : '',
            "img_placeholder" => $img_placeholder
        ]);

    echo $ribbon_promo_pages_gallery_view;

}