<?php
$module_dir = "yp_middle";
$module_css_path = ($line_domain['theme'] != "" && $line_domain['theme'] != "default") ? ($path . ('assets/themes/' . $line_domain['theme'] . '/')) : $path;

$in_catdom_id = $domain_id;
if(isset($catdom_id) && intval($catdom_id) > 0) {
	$in_catdom_id = $catdom_id;
}

$active_yp_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='yp_middle' and c.`type`='module' and c.location='middle' and c.module_params !='';";

if(isset($_REQUEST['debug_yp'])) {
    echo "<br>$q";
}

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

if($active_yp_business) {

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

	$optres['css'][] = $module_css_path . "amodules/yp_middle/css/yp_middle.css";

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

	// Check if YP Tables exist
	$yp_tables = array(
		"yp_businesses",
		"yp_businesses_categories",
		"yp_businesses_locations",
		"yp_categories",
		"yp_categories_links"
	);
	
	$have_yp_tables = true;
	$existing_yp_tables = array();
    $rt = mysql_query("SHOW TABLES FROM `".$db_name."` WHERE `Tables_in_".$db_name."` LIKE 'yp_%'");
	if($rt && mysql_num_rows($rt) > 0){
		while ($rowrt = mysql_fetch_array($rt)) {
			$existing_yp_tables [] = trim($rowrt[0]);
		}
		
		if(count($existing_yp_tables) < count($yp_tables)) {
			$have_yp_tables = false;
		}
	}
	else {
		$have_yp_tables = false;
	}
	
	if(isset($_REQUEST['debug'])) {
		echo '<pre>', var_export($existing_yp_tables, true), '</pre>';
	}
	
	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);
				
				if(isset($_REQUEST['debug'])) echo "<br/>$yp_create_query";
			}
		}
	}
	
	require_once(dirname(__FILE__) . "/check_yp_data.php");
	//echo '$yp_module = ' . $yp_module;
	
	if(isset($yp_module) && $yp_module == "directory") {
		if(isset($yp_business) && trim($yp_business) != "") {
			if(file_exists($modules_path . "$module_dir/yp_middle_business.php")) {
				include($modules_path . "$module_dir/yp_middle_business.php");
			}
		}
		else {
			if(file_exists($modules_path . "$module_dir/yp_middle_directory.php")) {
				include($modules_path . "$module_dir/yp_middle_directory.php");
			}
		}

        if(isset($module_params['module_yp_robots']) && trim($module_params['module_yp_robots']) != "" && isset($yp_module) && $yp_module == "directory") {
            $meta_robots = trim($module_params['module_yp_robots']);
        }
	}
	else {
		if(file_exists($modules_path . "$module_dir/yp_middle_init.php")) {
			include($modules_path . "$module_dir/yp_middle_init.php");
			
			if(!isset($module_params) || (isset($module_params['yp_middle_hide_default']) && intval($module_params['yp_middle_hide_default']) == 0)) {
				if(file_exists($modules_path . "$module_dir/yp_middle_default.php")) {
					include($modules_path . "$module_dir/yp_middle_default.php");
				}
				else {
					echo $modules_path . "$module_dir/yp_middle_default.php does not exist";
				}
			}
		}
	}
}
else {
	$send404 = true;
	require_once(dirname(__FILE__) . "/../404/404.php");
}


if(isset($module_params)) {

	unset($module_params);
}
?>