<?php
$show_init_form = false;	/* To display or not the initialization form */

/* Try to see if the call is from administration panel */
if(isset($admin_catdom_module_id) && $admin_catdom_module_id>0 && isset($_GET['action']) && $_GET['action'] == "custom_module") {
	$show_init_form = true;
	
	$p_catdom_id = intval($_REQUEST['catdom_id']);
	$p_domain_id = $p_catdom_id;
	$q = "select * from domaincategories where catdom_id='$p_catdom_id';";
	$r = mysql_query($q) or die("<strong>$q</strong>".mysql_error());
	if($r && mysql_num_rows($r)>0) {
		$row = mysql_fetch_assoc($r);
		if(intval($row['parent_id'])>0) {
			$p_domain_id = intval($row['parent_id']);
		}
	}
	
	$on_main_server = true;
	$q = "select * from domaincategories where catdom_id='$p_domain_id';";
	$r = mysql_query($q) or die("<strong>$q</strong>".mysql_error());
	if($r && mysql_num_rows($r)>0) {
		$row = mysql_fetch_assoc($r);
		$domainname = $row['domainname'];
		$on_main_server = (intval($row['hosting_server']) > 0) ? false : true;
	}
	
	
	// Add Booking Search tables to Domain Tables
	if(!$on_main_server) {
		$bookin_search_tables = array(
			"booking_cities",
			"booking_destinations",
			"booking_regions"
		);
		
		//$r = @mysql_query("delete from domain_tables where domainname='$domainname' and table_name in ();");
		$r = @mysql_query('ALTER TABLE `domain_tables` ADD UNIQUE `Unique_domain_table` ( `catdom_id` , `table_name` ) ');
		
		foreach ($bookin_search_tables as $i=>$tbl_name) {
			@mysql_query("REPLACE INTO domain_tables (catdom_id, domainname, table_name) values ('$p_domain_id', '$domainname', '$tbl_name');");
		}
	}
	
	// Initialize defaults
	$module_title = 'Cyprus Hotels';
	$module_title_small = '';
	$module_title_tag = 'span';
	$module_pre_area = '';
	
	$current_catdom_module_id = $admin_catdom_module_id;
}

if($show_init_form) {
	// Try to get any previously saved customization
	if(isset($module_params) && is_array($module_params)) {
		foreach ($module_params as $key=>$value) {
			${$key} = $value;
		}
	}
	
	$book_destinations = array();
	$book_regions = array();
	
	$q = "SELECT * FROM `booking_cities` where 1=1 order by full_name asc;";
	$r = @mysql_query($q);
	if($r && mysql_num_rows($r) > 0) {
		while ($row = mysql_fetch_assoc($r)) {
			$book_destinations [] = array_map('stripslashes', $row);
		}
	}
	
	$q = "SELECT * FROM `booking_regions` where country_code='cy' order by region_name asc;";
	$r = @mysql_query($q);
	if($r && mysql_num_rows($r) > 0) {
		while ($row = mysql_fetch_assoc($r)) {
			$book_regions [] = array_map('stripslashes', $row);
		}
	}
	
	?>
	<div class="inline_container">
	<form method="POST" action="custom_module_update.php" name="booking_search_frm" id="booking_search_frm">
		<table cellpadding="0" cellspacing="0" border="0" width="100%">
			<tr>
				<th colspan="2" align="left">
					Booking.com Search Module
				</th>
			</tr>
			<tr>
				<td nowrap class="itdh">Title</td>
				<td class="itd">
					<input type="text" name="module_title" value="<?php echo $module_title;?>" style="width:100%;"/>
				</td>
			</tr>
			<tr>
				<td nowrap class="itdh">Title Featurette</td>
				<td class="itd">
					<input type="text" name="module_title_small" value="<?php echo $module_title_small;?>" style="width:100%;"/>
				</td>
			</tr>
			<tr>
				<td nowrap class="itdh">Title TAG</td>
				<td class="itd">
					<?php
					$tags = array(
						'h1', 'h2', 'h3', 'span'
					);
					?>
					<select name="module_title_tag">
						<?php
						foreach ($tags as $tag) {
							$selected = "";
							if($tag == $module_title_tag) $selected = 'selected="selected"';
							echo '<option value="', $tag, '" ', $selected, '>', strtoupper($tag), '</option>';
						}
						?>
					</select>
				</td>
			</tr>
			<tr>
				<td nowrap class="itdh">Pre-Selected Area</td>
				<td class="itd">
					<select name="module_pre_area">
						<?php
						$selected = "";
						?>
						<option value="" selected="selected">All Cyprus</option>
						<optgroup label="Regions">
							<?php
							
							foreach ($book_regions as $i=>$region_data) {
								$selected = "";
								if($module_pre_area == 'r|' . $region_data['id']) $selected = 'selected="selected"';
								echo '<option value="r|', $region_data['id'], '" ',$selected,'>', $region_data['region_name'], '</option>';
							}
							?>
						</optgroup>
						<optgroup label="Areas">
							<?php
							
							foreach ($book_destinations as $i=>$city_data) {
								$selected = "";
								if($module_pre_area == 'c|' . $city_data['ufi']) $selected = 'selected="selected"';
								echo '<option value="c|', $city_data['ufi'], '" ',$selected,'>', $city_data['full_name'], '</option>';
							}
							?>
						</optgroup>
					</select>
				</td>
			</tr>
			<tr>
				<td colspan="2" align="right" class="footer">
					<input type="reset" value="Reset" name="submit" class="submit_button"/>
					<input type="submit" value="Save" name="submit" class="submit_button"/>
					<input type="hidden" value="<?php echo $current_catdom_module_id; ?>" name="admin_catdom_module_id" id="admin_catdom_module_id"/>
				</td>
			</tr>
		</table>
	</form>
	</div>
	<?php
	
	$module_forms = array(
		"form_name" => "booking_search_frm",
		"form_action" => "",
		"form_method" => "POST"
	);
}
else {
	// We are in the front-end
	// Initialize defaults
	$module_title = 'Cyprus Hotels';
	$module_title_small = '';
	$module_title_tag = 'span';
	$module_pre_area = '';
		
	// Try to get any customization done
	if(isset($module_params) && is_array($module_params)) {
		
		//echo '<pre>', var_export($module_params, true), '</pre>';
		
		foreach ($module_params as $key=>$value) {
			${$key} = $value;
		}
	}
}
?>
