#!/usr/bin/perl use lib '/home/dailyphoto/htdocs/content'; use lib '/home/lib'; #--------------------------------------------------------------------- #--------------------------------------------------------------------- use CNET::lib; use strict; use CGI::Fast; #--------------------------------------------------------------------- #--------------------------------------------------------------------- use wsys::DPGlobals; use webshots::WebshotsUser; use webshots::SuperLogEvent; use weather::WeatherUtility; #--------------------------------------------------------------------- #--------------------------------------------------------------------- my $CUSTOMIZE_CITY_TABLE = < _BODY_ EOM my $CUSTOMIZE_CITY_ROW = < _CITYNAME_ EOM #--------------------------------------------------------------------- # define main mappings and other worth globals #--------------------------------------------------------------------- my %CODE_TO_DESCRIPTION; # maps a city code to a city description my %REGION_TO_CODEARRAY; # maps a region code (state or country) to an array of city codes my %REGION_DESCRIPTION; # maps a region code to a region description my %STATE_LIST; # list of state region codes my %COUNTRY_LIST; # list of county region codes my %SEARCHABLE_CITY_MAP; # mapping of searchable city entries (DESC|CODE) my @SEARCHABLE_CITY_LIST; # array of searchable city codes (DESC|CODE) ReadCityCodeInfoFile("data/citylist.txt"); my $STATES_OPTION_BLOCK = GetOptionListForRegion(\%STATE_LIST); my $COUNTRY_OPTION_BLOCK = GetOptionListForRegion(\%COUNTRY_LIST); my $SEARCH_RESULTS_TEMPLATE = DPGlobals::ReadFile("search_results_template.html"); my $CUSTOMIZE_TEMPLATE = DPGlobals::ReadFile("customize_weather_main_template.html"); my $DISPLAY_TEMPLATE = DPGlobals::ReadFile("display_weather_main_template.html"); my $DISPLAY_SEARCH_RESULTS_TEMPLATE = DPGlobals::ReadFile("display_search_results_template.html"); my $FORCE_EXIT = 0; #--------------------------------------------------------------------- #--------------------------------------------------------------------- CONTINUE: while (my $cgi = new CGI::Fast) { my $user = new WebshotsUser; my $action = $cgi->param("action"); # the action to perform # (add, showlist, del, up, down, display) my $searchcity = $cgi->param("search"); # search city if applicable my $citycode = $cgi->param("city"); # citycode if applicable my $highlightcity = $cgi->param("highlightcity"); # citycode to highlight if applicable my $displaymode = $cgi->param("displaymode"); # should we show display or add links ####################################################### #------------------------------------------------------ # # CGI_STATE: search for a city # #------------------------------------------------------ ####################################################### if ($searchcity) { if (length($searchcity)<2) { DPGlobals::DisplayErrorPage("You must enter at least the first TWO letters of the city."); $FORCE_EXIT ? exit : next CONTINUE; } my @list; #------------------------------------------------------ # lookup by zipcode if available, otherwise by cityname #------------------------------------------------------ my $city; my $state; ($city,$state) = WebshotsUser::LookupCityStateForZipCode($searchcity); if ($city) { $searchcity="$city, $state"; lc($searchcity); @list = SearchForCity($searchcity); # if we found a city, but the search failed, display special msg if ($city && @list==0) { DPGlobals::DisplayErrorPage("We do not have a listing specifically for $searchcity, you might try typing in the name of a larger nearby city."); $FORCE_EXIT ? exit : next CONTINUE; } } else { # no matching zipcode, lookup by city name @list = SearchForCity($searchcity); } @list = sort(@list); #------------------------------------------------------ if (@list==0) { $searchcity = uc($searchcity); DPGlobals::DisplayErrorPage("No matching cities were found that begin with the letters $searchcity"); $FORCE_EXIT ? exit : next CONTINUE; } #------------------------------------------------------ if (@list==1) { # only ONE city, just add it by simulating the 'add' or 'display' command # and falling through if ($displaymode) { $action = 'display'; } else { $action = 'add'; } $citycode = $list[0]; } else { # # more than one city,display list # my $insert = CreateCityListHTML(\@list, $displaymode ? 'display' : 'add'); my $results_page = ($displaymode==0 ? $SEARCH_RESULTS_TEMPLATE : $DISPLAY_SEARCH_RESULTS_TEMPLATE); $results_page =~ s/_RESULTS_/$insert/; my $text = DPGlobals::GetMetaPageHTML ( TITLE => "Customize Weather", R_CORE => \$results_page, MDOCS_OPTIONAL => { -LOGJAM => 'weather'} ); print $cgi->header(); print $text; $FORCE_EXIT ? exit : next CONTINUE; } } ####################################################### #------------------------------------------------------ # # CGI_STATE: show a city list # #------------------------------------------------------ ####################################################### if ($action eq "showlist") { my $region = $cgi->param("region"); my $insert = CreateCityListHTMLForRegion($region, $displaymode ? 'display' : 'add'); my $results_page = ($displaymode==0 ? $SEARCH_RESULTS_TEMPLATE : $DISPLAY_SEARCH_RESULTS_TEMPLATE); $results_page =~ s/_RESULTS_/$insert/; my $text = DPGlobals::GetMetaPageHTML ( TITLE => "Customize Weather", R_CORE => \$results_page, MDOCS_OPTIONAL => { -LOGJAM => 'weather'} ); print $cgi->header(); print $text; $FORCE_EXIT ? exit : next CONTINUE; } ####################################################### #------------------------------------------------------ # # CGI_STATE: view a specific city # #------------------------------------------------------ ####################################################### if ($action eq 'display' && $citycode) { my $units; if (!$user->LoginFromCookieNoFail($cgi)) { $units='e_'; } else { my %settings; my $content = $user->GetContentModuleInfo("WEATHER"); WeatherUtility::ParseWeatherContentModuleString($content,\%settings); $units = $settings{METRIC} ? 'm_' : 'e_'; } my $file = "/content/weather/html/full/$units$citycode.html"; print $cgi->redirect($file); $FORCE_EXIT ? exit : next CONTINUE; } ####################################################### #------------------------------------------------------ # # CGI_STATE: pick a city to display from drop boxes # #------------------------------------------------------ ####################################################### if ($action eq '' && $displaymode==1) { my $maps = DPGlobals::ReadFile("maps_template.html"); my $text = DPGlobals::GetMetaPageHTML ( TITLE => "Lookup Weather", R_CORE => \$DISPLAY_TEMPLATE, MDOCS_OPTIONAL => { -LOGJAM => 'weather'} ); #------------------------------------------------------ # insert option blocks into combo boxes #------------------------------------------------------ $text =~ s/ EOM my $body; my @regions = sort(keys %$r_hash); foreach (@regions) { if ($_) { my $block = $template; my $desc = $REGION_DESCRIPTION{$_}; if ($_ && $desc) { $block =~ s/_SELECTED_//; $block =~ s/_VALUE_/$_/; $block =~ s/_DISPLAY_/$desc/; $body .= $block; } } } return $body; } #--------------------------------------------------------------------- #--------------------------------------------------------------------- sub SearchForCity { my $search = $_[0]; $search = Trim($search); my @matches = grep /^$search/i, @SEARCHABLE_CITY_LIST; my @return; foreach (@matches) { push @return, $SEARCHABLE_CITY_MAP{$_}; } return @return; } #--------------------------------------------------------------------- #--------------------------------------------------------------------- sub Trim { my $val = $_[0]; $val =~ s/^\s+//; $val =~ s/\s+$//; return $val; }