#!/usr/bin/perl
#use strict;
use CGI;
use DBI;
use Text::Template;
use HTML::Template;
use Text::Query;
use Astro::Time;
use Time::Local;
use Date::DayOfWeek;
require "/home/radish/www/lib/globalsearch.pm";
require "/home/radish/www/lib/stdin.pm";
require "/home/radish/www/lib/menus.pm";
require "/home/radish/www/htdocs/getalife/events/cgi/common.pm";
##########
#print STDOUT "Content-Type: text/html\n\n";
my $Template_file = '../tpl/7day.tpl';
my $Error_template = '../tpl/7day_error.tpl';
my(
$Query, $Now,
$Events,
$cgi, $dbh, $sth, $tpl, $search,$driver,$db,$host,$user,$port,$dsn,$dbh,
$men,$pagewrap
);
%dtrange = (
'1' => '24 hours',
'7' => 'One Week',
'30' => 'One Month',
'365' => 'One Year',
);
##########
#$ENV{'QUERY_STRING'} = qq(query=%&daterange=daterange=30);
###########read in STDIN variables
%data = &c::get_stdin;
@keys = keys %data;
##########
$data{'query'} =~ s/keywords here//g;
$Query = $data{'query'};
$Events = [];
$Now = time();##computes time in total seconds from some point in time
$Now -= 86400;##go back one day (84,400 seconds)
#goto ERROR unless $Query;
#+++++++++++
#db vars
$driver = "mysql";
$db = "events_web";
$host = "db.qconline.com";
$user = "public";
$port = 3306;
$dsn = "DBI:$driver:database=$db;host=$host;port=$port";
#connect
$dbh = DBI->connect($dsn, $user, undef);
#+++++++++++
@query_words = &c::get_query_words($Query);
$where_phrase = qq((a.title like "\%QUERYHERE\%" or a.body like "\%QUERYHERE\%" or a.venue like "\%QUERYHERE\%"));
$where_phrase_not = qq((a.title not like "\%QUERYHERE\%" and a.body not like "\%QUERYHERE\%" and a.venue not like "\%QUERYHERE\%"));
for(@query_words){
if($_ =~ /^\-/){
$where .= $where_phrase_not." and ";
}
else{
$where .= $where_phrase." and ";
}
$where =~ s/QUERYHERE/$_/g;
}
$where =~ s/ and $//;
$where =~ s/\"\%\-/\"\%/g; #get rid of not flag
if($where){$where = "and ".$where}
if($data{'type'}){
$sql = "select distinct a.id, a.title, a.body, a.venue, a.datestring, a.continuing, a.excluded, a.photo_id,a.tc_contest_id,a.free,a.related_events,a.related_stories,a.event_address,a.event_city,a.event_state,a.place_name,a.IsRadish from web as a,bydate as b,typelist as c where a.IsRadish=1 and a.id=b.id and a.id=c.id $where and b.unix>=$Now";
if($data{'daterange'}){
$sql .= " and unix<=".(time() + ($data{'daterange'} * 86400));
}
if($data{'type'}){
$sql .= " and c.types='$data{'type'}'";
}
}
else{
$sql = "select distinct a.id, a.title, a.body, a.venue, a.datestring, a.continuing, a.excluded, a.photo_id,a.tc_contest_id,a.free,a.related_events,a.related_stories,a.event_address,a.event_city,a.event_state,a.place_name,a.IsRadish from web as a,bydate as b where a.IsRadish=1 and a.id=b.id $where and b.unix>=$Now";
if($data{'daterange'}){
$sql .= " and b.unix<=".(time() + ($data{'daterange'} * 86400));
}
}
$sql .= " order by b.unix";
# LIMIT the number of returned upcoming events to the number listed here.
$sql .= " LIMIT 7";
#print "sql: $sql\n";
$sth = $dbh->prepare($sql);
$sth->execute or die $sth->errstr;
if(!$data{'index'}){
while ( my @row = $sth->fetchrow_array ) {
my %Record;
my $Dates = [];
@Record{'id', 'title', 'body', 'venue', 'datestring', 'continuing',
'excluded','photo_id','tc_contest_id','free','related_events','related_stories','event_address','event_city','event_state','place_name'} = @row;
$continue = 0;
$Record{datestring} =~ s/\,/\, /;
#if($Record{datestring} =~ /Every /){
# $msql = "select count(*) as count from bydate where id=$Record{id} and unix<=".$Now;
# my $sth = $dbh->prepare($msql);
# $sth->execute();
# while (my $ref = $sth->fetchrow_hashref()) {
# if($ref->{'count'} > 0){$continue = 1}
# }
# $sth->finish();
#}
#elsif($Record{datestring} =~ / to /){
# $msql = "select count(*) as count from daterange where id=$Record{id} and begin<=".$Now;
# my $sth = $dbh->prepare($msql);
# $sth->execute();
# while (my $ref = $sth->fetchrow_hashref()) {
# if($ref->{'count'} > 0){$continue = 1}
# }
# $sth->finish();
#}
#else{
$continue = 1;
#}
if($continue){
$Record{title} =~ s/\^/\'/g;
$Record{title} =~ s/\"/\'/g;
for(@query_words){
$Record{title} = &c::highlight_content($_,$Record{title});
}
$Record{body} =~ s/\^/\'/g;
for(@query_words){
$Record{body} = &c::highlight_content($_,$Record{body});
}
$Record{venue} =~ s/\^/\'/g;
$Record{venue} =~ s/\"/\'/g;
for(@query_words){
$Record{venue} = &c::highlight_content($_,$Record{venue});
}
if($Record{photo_id}){
#print "photo_id: $Record{photo_id}
";
$Record{photo_info} = &c::get_photo_info($Record{photo_id});
#print "photo_info: $Record{photo_info}
";
}
if($Record{tc_contest_id}){
$Record{tc_contest_link} = &c::get_contest_name($Record{tc_contest_id});
}
if($Record{related_events}){
$Record{related_event_links} = &c::get_related_event_links($Record{related_events});
}
if($Record{related_stories}){
$Record{related_story_links} = &c::get_related_story_links($Record{related_stories});
}
if($Record{event_address} && $Record{event_city} && $Record{event_state}){
$Record{mapquest_link} = &c::get_mapquest_link($Record{event_address},$Record{event_city},$Record{event_state});
}
if($Record{id}){
$Record{remind_datestring} = &c::get_remind_datestring($Record{id});
}
push(@$Events, \%Record);
}
}
}
$sth->finish;
$dbh->disconnect;
@$Events = map {$_} sort {$a->{sort} <=> $b->{sort}} map {$_} @$Events;
if ( scalar @$Events ) {
$tpl = Text::Template->new(
source => $Template_file,
delimiters => ['', '?>'],
);
my($Month, $Year) = (localtime)[4,5];
$Year += 1900;
$Month += 1;
require 'grid.lib';
print STDOUT "Content-Type: text/html\n\n";
$tpl->fill_in(
hash => {
query => $Query,
year => $Year,
month => $Month,
day => $Day+0,
events => $Events,
grid => \&Calendar_Grid,
type => $data{'type'},
sql => $sql,
daterange => $dtrange{$data{'daterange'}},
},
output => \*STDOUT,
);
}
else {
ERROR:;
$tpl = Text::Template->new(
source => $Error_template,
delimiters => ['', '?>'],
);
my($Month, $Year) = (localtime)[4,5];
$Year += 1900;
$Month += 1;
require '/home/radish/www/htdocs/getalife/events/cgi/grid.lib';
print STDOUT "Content-Type: text/html\n\n";
$tpl->fill_in(
hash => {
query => $Query,
year => $Year, month => $Month,
events => $Events,
grid => \&Calendar_Grid,
},
output => \*STDOUT,
);
}