separate time conditions

This commit is contained in:
Milan
2018-07-08 22:46:29 +02:00
parent a13180d649
commit bfcf56f05a

View File

@@ -255,19 +255,19 @@ sub modify_results {
#} #}
if ( defined $result->{image} ) { if ( defined $result->{image} ) {
my $url = $config->{locations}->{local_media_url}||''; my $url = $config->{locations}->{local_media_url} || '';
my $image = $result->{image}; my $image = $result->{image};
$result->{thumb_url} = $url.'/thumbs/'.$image; $result->{thumb_url} = $url . '/thumbs/' . $image;
$result->{icon_url} = $url.'/icons/'.$image; $result->{icon_url} = $url . '/icons/' . $image;
$result->{image_url} = $url.'/images/'.$image; $result->{image_url} = $url . '/images/' . $image;
} }
if ( defined $result->{series_image} ) { if ( defined $result->{series_image} ) {
my $url = $config->{locations}->{local_media_url}||''; my $url = $config->{locations}->{local_media_url} || '';
my $image = $result->{series_image}; my $image = $result->{series_image};
$result->{series_thumb_url} = $url.'/thumbs/'.$image; $result->{series_thumb_url} = $url . '/thumbs/' . $image;
$result->{series_icon_url} = $url.'/icons/'.$image; $result->{series_icon_url} = $url . '/icons/' . $image;
$result->{series_image_url} = $url.'/images/'.$image; $result->{series_image_url} = $url . '/images/' . $image;
} }
$result->{location_css} = $result->{location} || ''; $result->{location_css} = $result->{location} || '';
@@ -349,13 +349,14 @@ sub modify_results {
$counter++; $counter++;
if ( ( defined $params->{template} ) if ( ( defined $params->{template} )
&& ( $params->{template} =~ /(list|details)/ ) ){ && ( $params->{template} =~ /(list|details)/ ) )
if ( ( defined $result->{excerpt} ) && ( length($result->{excerpt}) > 250) ){ {
$result->{excerpt} = substr($result->{excerpt}, 0, 250).'...'; if ( ( defined $result->{excerpt} ) && ( length( $result->{excerpt} ) > 250 ) ) {
$result->{excerpt} = substr( $result->{excerpt}, 0, 250 ) . '...';
} }
if ( (defined $result->{user_excerpt}) && (length($result->{user_excerpt}) > 250) ){ if ( ( defined $result->{user_excerpt} ) && ( length( $result->{user_excerpt} ) > 250 ) ) {
$result->{user_excerpt} = substr($result->{user_excerpt}, 0, 250).'...'; $result->{user_excerpt} = substr( $result->{user_excerpt}, 0, 250 ) . '...';
} }
} }
@@ -562,32 +563,32 @@ sub calc_dates {
return $result; return $result;
} }
sub add_recordings{ sub add_recordings {
my $dbh = shift; my $dbh = shift;
my $config = shift; my $config = shift;
my $request = shift; my $request = shift;
my $events = shift; my $events = shift;
# print STDERR Dumper($results); # print STDERR Dumper($results);
return $events unless defined $events; return $events unless defined $events;
my $params = $request->{params}->{checked}; my $params = $request->{params}->{checked};
return $events unless defined $params; return $events unless defined $params;
return $events unless defined $params->{recordings}; return $events unless defined $params->{recordings};
my @ids=(); my @ids = ();
my $eventsById={}; my $eventsById = {};
#my $events = $results; #my $events = $results;
for my $event (@$events){ for my $event (@$events) {
my $eventId = $event->{event_id}; my $eventId = $event->{event_id};
push @ids, $eventId; push @ids, $eventId;
$eventsById->{$eventId} = $event; $eventsById->{$eventId} = $event;
} }
my $qms = join( ', ', ( map { '?' } @$events ) );
my $qms = join( ', ', (map { '?' } @$events)); my $bindValues = join( ', ', ( map { $_->{event_id} } @$events ) );
my $bindValues = join( ', ', (map { $_->{event_id} } @$events));
my $query = qq{ my $query = qq{
select * select *
@@ -598,9 +599,10 @@ sub add_recordings{
$dbh = db::connect($config) unless defined $dbh; $dbh = db::connect($config) unless defined $dbh;
my $recordings = db::get( $dbh, $query, $bindValues ); my $recordings = db::get( $dbh, $query, $bindValues );
#print STDERR Dumper($recordings); #print STDERR Dumper($recordings);
for my $entry (@$recordings){ for my $entry (@$recordings) {
my $eventId = $entry->{event_id}; my $eventId = $entry->{event_id};
my $event = $eventsById->{$eventId}; my $event = $eventsById->{$eventId};
push @{ $event->{recordings} }, $entry; push @{ $event->{recordings} }, $entry;
@@ -609,6 +611,186 @@ sub add_recordings{
return $events; return $events;
} }
sub getDateRange {
my $config = shift;
$config->{date}->{day_starting_hour};
}
sub getDateQueryConditions {
my $config = shift;
my $params = shift;
my $bind_values = shift;
# conditions by date
my $date_conds = [];
#date, today, tomorrow, yesterday
my $date = '';
$date = time::date_cond( $params->{date} ) if ( $params->{date} ne '' );
my $from_date = '';
$from_date = time::date_cond( $params->{from_date} )
if ( $params->{from_date} ne '' );
my $till_date = '';
$till_date = time::date_cond( $params->{till_date} )
if ( $params->{till_date} ne '' );
my $from_time = '';
$from_time = time::time_cond( $params->{from_time} )
if ( $params->{from_time} ne '' );
my $till_time = '';
$till_time = time::time_cond( $params->{till_time} )
if ( $params->{till_time} ne '' );
my $date_range_include = $params->{date_range_include};
#from_time is defined
if ( ( $params->{from_date} ne '' ) && ( $params->{from_time} ne '' ) ) {
my $datetime = time::datetime_cond( $from_date . 'T' . $from_time );
if ( $datetime ne '' ) {
if ( $date_range_include eq '1' ) {
push @$date_conds, ' end > ? ';
push @$bind_values, $datetime;
$from_date = '';
} else {
push @$date_conds, ' start >= ? ';
push @$bind_values, $datetime;
$from_date = '';
}
}
}
#till_time is defined
if ( ( $params->{till_date} ne '' ) && ( $params->{till_time} ne '' ) ) {
my $datetime = time::datetime_cond( $till_date . 'T' . $till_time );
if ( $datetime ne '' ) {
push @$date_conds, ' start < ? ';
push @$bind_values, $datetime;
$till_date = '';
}
}
#time is defined
if ( ( defined $params->{time} )
&& ( $params->{time} ne '' )
&& ( $params->{time} ne 'now' )
&& ( $params->{time} ne 'future' ) )
{
my $time = time::time_cond( $params->{time} );
if ( $time ne '' ) {
push @$date_conds, ' time(start) = ? ';
push @$bind_values, $time;
}
return $date_conds;
}
#event is not over
elsif ( ( defined $params->{time} ) && ( $params->{time} eq 'now' ) ) {
push @$date_conds, qq{
(
( unix_timestamp(end) > unix_timestamp(now() ) )
and
( unix_timestamp(start) <= unix_timestamp(now() ) )
)
};
return $date_conds;
} elsif ( ( defined $params->{time} ) && ( $params->{time} eq 'future' ) ) {
push @$date_conds, qq{
(
( unix_timestamp(end) > unix_timestamp(now() ) )
and
( unix_timestamp(end) - unix_timestamp(now() ) ) < 7*24*3600
)
};
return $date_conds;
} elsif ( $date ne '' ) {
if ( $date eq 'today' ) {
my $date = time::get_event_date($config);
if ( $date ne '' ) {
push @$date_conds, ' ( start_date = ? ) ';
push @$bind_values, $date;
}
} else {
#push @$date_conds, ' ( start_date = ? ) ';
#push @$bind_values, $date;
my $start = time::datetime_cond( $date . 'T00:00:00' );
if ( $start ne '' ) {
$start = time::add_hours_to_datetime( $start, $config->{date}->{day_starting_hour} );
my $end = time::add_hours_to_datetime( $start, 24 );
if ( $date_range_include eq '1' ) {
push @$date_conds, ' end > ? ';
push @$bind_values, $start;
} else {
push @$date_conds, ' start >= ? ';
push @$bind_values, $start;
}
if ( $end ne '' ) {
push @$date_conds, ' start < ? ';
push @$bind_values, $end;
}
}
}
return $date_conds;
} else {
if ( $from_date ne '' ) {
if ( $date_range_include eq '1' ) {
# end is after start
push @$date_conds, ' ( end_date >= ? )';
push @$bind_values, $from_date;
} else {
push @$date_conds, ' ( start_date >= ? ) ';
push @$bind_values, $from_date;
}
}
if ( $till_date ne '' ) {
if ( $date_range_include eq '1' ) {
# start is before end
push @$date_conds, ' ( start_date <= ? )';
push @$bind_values, $till_date;
} else {
push @$date_conds, ' ( end_date <= ? ) ';
push @$bind_values, $till_date;
}
}
if ( $params->{weekday} ne '' ) {
my $weekday = $params->{weekday};
$weekday += 1;
$weekday -= 7 if ( $weekday > 7 );
push @$date_conds, ' (dayofweek(start_date)= ?) ';
push @$bind_values, $weekday;
}
if ( $params->{archive} eq 'past' ) {
my $date = time::get_event_date($config);
if ( $date ne '' ) {
push @$date_conds, ' ( start_date < ? ) ';
push @$bind_values, $date;
}
} elsif ( $params->{archive} eq 'future' ) {
my $date = time::get_event_date($config);
if ( $date ne '' ) {
push @$date_conds, ' ( start_date >= ? ) ';
push @$bind_values, $date;
}
}
}
return $date_conds;
}
# if recordings is set in params, recordings date and path will be included # if recordings is set in params, recordings date and path will be included
sub get_query { sub get_query {
my $dbh = shift; my $dbh = shift;
@@ -646,162 +828,9 @@ sub get_query {
} else { } else {
# conditions by date my $date_conds = getDateQueryConditions( $config, $params, $bind_values );
my @date_conds = (); my $date_cond = join " and ", @$date_conds;
#date, today, tomorrow, yesterday
my $date = '';
$date = time::date_cond( $params->{date} ) if ( $params->{date} ne '' );
my $from_date = '';
$from_date = time::date_cond( $params->{from_date} )
if ( $params->{from_date} ne '' );
my $till_date = '';
$till_date = time::date_cond( $params->{till_date} )
if ( $params->{till_date} ne '' );
my $from_time = '';
$from_time = time::time_cond( $params->{from_time} )
if ( $params->{from_time} ne '' );
my $till_time = '';
$till_time = time::time_cond( $params->{till_time} )
if ( $params->{till_time} ne '' );
my $date_range_include = $params->{date_range_include};
#from_time is defined
if ( ( $params->{from_date} ne '' ) && ( $params->{from_time} ne '' ) ) {
my $datetime = time::datetime_cond( $from_date . 'T' . $from_time );
if ( $datetime ne '' ) {
if ( $date_range_include eq '1' ) {
push @date_conds, ' end > ? ';
push @$bind_values, $datetime;
$from_date = '';
} else {
push @date_conds, ' start >= ? ';
push @$bind_values, $datetime;
$from_date = '';
}
}
}
#till_time is defined
if ( ( $params->{till_date} ne '' ) && ( $params->{till_time} ne '' ) ) {
my $datetime = time::datetime_cond( $till_date . 'T' . $till_time );
if ( $datetime ne '' ) {
push @date_conds, ' start < ? ';
push @$bind_values, $datetime;
$till_date = '';
}
}
#time is defined
if ( ( defined $params->{time} )
&& ( $params->{time} ne '' )
&& ( $params->{time} ne 'now' )
&& ( $params->{time} ne 'future' ) )
{
my $time = time::time_cond( $params->{time} );
if ( $time ne '' ) {
push @date_conds, ' time(start) = ? ';
push @$bind_values, $time;
}
}
#event is not over
if ( ( defined $params->{time} ) && ( $params->{time} eq 'now' ) ) {
push @date_conds, qq{
(
( unix_timestamp(end) > unix_timestamp(now() ) )
and
( unix_timestamp(start) <= unix_timestamp(now() ) )
)
};
} elsif ( ( defined $params->{time} ) && ( $params->{time} eq 'future' ) ) {
push @date_conds, qq{
(
( unix_timestamp(end) > unix_timestamp(now() ) )
and
( unix_timestamp(end) - unix_timestamp(now() ) ) < 7*24*3600
)
};
} elsif ( $date ne '' ) {
if ( $date eq 'today' ) {
my $date = time::get_event_date($config);
if ( $date ne '' ) {
push @date_conds, ' ( start_date = ? ) ';
push @$bind_values, $date;
}
} else {
if ( $date ne '' ) {
push @date_conds, ' ( start_date = ? ) ';
push @$bind_values, $date;
}
}
} else {
if ( $from_date ne '' ) {
if ( $date_range_include eq '1' ) {
# end is after start
push @date_conds, ' ( end_date >= ? )';
push @$bind_values, $from_date;
} else {
push @date_conds, ' ( start_date >= ? ) ';
push @$bind_values, $from_date;
}
}
if ( $till_date ne '' ) {
if ( $date_range_include eq '1' ) {
# start is before end
push @date_conds, ' ( start_date <= ? )';
push @$bind_values, $till_date;
} else {
push @date_conds, ' ( end_date <= ? ) ';
push @$bind_values, $till_date;
}
}
if ( $params->{weekday} ne '' ) {
my $weekday = $params->{weekday};
$weekday += 1;
$weekday -= 7 if ( $weekday > 7 );
push @date_conds, ' (dayofweek(start_date)= ?) ';
push @$bind_values, $weekday;
}
if ( $params->{archive} eq 'past' ) {
my $date = time::get_event_date($config);
if ( $date ne '' ) {
push @date_conds, ' ( start_date < ? ) ';
push @$bind_values, $date;
}
} elsif ( $params->{archive} eq 'future' ) {
my $date = time::get_event_date($config);
if ( $date ne '' ) {
push @date_conds, ' ( start_date >= ? ) ';
push @$bind_values, $date;
}
}
}
if ( ( defined $params->{time_of_day} )
&& ( $params->{time_of_day} ne '' ) )
{
push @date_conds, ' ( time_of_day = ? ) ';
push @$bind_values, $params->{time_of_day};
}
my $date_cond = join " and ", @date_conds;
push @$where_cond, $date_cond if ( $date_cond ne '' ); push @$where_cond, $date_cond if ( $date_cond ne '' );
} }
@@ -923,6 +952,7 @@ sub get_query {
} }
} }
} }
#print STDERR $search_cond."\n"; #print STDERR $search_cond."\n";
my $project_cond = ''; my $project_cond = '';
@@ -1083,15 +1113,17 @@ sub get_query {
push @$bind_values, $params->{studio_id}; push @$bind_values, $params->{studio_id};
$query .= ', se.studio_id'; $query .= ', se.studio_id';
} }
#push @$where_cond, 'se.event_id=e.id'; #push @$where_cond, 'se.event_id=e.id';
} }
# add recordings field and conditions # add recordings field and conditions
if ($params->{recordings} eq '1' ) { if ( $params->{recordings} eq '1' ) {
$query .= ', ar.path'; $query .= ', ar.path';
$query .= ', ar.size'; $query .= ', ar.size';
$query .= ', ar.created_by uploaded_by'; $query .= ', ar.created_by uploaded_by';
$query .= ', ar.modified_at uploaded_at'; $query .= ', ar.modified_at uploaded_at';
#push @$where_cond, 'e.id=ar.event_id'; #push @$where_cond, 'e.id=ar.event_id';
} }
@@ -1099,9 +1131,10 @@ sub get_query {
# add tables # add tables
if ( ( $params->{project_id} ne '' ) || ( $params->{studio_id} ne '' ) ) { if ( ( $params->{project_id} ne '' ) || ( $params->{studio_id} ne '' ) ) {
# prepent series_events # prepent series_events
$query .= "\n calcms_series_events se inner join calcms_events e on se.event_id=e.id"; $query .= "\n calcms_series_events se inner join calcms_events e on se.event_id=e.id";
}else{ } else {
$query .= "\n calcms_events e"; $query .= "\n calcms_events e";
} }
@@ -1211,7 +1244,8 @@ sub render {
if ( $project->{name} ne '' ); if ( $project->{name} ne '' );
$template_parameters->{controllers} = $config->{controllers}; $template_parameters->{controllers} = $config->{controllers};
$template_parameters->{hide_event_images}=1 if (defined $config->{permissions}->{hide_event_images}) && ($config->{permissions}->{hide_event_images} == 1); $template_parameters->{hide_event_images} = 1
if ( defined $config->{permissions}->{hide_event_images} ) && ( $config->{permissions}->{hide_event_images} == 1 );
# use Data::Dumper;print STDERR Dumper($template_parameters)."\n"; # use Data::Dumper;print STDERR Dumper($template_parameters)."\n";
template::process( $_[0], $params->{template}, $template_parameters ); template::process( $_[0], $params->{template}, $template_parameters );
@@ -1251,11 +1285,10 @@ sub setDefaultEventConditions {
my $conditions = $_[0]; my $conditions = $_[0];
my $bind_values = $_[1]; my $bind_values = $_[1];
my $options = $_[2]; my $options = $_[2];
$options={} unless defined $options; $options = {} unless defined $options;
# exclude projects # exclude projects
if ( if ( ( defined $options->{exclude_projects} )
( defined $options->{exclude_projects} )
&& ( $options->{exclude_projects} == 1 ) && ( $options->{exclude_projects} == 1 )
&& ( defined $config->{filter} ) && ( defined $config->{filter} )
&& ( defined $config->{filter}->{projects_to_exclude} ) ) && ( defined $config->{filter}->{projects_to_exclude} ) )
@@ -1269,8 +1302,7 @@ sub setDefaultEventConditions {
} }
# exclude locations # exclude locations
if ( if ( ( defined $options->{exclude_locations} )
( defined $options->{exclude_locations} )
&& ( $options->{exclude_locations} == 1 ) && ( $options->{exclude_locations} == 1 )
&& ( defined $config->{filter} ) && ( defined $config->{filter} )
&& ( defined $config->{filter}->{locations_to_exclude} ) ) && ( defined $config->{filter}->{locations_to_exclude} ) )
@@ -1390,6 +1422,7 @@ sub get_previous_event_of_series {
return $events->[0]->{id}; return $events->[0]->{id};
} }
# used by calendar
sub get_by_date_range { sub get_by_date_range {
my $dbh = shift; my $dbh = shift;
my $config = shift; my $config = shift;
@@ -1515,11 +1548,13 @@ sub get_duration {
my $timezone = $config->{date}->{time_zone}; my $timezone = $config->{date}->{time_zone};
my $start = time::get_datetime( $event->{start}, $timezone ); my $start = time::get_datetime( $event->{start}, $timezone );
my $end = time::get_datetime( $event->{end}, $timezone ); my $end = time::get_datetime( $event->{end}, $timezone );
#my $seconds = $end->subtract($start)->in_units("minutes"); #my $seconds = $end->subtract($start)->in_units("minutes");
#return $seconds; #return $seconds;
return undef unless defined $start; return undef unless defined $start;
return undef unless defined $end; return undef unless defined $end;
my $duration = $end->epoch() - $start->epoch(); my $duration = $end->epoch() - $start->epoch();
#print STDERR "duration=$duration, end=".$end->datetime()." start=".$start->datetime()."\n"; #print STDERR "duration=$duration, end=".$end->datetime()." start=".$start->datetime()."\n";
return $duration / 60; return $duration / 60;
} }
@@ -1647,16 +1682,15 @@ sub check_params {
$projects_to_exclude =~ s/\s+/ /g; $projects_to_exclude =~ s/\s+/ /g;
} }
#enable exclude locations filter #enable exclude locations filter
my $exclude_locations = 0; my $exclude_locations = 0;
$exclude_locations = 1 if ( defined $params->{exclude_locations} ) && ( $params->{exclude_locations} eq '1' ) ; $exclude_locations = 1 if ( defined $params->{exclude_locations} ) && ( $params->{exclude_locations} eq '1' );
my $exclude_projects = 0; my $exclude_projects = 0;
$exclude_projects = 1 if ( defined $params->{exclude_projects} ) && ( $params->{exclude_projects} eq '1' ) ; $exclude_projects = 1 if ( defined $params->{exclude_projects} ) && ( $params->{exclude_projects} eq '1' );
my $exclude_event_images = 0; my $exclude_event_images = 0;
$exclude_event_images = 1 if ( defined $params->{exclude_event_images} ) && ( $params->{exclude_event_images} eq '1' ) ; $exclude_event_images = 1 if ( defined $params->{exclude_event_images} ) && ( $params->{exclude_event_images} eq '1' );
#show future events by default #show future events by default
my $archive = 'future'; my $archive = 'future';
@@ -1700,7 +1734,7 @@ sub check_params {
#print STDERR $params->{template}."\n"; #print STDERR $params->{template}."\n";
my $template = '.html'; my $template = '.html';
if ( ( defined $params->{template} ) && ($params->{template} eq 'no') ) { if ( ( defined $params->{template} ) && ( $params->{template} eq 'no' ) ) {
$template = 'no'; $template = 'no';
} else { } else {
$template = template::check( $params->{template}, 'event_list.html' ); $template = template::check( $params->{template}, 'event_list.html' );