<?php
$optres['css'][] = $module_css_path . 'amodules/weather_advanced_loc/css/common.css';

if(!isset($module_params) || !is_array($module_params) || count($module_params)<=0) {

    $catdom_id_adv = isset($catdom_id) ? $catdom_id : $domain_id;
    if(isset($_REQUEST['ccmi'])) {
        $catdom_module_id = intval($_REQUEST['ccmi']);
    }

	$q = "
		select c.* 
		from 
			left_content c, 
			module m 
		where 
			" . (!isset($catdom_module_id) ? "c.catdom_id='$catdom_id_adv'" : "c.catdom_module_id='$catdom_module_id'") . "
			and c.module_active='yes'
			and c.location='middle'
			and c.`type`='module'
			and c.module_id=m.module_id 
			and m.url='weather_advanced_loc';";

    //echo $q;

	$r = @mysql_query($q);
	if($r && mysql_num_rows($r)==1) {
		$row = mysql_fetch_assoc($r);
		$module_params = unserialize(base64_decode($row['module_params']));
	}
}

//echo '<pre>$module_params = ', var_export($module_params, true), '</pre>';


if(isset($module_params) && is_array($module_params) && count($module_params) >0) {
	$q_ws = "select * from module where url='weather_advanced_loc';";
	$r_ws = @mysql_query($q_ws);
	if($r_ws && mysql_num_rows($r_ws)>0) {
		$row_ws = mysql_fetch_assoc($r_ws);
		$weather_advanced_title = stripslashes($row_ws["title_en"]);
	}

    //echo '<pre>', var_export($module_params, true), '</pre>';
	
	$current_catdom_module_id = $module_params['admin_catdom_module_id'];
	
	// Pull in the NuSOAP code

	include_once(dirname(__FILE__) . "/functions.php");
	
	//$webservices_url = "http://localhost/webservices/cyws/srvdispatcher.php?wsdl";


	$adv_weather_debug = false;
	if(isset($_REQUEST['adv_weather_debug'])) $adv_weather_debug = true;

	
	$city_name_list = $module_params['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_link_title, true), '</pre>';
    $weather_advanced_loc_key = 'weather_advanced_loc_' . base64_encode(serialize($city_name_list));
    $weather_five_day_loc = $pfcache->get($weather_advanced_loc_key);

    if($weather_five_day_loc == null) {
        require_once(dirname(__FILE__) . "/../wsbin/lib/nusoap.php");
        $webservices_url = "https://www.apps24.com/srvdispatcher.php?wsdl";

        $ws_request_current = array(
            'server_name' 		=> 'weather',
            'function_name'		=> 'getCitiesWeather',
            'function_params'	=> base64_encode(serialize($city_name_list))
        );

        $ws_request_fiveday = array(
            'server_name' 		=> 'weather',
            //'function_name'		=> 'getCitiesWeatherFiveday',
            'function_name'		=> 'getCitiesWeather15day',
            'function_params'	=> base64_encode(serialize($city_name_list))
        );

        $x_weather_current = getCurrentCityWeather($webservices_url, $ws_request_current);
        $x_weather_fiveday = getCurrentCityWeather($webservices_url, $ws_request_fiveday);

        if($adv_weather_debug) {
            echo '<pre>',print_r($weather_current),'</pre>';
            echo '<pre>',print_r($weather_fiveday),'</pre>';
        }

        if(is_array($x_weather_current) && count($x_weather_current) > 0 && is_array($x_weather_fiveday) && count($x_weather_fiveday) > 0) {
            //echo "<br/>DO NOT Have it on cache";
            $cache_put = serialize(array($x_weather_current, $x_weather_fiveday));
            $pfcache->set($weather_advanced_loc_key, $cache_put, 3600);
            // Set the new data
            $weather_current = $x_weather_current;
            $weather_fiveday = $x_weather_fiveday;
        }
        elseif($adv_weather_debug) {
            echo "<br/>Did not receive weather updates from webservices. Continue with what is in cache.";
        }

    }
    else {
        $weather_five_day_loc = unserialize($weather_five_day_loc);
        $weather_current = $weather_five_day_loc[0];
        $weather_fiveday = $weather_five_day_loc[1];
    }
	
	//echo '<pre>', var_export($weather_current, true), '</pre>';
	//echo '<pre>', var_export($weather_fiveday, true), '</pre>';

	$c_to_f = isset($module_default_scale) ? $module_default_scale : "c";
	if(isset($_REQUEST['show_scale']) && $_REQUEST['show_scale'] == "f") {
		$c_to_f = "f";
	}
	elseif(isset($_REQUEST['show_scale']) && $_REQUEST['show_scale'] == "c") {
		$c_to_f = "c";
	}
	
	$show_loc_forecast = false;
	
	$wids = array_keys($weather_fiveday);
	
	$loc = $wids[0];
	$wdays = $weather_fiveday[$loc];
	//echo '<pre>', var_export($wdays, true), '</pre>';
	
	
	
	if(isset($loc) && intval($loc)> 0) {
		$adv_city_id = $loc;
		$adv_city_name = "";
		$adv_city_location = "";
		foreach ($weather_current as $k=>$city) {
			if($city['ws_city_id'] == $adv_city_id) {
				$adv_city_name = $city['ws_city_name'];
				$show_loc_forecast = true;
				$adv_city_location = $city['location'];
			}
		}
	}
	
	// Show first again the current data
	?>
	<div class="weather_advanced_container">
		<?php
		if($module_change_scale == 1 || $module_show_print_link == 1) {
			?>
            <div class="weather_tools noprint">
                <?php
                if($module_change_scale == 1) {
                    if($c_to_f == "c") {
                        $scale_name = "Fahrenheit (&deg;F)";
                        $scale_unit = "f";
                    }
                    if($c_to_f == "f") {
                        $scale_name = "Celsius (&deg;C)";
                        $scale_unit = "c";
                    }

                    $weather_link = $_SERVER['REQUEST_URI'];
                    if(($qmark = strpos($weather_link, "?")) === false) {
                        $weather_link .= "?show_scale=" . $scale_unit;
                    }
                    else {
                        $qs = substr($weather_link, $qmark, strlen($weather_link)-1);
                        if($qs == "?") {
                            $qs .= "show_scale=" . $scale_unit;
                        }
                        else {
                            $qp = str_replace("?", "", $qs);
                            $qp = explode("&", $qp);
                            if(is_array($qp) && count($qp)>0) {
                                $new_qs = "?";
                                foreach ($qp as $i=>$par) {
                                    if(strpos($par, "=") !== false) {
                                        $pars = explode("=", $par);
                                        if($pars[0] == "show_scale") {
                                            $new_qs .= "&show_scale=$scale_unit";
                                        }
                                        else {
                                            $new_qs .= "&$par";
                                        }
                                    }
                                    else {
                                        $new_qs .= "&$par";
                                    }
                                }
                                $qs = $new_qs;
                            }
                            else {
                                $qs .= "&show_scale=" . $scale_unit;
                            }
                        }
                        $new_weather_link = substr($weather_link, 0, $qmark);
                        $weather_link = $new_weather_link . $qs;
                    }
                    $weather_link = str_replace("?&", "?", $weather_link);
                    ?>
                    <a class="wa-change-scale" href="<?php echo $weather_link;?>"><?php echo tx("$scale_name");?></a>
                    <?php
                }

                if($module_show_print_link == 1) {
                    if(!isset($print_screen)) {
                        ?>
                        <a class="wa-print hidden-xs" href="?print=1&module_page=weather_advanced_loc&show_scale=<?php echo $c_to_f;?>&lang=en&action=print&ccmi=<?php echo $current_catdom_module_id;?>" target="_blank"><i class="fa fa-print fa-fw"></i></a>
                        <?php
                    }
                    else {
                        ?>
                        <a class="wa-print hidden-xs" href="?print=1&module_page=weather_advanced_loc&show_scale=<?php echo $c_to_f;?>&lang=en&action=print&ccmi=<?php echo $current_catdom_module_id;?>" target="_blank"><i class="fa fa-print fa-fw"></i></a>
                        <?php
                    }
                }

                ?>
            </div>
			
			<?php
		}
		if(is_array($weather_current) && count($weather_current)>0) {
			require_once(dirname(__FILE__) . "/show_current.php");
		}
		?>
		
	</div>
	<?php
	
	
	
		
	if($show_loc_forecast) {
        require_once(dirname(__FILE__) . "/show_loc_forecast.php");
	}
	else {
		
	}
}