<?php
//echo '<pre>', print_r($_POST), '</pre>';
$have_domainname = false;
if(isset($admin_catdom_module_id) && intval($admin_catdom_module_id) > 0) {
	$categs_uids = (isset($_POST['module_bl_categs'])) ? array_keys($_POST['module_bl_categs']) : array();
	// We come from Admin/Params
	$q = "select d.* from domaincategories d, left_content l where d.catdom_id=l.catdom_id and l.catdom_module_id='$admin_catdom_module_id'";
	//echo "\n" . $q;
	$r = @mysql_query($q);
	if($r && mysql_num_rows($r) == 1) {
		
		$row = mysql_fetch_assoc($r);
		$catdom_id = $row['catdom_id'];
		$parent_id = intval($row['parent_id']);
		$domainname = str_replace("www.", "", $row['domainname']);
		$title_el = stripslashes($row['title_el']);
		$title_en = stripslashes($row['title_en']);
		$friendly_url = $row['friendly_url'];
		
		
		if($parent_id > 0) {
			$q = "select * from domaincategories where catdom_id='$parent_id';";
			$r = @mysql_query($q);
			if($r && mysql_num_rows($r) == 1) {
				$row = mysql_fetch_assoc($r);
				//$catdom_id = $row['catdom_id'];
				$domainname = str_replace("www.", "", $row['domainname']);
				$domain_url = "/$friendly_url.html";
				$domain_title = $title_el;
				$domain_code = $catdom_id;
				$have_domainname = true;
			}
			
		}
		else {
			$domain_url = "/index.php";
			$domain_title = $title_en;
			$domain_code = $catdom_id;
			$have_domainname = true;
		}
	}
}
elseif (isset($domain_id) && intval($domain_id) > 0) {
	$in_subcat = false;
	if(isset($catdom_id) && intval($catdom_id) > 0 && $catdom_id != $domain_id) {
		$in_subcat = true;
	}
	else {
		$catdom_id = $domain_id;
	}
	
	if(isset($module_params['module_bl_categs']) && is_array($module_params['module_bl_categs']) && count($module_params['module_bl_categs']) > 0) {
		$module_bl_categs = $module_params['module_bl_categs'];
		$categs_uids = array_keys($module_bl_categs);
	}
	else {
		$categs_uids = array();
	}
	
	$q = "select * from domaincategories where catdom_id='$catdom_id';";
	$r = @mysql_query($q);
	if($r && mysql_num_rows($r) == 1) {
		$row = mysql_fetch_assoc($r);
		$parent_id = intval($row['parent_id']);
		$title_el = stripslashes($row['title_el']);
		$title_en = stripslashes($row['title_en']);
		$friendly_url = $row['friendly_url'];
		$domainname = str_replace("www.", "", $row['domainname']);
		$domain_url = "/index.php";
		$domain_title = $title_en;
		$domain_code = $catdom_id;
		
		if($parent_id > 0) {
			$q = "select * from domaincategories where catdom_id='$parent_id';";
			$r = @mysql_query($q);
			if($r && mysql_num_rows($r) == 1) {
				$row = mysql_fetch_assoc($r);
				//$catdom_id = $row['catdom_id'];
				$domainname = str_replace("www.", "", $row['domainname']);
				$domain_url = "/$friendly_url.html";
				$domain_title = $title_el;
				$domain_code = $catdom_id;
				$have_domainname = true;
			}
		}
		else {
			$have_domainname = true;
		}
		
	}
	
}

if($have_domainname) {
	
	if(substr($_SERVER['HTTP_HOST'], 0, 3) == "lc.") {
		echo "\n" . "Running ws_update from local";
	}
	else {
		require_once(dirname(__FILE__) . "/../wsbin/lib/nusoap.php");
		require_once(dirname(__FILE__) . "/../wsbin/ws_functions.php");
		
		$_request = array(
			"domainname"		=> $domainname,
			"domain_url"		=> $domain_url,
			"domain_title"		=> $domain_title,
			"domain_code"		=> $domain_code,
			"categs" 			=> $categs_uids,
			"admin_domain"		=> "cypruswebsites.com"
		);
		
		//print_r($_request);
		
		$ws_request = array(
			'server_name' 		=> 'yellowpages',
			'function_name'		=> 'updateRemoteBLDomains',
			'function_params'	=> base64_encode(serialize($_request))
		);
		
		$time_start = microtime(true);
		
		$webservices_url = "https://www.apps24.com/srvdispatcher.php?wsdl";
		$yp_update_data = send_WS_Request($webservices_url, $ws_request);
		if(is_array($yp_update_data) && count($yp_update_data)>0) {
			if(isset($_REQUEST['debug'])) {
				echo "\n" . "Webservices were updated with your selection.";
			}
		}
		else {
			if(isset($_REQUEST['debug'])) {
				echo "\n" . "ERROR :: Webservices were NOT updated with your selection.";
			}
		}
	}
}
else {
	if(isset($_REQUEST['debug'])) {
		echo "\n" . "ERROR :: No domainname found";
	}
}

?>