<?php
$show_init_form = false;	/* To display or not the initialization form */
$locations_map = array(
	"Famagusta"		=> "Famagusta",
	"Larnaca"		=> "Larnaca",
	"Limassol"		=> "Limassol",
	"Nicosia"		=> "Nicosia",
	"Paphos"		=> "Paphos"
);
/* Try to see if the call is from administration panel */
if(isset($admin_catdom_module_id) && $admin_catdom_module_id>0 && isset($_REQUEST['action']) && $_REQUEST['action'] == "custom_module") {
	$show_init_form = true;

	$have_domains_conflict = false;
	
	// Get domain
	$domainname = "";
	$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']);
		}
	}
	
	$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 = str_replace("www.", "", $row['domainname']);
	}
	
	
	// Get Domain Category
	$all_domains = array();
	$q = "select * from wb_categories_domains where wb_domainname='$domainname';";
	$r = @mysql_query($q);
	if($r && mysql_num_rows($r) > 0) {
		$row = mysql_fetch_assoc($r);
		$wb_uid = $row['wb_uid'];
		
		$q = "select * from wb_categories_domains where wb_uid='$wb_uid';";
		$r = @mysql_query($q);
		if($r && mysql_num_rows($r) > 0) {
			while ($row = mysql_fetch_assoc($r)) {
				if($row['wb_domainname'] != $domainname) {
					$all_domains [] = $row['wb_domainname'];
				}
			}
		}
		
	}
	
	$other_domains = array();
	
	if(count($all_domains) > 0) {
		$same_categ_domains = array();
		foreach ($all_domains as $i=>$alt_domain) {
			$q = "select * from domaincategories where domainname like '%$alt_domain' order by domainname asc;";
			$r = @mysql_query($q);
			if($r && mysql_num_rows($r) == 1) {
				$row = mysql_fetch_assoc($r);
				$alt_catdom_id = intval($row['catdom_id']);
				$title_en = stripslashes($row['title_en']);
				$alt_domainname = $row['domainname'];
				$same_categ_domains [] = $alt_catdom_id;
				
				$other_domains [$alt_catdom_id] = array(
					"text"	=> '<strong>'.$alt_domainname.'</strong> :: ' . $title_en,
					"bound"	=> 0,
					"catdom_module_id" => 0
				);
				
				$qc = "select * from domaincategories where parent_id = '$alt_catdom_id' order by title_en asc;";
				$rc = @mysql_query($qc);
				if($rc && mysql_num_rows($rc) > 0) {
					while ($rowc = mysql_fetch_assoc($rc)) {
						$alt_catdom_id = intval($rowc['catdom_id']);
						$same_categ_domains [] = $alt_catdom_id;
						$title_en = stripslashes($rowc['title_en']);
						$other_domains [$alt_catdom_id] = array(
							"text"	=> '<strong>'.$alt_domainname.'</strong> :: ' . $title_en,
							"bound"	=> 0,
							"catdom_module_id" => 0
						);
					}
				}
			}
		}
	}
	
	
	// Get module details
	$wb_module_id = 0;
	$q = "select * from module where url='wb_categories';";
	$r = @mysql_query($q);
	if($r && mysql_num_rows($r) == 1) {
		$row = mysql_fetch_assoc($r);
		$wb_module_id = intval($row['module_id']);
	}
	
	if(isset($same_categ_domains) && count($same_categ_domains) > 0) {
		$q = "select * from left_content where module_id='$wb_module_id' and `type`='module' and catdom_id IN (".implode(",", $same_categ_domains).")";
		//echo $q;
		$r = @mysql_query($q);
		if($r && mysql_num_rows($r) > 0) {
			
			while ($row = mysql_fetch_assoc($r)) {
				$catdom_module_id = $row['catdom_module_id'];
				$ex_catdom_id = $row['catdom_id'];
				$ex_status = $row['module_active'] == "yes" ? true : false;
				$ex_module_params = unserialize(base64_decode($row['module_params']));
				
				if(isset($ex_module_params['module_show_results']) && intval($ex_module_params['module_show_results']) == 1) {
					$other_domains[$ex_catdom_id]['bound'] = 1;
					$other_domains[$ex_catdom_id]['catdom_module_id'] = $catdom_module_id;
					$have_domains_conflict = true;
				}
			}
		}
		
	}
	$module_show_results = 1;
	$module_use_defaults = 0;
	$module_wb_locations = array_keys($locations_map);
	$module_wb_title = "";
	$module_wb_links = 1;
	//$module_wb_rpp = 5; // Initial number of results per page
	
	$current_catdom_module_id = $admin_catdom_module_id;

}

