<?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;
	// Initialize defaults
	$module_show_title = 1;
	$module_show_print_link = 1;
	$module_default_scale = "c";  // Centigrade
	$module_change_scale = 1;  // Allow to change the Scale
	
	$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;
		}
	}
	
	// Pull in the NuSOAP code
	require_once(dirname(__FILE__) . "/../wsbin/lib/nusoap.php");
	include_once(dirname(__FILE__) . "/../weather_advanced/functions.php");
	
	//$webservices_url = "http://localhost/webservices/cyws/srvdispatcher.php?wsdl";
	$webservices_url = "https://www.apps24.com/srvdispatcher.php?wsdl";

	$out_path_cache = "..";
	$all_cache_folder = "$out_path_cache/all_cache";
	$all_cache_folder = str_ireplace("//", "/", $all_cache_folder);
	$all_cache_weather_advanced = "$all_cache_folder/weather_advanced";
	
	$cities_list = $module_params['gen_cities_list'];
	
	$cities_list_link_title = (isset($module_params['cities_list_link_title'])) ? $module_params['cities_list_link_title'] : '';
	$cities_list_friendly_url = (isset($module_params['cities_list_friendly_url'])) ? $module_params['cities_list_friendly_url'] : '';
	
	//echo '<pre>', var_export($cities_list, true), '</pre>';
	
	$ws_request_available_locations = array(
		'server_name' 		=> 'weather',
		'function_name'		=> 'getCurrentLocations',
		'function_params'	=> base64_encode("all")
	);
	
	/* Check if cache folders are in place ******************************************************/
	if(!file_exists("$all_cache_folder") || !is_dir("$all_cache_folder")) {
		if( mkdir("$all_cache_folder", 0755) ) {
			
			if( mkdir("$all_cache_weather_advanced", 0755) ) {
				//echo "<br/>$all_cache_weather_advanced created";
			}
			else {
				//echo "<br/>Cannot create dir [$all_cache_weather_advanced]";
			}
		}
		else {
			//echo "<br/>Cannot create dir [$all_cache_folder]";
		}
	}
	else {
		if(!file_exists("$all_cache_weather_advanced") || !is_dir("$all_cache_weather_advanced")) {
			if( mkdir("$all_cache_weather_advanced", 0755) ) {
				//chmod("$general_path/all_cache/weather", 777);
				//echo "<br/>$all_cache_weather_advanced created";
			}
			else {
				//echo "<br/>Cannot create dir [$all_cache_weather_advanced]";
			}
		}
	}
	/* END Check for cache folders  *************************************************************/
	
	$have_it_on_cache = false;
	$md5 = md5(serialize($ws_request_available_locations));
	$cache_file = $all_cache_weather_advanced . "/" . $md5;
	if(file_exists($cache_file)) {
		//echo "<br/>[$cache_file] exists";
		$cache_time = filemtime($cache_file);
		$current_time = time();
		if($current_time - $cache_time < 800) {
			$have_it_on_cache = true;
			//echo "<br/>$cache_file time is earlier than 15 min";
		}
		else {
			unlink($cache_file);
			//echo "<br/>$cache_file time is older than 15 min";
		}
	}
	if($have_it_on_cache) {
		$cache_get = file_get_contents($cache_file);
	
		if($cache_get != "") {
			$cache_get = unserialize($cache_get);
			$current_weather_locations = $cache_get;
		}
	}
	else {
		//echo "<br/> --- here";
		$current_weather_locations = getCurrentLocations($webservices_url, $ws_request_available_locations);
		//echo "<br/>DO NOT Have it on cache";
		$cache_put = serialize($current_weather_locations);
		if( true == ($fc = fopen($cache_file, "w") ) ) {
			fwrite($fc, $cache_put, strlen($cache_put));
			fclose($fc);
		}
	}
	
	
	?>
	
	<form method="POST" action="custom_module_update.php" name="weather_advanced_middle_frm" id="weather_advanced_middle_frm">
		<table cellpadding="0" cellspacing="0" border="0" width="100%">
			<tr>
				<td align="left" width="50%" valign="top">
					<div class="inline_container">
					<table cellpadding="0" cellspacing="0" border="0" width="100%">
						<tr>
							<th colspan="2" align="left">
								Advanced Weather Customization 15 Day ONE TOWN
							</th>
						</tr>
						<tr>
							<td nowrap class="itdh">Show Advanced Weather Title</td>
							<td class="itd">
								<input type="radio" name="module_show_title" id="module_show_title" value="1" <?php if($module_show_title == 1) echo "checked"; ?> style="vertical-align:middle;"> Yes
								<input type="radio" name="module_show_title" id="module_show_title" value="0" <?php if($module_show_title == 0) echo "checked"; ?> style="vertical-align:middle;"> No
							</td>
						</tr>
						<tr>
							<td nowrap class="itdh">Display PRINT link?</td>
							<td class="itd">
								<input type="radio" name="module_show_print_link" id="module_show_print_link" value="1" <?php if($module_show_print_link == 1) echo "checked"; ?> style="vertical-align:middle;"> Yes
								<input type="radio" name="module_show_print_link" id="module_show_print_link" value="0" <?php if($module_show_print_link == 0) echo "checked"; ?> style="vertical-align:middle;"> No
							</td>
						</tr>
						<tr>
							<td nowrap class="itdh">Choose Default Scale</td>
							<td class="itd">
								<input type="radio" name="module_default_scale" id="module_default_scale_on" value="c" <?php if($module_default_scale == "c") echo "checked"; ?> style="vertical-align:middle;"> Celsius
								<input type="radio" name="module_default_scale" id="module_default_scale_off" value="f" <?php if($module_default_scale == "f") echo "checked"; ?> style="vertical-align:middle;"> Fahrenheit
							</td>
						</tr>
						<tr>
							<td nowrap class="itdh">Allow to change the Scale?</td>
							<td class="itd">
								<input type="radio" name="module_change_scale" id="module_change_scale" value="1" <?php if($module_change_scale == "1") echo "checked"; ?> style="vertical-align:middle;"> Yes
								<input type="radio" name="module_change_scale" id="module_change_scale" value="0" <?php if($module_change_scale == "0") echo "checked"; ?> style="vertical-align:middle;"> No
							</td>
						</tr>
						<tr>
							<td nowrap class="itdh">Select Cities</td>
							<td class="itd">
								<div class="city_selection" id="weather_locations_div">
									<ul class="cities_list">
										<?php
										$selected_locations_ar = array();
										if(isset($cities_list) && is_array($cities_list) && count($cities_list)>0) {
											foreach ($cities_list as $loc_id) {
												$selected_locations_ar[$loc_id] = array(
													"ws_location" 		=> $loc_id,
													"ws_city_name"		=> "",
													"ws_country_name"	=> ""
												);
											}
										}
										$current_country = "";
										foreach ($current_weather_locations as $k=>$row) {
											$ws_location = $row['ws_location'];
											$ws_city_name = stripslashes($row['ws_city_name']);
											$ws_country_name = stripslashes($row['ws_country_name']);
											
											$checked = "";
											if(isset($cities_list) && is_array($cities_list) && count($cities_list)>0) {
												if(in_array($ws_location, $cities_list)) {
													$checked = " checked";
													$selected_locations_ar [$ws_location]['ws_city_name'] = $ws_city_name;
													$selected_locations_ar [$ws_location]['ws_country_name'] = $ws_country_name;
												}
											}
											
											if($ws_country_name != $current_country) {
												$current_country = $ws_country_name;
												?>
												<li class="bolder"><strong><?php echo $ws_country_name;?></strong></li>
												<?php
											}
											
											?>
											<li <?php if($checked != "") echo 'class="scroll"';?>>
												<?php
												if(strtolower($ws_location) != "xxxx") {
													?>
													<input type="checkbox" name="gen_cities_list[]" id="gen_<?php echo $ws_location;?>" rel="<?php echo $ws_location.'|'.$ws_city_name.'|'.$ws_country_name;?>" class="gen_location" value="<?php echo $ws_location;?>" <?php echo $checked; ?>> <?php echo ($checked == "") ? $ws_city_name : '<strong style="color:red;">'.$ws_city_name.'</strong>'; ?>
													<?php
												}
												else {
													?>
													<strong>x</strong>  <?php echo $ws_city_name . "($ws_location)";?>
													<?php
												}
												?>
											</li>
											<?php
										}
										?>
									</ul>
								</div>
							</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>
					</div>
				</td>
				<td align="right" width="50%" valign="top">
					<div class="inline_container">
					<table cellpadding="0" cellspacing="0" border="0" width="100%">
						<tr>
							<th colspan="2" align="left">
								Selected Locations
							</th>
						</tr>
						<tr>
							<td colspan="2" class="itd">
								<div id="selected_locations" style="height:380px; overflow-y:auto;">
									<ul id="selected_locations_list">
										<?php 
										if(count($selected_locations_ar)>0) {
											foreach ($selected_locations_ar as $wid=>$data) {
												$link_title = $data['ws_city_name'];
												if(isset($cities_list_link_title) && is_array($cities_list_link_title) && isset($cities_list_link_title[$wid])) {
													$link_title = stripslashes($cities_list_link_title[$wid]);
												}
												
												$link_friendly_url = "";
												if(isset($cities_list_friendly_url) && is_array($cities_list_friendly_url) && isset($cities_list_friendly_url[$wid])) {
													$link_friendly_url = stripslashes($cities_list_friendly_url[$wid]);
												}
												
												
												$el = '';
												$el .= '<li id="' .$wid. '" class="ui-state-default">';
												$el .= '<input type="checkbox" name="cities_list[]" class="sel_loc" id="loc_'.$wid.'" style="vertical-align: middle;" value="'.$wid.'" checked="checked"> <strong>'.$data['ws_city_name']. '</strong>, '.$data['ws_country_name'];
												
												$el .= '<table cellpadding="0" cellspacing="0" border="0" width="100%">';
												$el .= '<tr>';
												$el .= '<td class="itd"><strong>Link Title:</strong></td><td class="itd_lb"><input type="text" name="cities_list_link_title['.$wid.']" id="cities_list_link_title_'.$wid.'" value="'.$link_title.'" style="width: 100%;"></td>';
												$el .= '</tr>';
												
												$el .= '<tr>';
												$el .= '<td class="itd"><strong>Friendly URL:</strong></td><td class="itd_lb"><input type="text" name="cities_list_friendly_url['.$wid.']" id="cities_list_friendly_url_'.$wid.'" value="'.$link_friendly_url.'" class="furl" style="width: 100%;"></td>';
												$el .= '</tr>';
												$el .= '</table>';
												
												$el .= '</li>';
												
												echo $el;
											}
										}
										?>
									</ul>
								</div>
							
							</td>
						</tr>
					</table>
					</div>	
				</td>
			</tr>
		</table>
		
	</form>

    <script type="text/javascript" src="/admin/js/jquery.scrollTo-min.js"></script>
	<script type="text/javascript">
		$(document).ready(function(){
			
			var $form = $('#weather_advanced_middle_frm');
			
			$('#selected_locations_list', $form).sortable();
			
			$('.jqmWindow').css('width', '800px');
			// Try to scroll to the parent category
			var $weather_locations_div = $("#weather_locations_div");
			var $target_location = $weather_locations_div.find('li.scroll');
			$weather_locations_div.stop().scrollTo( $target_location , 800 );
			
			/*
			$('input.gen_location').each(function(){
				var $loc = $(this);
				$loc.click(function(){
					var params = $(this).attr('rel').split('|');
					var location_id = params[0];
					var city_name = params[1];
					var country_name = params[2];
					var checked = $(this).attr('checked');
					if(checked) {
						//$('<li id="' + location_id + '" class="ui-state-default"><input type="checkbox" name="cities_list[]" class="sel_loc" id="loc_'+location_id+'" style="vertical-align: middle;" value="'+location_id+'" checked="checked"> <strong>'+city_name + '</strong>, ' + country_name +'</li>').appendTo($('#selected_locations_list'));
						
						var el = '';
						el = el +  '<li id="' +location_id+ '" class="ui-state-default">';
						el = el +  '<input type="checkbox" name="cities_list[]" class="sel_loc" id="loc_' +location_id+ '" style="vertical-align: middle;" value="'+location_id+'" checked="checked"> <strong>' +city_name+ '</strong>, ' + country_name;
						
						el = el +  '<table cellpadding="0" cellspacing="0" border="0" width="100%">';
						el = el +  '<tr>';
						el = el +  '<td class="itd"><strong>Link Title:</strong></td><td class="itd_lb"><input type="text" name="cities_list_link_title['+location_id+']" id="cities_list_link_title_'+location_id+'" value="'+city_name+'" style="width: 100%;"></td>';
						el = el +  '</tr>';
						
						el = el +  '<tr>';
						el = el +  '<td class="itd"><strong>Friendly URL:</strong></td><td class="itd_lb"><input type="text" name="cities_list_friendly_url['+location_id+']" id="cities_list_friendly_url_'+location_id+'" value="''" class="furl" style="width: 100%;"></td>';
						el = el +  '</tr>';
						el = el +  '</table>';
						
						el = el +  '</li>';
						
						$(el).appendTo($('#selected_locations_list'));
					}
					else {
						$('#' + location_id).remove();
					}
				});
				
			});
			
			*/
			$('input.gen_location', $form).click( function(){
				
				var params = $(this).attr('rel').split('|');
				var location_id = params[0];
				var city_name = params[1];
				var country_name = params[2];
				var checked = $(this).prop('checked');
				//var el = '';
				//alert(checked);
				
				if(checked) {
					//alert(checked);
					//$('<li id="' + location_id + '" class="ui-state-default"><input type="checkbox" name="cities_list[]" class="sel_loc" id="loc_'+location_id+'" style="vertical-align: middle;" value="'+location_id+'" checked="checked"> <strong>'+city_name + '</strong>, ' + country_name +'</li>').appendTo($('#selected_locations_list'));
					
					
					var ss = '<li id="' + location_id + '" class="ui-state-default">' +
							'<input type="checkbox" name="cities_list[]" class="sel_loc" id="loc_' +location_id+ '" style="vertical-align: middle;" value="'+location_id+'" checked="checked"> <strong>' +city_name+ '</strong>, ' + country_name + '' +
							
							'<table cellpadding="0" cellspacing="0" border="0" width="100%">' + 
							'<tr>' +
							'<td cla ss="itd"><strong>Link Title:</strong></td><td class="itd_lb"><input type="text" name="cities_list_link_title['+location_id+']" id="cities_list_link_title_'+location_id+'" value="'+city_name+'" style="width: 100%;"></td>' +
							'</tr>' +
							
							'<tr>' +
							'<td class="itd"><strong>Friendly URL:</strong></td><td class="itd_lb"><input type="text" name="cities_list_friendly_url['+location_id+']" id="cities_list_friendly_url_'+location_id+'" value="" class="furl" style="width: 100%;"></td>' +
							'</tr>' +
							'</table>' +
							
							'</li>';
					
					
					
					$(ss).appendTo($('#selected_locations_list'));
					
				}
				else {
					$('#' + location_id).remove();
				}
				
				
			});
			
			
			$('input.sel_loc').live('click', function(){
				var $loc = $(this);
				
				var params = $loc.attr('id').split('_');
				var location_id = params[1];

				var checked = $loc.prop('checked');
				if(!checked) {
					$('#' + location_id).remove();
					$('#gen_' + location_id).prop('checked', false);
				}
			});
			
		});
	</script>
	<?php
	
	$module_forms = array(
		"form_name" => "weather_advanced_middle_frm",
		"form_action" => "",
		"form_method" => "POST"
	);
}
else {
	// We are in the front-end
	// Initialize defaults
	$module_show_title = 1;
	$module_show_print_link = 1;
	$module_default_scale = "c";  // Centigrade
	$module_change_scale = 1;  // Allow to change the Scale
	
	// Try to get any customization done
	if(isset($module_params) && is_array($module_params)) {
		foreach ($module_params as $key=>$value) {
			${$key} = $value;
		}
	}
}
?>