<?php
$module_dir = "bl_middle";
$module_css_path = ($line_domain['theme'] != "" && $line_domain['theme'] != "default") ? ($path . ('assets/themes/' . $line_domain['theme'] . '/')) : $path;
$server_host = $_SERVER['HTTP_HOST'];
if(str_replace("www.", "", $server_host) != $real_domainname) {
    $server_host .= "/$real_addonname";
}

$in_catdom_id = $domain_id;
if(isset($catdom_id) && intval($catdom_id) > 0) {
	$in_catdom_id = $catdom_id;
}
$active_bl_business = true;
$q = "select c.* from left_content c, module m where c.catdom_id='$in_catdom_id' and c.module_id=m.module_id
	and m.url='bl_middle' and c.`type`='module' and c.location='middle' and c.module_params !='';";

$r = @mysql_query($q);
if(!$r || mysql_num_rows($r) <= 0) {
	$active_bl_business = false;
}

if($active_bl_business) {
	$row = @mysql_fetch_assoc($r);
	$module_params = unserialize(base64_decode($row['module_params']));

    $module_title = isset($module_params['module_bl_title']) && trim($module_params['module_bl_title']) != "" ? trim($module_params['module_bl_title']) : "Listings";
    $breadcrumbs [] = [
        "title" => $module_title,
        "link" => $protocol . $server_host . "/listings"
    ];


	$dlc = array(
		"ar" => "arabic",
		"cn" => "chinese",
		"bg" => "bulgarian",
		"el" => "greek",
		"de" => "german",
		"fr" => "french",
		"it" => "italian",
		"iw" => "hebrew",
		"ro" => "romanian",
		"ru" => "russian"
	);


	$districts_map = array(
		"Ammochostos" => "Famagusta",
		"Larnaka" => "Larnaca",
		"Lemesos" => "Limassol",
		"Lefkosia" => "Nicosia",
		"Pafos" => "Paphos"
	);

	$optres['css'][] = $module_css_path . "amodules/bl_middle/css/bl_middle.css";
// Check if YP Tables exist
	$bl_tables = array(
		"bl_businesses",
		"bl_businesses_tr",
		"bl_businesses_categories",
		"bl_businesses_locations",
		"bl_businesses_locations_tr",
		"bl_categories",
		"bl_categories_links",
		"bl_categories_links_tr"
	);

// Show or not adverts module
	$show_bl_adverts = true;
	if (str_replace('www.', '', $ultra_domainname) == "cyprus-hotel.com") {
		$show_bl_adverts = false;
	}

	$have_yp_tables = true;
	$existing_yp_tables = array();
	$rt = mysql_query("SHOW TABLES LIKE 'bl_%'");
	if ($rt && mysql_num_rows($rt) > 0) {
		while ($rowrt = mysql_fetch_array($rt)) {
			$existing_yp_tables [] = trim($rowrt[0]);
		}

		if (!in_array("bl_businesses_tr", $existing_yp_tables)) {
			foreach ($bl_tables as $i => $drop_table) {
				$qdel = "drop table `$drop_table`;";
				$rdel = @mysql_query($qdel);// or die("<strong>$qdel</strong>".mysql_error());
			}


			$existing_yp_tables = array();
		}

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

		if (count($existing_yp_tables) < count($bl_tables)) {
			$have_yp_tables = false;
		}
	} else {
		$have_yp_tables = false;
	}

	if (!$have_yp_tables) {
		require_once(dirname(__FILE__) . "/sql/init.sql.php");
		foreach ($yp_create_tables as $yp_tbl => $yp_create_query) {
			if (!in_array($yp_tbl, $existing_yp_tables)) {
				$rc = @mysql_query($yp_create_query);
			}
		}
	}

	require_once(dirname(__FILE__) . "/check_yp_data.php");
//echo '$yp_module = ' . $yp_module;

	if (isset($bl_module) && $bl_module == "directory") {
		if (isset($bl_business) && trim($bl_business) != "") {

			if (isset($_REQUEST['debug'])) {
				echo '<br>Include: ' . $modules_path . "$module_dir/bl_middle_business.php";
			}

			if (file_exists($modules_path . "$module_dir/bl_middle_business.php")) {
				include($modules_path . "$module_dir/bl_middle_business.php");
			}
		} else {

			if (isset($_REQUEST['debug'])) {
				echo '<br>Include: ' . $modules_path . "$module_dir/bl_middle_directory.php";
			}

			if (file_exists($modules_path . "$module_dir/bl_middle_directory.php")) {
				include($modules_path . "$module_dir/bl_middle_directory.php");
			}
		}

        if(isset($module_params['module_bl_robots']) && trim($module_params['module_bl_robots']) != "" && isset($bl_module) && $bl_module == "directory" ) {
            $meta_robots = trim($module_params['module_bl_robots']);
        }

	} else {
		if (file_exists($modules_path . "$module_dir/bl_middle_init.php")) {

			if (isset($_REQUEST['debug'])) {
				echo '<br>Include: ' . $modules_path . "$module_dir/bl_middle_default.php";
			}

			include($modules_path . "$module_dir/bl_middle_init.php");

			if (file_exists($modules_path . "$module_dir/bl_middle_default.php")) {
				include($modules_path . "$module_dir/bl_middle_default.php");
			} else {
				echo $modules_path . "$module_dir/bl_middle_default.php does not exist";
			}
		}
	}
}
else {
	$send404 = true;
	require_once(dirname(__FILE__) . "/../404/404.php");
}
if(isset($module_params)) {
	unset($module_params);
}
?>