if($show_init_form) {
	
	//require_once("modules/contents/functions.php");
	//require_once("functions/root_display.php");
	//require_once("classes/tree_radio_class.php");
	
	//print_r($module_params);
	// Try to get any previously saved customization
	if(isset($module_params) && is_array($module_params)) {
		foreach ($module_params as $key=>$value) {
			${$key} = $value;
		}
	}
	?>
	<style type="text/css">
		#wb_categs ul {
			margin:0;padding:0;list-style:none;
		}
		
		#wb_categs ul li{
			margin:3px;
			border-bottom: 1px solid #cccccc;
		}
		
		
		
	</style>
	<?php
	if(1==1) {
		?>
		
		<div id="wb_categs" class="inline_container">
			<form method="POST" action="custom_module_update.php" name="wb_categories_frm" id="wb_categories_frm">
			<table cellpadding="0" cellspacing="0" border="0" width="100%">
				<tr>
					<td nowrap class="itdh">Module Title</td>
					<td class="itd" width="90%">
						<input type="text" name="module_wb_title" value="<?php echo $module_wb_title; ?>" style="width:100%;"/>
					</td>
				</tr>
				<tr>
					<td nowrap class="itdh" valign="top">Show adverts <br/>from district:</td>
					<td class="itd">
						<ul id="locations">
						<?php
						foreach ($locations_map as $loc=>$label) {
							$checked = '';
							if(in_array($loc, $module_wb_locations)) {
								$checked = 'checked="checked"';
							}
							echo '<li><input type="checkbox" name="module_wb_locations[]" value="' . $loc . '" '. $checked . ' style="vertical-align: middle;"> ' . $label . '</li>';
						}
						
						?>
						</ul>
					</td>
				</tr>
				<?php
				/*
				<tr>
					<td nowrap class="itdh">Results per page</td>
					<td class="itd" width="90%">
						<input type="text" name="module_wb_rpp" value="<?php echo $module_wb_rpp; ?>" maxlength="3" style="width:20px;"/>
					</td>
				</tr>
				*/
				?>
				<tr>
					<td nowrap class="itdh">Show Locations Links?</td>
					<td class="itd">
						<input type="radio" name="module_wb_links" value="1" <?php echo (intval($module_wb_links) == 1) ? "checked" : ""; ?> style="vertical-align:middle;"/> Yes 
						<input type="radio" name="module_wb_links" value="0" <?php echo (intval($module_wb_links) == 0) ? "checked" : ""; ?> style="vertical-align:middle;"/> No
					</td>
				</tr>
				<tr>
					<td nowrap class="itdh">Show Adverts Results<br/> in this domain?</td>
					<td class="itd">
						<?php
						$disabled = '';
						/*if($have_domains_conflict) {
							$disabled = 'disabled="disabled"';
							$module_show_results = 0;
						}*/
						?>
						<input type="radio" name="module_show_results" value="1" <?php echo $disabled;?> <?php echo (intval($module_show_results) == 1) ? "checked" : ""; ?> style="vertical-align:middle;"/> Yes 
						<input type="radio" name="module_show_results" value="0" <?php echo (intval($module_show_results) == 0) ? "checked" : ""; ?> style="vertical-align:middle;"/> No
						<?php
						if($have_domains_conflict) {
							echo ' <span style="color:#ff0000;">You cannot make this module show results here until you not clear this option from the other domain.</span>';
						}
						?>
					</td>
				</tr>
				<tr>
					<td nowrap class="itdh">Use defaults?</td>
					<td class="itd">
						<input type="radio" name="module_use_defaults" value="1" <?php echo ($module_use_defaults == 1) ? "checked" : ""; ?> style="vertical-align:middle;"/> Yes 
						<input type="radio" name="module_use_defaults" value="0" <?php echo ($module_use_defaults == 0) ? "checked" : ""; ?> style="vertical-align:middle;"/> No
					</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
	}
	if(1==2 && $have_domains_conflict) {
		?>
		<div class="inline_container">
			<table cellpadding="0" cellspacing="0" border="0" width="100%">
				<tr>
					<th colspan="2">There are some domain conflicts</th>
				</tr>
				<tr>
					<td class="itd" align="left" colspan="2">
						The Domain Category associated with <strong><?php echo $domainname;?></strong> is also shared by another <strong><?php echo count($all_domains);?></strong> domains.
						It appears that the folowing domain(s) is/are already displaying search results based on the Advert/Domain Category.
					</td>
				</tr>
				<?php
				foreach ($other_domains as $bad_catdom_id => $data) {
					if($data['bound'] == 1) {
						?>
						<tr>
							<td class="itd" align="left"><?php echo $data['text'];?></td>
							<td class="itd_lb" align="left">
								<a href="<?php echo $protocol . $_SERVER['HTTP_HOST'];?>/admin/domain_module_middle.php?catdom_id=<?php echo $bad_catdom_id;?>&highlight_id=<?php echo $data['catdom_module_id'];?>#<?php echo $data['catdom_module_id'];?>" target="_blank">See Setting</a>
							</td>
						</tr>
						<?php
					}
				}
				?>
			</table>
		</div>
		<?php
	}
	
	$module_forms = array(
		"form_name" => "wb_categories_frm",
		"form_action" => "",
		"form_method" => "POST"
	);
}
else {
	// We are in the front-end
	// Initialize defaults
	$module_show_results = 0;
	$module_use_defaults = 0;
	$module_wb_locations = array_keys($locations_map);
	$module_wb_title = "";
	$module_wb_links = 1;
	//$module_wb_rpp = 5; // Initial number of results per page
	
	// Try to get any customization done
	if(isset($module_params) && is_array($module_params)) {
		foreach ($module_params as $key=>$value) {
			${$key} = $value;
		}
	}
}
?>
