<?php
$lngModule = "weather_advanced";
require_once(dirname(__FILE__) . "/functions.php");


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';";
	$r = @mysql_query($q);
	if($r && mysql_num_rows($r)==1) {
		$row = mysql_fetch_assoc($r);
		$module_params = unserialize(base64_decode($row['module_params']));
	}
}

if(isset($module_params) && is_array($module_params) && count($module_params) >0) {
	$q_ws = "select * from module where url='weather_advanced';";
	$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>';
	
	//$cities_list_friendly_url = isset($module_params['cities_list_friendly_url']) ? $module_params['cities_list_friendly_url'] : array();
	
	$current_catdom_module_id = $module_params['admin_catdom_module_id'];
	


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

	$city_name_list = isset($module_params['cities_list']) ? $module_params['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'] : '';

	if($adv_weather_debug) {
		echo '<pre>$city_name_list = ', var_export($city_name_list, true), '</pre>';
	}
    $weather_advanced_key = 'weather_advanced';
    $weather_five_day = $pfcache->get($weather_advanced_key);

    if($weather_five_day == null) {

        //error_reporting(E_ALL);
        //ini_set('display_errors', 'On');

        // Pull in the NuSOAP code
        require_once(dirname(__FILE__) . "/../wsbin/lib/nusoap.php");




        //$webservices_url = "http://localhost/webservices/cyws/srvdispatcher.php?wsdl";
        $webservices_url = "http://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>$city_name_list = ', var_export($city_name_list, true), '</pre>';
            echo '<pre>$x_weather_current = ',var_export($x_weather_current, true),'</pre>';
            echo '<pre>$x_weather_fiveday = ',var_export($x_weather_fiveday, true),'</pre>';
        }

        if(is_array($x_weather_current) && count($x_weather_current) > 0 && is_array($x_weather_fiveday) && count($x_weather_fiveday) > 0) {
            $cache_put = serialize(array($x_weather_current, $x_weather_fiveday));

            $pfcache->set($weather_advanced_key, $cache_put, 3600);

            $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 = unserialize($weather_five_day);
        $weather_current = $weather_five_day[0];
        $weather_fiveday = $weather_five_day[1];
    }

	if($adv_weather_debug) {
        echo '<pre>$city_name_list = ', var_export($city_name_list, true), '</pre>';
		echo '<pre>$weather_current = ',var_export($weather_current, true),'</pre>';
		echo '<pre>$weather_fiveday = ',var_export($weather_fiveday, true),'</pre>';
	}
	
	//echo '<pre>', var_export($weather_fiveday[intval($_REQUEST['loc'])], 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;
	if(isset($_REQUEST['loc']) && intval($_REQUEST['loc'])> 0) {
		$adv_city_id = intval($_REQUEST['loc']);
		$adv_city_name = "";
		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;
			}
		}
	}
	
	if($show_loc_forecast) {
		require_once(dirname(__FILE__) . "/loc_forecast.php");
	}
	else {
        require_once(dirname(__FILE__) . "/gen_forecast.php");
	}
}