<?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);

$interlinks_defaults = [
    'theme' => "default",
    'title' => null,
    'show_title' => 0
];

if(isset($module_params['interlinks']) ) {
    $interlinks_defaults = $module_params['interlinks'];
};


// Promos list
$interlinks = [];

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


require_once(dirname(__FILE__) . "/themes/{$interlinks_defaults['theme']}/theme_includes.php");
$optres['css'][] = $module_css_path . "amodules/interlinks/css/{$interlinks_defaults['theme']}-interlinks.css";
//$optres['css'][] = $path . "amodules/interlinks/css/{$interlinks_defaults['theme']}-interlinks.css";
$optres['js'][] = $path . "amodules/interlinks/js/{$interlinks_defaults['theme']}-interlinks.js";

$module_catdom_id = (isset($catdom_id) && $catdom_id != $domain_id) ? $catdom_id : $domain_id;

if(is_array($interlinks_defaults) ) {

    $q = "SELECT * FROM interlinks WHERE from_catdom_id='$module_catdom_id' ORDER BY anchor ASC;";
    //echo $q;
    $r = @mysql_query($q);
    if ($r && mysql_num_rows($r) > 0) {
        while ($row = mysql_fetch_assoc($r)) {
            $interlinks[$row['id']] = array_map('stripslashes', $row);
        }
    }
}
//echo '<pre>', var_export($ribbon_promo_buttons, true), '</pre>';
if (is_array($interlinks) && count($interlinks) > 0) {

    foreach ($interlinks as $l_id=>$l_data) {
        $l_data['content'] = nl2br($l_data['content']);
        $link = (object) $l_data;

        switch ($link->anchor_type) {
            case "exact":
                if($link->anchor_as_title != 1) {
                    $content = $link->content;
                    $anchor = $link->anchor;
                    $url = '<a href="' . $link->url . '" title="'.$link->title.'" target="_blank" rel="noopener">'.$anchor.'</a>';
                    $interlinks[$l_id]['content'] = str_replace('['.$anchor.']', $url, $content);

                }
                break;
            case "partial":
                if($link->anchor_as_title != 1) {
                    $content = $link->content;
                    $anchor = $link->anchor;
                    $url = '<a href="' . $link->url . '" title="'.$link->title.'" target="_blank" rel="noopener">'.$anchor.'</a>';
                    $interlinks[$l_id]['content'] = str_replace('['.$anchor.']', $url, $content);

                }
                break;
            case "generic":
                if($link->anchor_as_title != 1) {
                    $content = $link->content;
                    $anchor = $link->anchor;
                    $url = '<a href="' . $link->url . '" title="'.$link->title.'" target="_blank" rel="noopener">'.$anchor.'</a>';
                    $interlinks[$l_id]['content'] = str_replace('['.$anchor.']', $url, $content);

                }
                break;
            case "lsi":
                if($link->anchor_as_title != 1) {
                    $content = $link->content;
                    $anchor = $link->anchor;
                    $url = '<a href="' . $link->url . '" title="'.$link->title.'" target="_blank" rel="noopener">'.$anchor.'</a>';
                    $interlinks[$l_id]['content'] = str_replace('['.$anchor.']', $url, $content);

                }
                break;
            case "naked":
                $content = $link->content;
                $anchor = $link->anchor;
                $url = '<a href="' . $link->url . '" title="'.$link->title.'" target="_blank" rel="noopener">'.$anchor.'</a>';
                $interlinks[$l_id]['content'] = str_replace('[URL]', $url, $content);
                break;
        }

    }


    $interlinks_view = $blade->run("{$interlinks_defaults['theme']}/interlinks_default",
        [
            "interlinks_defaults" => $interlinks_defaults,
            "interlinks" => $interlinks,
            "views_folder" => $interlinks_defaults['theme']
        ]);

    echo $interlinks_view;

}