diff --git a/lib/calcms/UTF8DBI.pm b/lib/calcms/UTF8DBI.pm deleted file mode 100644 index daaff4c..0000000 --- a/lib/calcms/UTF8DBI.pm +++ /dev/null @@ -1,48 +0,0 @@ -# UTF8DBI.pm re-implementation by Pavel Kudinov http://search.cpan.org/~kudinov/ -# originally from: http://dysphoria.net/code/perl-utf8/ -# And patched again by Andrew Forrest, Jan 2007 - -use DBI 1.21; -use utf8; -use Encode(); - -package UTF8DBI; -use base DBI; - -sub _utf8_ { - if ( ref $_ eq 'ARRAY' ) { _utf8_() foreach @$_ } - elsif ( ref $_ eq 'HASH' ) { _utf8_() foreach values %$_ } - else { - Encode::_utf8_on($_); - if ( Encode::is_utf8($_) && !Encode::is_utf8( $_, 1 ) ) { - - #$_ = '⁂malformed-UTF8‼' #die "Malformed utf8 string in database" - #print data_string_desc($_)."\n" - #data_string_desc(STRING); - #Encode::decode_utf8($_); - #; - #Encode::_utf8_off($_); - #Encode::decode_utf8($_); - #Encode::encode_utf8($_); - } - } - $_; -} - -package UTF8DBI::db; -use base DBI::db; - -sub selectrow_arrayref { return UTF8DBI::_utf8_ for shift->SUPER::selectrow_arrayref(@_) } -sub selectrow_hashref { return UTF8DBI::_utf8_ for shift->SUPER::selectrow_hashref(@_) } -sub selectall_arrayref { return UTF8DBI::_utf8_ for shift->SUPER::selectall_arrayref(@_) } -sub selectall_hashref { return UTF8DBI::_utf8_ for shift->SUPER::selectall_hashref(@_) } -sub selectcol_arrayref { return UTF8DBI::_utf8_ for shift->SUPER::selectcol_arrayref(@_) } - -sub selectrow_array { @{ shift->selectrow_arrayref(@_) } } - -package UTF8DBI::st; -use base DBI::st; - -sub fetch { return UTF8DBI::_utf8_ for shift->SUPER::fetch(@_) } - -1; diff --git a/lib/calcms/aggregator.pm b/lib/calcms/aggregator.pm index 4781e33..90a4e55 100644 --- a/lib/calcms/aggregator.pm +++ b/lib/calcms/aggregator.pm @@ -115,9 +115,9 @@ sub get_calendar { #set query string for caching my $options = []; - push( @$options, 'date=' . $params->{date} ) if ( $params->{date} ne '' ); - push( @$options, 'from_date=' . $params->{from_date} ) if ( $params->{from_date} ne '' ); - push( @$options, 'till_date=' . $params->{till_date} ) if ( $params->{till_date} ne '' ); + push @$options, 'date=' . $params->{date} if $params->{date} ne ''; + push @$options, 'from_date=' . $params->{from_date} if $params->{from_date} ne ''; + push @$options, 'till_date=' . $params->{till_date} if $params->{till_date} ne ''; $ENV{QUERY_STRING} = '' . join( "&", @$options ); my $content = ''; @@ -270,10 +270,10 @@ sub check_params { #set query string for caching if ( ( !exists $ENV{QUERY_STRING} ) || ( $ENV{QUERY_STRING} eq '' ) ) { my $options = []; - push( @$options, 'date=' . $date ) if $date ne ''; - push( @$options, 'from_date=' . $from_date ) if $from_date ne ''; - push( @$options, 'till_date=' . $till_date ) if $till_date ne ''; - push( @$options, 'event_id=' . $event_id ) if $event_id ne ''; + push @$options, 'date=' . $date if $date ne ''; + push @$options, 'from_date=' . $from_date if $from_date ne ''; + push @$options, 'till_date=' . $till_date if $till_date ne ''; + push @$options, 'event_id=' . $event_id if $event_id ne ''; $ENV{QUERY_STRING} = '' . join( "&", @$options ); } diff --git a/lib/calcms/cache.pm b/lib/calcms/cache.pm index c3af0e5..9a02869 100644 --- a/lib/calcms/cache.pm +++ b/lib/calcms/cache.pm @@ -147,8 +147,8 @@ sub set { sub save { my $cache = shift; - return if ( $cache->{action} ne 'save' ); - return if ( ( !defined $cache->{filename} ) || ( $cache->{filename} eq '' ) ); + return if $cache->{action} ne 'save'; + return if ( !defined $cache->{filename} ) || ( $cache->{filename} eq '' ); log::save_file( $cache->{filename}, $cache->{content} ); chmod 0664, $cache->{filename}; diff --git a/lib/calcms/calendar.pm b/lib/calcms/calendar.pm index 07ca9be..781801b 100644 --- a/lib/calcms/calendar.pm +++ b/lib/calcms/calendar.pm @@ -264,8 +264,8 @@ sub get { }; $week_result->{class} .= ' selected' - if ( ( ( defined $params->{from_date} ) && ( $start_date eq $params->{from_date} ) ) - || ( ( defined $params->{till_date} ) && ( $end_date eq $params->{till_date} ) ) ); + if ( ( defined $params->{from_date} ) && ( $start_date eq $params->{from_date} ) ) + || ( ( defined $params->{till_date} ) && ( $end_date eq $params->{till_date} ) ); $week_result->{class} .= " week_$weekCounter"; $week_result->{class} =~ s/^\s+//g; diff --git a/lib/calcms/comments.pm b/lib/calcms/comments.pm index 938a41b..7cec0d6 100644 --- a/lib/calcms/comments.pm +++ b/lib/calcms/comments.pm @@ -23,141 +23,141 @@ sub init { sub get_cached_or_render { - # my $response=$_[0]; - my $config = $_[1]; - my $request = $_[2]; - my $mark_locked = $_[3]; + # my $response=$_[0]; + my $config = $_[1]; + my $request = $_[2]; + my $mark_locked = $_[3]; - my $params = $request->{params}->{checked}; + my $params = $request->{params}->{checked}; - #print STDERR Dumper($params); - $config->{app_name} = $config->{controllers}->{comments}; + #print STDERR Dumper($params); + $config->{app_name} = $config->{controllers}->{comments}; - my $comment = $request->{params}->{checked}; + my $comment = $request->{params}->{checked}; - my $filename = ''; - my $cache = {}; + my $filename = ''; + my $cache = {}; - my $results = comments::get( $config, $request ); + my $results = comments::get( $config, $request ); - if ( ( defined $mark_locked ) && ( $mark_locked eq 'mark_locked' ) ) { - for my $result (@$results) { - if ( $result->{lock_status} ne 'show' ) { - $result->{author} = 'Zensur'; - $result->{content} = 'Dieser Eintrag wurde gelöscht.'; - } - } - } elsif ( ( defined $mark_locked ) && ( $mark_locked eq 'filter_locked' ) ) { - my @results2 = (); - for my $result (@$results) { - push @results2, $result if ( $result->{lock_status} eq 'show' ); - } - $results = \@results2; - } + if ( ( defined $mark_locked ) && ( $mark_locked eq 'mark_locked' ) ) { + for my $result (@$results) { + if ( $result->{lock_status} ne 'show' ) { + $result->{author} = 'Zensur'; + $result->{content} = 'Dieser Eintrag wurde gelöscht.'; + } + } + } elsif ( ( defined $mark_locked ) && ( $mark_locked eq 'filter_locked' ) ) { + my @results2 = (); + for my $result (@$results) { + push @results2, $result if ( $result->{lock_status} eq 'show' ); + } + $results = \@results2; + } - comments::modify_results( $results, $config, $request ); + comments::modify_results( $results, $config, $request ); - #print STDERR Dumper($results); - $results = comments::sort( $config, $results ) if ( $comment->{type} eq 'tree' ); + #print STDERR Dumper($results); + $results = comments::sort( $config, $results ) if ( $comment->{type} eq 'tree' ); - #print STDERR Dumper($results); - # if ($comment->{sort_order}eq'desc'){ - # my @results= reverse(@$results); - # $results=\@results; - # } + #print STDERR Dumper($results); + # if ($comment->{sort_order}eq'desc'){ + # my @results= reverse(@$results); + # $results=\@results; + # } - if ( ( $params->{show_max} ne '' ) - && ( $params->{limit} ne '' ) - && ( $params->{show_max} < $params->{limit} ) ) - { - my @results2 = (); - my $c = 0; - for my $result (@$results) { - push @results2, $result; - $c++; - last if ( $c >= $params->{show_max} ); - } - $results = \@results2; - } + if ( ( $params->{show_max} ne '' ) + && ( $params->{limit} ne '' ) + && ( $params->{show_max} < $params->{limit} ) ) + { + my @results2 = (); + my $c = 0; + for my $result (@$results) { + push @results2, $result; + $c++; + last if ( $c >= $params->{show_max} ); + } + $results = \@results2; + } - comments::render( $_[0], $config, $request, $results ); + comments::render( $_[0], $config, $request, $results ); } sub get { - my $config = shift; - my $request = shift; + my $config = shift; + my $request = shift; - my $params = $request->{params}->{checked}; + my $params = $request->{params}->{checked}; - my $dbh = db::connect( $config, $request ); + my $dbh = db::connect( $config, $request ); - ( my $query, my $bind_values ) = comments::get_query( $dbh, $config, $request ); + ( my $query, my $bind_values ) = comments::get_query( $dbh, $config, $request ); - #print STDERR Dumper($$query); - #print STDERR Dumper($bind_values); - my $results = db::get( $dbh, $$query, $bind_values ); + #print STDERR Dumper($$query); + #print STDERR Dumper($bind_values); + my $results = db::get( $dbh, $$query, $bind_values ); - #print STDERR Dumper($results); - return $results; + #print STDERR Dumper($results); + return $results; } sub get_query { - my $dbh = shift; - my $config = shift; - my $request = shift; + my $dbh = shift; + my $config = shift; + my $request = shift; - my $params = $request->{params}->{checked}; + my $params = $request->{params}->{checked}; - my $event_id = undef; - my $event_start = undef; - my $from = 'calcms_comments c'; - my $where = ''; - my $limit = ''; - my @conditions = (); - my $bind_values = []; + my $event_id = undef; + my $event_start = undef; + my $from = 'calcms_comments c'; + my $where = ''; + my $limit = ''; + my @conditions = (); + my $bind_values = []; - #exclude comments from config filter/locations_to_exclude - if ( ( defined $config->{filter} ) - && ( defined $config->{filter}->{locations_to_exclude} ) ) - { - my @locations_to_exclude = split( /[,\s]+/, $config->{filter}->{locations_to_exclude} ); - my $locations_to_exclude = join( ', ', map { '?' } @locations_to_exclude ); + #exclude comments from config filter/locations_to_exclude + if ( ( defined $config->{filter} ) + && ( defined $config->{filter}->{locations_to_exclude} ) ) + { + my @locations_to_exclude = split( /[,\s]+/, $config->{filter}->{locations_to_exclude} ); + my $locations_to_exclude = join( ', ', map { '?' } @locations_to_exclude ); - $from .= ',calcms_events e'; - push @conditions, 'e.id=c.event_id'; - push @conditions, 'e.location not in (' . $locations_to_exclude . ')'; - for my $location (@locations_to_exclude) { - push @$bind_values, $location; - } - } + $from .= ',calcms_events e'; + push @conditions, 'e.id=c.event_id'; + push @conditions, 'e.location not in (' . $locations_to_exclude . ')'; + for my $location (@locations_to_exclude) { + push @$bind_values, $location; + } + } - if ( ( defined $params->{event_id} && $params->{event_id} ne '' ) - && ( defined $params->{event_start} && $params->{event_start} ne '' ) ) - { - #$where =qq{ and (event_id=? or event_start=?) }; - push @conditions, q{ (event_id=? or event_start=?) }; - push @$bind_values, $params->{event_id}; - push @$bind_values, $params->{event_start}; - } + if ( ( defined $params->{event_id} && $params->{event_id} ne '' ) + && ( defined $params->{event_start} && $params->{event_start} ne '' ) ) + { + #$where =qq{ and (event_id=? or event_start=?) }; + push @conditions, q{ (event_id=? or event_start=?) }; + push @$bind_values, $params->{event_id}; + push @$bind_values, $params->{event_start}; + } - my $sort_order = $params->{sort_order}; + my $sort_order = $params->{sort_order}; - if ( $params->{limit} ne '' ) { - $limit = 'limit ?'; - push @$bind_values, $params->{limit}; - } + if ( $params->{limit} ne '' ) { + $limit = 'limit ?'; + push @$bind_values, $params->{limit}; + } - if ( @conditions > 0 ) { - $where = 'where ' . join( ' and ', @conditions ); - } + if ( @conditions > 0 ) { + $where = 'where ' . join( ' and ', @conditions ); + } - my $dbcols = [ - 'id', 'event_start', 'event_id', 'content', 'ip', 'author', 'email', 'lock_status', - 'created_at', 'title', 'parent_id', 'level', 'news_status', 'project' - ]; - my $cols = join( ', ', map { 'c.' . $_ } @$dbcols ); - my $query = qq{ + my $dbcols = [ + 'id', 'event_start', 'event_id', 'content', 'ip', 'author', 'email', 'lock_status', + 'created_at', 'title', 'parent_id', 'level', 'news_status', 'project' + ]; + my $cols = join( ', ', map { 'c.' . $_ } @$dbcols ); + my $query = qq{ select $cols from $from $where @@ -165,101 +165,101 @@ sub get_query { $limit }; - # where lock_status='show' - # use Data::Dumper;print STDERR Dumper($query); + # where lock_status='show' + # use Data::Dumper;print STDERR Dumper($query); - return ( \$query, $bind_values ); + return ( \$query, $bind_values ); } sub modify_results { - my $results = $_[0]; - my $config = $_[1]; - my $request = $_[2]; + my $results = $_[0]; + my $config = $_[1]; + my $request = $_[2]; - my $params = $request->{params}->{checked}; + my $params = $request->{params}->{checked}; - my $time_diff = ''; - if ( $params->{template} =~ /\.xml/ ) { - $time_diff = time::utc_offset( $config->{date}->{time_zone} ); - $time_diff =~ s/(\d\d)(\d\d)/$1\:$2/g; - } + my $time_diff = ''; + if ( $params->{template} =~ /\.xml/ ) { + $time_diff = time::utc_offset( $config->{date}->{time_zone} ); + $time_diff =~ s/(\d\d)(\d\d)/$1\:$2/g; + } - my $language = $config->{date}->{language} || 'en'; + my $language = $config->{date}->{language} || 'en'; - for my $result (@$results) { - $result->{allow}->{new_comments} = 1 if ( $params->{allow}->{new_comments} ); - $result->{start_date_name} = time::date_format( $result->{created_at}, $language ); - $result->{start_time_name} = time::time_format( $result->{created_at} ); - my $comment_limit = 100; - if ( length( $result->{content} ) > $comment_limit ) { - $result->{short_content} = substr( $result->{content}, 0, $comment_limit ) . '...'; - } else { - $result->{short_content} = $result->{content}; - } - $result->{base_url} = $config->{locations}->{base_url}; - $result->{cache_base_url} = $config->{cache}->{base_url}; + for my $result (@$results) { + $result->{allow}->{new_comments} = 1 if ( $params->{allow}->{new_comments} ); + $result->{start_date_name} = time::date_format( $result->{created_at}, $language ); + $result->{start_time_name} = time::time_format( $result->{created_at} ); + my $comment_limit = 100; + if ( length( $result->{content} ) > $comment_limit ) { + $result->{short_content} = substr( $result->{content}, 0, $comment_limit ) . '...'; + } else { + $result->{short_content} = $result->{content}; + } + $result->{base_url} = $config->{locations}->{base_url}; + $result->{cache_base_url} = $config->{cache}->{base_url}; - if ( $params->{template} =~ /\.xml/ ) { + if ( $params->{template} =~ /\.xml/ ) { - # $result->{content} =~s/(\[\[.*?\]\])//gi; - # $result->{content} =markup::plain_to_xml($result->{content}); - # $result->{content} =$result->{html_content}; + # $result->{content} =~s/(\[\[.*?\]\])//gi; + # $result->{content} =markup::plain_to_xml($result->{content}); + # $result->{content} =$result->{html_content}; - $result->{content} = markup::html_to_plain( $result->{html_content} ); - $result->{short_content} = markup::html_to_plain( $result->{short_content} ); - $result->{excerpt} = "lass dich ueberraschen" if ( ( defined $result->{excerpt} ) && ( $result->{excerpt} eq '' ) ); - $result->{excerpt} = markup::html_to_plain( $result->{excerpt} ); - $result->{title} = markup::html_to_plain( $result->{title} ); - $result->{series_name} = markup::html_to_plain( $result->{series_name} ); - $result->{program} = markup::html_to_plain( $result->{program} ); + $result->{content} = markup::html_to_plain( $result->{html_content} ); + $result->{short_content} = markup::html_to_plain( $result->{short_content} ); + $result->{excerpt} = "lass dich ueberraschen" if ( defined $result->{excerpt} ) && ( $result->{excerpt} eq '' ); + $result->{excerpt} = markup::html_to_plain( $result->{excerpt} ); + $result->{title} = markup::html_to_plain( $result->{title} ); + $result->{series_name} = markup::html_to_plain( $result->{series_name} ); + $result->{program} = markup::html_to_plain( $result->{program} ); - if ( defined $result->{created_at} ) { - $result->{created_at} =~ s/ /T/gi; - $result->{created_at} .= $time_diff; - } + if ( defined $result->{created_at} ) { + $result->{created_at} =~ s/ /T/gi; + $result->{created_at} .= $time_diff; + } - if ( defined $result->{modified_at} ) { - $result->{modified_at} =~ s/ /T/gi; - $result->{modified_at} .= $time_diff; - } - } - } - return $results; + if ( defined $result->{modified_at} ) { + $result->{modified_at} =~ s/ /T/gi; + $result->{modified_at} .= $time_diff; + } + } + } + return $results; } sub render { - # my $response =$_[0]; - my $config = $_[1]; - my $request = $_[2]; - my $results = $_[3]; + # my $response =$_[0]; + my $config = $_[1]; + my $request = $_[2]; + my $results = $_[3]; - my $params = $request->{params}->{checked}; + my $params = $request->{params}->{checked}; - my %template_parameters = %$params; - my $template_parameters = \%template_parameters; + my %template_parameters = %$params; + my $template_parameters = \%template_parameters; - $template_parameters->{comments} = $results; - $template_parameters->{comment_count} = (@$results) + 0; - $template_parameters->{one_result} = 1 if ( $template_parameters->{comment_count} == 1 ); - $template_parameters->{allow}->{new_comments} = 1 if ( $params->{allow}->{new_comments} ); + $template_parameters->{comments} = $results; + $template_parameters->{comment_count} = scalar(@$results) + 0; + $template_parameters->{one_result} = 1 if ( $template_parameters->{comment_count} == 1 ); + $template_parameters->{allow}->{new_comments} = 1 if ( $params->{allow}->{new_comments} ); - $template_parameters->{event_id} = $params->{event_id}; - $template_parameters->{event_start} = $params->{event_start}; + $template_parameters->{event_id} = $params->{event_id}; + $template_parameters->{event_start} = $params->{event_start}; - $template_parameters->{server_cache} = $config->{cache}->{server_cache} if ( $config->{cache}->{server_cache} ); - $template_parameters->{use_client_cache} = $config->{cache}->{use_client_cache} if ( $config->{cache}->{use_client_cache} ); - $template_parameters->{controllers} = $config->{controllers}; - template::process( $_[0], $params->{template}, $template_parameters ); + $template_parameters->{server_cache} = $config->{cache}->{server_cache} if ( $config->{cache}->{server_cache} ); + $template_parameters->{use_client_cache} = $config->{cache}->{use_client_cache} if ( $config->{cache}->{use_client_cache} ); + $template_parameters->{controllers} = $config->{controllers}; + template::process( $_[0], $params->{template}, $template_parameters ); } #check if comment exists already sub check { - my $dbh = shift; - my $config = shift; - my $comment = shift; + my $dbh = shift; + my $config = shift; + my $comment = shift; - my $query = qq{ + my $query = qq{ select id from calcms_comments where ( @@ -271,32 +271,32 @@ sub check { and ip=? and content=? }; - my $bind_values = - [ $comment->{event_start}, $comment->{event_id}, $comment->{parent_id}, $comment->{author}, $comment->{ip}, $comment->{content} ]; + my $bind_values = + [ $comment->{event_start}, $comment->{event_id}, $comment->{parent_id}, $comment->{author}, $comment->{ip}, $comment->{content} ]; - my $comments = db::get( $dbh, $query, $bind_values ); + my $comments = db::get( $dbh, $query, $bind_values ); - my @comments = @$comments; - return 0 if ( @comments > 0 ); - return 1; + my @comments = @$comments; + return 0 if scalar @comments > 0; + return 1; } #used for insert sub get_level { - my $dbh = shift; - my $config = shift; - my $comment = shift; + my $dbh = shift; + my $config = shift; + my $comment = shift; - my $parent_id = $comment->{parent_id}; - return 0 unless defined $parent_id; - if ( $parent_id =~ /(\d+)/ ) { - $parent_id = $1; - } - return 0 unless $parent_id =~ /^\d+$/; - return 0 if $parent_id == 0; + my $parent_id = $comment->{parent_id}; + return 0 unless defined $parent_id; + if ( $parent_id =~ /(\d+)/ ) { + $parent_id = $1; + } + return 0 unless $parent_id =~ /^\d+$/; + return 0 if $parent_id == 0; - #get level from parent node - my $query = qq{ + #get level from parent node + my $query = qq{ select level from calcms_comments where ( @@ -306,49 +306,49 @@ sub get_level { and id=? limit 1 }; - my $bind_values = [ $comment->{event_start}, $comment->{event_id}, $parent_id ]; + my $bind_values = [ $comment->{event_start}, $comment->{event_id}, $parent_id ]; - my $comments = db::get( $dbh, $query, $bind_values ); + my $comments = db::get( $dbh, $query, $bind_values ); - my @comments = @$comments; - if ( @comments > 0 ) { - return $comments->[0]->{level} + 1; - } - return 0; + my @comments = @$comments; + if ( scalar @comments > 0 ) { + return $comments->[0]->{level} + 1; + } + return 0; } sub get_by_event { - my $dbh = shift; - my $config = shift; - my $request = $_[0]; + my $dbh = shift; + my $config = shift; + my $request = $_[0]; - my $params = $request->{params}->{checked}->{comment}; + my $params = $request->{params}->{checked}->{comment}; - my $event_id = undef; - my $search = undef; - my $where = ''; - my $limit = ''; - my $bind_values = []; + my $event_id = undef; + my $search = undef; + my $where = ''; + my $limit = ''; + my $bind_values = []; - if ( $params->{event_id} ne '' ) { - $where = qq{ event_id=? }; - $bind_values = [ $params->{event_id} ]; - } + if ( $params->{event_id} ne '' ) { + $where = qq{ event_id=? }; + $bind_values = [ $params->{event_id} ]; + } - if ( ( defined $params->{search} ) && ( $params->{search} ne '' ) ) { - $search = '%' . $params->{search} . '%'; - $where = qq{ (content like ?) or (email like ?) or (author like ?) or (ip like ?)}; - $bind_values = [ $search, $search, $search, $search ]; - } + if ( ( defined $params->{search} ) && ( $params->{search} ne '' ) ) { + $search = '%' . $params->{search} . '%'; + $where = qq{ (content like ?) or (email like ?) or (author like ?) or (ip like ?)}; + $bind_values = [ $search, $search, $search, $search ]; + } - my $sort_order = $params->{sort_order} || 'desc'; + my $sort_order = $params->{sort_order} || 'desc'; - if ( ( defined $params->{limit} ) && ( $params->{limit} ne '' ) ) { - $limit = 'limit ?'; - push @$bind_values, $params->{limit}; - } + if ( ( defined $params->{limit} ) && ( $params->{limit} ne '' ) ) { + $limit = 'limit ?'; + push @$bind_values, $params->{limit}; + } - my $query = qq{ + my $query = qq{ select * from calcms_comments where $where @@ -356,21 +356,21 @@ sub get_by_event { $limit }; - #print STDERR $query."\n"; - my $comments = db::get( $dbh, $query, $bind_values ); + #print STDERR $query."\n"; + my $comments = db::get( $dbh, $query, $bind_values ); - return $comments; + return $comments; } sub get_by_time { - my $dbh = shift; - my $config = shift; - my $comment = shift; + my $dbh = shift; + my $config = shift; + my $comment = shift; - my $where = ''; - my $bind_values = []; - if ( $comment->{age} ne '' ) { - $where = qq{ + my $where = ''; + my $bind_values = []; + if ( $comment->{age} ne '' ) { + $where = qq{ where event_id in ( select distinct event_id from calcms_comments @@ -379,9 +379,9 @@ sub get_by_time { ) ) }; - $bind_values = [ $comment->{age} * 3600, ]; - } elsif ( ( $comment->{from} ne '' ) && ( $comment->{till} ne '' ) ) { - $where = qq{ + $bind_values = [ $comment->{age} * 3600, ]; + } elsif ( ( $comment->{from} ne '' ) && ( $comment->{till} ne '' ) ) { + $where = qq{ where event_id in ( select distinct event_id from calcms_comments @@ -389,309 +389,309 @@ sub get_by_time { and created_at <= ? ) }; - $bind_values = [ $comment->{from}, $comment->{till} ]; - } - my $query = qq{ + $bind_values = [ $comment->{from}, $comment->{till} ]; + } + my $query = qq{ select * from calcms_comments $where order by event_id, id }; - my $comments = db::get( $dbh, $query, $bind_values ); - return $comments; + my $comments = db::get( $dbh, $query, $bind_values ); + return $comments; } sub get_events { - my $dbh = shift; - my $config = shift; - my $request = shift; - my $comments = shift; + my $dbh = shift; + my $config = shift; + my $request = shift; + my $comments = shift; - my $params = $request->{params}->{checked}->{comment}; + my $params = $request->{params}->{checked}->{comment}; - #get event_ids from comments - my $event_ids = {}; - for my $comment (@$comments) { - my $event_id = $comment->{event_id}; - $event_ids->{$event_id} = 1; - } + #get event_ids from comments + my $event_ids = {}; + for my $comment (@$comments) { + my $event_id = $comment->{event_id}; + $event_ids->{$event_id} = 1; + } - #get events from comment's event ids - return [] if ( ( keys %{$event_ids} ) == 0 ); + #get events from comment's event ids + return [] if ( scalar keys %{$event_ids} ) == 0; - #my $quoted_event_ids=join "," ,(map {$dbh->quote($_)}(keys %{$event_ids})); - my @bind_values = keys %{$event_ids}; - my $event_id_values = join ",", ( map { '?' } ( keys %{$event_ids} ) ); + #my $quoted_event_ids=join "," ,(map {$dbh->quote($_)}(keys %{$event_ids})); + my @bind_values = keys %{$event_ids}; + my $event_id_values = join ",", ( map { '?' } ( keys %{$event_ids} ) ); - my $query = qq{ + my $query = qq{ select id, start, program, series_name, title, excerpt from calcms_events where id in ($event_id_values) }; - my $events = db::get( $dbh, $query, \@bind_values ); + my $events = db::get( $dbh, $query, \@bind_values ); - #build lookup table for events by id - my $events_by_id = {}; - for my $event (@$events) { - $events_by_id->{ $event->{id} } = $event; - $event->{max_comment_id} = 0; - } + #build lookup table for events by id + my $events_by_id = {}; + for my $event (@$events) { + $events_by_id->{ $event->{id} } = $event; + $event->{max_comment_id} = 0; + } - #add unassigned events - # for my $event_id (keys %{$event_ids}){ - # if ($events_by_id->{$event_id}eq''){ - # my $event={ - # title => "not assigned", - # max_comment_id => 0 - # - # }; - # push @$events,$event; - # $events_by_id->{$event_id}=$event; - # } - # } + #add unassigned events + # for my $event_id (keys %{$event_ids}){ + # if ($events_by_id->{$event_id}eq''){ + # my $event={ + # title => "not assigned", + # max_comment_id => 0 + # + # }; + # push @$events,$event; + # $events_by_id->{$event_id}=$event; + # } + # } - for my $comment (@$comments) { - my $event_id = $comment->{event_id}; - my $event = $events_by_id->{$event_id}; - next unless ( defined $event ); - $event->{comment_count}++; - push @{ $event->{comments} }, $comment; # if ($params->{event_id}ne''); - $event->{max_comment_id} = $comment->{id} if ( $comment->{id} > $event->{max_comment_id} ); - for my $name ( keys %{ $config->{controllers} } ) { - $comment->{ "controller_" . $name } = $config->{controllers}->{$name} || ''; + for my $comment (@$comments) { + my $event_id = $comment->{event_id}; + my $event = $events_by_id->{$event_id}; + next unless ( defined $event ); + $event->{comment_count}++; + push @{ $event->{comments} }, $comment; # if ($params->{event_id}ne''); + $event->{max_comment_id} = $comment->{id} if ( $comment->{id} > $event->{max_comment_id} ); + for my $name ( keys %{ $config->{controllers} } ) { + $comment->{ "controller_" . $name } = $config->{controllers}->{$name} || ''; - # $event->{"controller_$name"}=$config->{controllers}->{$name}; - } - } - my @sorted_events = reverse sort { $a->{max_comment_id} <=> $b->{max_comment_id} } @$events; - return \@sorted_events; + # $event->{"controller_$name"}=$config->{controllers}->{$name}; + } + } + my @sorted_events = reverse sort { $a->{max_comment_id} <=> $b->{max_comment_id} } @$events; + return \@sorted_events; } sub insert { - my $dbh = shift; - my $config = shift; - my $comment = shift; + my $dbh = shift; + my $config = shift; + my $comment = shift; - $comment->{level} = comments::get_level( $dbh, $config, $comment ); + $comment->{level} = comments::get_level( $dbh, $config, $comment ); - my $entry = { - event_start => $comment->{event_start}, - event_id => $comment->{event_id}, - parent_id => $comment->{parent_id}, - level => $comment->{level}, - title => $comment->{title}, - content => $comment->{content}, - author => $comment->{author}, - email => $comment->{email}, - ip => $comment->{ip} - }; + my $entry = { + event_start => $comment->{event_start}, + event_id => $comment->{event_id}, + parent_id => $comment->{parent_id}, + level => $comment->{level}, + title => $comment->{title}, + content => $comment->{content}, + author => $comment->{author}, + email => $comment->{email}, + ip => $comment->{ip} + }; - my $comment_id = db::insert( $dbh, 'calcms_comments', $entry ); - return $comment_id; + my $comment_id = db::insert( $dbh, 'calcms_comments', $entry ); + return $comment_id; } sub set_lock_status { - my $dbh = shift; - my $config = shift; - my $comment = shift; + my $dbh = shift; + my $config = shift; + my $comment = shift; - my $id = $comment->{id}; - my $lock_status = $comment->{set_lock_status}; - my $query = qq{ + my $id = $comment->{id}; + my $lock_status = $comment->{set_lock_status}; + my $query = qq{ update calcms_comments set lock_status = ? where id = ? }; - my $bind_values = [ $lock_status, $id ]; - db::put( $dbh, $query, $bind_values ); + my $bind_values = [ $lock_status, $id ]; + db::put( $dbh, $query, $bind_values ); - $query = qq{ + $query = qq{ select event_id from calcms_comments where id=? }; - $bind_values = [$id]; - my $comments = db::get( $dbh, $query, $bind_values ); - if ( @$comments > 0 ) { - $comment->{event_id} = $comments->[0]->{event_id}; - update_comment_count( $dbh, $comment ); - } + $bind_values = [$id]; + my $comments = db::get( $dbh, $query, $bind_values ); + if ( scalar @$comments > 0 ) { + $comment->{event_id} = $comments->[0]->{event_id}; + update_comment_count( $dbh, $comment ); + } } sub set_news_status { - my $dbh = shift; - my $config = shift; - my $comment = shift; + my $dbh = shift; + my $config = shift; + my $comment = shift; - my $id = $comment->{id}; - my $news_status = $comment->{set_news_status}; - my $query = qq{ + my $id = $comment->{id}; + my $news_status = $comment->{set_news_status}; + my $query = qq{ update calcms_comments set news_status= ? where id = ? }; - my $bind_values = [ $news_status, $id ]; - db::put( $dbh, $query, $bind_values ); + my $bind_values = [ $news_status, $id ]; + db::put( $dbh, $query, $bind_values ); } sub update_comment_count { - my $dbh = shift; - my $config = shift; - my $comment = shift; + my $dbh = shift; + my $config = shift; + my $comment = shift; - my $query = qq{ + my $query = qq{ select count(id) count from calcms_comments where lock_status='show' and event_id=? }; - my $bind_values = [ $comment->{event_id} ]; - my $comments = db::get( $dbh, $query, $bind_values ); + my $bind_values = [ $comment->{event_id} ]; + my $comments = db::get( $dbh, $query, $bind_values ); - my $count = 0; - $count = $comments->[0]->{count} if ( @$comments > 0 ); - $query = qq{ + my $count = 0; + $count = $comments->[0]->{count} if scalar @$comments > 0; + $query = qq{ update calcms_events set comment_count=? where id=? }; - $bind_values = [ $count, $comment->{event_id} ]; - db::put( $dbh, $query, $bind_values ); + $bind_values = [ $count, $comment->{event_id} ]; + db::put( $dbh, $query, $bind_values ); } #precondition: results are presorted by creation date (by sql) sub sort { - my $config = shift; - my $results = shift; + my $config = shift; + my $results = shift; - #define parent nodes - my $nodes = {}; - for my $node (@$results) { - $nodes->{ $node->{id} } = $node; - } - my @root_nodes = (); - for my $node (@$results) { + #define parent nodes + my $nodes = {}; + for my $node (@$results) { + $nodes->{ $node->{id} } = $node; + } + my @root_nodes = (); + for my $node (@$results) { - #fill childs into parent nodes - push @{ $nodes->{ $node->{parent_id} }->{childs} }, $node; + #fill childs into parent nodes + push @{ $nodes->{ $node->{parent_id} }->{childs} }, $node; - #define root nodes - push @root_nodes, $node if ( $node->{level} == 0 ); - } + #define root nodes + push @root_nodes, $node if ( $node->{level} == 0 ); + } - #print STDERR Dumper(\@root_nodes); + #print STDERR Dumper(\@root_nodes); - #sort root nodes from newest to oldest - my $sorted_nodes = []; - for my $node (@root_nodes) { + #sort root nodes from newest to oldest + my $sorted_nodes = []; + for my $node (@root_nodes) { - #for my $node (reverse @root_nodes){ - sort_childs( $node, $nodes, $sorted_nodes ); - } - return $sorted_nodes; + #for my $node (reverse @root_nodes){ + sort_childs( $node, $nodes, $sorted_nodes ); + } + return $sorted_nodes; } sub sort_childs { - my $node = shift; - my $nodes = shift; - my $sorted_nodes = shift; + my $node = shift; + my $nodes = shift; + my $sorted_nodes = shift; - #push node into list of sorted nodes - push @{$sorted_nodes}, $node; + #push node into list of sorted nodes + push @{$sorted_nodes}, $node; - #return if node is leaf - return $sorted_nodes unless ( defined $node->{childs} ); + #return if node is leaf + return $sorted_nodes unless ( defined $node->{childs} ); - #process child nodes - for my $child ( @{ $node->{childs} } ) { - $sorted_nodes = sort_childs( $child, $nodes, $sorted_nodes ); - } - return $sorted_nodes; + #process child nodes + for my $child ( @{ $node->{childs} } ) { + $sorted_nodes = sort_childs( $child, $nodes, $sorted_nodes ); + } + return $sorted_nodes; } sub configure_cache { - my $config = shift; + my $config = shift; - cache::init(); + cache::init(); - my $date_pattern = $cache::date_pattern; - my $datetime_pattern = $cache::datetime_pattern; - my $controllers = $config->{controllers}; + my $date_pattern = $cache::date_pattern; + my $datetime_pattern = $cache::datetime_pattern; + my $controllers = $config->{controllers}; - cache::add_map( 'template=comments_newest&limit=3&type=list', $controllers->{comments} . '/neueste.html' ); - cache::add_map( 'template=comments_atom.xml&limit=20', $controllers->{comments} . '/feed.xml' ); - cache::add_map( 'template=comments.html&event_id=(\d+)&event_start=' . $datetime_pattern, - $controllers->{comments} . '/$1_$2-$3-$4_$5-$6.html' ); + cache::add_map( 'template=comments_newest&limit=3&type=list', $controllers->{comments} . '/neueste.html' ); + cache::add_map( 'template=comments_atom.xml&limit=20', $controllers->{comments} . '/feed.xml' ); + cache::add_map( 'template=comments.html&event_id=(\d+)&event_start=' . $datetime_pattern, + $controllers->{comments} . '/$1_$2-$3-$4_$5-$6.html' ); } sub check_params { - my $config = shift; - my $params = shift; + my $config = shift; + my $params = shift; - my $comment = {}; + my $comment = {}; - $comment->{event_start} = ''; - if ( ( defined $params->{event_start} ) && ( $params->{event_start} =~ /(\d\d\d\d\-\d\d\-\d\d[T ]\d\d\:\d\d)(\:\d\d)?/ ) ) { - $comment->{event_start} = $1; - } + $comment->{event_start} = ''; + if ( ( defined $params->{event_start} ) && ( $params->{event_start} =~ /(\d\d\d\d\-\d\d\-\d\d[T ]\d\d\:\d\d)(\:\d\d)?/ ) ) { + $comment->{event_start} = $1; + } - $comment->{sort_order} = 'desc'; - $comment->{limit} = ''; - if ( ( defined $params->{limit} ) && ( $params->{limit} =~ /(\d+)/ ) ) { - $comment->{limit} = $1; - } + $comment->{sort_order} = 'desc'; + $comment->{limit} = ''; + if ( ( defined $params->{limit} ) && ( $params->{limit} =~ /(\d+)/ ) ) { + $comment->{limit} = $1; + } - $comment->{show_max} = ''; - if ( ( defined $params->{show_max} ) && ( $params->{show_max} =~ /(\d+)/ ) ) { - $comment->{show_max} = $1; - } + $comment->{show_max} = ''; + if ( ( defined $params->{show_max} ) && ( $params->{show_max} =~ /(\d+)/ ) ) { + $comment->{show_max} = $1; + } - if ( ( defined $params->{sort_order} ) && ( $params->{sort_order} eq 'asc' ) ) { - $comment->{sort_order} = 'asc'; - } + if ( ( defined $params->{sort_order} ) && ( $params->{sort_order} eq 'asc' ) ) { + $comment->{sort_order} = 'asc'; + } - $comment->{event_id} = ''; - if ( ( defined $params->{event_id} ) && ( $params->{event_id} =~ /(\d+)/ ) ) { - $comment->{event_id} = $1; - } + $comment->{event_id} = ''; + if ( ( defined $params->{event_id} ) && ( $params->{event_id} =~ /(\d+)/ ) ) { + $comment->{event_id} = $1; + } - if ( ( defined $params->{parent_id} ) && ( $params->{parent_id} =~ /(\d+)/ ) ) { - $comment->{parent_id} = $1; - } + if ( ( defined $params->{parent_id} ) && ( $params->{parent_id} =~ /(\d+)/ ) ) { + $comment->{parent_id} = $1; + } - if ( ( defined $params->{type} ) && ( $params->{type} eq 'list' ) ) { - $comment->{type} = 'list'; - } else { - $comment->{type} = 'tree'; - } + if ( ( defined $params->{type} ) && ( $params->{type} eq 'list' ) ) { + $comment->{type} = 'list'; + } else { + $comment->{type} = 'tree'; + } - my $debug = $params->{debug} || ''; - if ( $debug =~ /([a-z\_\,]+)/ ) { - $comment->{debug} = $1; - } + my $debug = $params->{debug} || ''; + if ( $debug =~ /([a-z\_\,]+)/ ) { + $comment->{debug} = $1; + } - log::error( $config, 'missing parameter a' ) if ( ( defined $params->{limit} ) && ( $comment->{limit} eq '' ) ); - log::error( $config, 'missing parameter b' ) if ( ( defined $params->{event_id} ) && ( $comment->{event_id} eq '' ) ); - log::error( $config, 'missing parameter c' ) if ( ( defined $params->{event_start} ) && ( $comment->{event_start} eq '' ) ); + log::error( $config, 'missing parameter a' ) if ( defined $params->{limit} ) && ( $comment->{limit} eq '' ); + log::error( $config, 'missing parameter b' ) if ( defined $params->{event_id} ) && ( $comment->{event_id} eq '' ); + log::error( $config, 'missing parameter c' ) if ( defined $params->{event_start} ) && ( $comment->{event_start} eq '' ); - my $delta_days = 1; - if ( $comment->{event_start} ne '' ) { - my $today = time::datetime_to_array( time::time_to_datetime() ); - my $date = time::datetime_to_array( $comment->{event_start} ); - $delta_days = time::days_between( $today, $date ); - } - if ( ( $delta_days > $config->{permissions}->{no_new_comments_before} ) - || ( $delta_days < -1 * $config->{permissions}->{no_new_comments_after} ) ) - { - $comment->{allow}->{new_comments} = 0; - } else { - $comment->{allow}->{new_comments} = 1; - } + my $delta_days = 1; + if ( $comment->{event_start} ne '' ) { + my $today = time::datetime_to_array( time::time_to_datetime() ); + my $date = time::datetime_to_array( $comment->{event_start} ); + $delta_days = time::days_between( $today, $date ); + } + if ( ( $delta_days > $config->{permissions}->{no_new_comments_before} ) + || ( $delta_days < -1 * $config->{permissions}->{no_new_comments_after} ) ) + { + $comment->{allow}->{new_comments} = 0; + } else { + $comment->{allow}->{new_comments} = 1; + } - $comment->{template} = template::check( $params->{template}, 'comments.html' ); + $comment->{template} = template::check( $params->{template}, 'comments.html' ); - return $comment; + return $comment; } #do not delete last line! diff --git a/lib/calcms/config.pm b/lib/calcms/config.pm index 6dc81c9..6f3a865 100644 --- a/lib/calcms/config.pm +++ b/lib/calcms/config.pm @@ -12,12 +12,7 @@ our $config = undef; sub get { my $filename = shift; - - #return config if known - #my $age=(-M $filename); - #return $config::config if ((defined $config::config) && ($age <= $config::modified_at)); - - #reload config if changed + my $configuration = new Config::General( -ConfigFile => $filename, -UTF8 => 1 @@ -25,8 +20,6 @@ sub get { $config::config = $configuration->{DefaultConfig}->{config}; $config::modified_at = $age; - #print STDERR "reload $filename\n"; - return $config::config; } diff --git a/lib/calcms/creole_wiki.pm b/lib/calcms/creole_wiki.pm index 708be3f..a235fd6 100644 --- a/lib/calcms/creole_wiki.pm +++ b/lib/calcms/creole_wiki.pm @@ -12,258 +12,259 @@ our %EXPORT_TAGS = ( 'all' => [@EXPORT_OK] ); #convert creole wiki text to event sub extractEventFromWikiText { - my $params = shift; - my $event = shift; - $event = {} unless ( defined $event ); + my $params = shift; + my $event = shift; + $event = {} unless ( defined $event ); - my $title = $params->{title} || ''; - my $content = $params->{content} || ''; - my $local_media_url = $params->{local_media_url} || ''; + my $title = $params->{title} || ''; + my $content = $params->{content} || ''; + my $local_media_url = $params->{local_media_url} || ''; - #split content into excerpt, content and comments - $content =~ s/\s*\,\s*/, /g; - my @lines = split( /\s*\-{10,99}\s*/, $content ); - my $lines = \@lines; - for my $line (@$lines) { - $line =~ s/^\s+|\s+$//g; - } - if ( @lines == 1 ) { - $event->{content} = shift @lines; - } elsif ( @lines == 2 ) { - $event->{excerpt} = shift @lines; - $event->{content} = shift @lines; - } else { - $event->{excerpt} = shift @lines; - $event->{content} = shift @lines; - $event->{comments} = join( "--------------------\n", @lines ); - } - if ( defined $event->{excerpt} ) { - $event->{excerpt} = markup::html_to_plain( $event->{excerpt} ); - } + #split content into excerpt, content and comments + $content =~ s/\s*\,\s*/, /g; + my @lines = split( /\s*\-{10,99}\s*/, $content ); + my $lines = \@lines; + for my $line (@$lines) { + $line =~ s/^\s+|\s+$//g; + } + if ( @lines == 1 ) { + $event->{content} = shift @lines; + } elsif ( @lines == 2 ) { + $event->{excerpt} = shift @lines; + $event->{content} = shift @lines; + } else { + $event->{excerpt} = shift @lines; + $event->{content} = shift @lines; + $event->{comments} = join( "--------------------\n", @lines ); + } + if ( defined $event->{excerpt} ) { + $event->{excerpt} = markup::html_to_plain( $event->{excerpt} ); + } - #extract program from title - $event->{program} = ''; + #extract program from title + $event->{program} = ''; - if ( $title =~ /^(.*?)\:/ ) { - my $program = $1; - unless ( $program =~ /\s\-\s/ ) { - $event->{program} = $program; - $event->{program} =~ s/^\s+|\s+$//g; - $event->{program} =~ s/\s+/ /g; - $title =~ s/^.*?\:\s+//gi; - } - } + if ( $title =~ /^(.*?)\:/ ) { + my $program = $1; + unless ( $program =~ /\s\-\s/ ) { + $event->{program} = $program; + $event->{program} =~ s/^\s+|\s+$//g; + $event->{program} =~ s/\s+/ /g; + $title =~ s/^.*?\:\s+//gi; + } + } - #extract series_name from title - $event->{series_name} = ''; - if ( $title =~ /^(.*?)\s+\-\s+/ ) { - $event->{series_name} = $1; - $event->{series_name} =~ s/^\s+|\s+$//g; - $event->{series_name} =~ s/\s+/ /g; - $title =~ s/^(.*?)\s+\-\s+//gi; - } + #extract series_name from title + $event->{series_name} = ''; + if ( $title =~ /^(.*?)\s+\-\s+/ ) { + $event->{series_name} = $1; + $event->{series_name} =~ s/^\s+|\s+$//g; + $event->{series_name} =~ s/\s+/ /g; + $title =~ s/^(.*?)\s+\-\s+//gi; + } - #extract categories from title - my @categories = (); - while ( $title =~ /\((.*?),(.*?)\)/ ) { - my $category = $1; - $category =~ s/\s+/ /g; - $category =~ s/^\s+|\s+$//g; - $category =~ s/\&/\+/g; - push @categories, $category if ( defined $category && $category =~ /\S/ ); + #extract categories from title + my @categories = (); + while ( $title =~ /\((.*?),(.*?)\)/ ) { + my $category = $1; + $category =~ s/\s+/ /g; + $category =~ s/^\s+|\s+$//g; + $category =~ s/\&/\+/g; + push @categories, $category if ( defined $category && $category =~ /\S/ ); - $category = ''; - $category = $2 if ( defined $2 ); - $category =~ s/\s+/ /g; - $category =~ s/^\s+|\s+$//g; - $category =~ s/\&/\+/g; - push @categories, $category if ( defined $category && $category =~ /\S/ ); - $title =~ s/\((.*?),(.*?)\)/\($2\)/; - } - if ( $title =~ /\((.*?)\)/ ) { - my $category = $1; - $category =~ s/\s+/ /g; - $category =~ s/^\s+|\s+$//g; - $category =~ s/\&/\+/g; + $category = ''; + $category = $2 if ( defined $2 ); + $category =~ s/\s+/ /g; + $category =~ s/^\s+|\s+$//g; + $category =~ s/\&/\+/g; + push @categories, $category if ( defined $category && $category =~ /\S/ ); + $title =~ s/\((.*?),(.*?)\)/\($2\)/; + } + if ( $title =~ /\((.*?)\)/ ) { + my $category = $1; + $category =~ s/\s+/ /g; + $category =~ s/^\s+|\s+$//g; + $category =~ s/\&/\+/g; - # print $category."\n"; - push @categories, $category if ( defined $category && $category =~ /\S/ ); - $title =~ s/\((.*?)\)//; - } - $event->{categories} = \@categories if ( @categories > 0 ); - $event->{title} = $title; - $event->{title} =~ s/^\s+|\s+$//g; + # print $category."\n"; + push @categories, $category if ( defined $category && $category =~ /\S/ ); + $title =~ s/\((.*?)\)//; + } + $event->{categories} = \@categories if ( @categories > 0 ); + $event->{title} = $title; + $event->{title} =~ s/^\s+|\s+$//g; - if ( defined $event->{content} ) { + if ( defined $event->{content} ) { - #extract podcast_url from content link 'podcast' - my $podcast_url = ''; - if ( $event->{content} =~ /\[\[\s*([^\|\]]+)\s*\|\s*podcast\s*\]\]/i ) { - $podcast_url = $1; + #extract podcast_url from content link 'podcast' + my $podcast_url = ''; + if ( $event->{content} =~ /\[\[\s*([^\|\]]+)\s*\|\s*podcast\s*\]\]/i ) { + $podcast_url = $1; - # $podcast_url=~s/\|.*//g; - # print "found podcast:".$podcast_url."\n"; - } - $event->{podcast_url} = $podcast_url; + # $podcast_url=~s/\|.*//g; + # print "found podcast:".$podcast_url."\n"; + } + $event->{podcast_url} = $podcast_url; - #extract media_url from content link 'download' - my $media_url = ''; - if ( $event->{content} =~ /\[\[\s*([^\|\]]+)\s*\|\s*(direct\s+)?download\s*\]\]/i ) { - $media_url = $1; + #extract media_url from content link 'download' + my $media_url = ''; + if ( $event->{content} =~ /\[\[\s*([^\|\]]+)\s*\|\s*(direct\s+)?download\s*\]\]/i ) { + $media_url = $1; - # $podcast_url=~s/\|.*//g; - # print "found media:".$media_url."\n"; - } - $event->{media_url} = $media_url; + # $podcast_url=~s/\|.*//g; + # print "found media:".$media_url."\n"; + } + $event->{media_url} = $media_url; - #replace "thumbs/xxx" link by link to local media URI - $event->{content} =~ - s/\{\{\s*thumbs\/+(.*?)\s*\|\s*(.*?)\s*\}\}/\[\[$local_media_url\/images\/$1\|\{\{$local_media_url\/thumbs\/$1\|$2\}\}\]\]/g; + #replace "thumbs/xxx" link by link to local media URI + $event->{content} =~ + s/\{\{\s*thumbs\/+(.*?)\s*\|\s*(.*?)\s*\}\}/\[\[$local_media_url\/images\/$1\|\{\{$local_media_url\/thumbs\/$1\|$2\}\}\]\]/g; - #extract image from content - if ( $event->{content} =~ /\{\{(.*?)(\||\}\})/ ) { - $event->{image} = $1; - } - } + #extract image from content + if ( $event->{content} =~ /\{\{(.*?)(\||\}\})/ ) { + $event->{image} = $1; + } + } - #meta - if ( defined $event->{comments} ) { - my $meta = extractMeta( $event->{comments} ); - $event->{meta} = $meta if ( @$meta > 0 ); - } + #meta + if ( defined $event->{comments} ) { + my $meta = extractMeta( $event->{comments} ); + $event->{meta} = $meta if scalar @$meta > 0; + } - return $event; + return $event; } sub eventToWikiText { - my $event = shift; - my $local_media_url = $event->{local_media_url} || ''; + my $event = shift; + my $local_media_url = $event->{local_media_url} || ''; - $event->{program} =~ s/^\s+|\s+$//g; - $event->{series_name} =~ s/^\s+|\s+$//g; - $event->{title} =~ s/^\s+|\s+$//g; - $event->{excerpt} =~ s/^\s+|\s+$//g; - $event->{content} =~ s/^\s+|\s+$//g; - $event->{comments} =~ s/^\s+|\s+$//g; + $event->{program} =~ s/^\s+|\s+$//g; + $event->{series_name} =~ s/^\s+|\s+$//g; + $event->{title} =~ s/^\s+|\s+$//g; + $event->{excerpt} =~ s/^\s+|\s+$//g; + $event->{content} =~ s/^\s+|\s+$//g; + $event->{comments} =~ s/^\s+|\s+$//g; - my $title = ''; - if ( $event->{program} ne '' ) { - $title = $event->{program}; - $title .= ': ' if ( ( $event->{series_name} ne '' ) || ( $event->{title} ne '' ) ); - } - if ( $event->{series_name} ne '' ) { - $title .= $event->{series_name}; - $title .= ' - ' if ( $event->{title} ne '' ); - } - $title .= $event->{title}; - if ( $event->{categories} ) { - $title .= ' (' . join( ",", @{ $event->{categories} } ) . ')' if ( @{ $event->{categories} } > 0 ); - } + my $title = ''; + if ( $event->{program} ne '' ) { + $title = $event->{program}; + $title .= ': ' if ( $event->{series_name} ne '' ) || ( $event->{title} ne '' ); + } + if ( $event->{series_name} ne '' ) { + $title .= $event->{series_name}; + $title .= ' - ' if ( $event->{title} ne '' ); + } + $title .= $event->{title}; + if ( $event->{categories} ) { + my $categories = $event->{categories}; + $title .= ' (' . join( ",", @$categories ) . ')' if ( scalar @$categories > 0 ); + } - my $meta = extractMeta( $event->{comments}, $event->{meta} ); + my $meta = extractMeta( $event->{comments}, $event->{meta} ); - #use Data::Dumper;print "extracted meta".Dumper($meta); + #use Data::Dumper;print "extracted meta".Dumper($meta); - $event->{comments} = removeMeta( $event->{comments} ); - $event->{wiki_comments} = $event->{comments} . "\n\n" . metaToWiki($meta); + $event->{comments} = removeMeta( $event->{comments} ); + $event->{wiki_comments} = $event->{comments} . "\n\n" . metaToWiki($meta); - #use Data::Dumper;print "event content".Dumper($event->{content}); + #use Data::Dumper;print "event content".Dumper($event->{content}); - #rich content editors: - #$event->{wiki_content}=markup::html_to_creole($event->{content}); + #rich content editors: + #$event->{wiki_content}=markup::html_to_creole($event->{content}); - #markup editors - $event->{wiki_content} = $event->{content}; + #markup editors + $event->{wiki_content} = $event->{content}; # [[http://localhost/agenda_files/media/images/Vl8X7YmaWrmm9RMN_OMywA.jpg|{{http://localhost/agenda_files/media/thumbs/Vl8X7YmaWrmm9RMN_OMywA.jpg|}}]] #replace "thumbs/xxx" link by link to local media URI # while ($event->{wiki_content}=~/\[\[.*?\/+media\/+images\/+(.*?)\s*\|.*?\{\{.*?\/+media\/+thumbs\/+(.*?)\s*\|\s*(.*?)\s*\}\}\]\]/){ - $event->{wiki_content} =~ - s/\[\[.*?\/+media\/+images\/+(.*?)\s*\|.*?\{\{.*?\/+media\/+thumbs\/+(.*?)\s*\|\s*(.*?)\s*\}\}\]\]/\{\{thumbs\/$1\|$3\}\}/g; + $event->{wiki_content} =~ + s/\[\[.*?\/+media\/+images\/+(.*?)\s*\|.*?\{\{.*?\/+media\/+thumbs\/+(.*?)\s*\|\s*(.*?)\s*\}\}\]\]/\{\{thumbs\/$1\|$3\}\}/g; - # } + # } - my $wiki_content = join( "\n" . ( "-" x 20 ) . "\n", ( $event->{excerpt}, $event->{wiki_content} ) ); - $wiki_content .= "\n" . ( "-" x 20 ) . "\n" . $event->{wiki_comments} if ( $event->{wiki_comments} =~ /\S/ ); + my $wiki_content = join( "\n" . ( "-" x 20 ) . "\n", ( $event->{excerpt}, $event->{wiki_content} ) ); + $wiki_content .= "\n" . ( "-" x 20 ) . "\n" . $event->{wiki_comments} if ( $event->{wiki_comments} =~ /\S/ ); - return { - title => $title, - content => $event->{content}, - wiki_content => $wiki_content - }; + return { + title => $title, + content => $event->{content}, + wiki_content => $wiki_content + }; } #extrace meta tags from comment text sub extractMeta { - my $comments = shift; - my $meta = shift; + my $comments = shift; + my $meta = shift; - $meta = [] unless ( defined $meta ); + $meta = [] unless ( defined $meta ); - #push meta tags into meta list - if ( defined $comments ) { + #push meta tags into meta list + if ( defined $comments ) { - #build index for meta already defined - my $meta_keys = {}; - for my $pair (@$meta) { - $meta_keys->{ $pair->{name} . '=' . $pair->{value} } = 1; - } + #build index for meta already defined + my $meta_keys = {}; + for my $pair (@$meta) { + $meta_keys->{ $pair->{name} . '=' . $pair->{value} } = 1; + } - while ( $comments =~ /\~\~META\:(.+?)\=(.+?)\~\~/g ) { - my $name = $1; - my $value = $2; + while ( $comments =~ /\~\~META\:(.+?)\=(.+?)\~\~/g ) { + my $name = $1; + my $value = $2; - #fix meta values - $name = lc($name); - $name =~ s/^\s+|\s+$//g; - $value =~ s/^\s+|\s+$//g; + #fix meta values + $name = lc($name); + $name =~ s/^\s+|\s+$//g; + $value =~ s/^\s+|\s+$//g; - #insert into list, if not defined yet - unless ( ( $name eq '' ) || ( $value eq '' ) || ( exists $meta_keys->{ $name . '=' . $value } ) ) { - push @$meta, - { - name => $name, - value => $value, - }; - $meta_keys->{ $name . '=' . $value } = 1; - } - } - } + #insert into list, if not defined yet + unless ( ( $name eq '' ) || ( $value eq '' ) || ( exists $meta_keys->{ $name . '=' . $value } ) ) { + push @$meta, + { + name => $name, + value => $value, + }; + $meta_keys->{ $name . '=' . $value } = 1; + } + } + } - # use Data::Dumper;print Dumper($meta); - return $meta; + # use Data::Dumper;print Dumper($meta); + return $meta; } #remove meta tags from comment text sub removeMeta { - my $comments = shift || ''; + my $comments = shift || ''; - my $result = ''; - for my $line ( split( /\n/, $comments ) ) { - $result .= $line unless ( $line =~ /\~\~META\:(.+?)\=(.+?)\~\~/g ); - } + my $result = ''; + for my $line ( split( /\n/, $comments ) ) { + $result .= $line unless ( $line =~ /\~\~META\:(.+?)\=(.+?)\~\~/g ); + } - #use Data::Dumper;print "removed metsas:".Dumper($result); - $result =~ s/^\s+//g; - $result =~ s/\s+$//g; + #use Data::Dumper;print "removed metsas:".Dumper($result); + $result =~ s/^\s+//g; + $result =~ s/\s+$//g; - return $result; + return $result; } #add meta tags to comment text sub metaToWiki { - my $meta = shift; + my $meta = shift; - my $result = ''; - for my $pair (@$meta) { + my $result = ''; + for my $pair (@$meta) { - # use Data::Dumper;print Dumper($pair); - $result .= '~~META:' . $pair->{name} . '=' . $pair->{value} . '~~' . "\n"; - } - return $result; + # use Data::Dumper;print Dumper($pair); + $result .= '~~META:' . $pair->{name} . '=' . $pair->{value} . '~~' . "\n"; + } + return $result; - #use Data::Dumper;print Dumper($meta); + #use Data::Dumper;print Dumper($meta); } diff --git a/lib/calcms/db.pm b/lib/calcms/db.pm index efdce4f..ed07f4e 100644 --- a/lib/calcms/db.pm +++ b/lib/calcms/db.pm @@ -32,7 +32,7 @@ sub connect { my $options = shift; my $request = shift; - return $request->{connection} if ( ( defined $request ) && ( defined $request->{connection} ) ); + return $request->{connection} if ( defined $request ) && ( defined $request->{connection} ); my $access_options = $options->{access}; diff --git a/lib/calcms/eventOps.pm b/lib/calcms/eventOps.pm index 87d5bb2..aa42ddc 100644 --- a/lib/calcms/eventOps.pm +++ b/lib/calcms/eventOps.pm @@ -37,7 +37,7 @@ sub setAttributesFromSeriesTemplate { series_id => $params->{series_id}, } ); - if ( @$series != 1 ) { + if ( scalar @$series != 1 ) { uac::print_error("series not found"); return undef; } @@ -261,7 +261,7 @@ sub createEvent { series_id => $event->{series_id}, } ); - if ( @$series != 1 ) { + if ( scalar @$series != 1 ) { uac::print_error("series not found"); return undef; } @@ -279,7 +279,7 @@ sub createEvent { uac::print_error("studio not found"); return undef; } - unless ( @$studios == 1 ) { + unless ( scalar @$studios == 1 ) { uac::print_error("studio not found"); return undef; } diff --git a/lib/calcms/event_history.pm b/lib/calcms/event_history.pm index e2666bc..3f4604a 100644 --- a/lib/calcms/event_history.pm +++ b/lib/calcms/event_history.pm @@ -13,62 +13,62 @@ our %EXPORT_TAGS = ( 'all' => [@EXPORT_OK] ); sub debug; sub get_columns { - my $config = shift; + my $config = shift; - my $dbh = db::connect($config); - my $cols = db::get_columns( $dbh, 'calcms_event_history' ); - my $columns = {}; - for my $col (@$cols) { - $columns->{$col} = 1; - } - return $columns; + my $dbh = db::connect($config); + my $cols = db::get_columns( $dbh, 'calcms_event_history' ); + my $columns = {}; + for my $col (@$cols) { + $columns->{$col} = 1; + } + return $columns; } sub get { - my $config = shift; - my $condition = shift; + my $config = shift; + my $condition = shift; - return undef unless defined $condition->{studio_id}; + return undef unless defined $condition->{studio_id}; - my $dbh = db::connect($config); + my $dbh = db::connect($config); - my @conditions = (); - my @bind_values = (); + my @conditions = (); + my @bind_values = (); - if ( ( defined $condition->{project_id} ) && ( $condition->{project_id} ne '' ) ) { - push @conditions, 'project_id=?'; - push @bind_values, $condition->{project_id}; - } + if ( ( defined $condition->{project_id} ) && ( $condition->{project_id} ne '' ) ) { + push @conditions, 'project_id=?'; + push @bind_values, $condition->{project_id}; + } - if ( ( defined $condition->{studio_id} ) && ( $condition->{studio_id} ne '' ) ) { - push @conditions, 'studio_id=?'; - push @bind_values, $condition->{studio_id}; - } + if ( ( defined $condition->{studio_id} ) && ( $condition->{studio_id} ne '' ) ) { + push @conditions, 'studio_id=?'; + push @bind_values, $condition->{studio_id}; + } - if ( ( defined $condition->{series_id} ) && ( $condition->{series_id} ne '' ) ) { - push @conditions, 'series_id=?'; - push @bind_values, $condition->{series_id}; - } + if ( ( defined $condition->{series_id} ) && ( $condition->{series_id} ne '' ) ) { + push @conditions, 'series_id=?'; + push @bind_values, $condition->{series_id}; + } - if ( ( defined $condition->{event_id} ) && ( $condition->{event_id} ne '' ) ) { - push @conditions, 'event_id=?'; - push @bind_values, $condition->{event_id}; - } + if ( ( defined $condition->{event_id} ) && ( $condition->{event_id} ne '' ) ) { + push @conditions, 'event_id=?'; + push @bind_values, $condition->{event_id}; + } - if ( ( defined $condition->{change_id} ) && ( $condition->{change_id} ne '' ) ) { - push @conditions, 'id=?'; - push @bind_values, $condition->{change_id}; - } + if ( ( defined $condition->{change_id} ) && ( $condition->{change_id} ne '' ) ) { + push @conditions, 'id=?'; + push @bind_values, $condition->{change_id}; + } - my $limit = ''; - if ( ( defined $condition->{limit} ) && ( $condition->{limit} ne '' ) ) { - $limit = 'limit ' . $condition->{limit}; - } + my $limit = ''; + if ( ( defined $condition->{limit} ) && ( $condition->{limit} ne '' ) ) { + $limit = 'limit ' . $condition->{limit}; + } - my $conditions = ''; - $conditions = " where " . join( " and ", @conditions ) if ( @conditions > 0 ); + my $conditions = ''; + $conditions = " where " . join( " and ", @conditions ) if ( @conditions > 0 ); - my $query = qq{ + my $query = qq{ select * from calcms_event_history $conditions @@ -76,102 +76,100 @@ sub get { $limit }; - #print STDERR Dumper($query).Dumper(\@bind_values); + #print STDERR Dumper($query).Dumper(\@bind_values); - my $changes = db::get( $dbh, $query, \@bind_values ); + my $changes = db::get( $dbh, $query, \@bind_values ); - #print STDERR Dumper($changes); - - for my $change (@$changes) { - $change->{change_id} = $change->{id}; - delete $change->{id}; - } - return $changes; + for my $change (@$changes) { + $change->{change_id} = $change->{id}; + delete $change->{id}; + } + return $changes; } sub get_by_id { - my $config = shift; - my $id = shift; + my $config = shift; + my $id = shift; - my $dbh = db::connect($config); + my $dbh = db::connect($config); - my $query = qq{ + my $query = qq{ select * from calcms_event_history where event_id=? }; - my $studios = db::get( $dbh, $query, [$id] ); - return undef if ( @$studios != 1 ); - return $studios->[0]; + my $studios = db::get( $dbh, $query, [$id] ); + return undef if ( @$studios != 1 ); + return $studios->[0]; } sub insert { - my $config = shift; - my $entry = shift; + my $config = shift; + my $entry = shift; - $entry->{modified_at} = time::time_to_datetime( time() ); + $entry->{modified_at} = time::time_to_datetime( time() ); - $entry->{event_id} = $entry->{id} if ( ( defined $entry->{id} ) && ( !( defined $entry->{event_id} ) ) ); - delete $entry->{id}; + $entry->{event_id} = $entry->{id} if ( defined $entry->{id} ) && ( !( defined $entry->{event_id} ) ); + delete $entry->{id}; - #TODO:filter for existing attributes - my $columns = get_columns($config); - my $event = {}; - for my $column ( keys %$columns ) { - $event->{$column} = $entry->{$column} if defined $entry->{$column}; - } + #TODO:filter for existing attributes + my $columns = get_columns($config); + my $event = {}; + for my $column ( keys %$columns ) { + $event->{$column} = $entry->{$column} if defined $entry->{$column}; + } - my $dbh = db::connect($config); - my $id = db::insert( $dbh, 'calcms_event_history', $event ); - return $id; + my $dbh = db::connect($config); + my $id = db::insert( $dbh, 'calcms_event_history', $event ); + return $id; } # insert event sub insert_by_event_id { - my $config = shift; - my $options = shift; + my $config = shift; + my $options = shift; - return undef unless defined $options->{project_id}; - return undef unless defined $options->{studio_id}; - return undef unless defined $options->{series_id}; - return undef unless defined $options->{event_id}; - return undef unless defined $options->{user}; + return undef unless defined $options->{project_id}; + return undef unless defined $options->{studio_id}; + return undef unless defined $options->{series_id}; + return undef unless defined $options->{event_id}; + return undef unless defined $options->{user}; - my $sql = q{ + my $sql = q{ select * from calcms_events where id=? }; - my $bind_values = [ $options->{event_id} ]; - my $dbh = db::connect($config); - my $results = db::get( $dbh, $sql, $bind_values ); - if ( @$results != 1 ) { - print STDERR "cannot find event with event_id=$options->{event_id}"; - return 0; - } + my $bind_values = [ $options->{event_id} ]; + my $dbh = db::connect($config); + my $results = db::get( $dbh, $sql, $bind_values ); + if ( @$results != 1 ) { + print STDERR "cannot find event with event_id=$options->{event_id}"; + return 0; + } - # add to history - my $event = $results->[0]; - $event->{project_id} = $options->{project_id}; - $event->{studio_id} = $options->{studio_id}; - $event->{series_id} = $options->{series_id}; - $event->{event_id} = $options->{event_id}; - $event->{user} = $options->{user}; - $event->{deleted} = 1; - event_history::insert( $config, $event ); + # add to history + my $event = $results->[0]; + $event->{project_id} = $options->{project_id}; + $event->{studio_id} = $options->{studio_id}; + $event->{series_id} = $options->{series_id}; + $event->{event_id} = $options->{event_id}; + $event->{user} = $options->{user}; + $event->{deleted} = 1; + event_history::insert( $config, $event ); } sub delete { - my $config = shift; - my $entry = shift; + my $config = shift; + my $entry = shift; - my $dbh = db::connect($config); - db::put( $dbh, 'delete from calcms_event_history where event_id=?', [ $entry->{id} ] ); + my $dbh = db::connect($config); + db::put( $dbh, 'delete from calcms_event_history where event_id=?', [ $entry->{id} ] ); } sub error { - my $msg = shift; - print "ERROR: $msg
\n"; + my $msg = shift; + print "ERROR: $msg
\n"; } #do not delete last line! diff --git a/lib/calcms/events.pm b/lib/calcms/events.pm index 317b12f..26fdd04 100644 --- a/lib/calcms/events.pm +++ b/lib/calcms/events.pm @@ -41,985 +41,982 @@ sub init { sub get_cached_or_render { - # my $response=$_[0]; - my $config = $_[1]; - my $request = $_[2]; + # my $response=$_[0]; + my $config = $_[1]; + my $request = $_[2]; - my $params = $request->{params}->{checked}; - my $debug = $config->{system}->{debug}; + my $params = $request->{params}->{checked}; + my $debug = $config->{system}->{debug}; - my $cache = {}; - if ( $config->{cache}->{use_cache} == 1 ) { - events::configure_cache($config); - $cache = cache::load( $config, $params ); - if ( defined $cache->{content} ) { - $_[0] = $cache->{content}; - return; - } - } + my $cache = {}; + if ( $config->{cache}->{use_cache} == 1 ) { + events::configure_cache($config); + $cache = cache::load( $config, $params ); + if ( defined $cache->{content} ) { + $_[0] = $cache->{content}; + return; + } + } - my $results = events::get( $config, $request ); - events::render( $_[0], $config, $request, $results ); + my $results = events::get( $config, $request ); + events::render( $_[0], $config, $request, $results ); - #write to cache - if ( $config->{cache}->{use_cache} == 1 ) { + #write to cache + if ( $config->{cache}->{use_cache} == 1 ) { - #todo:put out reference only - $cache->{content} = $_[0]; - cache::save($cache); - } - - return $_[0]; + #todo:put out reference only + $cache->{content} = $_[0]; + cache::save($cache); + } + + return $_[0]; } sub get { - my $config = shift; - my $request = shift; + my $config = shift; + my $request = shift; - my $debug = $config->{system}->{debug}; + my $debug = $config->{system}->{debug}; - my $dbh = db::connect( $config, $request ); + my $dbh = db::connect( $config, $request ); - ( my $query, my $bind_values ) = events::get_query( $dbh, $config, $request ); + ( my $query, my $bind_values ) = events::get_query( $dbh, $config, $request ); - #print STDERR Dumper($query).Dumper($bind_values)."\n"; + #print STDERR Dumper($query).Dumper($bind_values)."\n"; - my $results = db::get( $dbh, $$query, $bind_values ); + my $results = db::get( $dbh, $$query, $bind_values ); - #$results = events::add_recordings($dbh, $config, $request, $results); + #$results = events::add_recordings($dbh, $config, $request, $results); - #print STDERR Dumper($results); - $results = events::modify_results( $dbh, $config, $request, $results ); + #print STDERR Dumper($results); + $results = events::modify_results( $dbh, $config, $request, $results ); - return $results; + return $results; } sub modify_results { - my $dbh = shift; - my $config = shift; - my $request = shift; - my $results = shift; + my $dbh = shift; + my $config = shift; + my $request = shift; + my $results = shift; - my $params = $request->{params}->{checked}; + my $params = $request->{params}->{checked}; - my $running_event_id = 0; - my $projects = {}; - my $studios = {}; + my $running_event_id = 0; + my $projects = {}; + my $studios = {}; - # print $running_event_id." ".$running_events->[0]->{start}." ".$running_events->[0]->{title} if ($debug ne''); - my $time_diff = ''; - if ( scalar @$results > 0 ) { - $results->[0]->{__first__} = 1; - $results->[-1]->{__last__} = 1; - $running_event_id = events::get_running_event_id($dbh); - } + # print $running_event_id." ".$running_events->[0]->{start}." ".$running_events->[0]->{title} if ($debug ne''); + my $time_diff = ''; + if ( scalar @$results > 0 ) { + $results->[0]->{__first__} = 1; + $results->[-1]->{__last__} = 1; + $running_event_id = events::get_running_event_id($dbh); + } - if ( ( defined $params->{template} ) && ( $params->{template} =~ /\.xml/ ) ) { - $time_diff = time::utc_offset( $config->{date}->{time_zone} ); - $time_diff =~ s/(\d\d)(\d\d)/$1\:$2/g; - } + if ( ( defined $params->{template} ) && ( $params->{template} =~ /\.xml/ ) ) { + $time_diff = time::utc_offset( $config->{date}->{time_zone} ); + $time_diff =~ s/(\d\d)(\d\d)/$1\:$2/g; + } - my $previous_result = { start_date => '' }; - my $counter = 1; - for my $result (@$results) { - if ( defined $params->{template} ) { - if ( $params->{template} =~ /\.ics$/ ) { - $result->{content_ical} = - markup::plain_to_ical( $result->{content} ); - $result->{title_ical} = - markup::plain_to_ical( $result->{title} ); - $result->{user_title_ical} = - markup::plain_to_ical( $result->{user_title} ); - $result->{excerpt_ical} = - markup::plain_to_ical( $result->{excerpt} ); - $result->{user_excerpt_ical} = - markup::plain_to_ical( $result->{user_excerpt} ); - $result->{series_name} = - markup::plain_to_ical( $result->{series_name} ); - $result->{created_at} =~ s/ /T/gi; - $result->{created_at} =~ s/[\:\-]//gi; - $result->{modified_at} =~ s/ /T/gi; - $result->{modified_at} =~ s/[\:\-]//gi; + my $previous_result = { start_date => '' }; + my $counter = 1; + for my $result (@$results) { + if ( defined $params->{template} ) { + if ( $params->{template} =~ /\.ics$/ ) { + $result->{content_ical} = + markup::plain_to_ical( $result->{content} ); + $result->{title_ical} = + markup::plain_to_ical( $result->{title} ); + $result->{user_title_ical} = + markup::plain_to_ical( $result->{user_title} ); + $result->{excerpt_ical} = + markup::plain_to_ical( $result->{excerpt} ); + $result->{user_excerpt_ical} = + markup::plain_to_ical( $result->{user_excerpt} ); + $result->{series_name} = + markup::plain_to_ical( $result->{series_name} ); + $result->{created_at} =~ s/ /T/gi; + $result->{created_at} =~ s/[\:\-]//gi; + $result->{modified_at} =~ s/ /T/gi; + $result->{modified_at} =~ s/[\:\-]//gi; - } elsif ( $params->{template} =~ /\.atom\.xml/ ) { - $result->{excerpt} = '' unless defined( $result->{excerpt} ); - $result->{excerpt} = "lass dich ueberraschen" - if ( $result->{excerpt} eq '' ); + } elsif ( $params->{template} =~ /\.atom\.xml/ ) { + $result->{excerpt} = '' unless defined( $result->{excerpt} ); + $result->{excerpt} = "lass dich ueberraschen" + if ( $result->{excerpt} eq '' ); - # $result->{excerpt} =markup::plain_to_xml($result->{excerpt}); - # $result->{title} =markup::plain_to_xml($result->{title}); - # $result->{series_name} =markup::plain_to_xml($result->{series_name}); - # $result->{program} =markup::plain_to_xml($result->{program}); + # $result->{excerpt} =markup::plain_to_xml($result->{excerpt}); + # $result->{title} =markup::plain_to_xml($result->{title}); + # $result->{series_name} =markup::plain_to_xml($result->{series_name}); + # $result->{program} =markup::plain_to_xml($result->{program}); - $result->{created_at} =~ s/ /T/gi; - $result->{created_at} .= $time_diff; - $result->{modified_at} =~ s/ /T/gi; - $result->{modified_at} .= $time_diff; - } elsif ( $params->{template} =~ /\.rss\.xml/ ) { - $result->{excerpt} = '' unless defined( $result->{excerpt} ); - $result->{excerpt} = "lass dich ueberraschen" - if ( $result->{excerpt} eq '' ); + $result->{created_at} =~ s/ /T/gi; + $result->{created_at} .= $time_diff; + $result->{modified_at} =~ s/ /T/gi; + $result->{modified_at} .= $time_diff; + } elsif ( $params->{template} =~ /\.rss\.xml/ ) { + $result->{excerpt} = '' unless defined( $result->{excerpt} ); + $result->{excerpt} = "lass dich ueberraschen" + if ( $result->{excerpt} eq '' ); - # $result->{excerpt} =markup::plain_to_xml($result->{excerpt}); - # $result->{title} =markup::plain_to_xml($result->{title}); - # $result->{series_name} =markup::plain_to_xml($result->{series_name}); - # $result->{program} =markup::plain_to_xml($result->{program}); - #print STDERR "created:$result->{created_at} modified:$result->{modified_at}\n"; - $result->{modified_at} = - time::datetime_to_rfc822( $result->{modified_at} ); - if ( $result->{created_at} =~ /[1-9]/ ) { - $result->{created_at} = - time::datetime_to_rfc822( $result->{created_at} ); - } else { - $result->{created_at} = $result->{modified_at}; - } + # $result->{excerpt} =markup::plain_to_xml($result->{excerpt}); + # $result->{title} =markup::plain_to_xml($result->{title}); + # $result->{series_name} =markup::plain_to_xml($result->{series_name}); + # $result->{program} =markup::plain_to_xml($result->{program}); + #print STDERR "created:$result->{created_at} modified:$result->{modified_at}\n"; + $result->{modified_at} = + time::datetime_to_rfc822( $result->{modified_at} ); + if ( $result->{created_at} =~ /[1-9]/ ) { + $result->{created_at} = + time::datetime_to_rfc822( $result->{created_at} ); + } else { + $result->{created_at} = $result->{modified_at}; + } - } - } - $result->{series_name} ||= ''; - $result->{series_name} = '' if $result->{series_name} eq '_single_'; + } + } + $result->{series_name} ||= ''; + $result->{series_name} = '' if $result->{series_name} eq '_single_'; - $result->{rerun} = '' unless defined $result->{rerun}; + $result->{rerun} = '' unless defined $result->{rerun}; - $result->{title} = '' unless defined $result->{title}; - if ( $result->{title} =~ /\#(\d+)([a-z])?\s*$/ ) { - $result->{episode} = $1 unless defined $result->{episode}; - $result->{rerun} = $2 || '' unless ( $result->{rerun} =~ /\d/ ); - $result->{title} =~ s/\#\d+[a-z]?\s*$//; - $result->{title} =~ s/\s+$//; - } - $result->{rerun} = '' if ( $result->{rerun} eq '0' ); + $result->{title} = '' unless defined $result->{title}; + if ( $result->{title} =~ /\#(\d+)([a-z])?\s*$/ ) { + $result->{episode} = $1 unless defined $result->{episode}; + $result->{rerun} = $2 || '' unless ( $result->{rerun} =~ /\d/ ); + $result->{title} =~ s/\#\d+[a-z]?\s*$//; + $result->{title} =~ s/\s+$//; + } + $result->{rerun} = '' if ( $result->{rerun} eq '0' ); - if ( ( defined $result->{recurrence_count} ) - && ( $result->{recurrence_count} > 0 ) ) - { - $result->{recurrence_count_alpha} = - markup::base26( $result->{recurrence_count} + 1 ); - $result->{recurrence_id} = $result->{recurrence}; - } else { - $result->{recurrence_count_alpha} = ''; - $result->{recurrence_count} = ''; - } + if ( ( defined $result->{recurrence_count} ) + && ( $result->{recurrence_count} > 0 ) ) + { + $result->{recurrence_count_alpha} = + markup::base26( $result->{recurrence_count} + 1 ); + $result->{recurrence_id} = $result->{recurrence}; + } else { + $result->{recurrence_count_alpha} = ''; + $result->{recurrence_count} = ''; + } - # set title keys - my $keys = get_keys($result); - for my $key ( keys %$keys ) { - $result->{$key} = $keys->{$key}; - } + # set title keys + my $keys = get_keys($result); + for my $key ( keys %$keys ) { + $result->{$key} = $keys->{$key}; + } - $result = calc_dates( $config, $result, $params, $previous_result, $time_diff ); + $result = calc_dates( $config, $result, $params, $previous_result, $time_diff ); - $result->{event_uri} = ''; - if ( ( defined $result->{program} ) && ( $result->{program} ne '' ) ) { - $result->{event_uri} .= $result->{program}; - $result->{event_uri} .= '-' - if ( ( $result->{series_name} ne '' ) - || ( $result->{title} ne '' ) ); - } - if ( $result->{series_name} ne '' ) { - $result->{event_uri} .= $result->{series_name}; - $result->{event_uri} .= '-' if ( $result->{title} ne '' ); - } - $result->{event_uri} .= $result->{title} if $result->{title} ne ''; - $result->{event_uri} =~ s/\#/Nr./g; - $result->{event_uri} =~ s/\&/und/g; - $result->{event_uri} =~ s/\//\%2f/g; - $result->{event_uri} =~ s/[?]//g; + $result->{event_uri} = ''; + if ( ( defined $result->{program} ) && ( $result->{program} ne '' ) ) { + $result->{event_uri} .= $result->{program}; + $result->{event_uri} .= '-' + if ( ( $result->{series_name} ne '' ) + || ( $result->{title} ne '' ) ); + } + if ( $result->{series_name} ne '' ) { + $result->{event_uri} .= $result->{series_name}; + $result->{event_uri} .= '-' if ( $result->{title} ne '' ); + } + $result->{event_uri} .= $result->{title} if $result->{title} ne ''; + $result->{event_uri} =~ s/\#/Nr./g; + $result->{event_uri} =~ s/\&/und/g; + $result->{event_uri} =~ s/\//\%2f/g; + $result->{event_uri} =~ s/[?]//g; - $result->{rds_title} = $result->{event_uri}; - $result->{rds_title} =~ s/[^a-zA-Z0-9\-]/\_/gi; - $result->{rds_title} =~ s/\_{2,99}/\_/gi; - $result->{rds_title} = substr( $result->{rds_title}, 0, 63 ); + $result->{rds_title} = $result->{event_uri}; + $result->{rds_title} =~ s/[^a-zA-Z0-9\-]/\_/gi; + $result->{rds_title} =~ s/\_{2,99}/\_/gi; + $result->{rds_title} = substr( $result->{rds_title}, 0, 63 ); - #$result->{event_id}=$result->{id}; + #$result->{event_id}=$result->{id}; - $result->{base_url} = $request->{base_url}; - $result->{base_domain} = $config->{locations}->{base_domain}; - $result->{static_files_url} = $config->{locations}->{static_files_url}; - $result->{source_base_url} = $config->{locations}->{source_base_url}; - $result->{cache_base_url} = $config->{cache}->{base_url}; + $result->{base_url} = $request->{base_url}; + $result->{base_domain} = $config->{locations}->{base_domain}; + $result->{static_files_url} = $config->{locations}->{static_files_url}; + $result->{source_base_url} = $config->{locations}->{source_base_url}; + $result->{cache_base_url} = $config->{cache}->{base_url}; - $result->{is_running} = 1 - if ( $running_event_id eq $result->{event_id} ); - $result->{one_comment} = 1 if ( $result->{comment_count} == 1 ); - $result->{no_comment} = 1 if ( $result->{comment_count} == 0 ); + $result->{is_running} = 1 + if ( $running_event_id eq $result->{event_id} ); + $result->{one_comment} = 1 if ( $result->{comment_count} == 1 ); + $result->{no_comment} = 1 if ( $result->{comment_count} == 0 ); - #fix image url - #$params->{exclude_event_images}=0 unless defined $params->{exclude_event_images}; - #if ($params->{exclude_event_images}==1){ - # if ( (defined $config->{permissions}->{hide_event_images}) && ($config->{permissions}->{hide_event_images} eq '1') ){ - # $result->{image} = $result->{series_image}; - # $result->{image_label} = $result->{series_image_label}; - # } - #} + #fix image url + #$params->{exclude_event_images}=0 unless defined $params->{exclude_event_images}; + #if ($params->{exclude_event_images}==1){ + # if ( (defined $config->{permissions}->{hide_event_images}) && ($config->{permissions}->{hide_event_images} eq '1') ){ + # $result->{image} = $result->{series_image}; + # $result->{image_label} = $result->{series_image_label}; + # } + #} - if ( defined $result->{image} ) { - my $url = $config->{locations}->{local_media_url} || ''; - my $image = $result->{image}; - $result->{thumb_url} = $url . '/thumbs/' . $image; - $result->{icon_url} = $url . '/icons/' . $image; - $result->{image_url} = $url . '/images/' . $image; - } + if ( defined $result->{image} ) { + my $url = $config->{locations}->{local_media_url} || ''; + my $image = $result->{image}; + $result->{thumb_url} = $url . '/thumbs/' . $image; + $result->{icon_url} = $url . '/icons/' . $image; + $result->{image_url} = $url . '/images/' . $image; + } - if ( defined $result->{series_image} ) { - my $url = $config->{locations}->{local_media_url} || ''; - my $image = $result->{series_image}; - $result->{series_thumb_url} = $url . '/thumbs/' . $image; - $result->{series_icon_url} = $url . '/icons/' . $image; - $result->{series_image_url} = $url . '/images/' . $image; - } + if ( defined $result->{series_image} ) { + my $url = $config->{locations}->{local_media_url} || ''; + my $image = $result->{series_image}; + $result->{series_thumb_url} = $url . '/thumbs/' . $image; + $result->{series_icon_url} = $url . '/icons/' . $image; + $result->{series_image_url} = $url . '/images/' . $image; + } - $result->{location_css} = $result->{location} || ''; - $result->{location_css} = lc( $result->{location_css} ); - $result->{location_css} =~ s/\.//g; - $result->{location_css} =~ s/\s//g; - $result->{ 'location_label_' . $result->{location_css} } = 1; + $result->{location_css} = $result->{location} || ''; + $result->{location_css} = lc( $result->{location_css} ); + $result->{location_css} =~ s/\.//g; + $result->{location_css} =~ s/\s//g; + $result->{ 'location_label_' . $result->{location_css} } = 1; - # add project by name - my $project_name = $result->{project}; - if ( defined $project_name ) { + # add project by name + my $project_name = $result->{project}; + if ( defined $project_name ) { - #print STDERR "found project:$project_name\n"; - unless ( defined $projects->{$project_name} ) { - my $results = project::get( $config, { name => $project_name } ); - $projects->{$project_name} = $results->[0] || {}; - } - my $project = $projects->{$project_name}; - for my $key ( keys %$project ) { - $result->{ 'project_' . $key } = $project->{$key}; - } - } else { - print STDERR "events::get - unknown project for event $result->{id}\n"; - } + #print STDERR "found project:$project_name\n"; + unless ( defined $projects->{$project_name} ) { + my $results = project::get( $config, { name => $project_name } ); + $projects->{$project_name} = $results->[0] || {}; + } + my $project = $projects->{$project_name}; + for my $key ( keys %$project ) { + $result->{ 'project_' . $key } = $project->{$key}; + } + } else { + print STDERR "events::get - unknown project for event $result->{id}\n"; + } - #if project_id is set add columns from project (cached) - my $project_id = $result->{project_id}; - if ( defined $project_id ) { - unless ( defined $projects->{$project_id} ) { - my $results = project::get( $config, { project_id => $project_id } ); - $projects->{$project_id} = $results->[0] || {}; - } - my $project = $projects->{$project_id}; - for my $key ( keys %$project ) { - $result->{ 'project_' . $key } = $project->{$key}; - } - } + #if project_id is set add columns from project (cached) + my $project_id = $result->{project_id}; + if ( defined $project_id ) { + unless ( defined $projects->{$project_id} ) { + my $results = project::get( $config, { project_id => $project_id } ); + $projects->{$project_id} = $results->[0] || {}; + } + my $project = $projects->{$project_id}; + for my $key ( keys %$project ) { + $result->{ 'project_' . $key } = $project->{$key}; + } + } - #if studio_id is set add columns from studio (cached) - my $studio_id = $result->{studio_id}; - if ( defined $studio_id ) { - unless ( defined $studios->{$studio_id} ) { - my $results = studios::get( $config, { studio_id => $studio_id } ); - $studios->{$studio_id} = $results->[0] || {}; - } - my $studio = $studios->{$studio_id}; - for my $key ( keys %$studio ) { - $result->{ 'studio_' . $key } = $studio->{$key}; - } - } + #if studio_id is set add columns from studio (cached) + my $studio_id = $result->{studio_id}; + if ( defined $studio_id ) { + unless ( defined $studios->{$studio_id} ) { + my $results = studios::get( $config, { studio_id => $studio_id } ); + $studios->{$studio_id} = $results->[0] || {}; + } + my $studio = $studios->{$studio_id}; + for my $key ( keys %$studio ) { + $result->{ 'studio_' . $key } = $studio->{$key}; + } + } - #$result->{'project_title'}=$project->{title} if (defined $project->{title} && $project->{title} ne ''); + #$result->{'project_title'}=$project->{title} if (defined $project->{title} && $project->{title} ne ''); - #print STDERR "project:'$project_name'\n"; - #print STDERR "title:'$result->{project_title}'\n"; - #use Data::Dumper;print STDERR Dumper($result); + #print STDERR "project:'$project_name'\n"; + #print STDERR "title:'$result->{project_title}'\n"; + #use Data::Dumper;print STDERR Dumper($result); - for my $name ( keys %{ $config->{mapping}->{events} } ) { - my $val = ''; - if ( ( defined $name ) - && ( defined $config->{mapping}->{events}->{$name} ) - && ( defined $result->{$name} ) ) - { - $val = $config->{mapping}->{events}->{$name}->{ $result->{$name} } - || ''; - $result->{ $name . '_mapped' } = $val if ( $val ne '' ); - } - } + for my $name ( keys %{ $config->{mapping}->{events} } ) { + my $val = ''; + if ( ( defined $name ) + && ( defined $config->{mapping}->{events}->{$name} ) + && ( defined $result->{$name} ) ) + { + $val = $config->{mapping}->{events}->{$name}->{ $result->{$name} } + || ''; + $result->{ $name . '_mapped' } = $val if ( $val ne '' ); + } + } - #for my $name (keys %{$config->{controllers}}){ - # $result->{"controller_$name"}=$config->{controllers}->{$name}; - #} + #for my $name (keys %{$config->{controllers}}){ + # $result->{"controller_$name"}=$config->{controllers}->{$name}; + #} - $previous_result = $result; + $previous_result = $result; - #print "Content-type:text/text\n\n"; - #use Data::Dumper;print STDERR Dumper($result); - $result->{ 'counter_' . $counter } = 1; - $counter++; + #print "Content-type:text/text\n\n"; + #use Data::Dumper;print STDERR Dumper($result); + $result->{ 'counter_' . $counter } = 1; + $counter++; - if ( ( defined $params->{template} ) - && ( $params->{template} =~ /(list|details)/ ) ) - { - if ( ( defined $result->{excerpt} ) && ( length( $result->{excerpt} ) > 250 ) ) { - $result->{excerpt} = substr( $result->{excerpt}, 0, 250 ) . '...'; - } + if ( ( defined $params->{template} ) + && ( $params->{template} =~ /(list|details)/ ) ) + { + 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 ) ) { - $result->{user_excerpt} = substr( $result->{user_excerpt}, 0, 250 ) . '...'; - } - } + if ( ( defined $result->{user_excerpt} ) && ( length( $result->{user_excerpt} ) > 250 ) ) { + $result->{user_excerpt} = substr( $result->{user_excerpt}, 0, 250 ) . '...'; + } + } - #build content - if ( ( defined $params->{template} ) - && ( $params->{template} =~ /\.html/ ) ) - { - if ( defined $result->{content} ) { - $result->{content} = - markup::fix_line_ends( $result->{content} ); - $result->{content} = - markup::creole_to_html( $result->{content} ); - $result->{html_content} = $result->{content}; + #build content + if ( ( defined $params->{template} ) + && ( $params->{template} =~ /\.html/ ) ) + { + if ( defined $result->{content} ) { + $result->{content} = + markup::fix_line_ends( $result->{content} ); + $result->{content} = + markup::creole_to_html( $result->{content} ); + $result->{html_content} = $result->{content}; - } - if ( defined $result->{topic} ) { - $result->{topic} = markup::fix_line_ends( $result->{topic} ); - $result->{topic} = markup::creole_to_html( $result->{topic} ); - $result->{html_topic} = $result->{topic}; - } - } + } + if ( defined $result->{topic} ) { + $result->{topic} = markup::fix_line_ends( $result->{topic} ); + $result->{topic} = markup::creole_to_html( $result->{topic} ); + $result->{html_topic} = $result->{topic}; + } + } - #detect if images are in content or topic field - my $image_in_text = 0; - $image_in_text = 1 - if ( ( defined $result->{content} ) - && ( $result->{content} =~ /{topic} ) - && ( $result->{topic} =~ /{no_image_in_text} = 1 if $image_in_text == 0; + #detect if images are in content or topic field + my $image_in_text = 0; + $image_in_text = 1 + if ( defined $result->{content} ) + && ( $result->{content} =~ /{topic} ) + && ( $result->{topic} =~ /{no_image_in_text} = 1 if $image_in_text == 0; - if ( - ( defined $params->{template} ) - && ( ( $params->{template} =~ /event_perl\.txt$/ ) - || ( $params->{template} =~ /event_file_export\.txt$/ ) ) + if ( + ( defined $params->{template} ) + && ( ( $params->{template} =~ /event_perl\.txt$/ ) + || ( $params->{template} =~ /event_file_export\.txt$/ ) ) - ) - { - for my $key ( keys %$result ) { - $result->{$key} =~ s/\|/\\\|/g if ( defined $result->{$key} ); - } + ) + { + for my $key ( keys %$result ) { + $result->{$key} =~ s/\|/\\\|/g if ( defined $result->{$key} ); + } - # $result->{content}='no'; - } + # $result->{content}='no'; + } - } # end for results - add_recurrence_dates( $config, $results ); - return $results; + } # end for results + add_recurrence_dates( $config, $results ); + return $results; } sub add_recurrence_dates { - my $config = shift; - my $results = shift; + my $config = shift; + my $results = shift; - # get unique list of recurrence ids from results - my $recurrence_dates = {}; - for my $result (@$results) { - next unless defined $result->{recurrence}; - next if $result->{recurrence} == 0; - $recurrence_dates->{ $result->{recurrence} } = 0; - } + # get unique list of recurrence ids from results + my $recurrence_dates = {}; + for my $result (@$results) { + next unless defined $result->{recurrence}; + next if $result->{recurrence} == 0; + $recurrence_dates->{ $result->{recurrence} } = 0; + } - my @event_ids = keys %$recurrence_dates; - return if @event_ids == 0; + my @event_ids = keys %$recurrence_dates; + return if @event_ids == 0; - # query start date of recurrences - my $conditions = []; - my $bind_values = []; - for my $id (@event_ids) { - push @$conditions, '?'; - push @$bind_values, $id; - } - $conditions = join( ',', @$conditions ); + # query start date of recurrences + my $conditions = []; + my $bind_values = []; + for my $id (@event_ids) { + push @$conditions, '?'; + push @$bind_values, $id; + } + $conditions = join( ',', @$conditions ); - my $query = qq{ + my $query = qq{ select id event_id, start from calcms_events where id in ($conditions) }; - #print STDERR Dumper($query); - #return; - my $dbh = db::connect($config); - my $events = db::get( $dbh, $query, $bind_values ); + #print STDERR Dumper($query); + #return; + my $dbh = db::connect($config); + my $events = db::get( $dbh, $query, $bind_values ); - # store start dates by recurrence id - for my $event (@$events) { - $recurrence_dates->{ $event->{event_id} } = $event->{start}; - } + # store start dates by recurrence id + for my $event (@$events) { + $recurrence_dates->{ $event->{event_id} } = $event->{start}; + } - # set start dates to results - for my $result (@$results) { - next unless defined $result->{recurrence}; - next if $result->{recurrence} == 0; - $result->{recurrence_date} = - $recurrence_dates->{ $result->{recurrence} }; - } + # set start dates to results + for my $result (@$results) { + next unless defined $result->{recurrence}; + next if $result->{recurrence} == 0; + $result->{recurrence_date} = + $recurrence_dates->{ $result->{recurrence} }; + } - #print STDERR Dumper($recurrence_dates); + #print STDERR Dumper($recurrence_dates); } sub calc_dates { - my $config = shift; - my $result = shift; - my $params = shift || {}; - my $previous_result = shift || {}; - my $time_diff = shift || ''; + my $config = shift; + my $result = shift; + my $params = shift || {}; + my $previous_result = shift || {}; + my $time_diff = shift || ''; - $result->{utc_offset} = $time_diff; - $result->{time_zone} = $config->{date}->{time_zone}; - my $language = $config->{date}->{language} || 'en'; + $result->{utc_offset} = $time_diff; + $result->{time_zone} = $config->{date}->{time_zone}; + my $language = $config->{date}->{language} || 'en'; - $result->{start_datetime} = $result->{start}; - $result->{start_datetime} =~ s/ /T/gi; - if ( $result->{start_datetime} =~ /(\d\d\d\d)\-(\d\d)\-(\d\d)T(\d\d)\:(\d\d)/ ) { - $result->{start_year} = $1; - $result->{start_month} = $2; - $result->{start_day} = $3; - $result->{start_hour} = $4; - $result->{start_minute} = $5; - } + $result->{start_datetime} = $result->{start}; + $result->{start_datetime} =~ s/ /T/gi; + if ( $result->{start_datetime} =~ /(\d\d\d\d)\-(\d\d)\-(\d\d)T(\d\d)\:(\d\d)/ ) { + $result->{start_year} = $1; + $result->{start_month} = $2; + $result->{start_day} = $3; + $result->{start_hour} = $4; + $result->{start_minute} = $5; + } - unless ( defined $result->{day} ) { - my $d = time::datetime_to_array( $result->{start} ); - my $hour = $d->[3]; - if ( ( defined $hour ) && ( $hour < 6 ) ) { - $result->{day} = time::add_days_to_date( $result->{start}, -1 ); - } else { - $result->{day} = time::datetime_to_date( $result->{start} ); - } - } - unless ( defined $result->{start_date} ) { - $result->{start_date} = time::datetime_to_date( $result->{start} ); - } - unless ( defined $result->{end_date} ) { - $result->{end_date} = time::datetime_to_date( $result->{end} ); - } + unless ( defined $result->{day} ) { + my $d = time::datetime_to_array( $result->{start} ); + my $hour = $d->[3]; + if ( ( defined $hour ) && ( $hour < 6 ) ) { + $result->{day} = time::add_days_to_date( $result->{start}, -1 ); + } else { + $result->{day} = time::datetime_to_date( $result->{start} ); + } + } + unless ( defined $result->{start_date} ) { + $result->{start_date} = time::datetime_to_date( $result->{start} ); + } + unless ( defined $result->{end_date} ) { + $result->{end_date} = time::datetime_to_date( $result->{end} ); + } - $result->{dtstart} = $result->{start_datetime}; - $result->{dtstart} =~ s/[\:\-]//gi; + $result->{dtstart} = $result->{start_datetime}; + $result->{dtstart} =~ s/[\:\-]//gi; - if ( ( defined $params->{template} ) - && ( $params->{template} =~ /(\.txt|\.json)/ ) ) - { - $result->{start_utc_epoch} = time::datetime_to_utc( $result->{start_datetime}, $config->{date}->{time_zone} ); - } - if ( ( defined $params->{template} ) - && ( $params->{template} =~ /(\.xml)/ ) ) - { - $result->{start_datetime_utc} = time::datetime_to_utc_datetime( $result->{start_datetime}, $config->{date}->{time_zone} ); - } + if ( ( defined $params->{template} ) + && ( $params->{template} =~ /(\.txt|\.json)/ ) ) + { + $result->{start_utc_epoch} = time::datetime_to_utc( $result->{start_datetime}, $config->{date}->{time_zone} ); + } + if ( ( defined $params->{template} ) + && ( $params->{template} =~ /(\.xml)/ ) ) + { + $result->{start_datetime_utc} = time::datetime_to_utc_datetime( $result->{start_datetime}, $config->{date}->{time_zone} ); + } - $result->{end_datetime} = $result->{end}; - $result->{end_datetime} =~ s/ /T/gi; + $result->{end_datetime} = $result->{end}; + $result->{end_datetime} =~ s/ /T/gi; - $result->{dtend} = $result->{end_datetime}; - $result->{dtend} =~ s/[\:\-]//gi; + $result->{dtend} = $result->{end_datetime}; + $result->{dtend} =~ s/[\:\-]//gi; - if ( ( defined $params->{template} ) - && ( $params->{template} =~ /(\.txt|\.json)/ ) ) - { - $result->{end_utc_epoch} = time::datetime_to_utc( $result->{end_datetime}, $config->{date}->{time_zone} ); - } - if ( ( defined $params->{template} ) - && ( $params->{template} =~ /(\.xml)/ ) ) - { - $result->{end_datetime_utc} = time::datetime_to_utc_datetime( $result->{end_datetime}, $config->{date}->{time_zone} ); - } + if ( ( defined $params->{template} ) + && ( $params->{template} =~ /(\.txt|\.json)/ ) ) + { + $result->{end_utc_epoch} = time::datetime_to_utc( $result->{end_datetime}, $config->{date}->{time_zone} ); + } + if ( ( defined $params->{template} ) + && ( $params->{template} =~ /(\.xml)/ ) ) + { + $result->{end_datetime_utc} = time::datetime_to_utc_datetime( $result->{end_datetime}, $config->{date}->{time_zone} ); + } - if ( ( defined $previous_result ) - && ( defined $previous_result->{start_date} ) - && ( $result->{start_date} ne $previous_result->{start_date} ) ) - { - $result->{is_first_of_day} = 1; - $previous_result->{is_last_of_day} = 1; - } + if ( ( defined $previous_result ) + && ( defined $previous_result->{start_date} ) + && ( $result->{start_date} ne $previous_result->{start_date} ) ) + { + $result->{is_first_of_day} = 1; + $previous_result->{is_last_of_day} = 1; + } - $result->{start_date_name} = - time::date_format( $result->{start_date}, $language ); - $result->{end_date_name} = - time::date_format( $result->{end_date}, $language ); + $result->{start_date_name} = + time::date_format( $result->{start_date}, $language ); + $result->{end_date_name} = + time::date_format( $result->{end_date}, $language ); - if ( $result->{start} =~ /(\d\d\:\d\d)\:\d\d/ ) { - $result->{start_time_name} = $1; - $result->{start_time} = $1; - } + if ( $result->{start} =~ /(\d\d\:\d\d)\:\d\d/ ) { + $result->{start_time_name} = $1; + $result->{start_time} = $1; + } - if ( $result->{end} =~ /(\d\d\:\d\d)\:\d\d/ ) { - $result->{end_time_name} = $1; - $result->{end_time} = $1; - } + if ( $result->{end} =~ /(\d\d\:\d\d)\:\d\d/ ) { + $result->{end_time_name} = $1; + $result->{end_time} = $1; + } - if ( defined $result->{weekday} ) { - my $language = $config::config->{date}->{language} || 'en'; + if ( defined $result->{weekday} ) { + my $language = $config::config->{date}->{language} || 'en'; - #my $weekdays=time::get_weekdays(); - my $weekday_index = $time::weekday_index->{ $result->{weekday} . '' } || 0; - $result->{weekday_name} = - $time::names->{$language}->{weekdays}->[$weekday_index]; - $result->{weekday_short_name} = - $time::names->{$language}->{weekdays_abbr}->[$weekday_index]; - } - return $result; + #my $weekdays=time::get_weekdays(); + my $weekday_index = $time::weekday_index->{ $result->{weekday} . '' } || 0; + $result->{weekday_name} = + $time::names->{$language}->{weekdays}->[$weekday_index]; + $result->{weekday_short_name} = + $time::names->{$language}->{weekdays_abbr}->[$weekday_index]; + } + return $result; } sub add_recordings { - my $dbh = shift; - my $config = shift; - my $request = shift; - my $events = shift; + my $dbh = shift; + my $config = shift; + my $request = shift; + my $events = shift; - # print STDERR Dumper($results); - return $events unless defined $events; + # print STDERR Dumper($results); + return $events unless defined $events; - my $params = $request->{params}->{checked}; - return $events unless defined $params; - return $events unless defined $params->{recordings}; + my $params = $request->{params}->{checked}; + return $events unless defined $params; + return $events unless defined $params->{recordings}; - my @ids = (); - my $eventsById = {}; + my @ids = (); + my $eventsById = {}; - #my $events = $results; + #my $events = $results; - for my $event (@$events) { - my $eventId = $event->{event_id}; - push @ids, $eventId; - $eventsById->{$eventId} = $event; - } + for my $event (@$events) { + my $eventId = $event->{event_id}; + push @ids, $eventId; + $eventsById->{$eventId} = $event; + } - my $qms = join( ', ', ( map { '?' } @$events ) ); - my $bindValues = join( ', ', ( map { $_->{event_id} } @$events ) ); + my $qms = join( ', ', ( map { '?' } @$events ) ); + my $bindValues = join( ', ', ( map { $_->{event_id} } @$events ) ); - my $query = qq{ + my $query = qq{ select * from calcms_audio_recordings where event_id in ($qms) order by created_at; }; - $dbh = db::connect($config) unless defined $dbh; - my $recordings = db::get( $dbh, $query, $bindValues ); + $dbh = db::connect($config) unless defined $dbh; + my $recordings = db::get( $dbh, $query, $bindValues ); - #print STDERR Dumper($recordings); + #print STDERR Dumper($recordings); - for my $entry (@$recordings) { - my $eventId = $entry->{event_id}; - my $event = $eventsById->{$eventId}; - push @{ $event->{recordings} }, $entry; - } + for my $entry (@$recordings) { + my $eventId = $entry->{event_id}; + my $event = $eventsById->{$eventId}; + push @{ $event->{recordings} }, $entry; + } - return $events; + return $events; } sub getDateQueryConditions { - my $config = shift; - my $params = shift; - my $bind_values = shift; + my $config = shift; + my $params = shift; + my $bind_values = shift; - # conditions by date - my $date_conds = []; + # conditions by date + my $date_conds = []; - #date, today, tomorrow, yesterday - my $date = ''; - $date = time::date_cond( $params->{date} ) if $params->{date} ne ''; + #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 $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 $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 $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 $till_time = ''; + $till_time = time::time_cond( $params->{till_time} ) if $params->{till_time} ne ''; - my $time = $params->{time}; - $time = '' unless defined $time; + my $time = $params->{time}; + $time = '' unless defined $time; - my $date_range_include = $params->{date_range_include}; - my $day_starting_hour = $config->{date}->{day_starting_hour}; + my $date_range_include = $params->{date_range_include}; + my $day_starting_hour = $config->{date}->{day_starting_hour}; - if ( $date eq 'today' ) { - my $date = time::get_event_date($config); - push @$date_conds, ' ( start_date = ? ) '; - push @$bind_values, $date; - return $date_conds; - } + if ( $date eq 'today' ) { + my $date = time::get_event_date($config); + push @$date_conds, ' ( start_date = ? ) '; + push @$bind_values, $date; + return $date_conds; + } - # given date - my $start = time::datetime_cond( $date . 'T00:00:00' ); - if ( $start ne '' ) { - $start = time::add_hours_to_datetime( $start, $day_starting_hour ); - my $end = time::add_hours_to_datetime( $start, 24 ); + # given date + my $start = time::datetime_cond( $date . 'T00:00:00' ); + if ( $start ne '' ) { + $start = time::add_hours_to_datetime( $start, $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 ( $date_range_include eq '1' ) { + push @$date_conds, ' end > ? '; + push @$bind_values, $start; + } else { + push @$date_conds, ' start >= ? '; + push @$bind_values, $start; + } - push @$date_conds, ' start < ? '; - push @$bind_values, $end; - return $date_conds; - } + push @$date_conds, ' start < ? '; + push @$bind_values, $end; + return $date_conds; + } - if ( $time eq 'now' ) { - push @$date_conds, qq{ + if ( $time eq 'now' ) { + push @$date_conds, qq{ ( ( unix_timestamp(end) > unix_timestamp(now() ) ) and ( unix_timestamp(start) <= unix_timestamp(now() ) ) ) }; - return $date_conds; - } + return $date_conds; + } - if ( $time eq 'future' ) { - push @$date_conds, qq{ + if ( $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; - } + return $date_conds; + } - #from_date and from_time is defined - if ( ( $from_date ne '' ) && ( $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 = ''; - } - } - } + #from_date and from_time is defined + if ( ( $from_date ne '' ) && ( $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_date and till_time is defined - if ( ( $till_date ne '' ) && ( $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 = ''; - } - } + #till_date and till_time is defined + if ( ( $till_date ne '' ) && ( $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 = ''; + } + } - # after start of daily broadcast - if ( ( $from_date ne '' ) && ( $from_time eq '' ) ) { - my $start = time::datetime_cond( $from_date . 'T00:00:00' ); - $start = time::add_hours_to_datetime( $start, $day_starting_hour ); + # after start of daily broadcast + if ( ( $from_date ne '' ) && ( $from_time eq '' ) ) { + my $start = time::datetime_cond( $from_date . 'T00:00:00' ); + $start = time::add_hours_to_datetime( $start, $day_starting_hour ); - if ( $date_range_include eq '1' ) { + if ( $date_range_include eq '1' ) { - # end is after start - push @$date_conds, ' ( end >= ? )'; - push @$bind_values, $start; - } else { - push @$date_conds, ' ( start >= ? ) '; - push @$bind_values, $start; - } - } + # end is after start + push @$date_conds, ' ( end >= ? )'; + push @$bind_values, $start; + } else { + push @$date_conds, ' ( start >= ? ) '; + push @$bind_values, $start; + } + } - # before end of daily broadcast - if ( ( $till_date ne '' ) && ( $till_time eq '' ) ) { - my $end = time::datetime_cond( $till_date . 'T00:00:00' ); - $end = time::add_hours_to_datetime( $end, $day_starting_hour ); - if ( $date_range_include eq '1' ) { + # before end of daily broadcast + if ( ( $till_date ne '' ) && ( $till_time eq '' ) ) { + my $end = time::datetime_cond( $till_date . 'T00:00:00' ); + $end = time::add_hours_to_datetime( $end, $day_starting_hour ); + if ( $date_range_include eq '1' ) { - # start is before end - push @$date_conds, ' ( start <= ? )'; - push @$bind_values, $end; - } else { - push @$date_conds, ' ( end <= ? ) '; - push @$bind_values, $end; - } - } + # start is before end + push @$date_conds, ' ( start <= ? )'; + push @$bind_values, $end; + } else { + push @$date_conds, ' ( end <= ? ) '; + push @$bind_values, $end; + } + } - if ( $params->{weekday} ne '' ) { - my $weekday = $params->{weekday}; - $weekday += 1; - $weekday -= 7 if ( $weekday > 7 ); - push @$date_conds, ' (dayofweek(start)= ?) '; - push @$bind_values, $weekday; - } + if ( $params->{weekday} ne '' ) { + my $weekday = $params->{weekday}; + $weekday += 1; + $weekday -= 7 if ( $weekday > 7 ); + push @$date_conds, ' (dayofweek(start)= ?) '; + push @$bind_values, $weekday; + } - if ( $params->{archive} eq 'past' ) { - my $date = time::get_event_date($config); - if ( $date ne '' ) { - push @$date_conds, ' ( start < ? ) '; - push @$bind_values, $date; - } + if ( $params->{archive} eq 'past' ) { + my $date = time::get_event_date($config); + if ( $date ne '' ) { + push @$date_conds, ' ( start < ? ) '; + push @$bind_values, $date; + } - } - if ( $params->{archive} eq 'future' ) { - my $date = time::get_event_date($config); - if ( $date ne '' ) { - push @$date_conds, ' ( end >= ? ) '; - push @$bind_values, $date; - } - } + } + if ( $params->{archive} eq 'future' ) { + my $date = time::get_event_date($config); + if ( $date ne '' ) { + push @$date_conds, ' ( end >= ? ) '; + push @$bind_values, $date; + } + } - return $date_conds; + return $date_conds; } # if recordings is set in params, recordings date and path will be included sub get_query { - my $dbh = shift; - my $config = shift; - my $request = shift; + my $dbh = shift; + my $config = shift; + my $request = shift; - my $params = $request->{params}->{checked}; - my $debug = $config->{system}->{debug}; + my $params = $request->{params}->{checked}; + my $debug = $config->{system}->{debug}; - $params->{recordings} = '' unless defined $params->{recordings}; + $params->{recordings} = '' unless defined $params->{recordings}; - my $bind_values = []; - my $where_cond = []; - my $order_cond = ''; - my $limit_cond = ''; + my $bind_values = []; + my $where_cond = []; + my $order_cond = ''; + my $limit_cond = ''; - if ( $params->{event_id} ne '' ) { + if ( $params->{event_id} ne '' ) { - # conditions by event id - push @$where_cond, 'e.id=?'; - $bind_values = [ $params->{event_id} ]; + # conditions by event id + push @$where_cond, 'e.id=?'; + $bind_values = [ $params->{event_id} ]; - #filter by published, default=1 to see published only, set published='all' to see all - my $published = $params->{published} || '1'; - if ( ( $published eq '0' ) || ( $published eq '1' ) ) { - push @$where_cond, 'published=?'; - push @$bind_values, $published; - } + #filter by published, default=1 to see published only, set published='all' to see all + my $published = $params->{published} || '1'; + if ( ( $published eq '0' ) || ( $published eq '1' ) ) { + push @$where_cond, 'published=?'; + push @$bind_values, $published; + } - my $draft = $params->{draft} || '0'; - if ( ( $draft eq '0' ) || ( $draft eq '1' ) ) { - push @$where_cond, 'draft=?'; - push @$bind_values, $draft; - } + my $draft = $params->{draft} || '0'; + if ( ( $draft eq '0' ) || ( $draft eq '1' ) ) { + push @$where_cond, 'draft=?'; + push @$bind_values, $draft; + } - } else { + } else { - my $date_conds = getDateQueryConditions( $config, $params, $bind_values ); - my $date_cond = join " and ", @$date_conds; + my $date_conds = getDateQueryConditions( $config, $params, $bind_values ); + 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 '' ); + } - # location - my $location_cond = ''; - if ( $params->{location} ne '' ) { - my $location = ( split( /\,/, $params->{location} ) )[0]; - $location =~ s/[^a-zA-Z0-9]/%/g; - $location =~ s/%{2,99}/%/g; - if ( $location ne '' ) { - $location_cond = ' location like ? '; - push @$bind_values, $location; - } - } + # location + my $location_cond = ''; + if ( $params->{location} ne '' ) { + my $location = ( split( /\,/, $params->{location} ) )[0]; + $location =~ s/[^a-zA-Z0-9]/%/g; + $location =~ s/%{2,99}/%/g; + if ( $location ne '' ) { + $location_cond = ' location like ? '; + push @$bind_values, $location; + } + } - # exclude location - my $exclude_location_cond = ''; - if ( $params->{exclude_locations} eq '1' ) { - if ( $params->{locations_to_exclude} ne '' ) { - my @locations_to_exclude = split( /,/, $params->{locations_to_exclude} ); - $exclude_location_cond = 'location not in (' . join( ",", map { '?' } @locations_to_exclude ) . ')'; - for my $location (@locations_to_exclude) { - $location =~ s/^\s+//g; - $location =~ s/\s+$//g; - push @$bind_values, $location; - } - } - } + # exclude location + my $exclude_location_cond = ''; + if ( $params->{exclude_locations} eq '1' ) { + if ( $params->{locations_to_exclude} ne '' ) { + my @locations_to_exclude = split( /,/, $params->{locations_to_exclude} ); + $exclude_location_cond = 'location not in (' . join( ",", map { '?' } @locations_to_exclude ) . ')'; + for my $location (@locations_to_exclude) { + $location =~ s/^\s+//g; + $location =~ s/\s+$//g; + push @$bind_values, $location; + } + } + } - # exclude project - my $exclude_project_cond = ''; - if ( $params->{exclude_projects} eq '1' ) { - if ( $params->{projects_to_exclude} ne '' ) { - my @projects_to_exclude = split( /,/, $params->{projects_to_exclude} ); - $exclude_project_cond = 'project not in (' . join( ",", map { '?' } @projects_to_exclude ) . ')'; - for my $project (@projects_to_exclude) { - $project =~ s/^\s+//g; - $project =~ s/\s+$//g; - push @$bind_values, $project; - } - } - } + # exclude project + my $exclude_project_cond = ''; + if ( $params->{exclude_projects} eq '1' ) { + if ( $params->{projects_to_exclude} ne '' ) { + my @projects_to_exclude = split( /,/, $params->{projects_to_exclude} ); + $exclude_project_cond = 'project not in (' . join( ",", map { '?' } @projects_to_exclude ) . ')'; + for my $project (@projects_to_exclude) { + $project =~ s/^\s+//g; + $project =~ s/\s+$//g; + push @$bind_values, $project; + } + } + } - #filter for category - my $category_cond = ''; - if ( $params->{category} ne '' ) { - my $category = ( split( /\,/, $params->{category} ) )[0]; - $category =~ s/[^a-zA-Z0-9]/%/g; - $category =~ s/%{2,99}/%/g; - if ( $category ne '' ) { - $category_cond = qq{ + #filter for category + my $category_cond = ''; + if ( $params->{category} ne '' ) { + my $category = ( split( /\,/, $params->{category} ) )[0]; + $category =~ s/[^a-zA-Z0-9]/%/g; + $category =~ s/%{2,99}/%/g; + if ( $category ne '' ) { + $category_cond = qq{ id in( select event_id from calcms_categories where name like ? ) }; - } - push @$bind_values, $category; - } + } + push @$bind_values, $category; + } - my $series_name_cond = ''; - if ( ( defined $params->{series_name} ) - && ( $params->{series_name} ne '' ) ) - { - my $series_name = ( split( /\,/, $params->{series_name} ) )[0]; - $series_name =~ s/[^a-zA-Z0-9]/%/g; - $series_name =~ s/%{2,99}/%/g; - if ( $series_name ne '' ) { - $series_name_cond = ' series_name like ? '; - push @$bind_values, $series_name; - } - } + my $series_name_cond = ''; + if ( ( defined $params->{series_name} ) + && ( $params->{series_name} ne '' ) ) + { + my $series_name = ( split( /\,/, $params->{series_name} ) )[0]; + $series_name =~ s/[^a-zA-Z0-9]/%/g; + $series_name =~ s/%{2,99}/%/g; + if ( $series_name ne '' ) { + $series_name_cond = ' series_name like ? '; + push @$bind_values, $series_name; + } + } - #filter for tags - my $tag_cond = ''; - if ( ( defined $params->{tag} ) && ( $params->{tag} ne '' ) ) { - my @tags = ( split( /\,/, $params->{tag} ) ); - if ( scalar @tags > 0 ) { - my $tags = join ",", ( map { '?' } @tags ); - for my $tag (@tags) { - push @$bind_values, $tag; - } - $tag_cond = qq{ + #filter for tags + my $tag_cond = ''; + if ( ( defined $params->{tag} ) && ( $params->{tag} ne '' ) ) { + my @tags = ( split( /\,/, $params->{tag} ) ); + if ( scalar @tags > 0 ) { + my $tags = join ",", ( map { '?' } @tags ); + for my $tag (@tags) { + push @$bind_values, $tag; + } + $tag_cond = qq{ id in( select event_id from calcms_tags where name in($tags) ) }; - } - } - $tag_cond = ''; + } + } + $tag_cond = ''; - my $title_cond = ''; - if ( ( defined $params->{title} ) && ( $params->{title} ne '' ) ) { - my $title = ( split( /\,/, $params->{title} ) )[0]; - $title =~ s/[^a-zA-Z0-9]/%/g; - $title =~ s/%{2,99}/%/g; - $title =~ s/^\%//; - $title =~ s/\%$//; - $title = '%' . $title . '%'; - if ( $title ne '' ) { - $title_cond = ' title like ? '; - push @$bind_values, $title; - } - } + my $title_cond = ''; + if ( ( defined $params->{title} ) && ( $params->{title} ne '' ) ) { + my $title = ( split( /\,/, $params->{title} ) )[0]; + $title =~ s/[^a-zA-Z0-9]/%/g; + $title =~ s/%{2,99}/%/g; + $title =~ s/^\%//; + $title =~ s/\%$//; + $title = '%' . $title . '%'; + if ( $title ne '' ) { + $title_cond = ' title like ? '; + push @$bind_values, $title; + } + } - my $search_cond = ''; - if ( ( defined $params->{search} ) && ( $params->{search} ne '' ) ) { - my $search = lc $params->{search}; - $search =~ s/(?=[\\%_])/\\/g; - $search =~ s/^[\%\s]+//; - $search =~ s/[\%\s]+$//; - if ( $search ne '' ) { - $search = '%' . $search . '%'; - my @attr = ( 'title', 'series_name', 'excerpt', 'category', 'content', 'topic' ); - $search_cond = "(" . join( " or ", map { 'lower(' . $_ . ') like ?' } @attr ) . ")"; - for my $attr (@attr) { - push @$bind_values, $search; - } - } - } + my $search_cond = ''; + if ( ( defined $params->{search} ) && ( $params->{search} ne '' ) ) { + my $search = lc $params->{search}; + $search =~ s/(?=[\\%_])/\\/g; + $search =~ s/^[\%\s]+//; + $search =~ s/[\%\s]+$//; + if ( $search ne '' ) { + $search = '%' . $search . '%'; + my @attr = ( 'title', 'series_name', 'excerpt', 'category', 'content', 'topic' ); + $search_cond = "(" . join( " or ", map { 'lower(' . $_ . ') like ?' } @attr ) . ")"; + for my $attr (@attr) { + push @$bind_values, $search; + } + } + } - #print STDERR $search_cond."\n"; + my $project_cond = ''; - my $project_cond = ''; + my $project = undef; + $project = $params->{project} + if ( defined $params->{project} ) && ( $params->{project} ne '' ); - # if ($filter_by_project eq'1'){ - my $project = undef; #$params->{default_project}; - $project = $params->{project} - if ( ( defined $params->{project} ) && ( $params->{project} ne '' ) ); - my $project_name = ''; - $project_name = $project->{name} - if ( ( defined $project ) - && ( defined $project->{name} ) - && ( $project->{name} ne '' ) ); - if ( ( $project_name ne '' ) && ( $project_name ne 'all' ) ) { - $project_cond = '(project=?)'; - push @$bind_values, $project_name; - } + my $project_name = ''; + $project_name = $project->{name} + if ( defined $project ) + && ( defined $project->{name} ) + && ( $project->{name} ne '' ); - # } + if ( ( $project_name ne '' ) && ( $project_name ne 'all' ) ) { + $project_cond = '(project=?)'; + push @$bind_values, $project_name; + } - #filter by published, default =1, set to 'all' to see all - my $published_cond = ''; - my $published = $params->{published} || '1'; - if ( ( $published eq '0' ) || ( $published eq '1' ) ) { - $published_cond = 'published=?'; - push @$bind_values, $published; - } + #filter by published, default =1, set to 'all' to see all + my $published_cond = ''; + my $published = $params->{published} || '1'; + if ( ( $published eq '0' ) || ( $published eq '1' ) ) { + $published_cond = 'published=?'; + push @$bind_values, $published; + } - #filter by draft, default =1, set to 'all' to see all - my $draft_cond = ''; - my $draft = $params->{draft} || '0'; - if ( ( $draft eq '0' ) || ( $draft eq '1' ) ) { - $draft_cond = 'draft=?'; - push @$bind_values, $draft; - } + #filter by draft, default =1, set to 'all' to see all + my $draft_cond = ''; + my $draft = $params->{draft} || '0'; + if ( ( $draft eq '0' ) || ( $draft eq '1' ) ) { + $draft_cond = 'draft=?'; + push @$bind_values, $draft; + } - my $disable_event_sync_cond = ''; - my $disable_event_sync = $params->{disable_event_sync} || ''; - if ( ( $disable_event_sync eq '0' ) || ( $disable_event_sync eq '1' ) ) { - $disable_event_sync_cond = 'disable_event_sync=?'; - push @$bind_values, $disable_event_sync; - } + my $disable_event_sync_cond = ''; + my $disable_event_sync = $params->{disable_event_sync} || ''; + if ( ( $disable_event_sync eq '0' ) || ( $disable_event_sync eq '1' ) ) { + $disable_event_sync_cond = 'disable_event_sync=?'; + push @$bind_values, $disable_event_sync; + } - #print STDERR $disable_event_sync_cond." ".$bind_values->[-1]."\n"; + #print STDERR $disable_event_sync_cond." ".$bind_values->[-1]."\n"; - #combine date, location, category, series_name, tag, search and project + #combine date, location, category, series_name, tag, search and project - push @$where_cond, $location_cond if ( $location_cond =~ /\S/ ); - push @$where_cond, $exclude_location_cond - if ( $exclude_location_cond =~ /\S/ ); - push @$where_cond, $exclude_project_cond - if ( $exclude_project_cond =~ /\S/ ); - push @$where_cond, $category_cond if ( $category_cond =~ /\S/ ); - push @$where_cond, $series_name_cond if ( $series_name_cond =~ /\S/ ); - push @$where_cond, $tag_cond if ( $tag_cond =~ /\S/ ); - push @$where_cond, $title_cond if ( $title_cond =~ /\S/ ); - push @$where_cond, $search_cond if ( $search_cond =~ /\S/ ); - push @$where_cond, $project_cond if ( $project_cond =~ /\S/ ); - push @$where_cond, $published_cond if ( $published_cond =~ /\S/ ); - push @$where_cond, $draft_cond if ( $draft_cond =~ /\S/ ); - push @$where_cond, $disable_event_sync_cond - if ( $disable_event_sync_cond ne '' ); + push @$where_cond, $location_cond if ( $location_cond =~ /\S/ ); + push @$where_cond, $exclude_location_cond + if ( $exclude_location_cond =~ /\S/ ); + push @$where_cond, $exclude_project_cond + if ( $exclude_project_cond =~ /\S/ ); + push @$where_cond, $category_cond if ( $category_cond =~ /\S/ ); + push @$where_cond, $series_name_cond if ( $series_name_cond =~ /\S/ ); + push @$where_cond, $tag_cond if ( $tag_cond =~ /\S/ ); + push @$where_cond, $title_cond if ( $title_cond =~ /\S/ ); + push @$where_cond, $search_cond if ( $search_cond =~ /\S/ ); + push @$where_cond, $project_cond if ( $project_cond =~ /\S/ ); + push @$where_cond, $published_cond if ( $published_cond =~ /\S/ ); + push @$where_cond, $draft_cond if ( $draft_cond =~ /\S/ ); + push @$where_cond, $disable_event_sync_cond + if ( $disable_event_sync_cond ne '' ); - #print STDERR Dumper($where_cond); + #print STDERR Dumper($where_cond); - #order is forced - if ( $params->{order} eq 'asc' ) { - $order_cond = 'order by start'; - } elsif ( $params->{order} eq 'desc' ) { - $order_cond = 'order by start desc'; - } else { + #order is forced + if ( $params->{order} eq 'asc' ) { + $order_cond = 'order by start'; + } elsif ( $params->{order} eq 'desc' ) { + $order_cond = 'order by start desc'; + } else { - #derivate order from archive flag - if ( $params->{archive} eq 'past' ) { - $order_cond = 'order by start desc'; - } else { - $order_cond = 'order by start'; - } - } + #derivate order from archive flag + if ( $params->{archive} eq 'past' ) { + $order_cond = 'order by start desc'; + } else { + $order_cond = 'order by start'; + } + } - if ( ( defined $params->{limit} ne '' ) && ( $params->{limit} ne '' ) ) { - $limit_cond = 'limit ' . $params->{limit}; - } + if ( ( defined $params->{limit} ne '' ) && ( $params->{limit} ne '' ) ) { + $limit_cond = 'limit ' . $params->{limit}; + } - my $query = qq{ + my $query = qq{ select date(e.start) start_date ,date(e.end) end_date @@ -1062,183 +1059,183 @@ sub get_query { ,e.disable_event_sync ,e.episode }; - my $template = $params->{template} || ''; + my $template = $params->{template} || ''; - $query .= ',e.excerpt' unless ( $template =~ /menu/ ); + $query .= ',e.excerpt' unless ( $template =~ /menu/ ); - # $query.=',e.project' unless ($template=~/menu/ || $template=~/list/); + # $query.=',e.project' unless ($template=~/menu/ || $template=~/list/); - my $get = $params->{get} || ''; - unless ( $get eq 'no_content' ) { - if ( $template =~ /\.html/ ) { - unless ( $template =~ /menu/ || $template =~ /list/ ) { - $query .= ', e.content, e.topic, e.html_content, e.html_topic'; + my $get = $params->{get} || ''; + unless ( $get eq 'no_content' ) { + if ( $template =~ /\.html/ ) { + unless ( $template =~ /menu/ || $template =~ /list/ ) { + $query .= ', e.content, e.topic, e.html_content, e.html_topic'; - #$query.=',html_content content, html_topic topic' ; - } - } else { - unless ( $template =~ /menu/ || $template =~ /list/ ) { - $query .= ', e.content, e.topic, e.html_content, e.html_topic'; - } - } - } + #$query.=',html_content content, html_topic topic' ; + } + } else { + unless ( $template =~ /menu/ || $template =~ /list/ ) { + $query .= ', e.content, e.topic, e.html_content, e.html_topic'; + } + } + } - # add project id and series id - if ( ( $params->{project_id} ne '' ) || ( $params->{studio_id} ne '' ) ) { - if ( $params->{project_id} ne '' ) { - push @$where_cond, 'se.project_id=?'; - push @$bind_values, $params->{project_id}; - $query .= ', se.project_id'; - } - if ( $params->{studio_id} ne '' ) { - push @$where_cond, 'se.studio_id=?'; - push @$bind_values, $params->{studio_id}; - $query .= ', se.studio_id'; - } + # add project id and series id + if ( ( $params->{project_id} ne '' ) || ( $params->{studio_id} ne '' ) ) { + if ( $params->{project_id} ne '' ) { + push @$where_cond, 'se.project_id=?'; + push @$bind_values, $params->{project_id}; + $query .= ', se.project_id'; + } + if ( $params->{studio_id} ne '' ) { + push @$where_cond, 'se.studio_id=?'; + push @$bind_values, $params->{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 - if ( $params->{recordings} eq '1' ) { - $query .= ', ar.path'; - $query .= ', ar.size'; - $query .= ', ar.created_by uploaded_by'; - $query .= ', ar.modified_at uploaded_at'; + # add recordings field and conditions + if ( $params->{recordings} eq '1' ) { + $query .= ', ar.path'; + $query .= ', ar.size'; + $query .= ', ar.created_by uploaded_by'; + $query .= ', ar.modified_at uploaded_at'; - #push @$where_cond, 'e.id=ar.event_id'; - } + #push @$where_cond, 'e.id=ar.event_id'; + } - $query .= "\n from"; + $query .= "\n from"; - # add tables - if ( ( $params->{project_id} ne '' ) || ( $params->{studio_id} ne '' ) ) { + # add tables + if ( ( $params->{project_id} ne '' ) || ( $params->{studio_id} ne '' ) ) { - # prepent series_events - $query .= "\n calcms_series_events se inner join calcms_events e on se.event_id=e.id"; - } else { - $query .= "\n calcms_events e"; - } + # prepent series_events + $query .= "\n calcms_series_events se inner join calcms_events e on se.event_id=e.id"; + } else { + $query .= "\n calcms_events e"; + } - # add recordings table - if ( $params->{recordings} eq '1' ) { - $query .= "\n left join calcms_audio_recordings ar on e.id=ar.event_id"; - } + # add recordings table + if ( $params->{recordings} eq '1' ) { + $query .= "\n left join calcms_audio_recordings ar on e.id=ar.event_id"; + } - if ( scalar @$where_cond > 0 ) { - $query .= "\nwhere " . join( ' and ', @$where_cond ); - } + if ( scalar @$where_cond > 0 ) { + $query .= "\nwhere " . join( ' and ', @$where_cond ); + } - $query .= "\n" . $order_cond if ( $order_cond ne '' ); - $query .= "\n" . $limit_cond if ( $limit_cond ne '' ); + $query .= "\n" . $order_cond if ( $order_cond ne '' ); + $query .= "\n" . $limit_cond if ( $limit_cond ne '' ); - #print STDERR Dumper($query).Dumper($bind_values)."\n"; + #print STDERR Dumper($query).Dumper($bind_values)."\n"; - return ( \$query, $bind_values ); + return ( \$query, $bind_values ); } sub render { - # my $response = $_[0]; - my $config = $_[1]; - my $request = $_[2]; - my $results = $_[3]; - my $root_params = $_[4]; + # my $response = $_[0]; + my $config = $_[1]; + my $request = $_[2]; + my $results = $_[3]; + my $root_params = $_[4]; - my $params = $request->{params}->{checked}; - if ( ref($root_params) eq 'HASH' ) { - for my $param ( keys %$root_params ) { - $params->{$param} = $root_params->{$param}; - } - } - my $debug = $config->{system}->{debug}; + my $params = $request->{params}->{checked}; + if ( ref($root_params) eq 'HASH' ) { + for my $param ( keys %$root_params ) { + $params->{$param} = $root_params->{$param}; + } + } + my $debug = $config->{system}->{debug}; - my %template_parameters = %$params; - my $template_parameters = \%template_parameters; - $template_parameters->{events} = $results; - $template_parameters->{debug} = $debug; - $template_parameters->{server_cache} = $config->{cache}->{server_cache} - if ( $config->{cache}->{server_cache} ); - $template_parameters->{use_client_cache} = $config->{cache}->{use_client_cache} - if ( $config->{cache}->{use_client_cache} ); + my %template_parameters = %$params; + my $template_parameters = \%template_parameters; + $template_parameters->{events} = $results; + $template_parameters->{debug} = $debug; + $template_parameters->{server_cache} = $config->{cache}->{server_cache} + if ( $config->{cache}->{server_cache} ); + $template_parameters->{use_client_cache} = $config->{cache}->{use_client_cache} + if ( $config->{cache}->{use_client_cache} ); - if ( scalar @$results > 0 ) { - my $result = $results->[0]; - $template_parameters->{event_id} = $result->{event_id}; - $template_parameters->{event_dtstart} = $result->{dtstart}; - } + if ( scalar @$results > 0 ) { + my $result = $results->[0]; + $template_parameters->{event_id} = $result->{event_id}; + $template_parameters->{event_dtstart} = $result->{dtstart}; + } - # $template_parameters->{print} =1 if ($params->{print} eq '1'); - $template_parameters->{base_url} = $config->{locations}->{base_url}; - $template_parameters->{base_domain} = $config->{locations}->{base_domain}; - $template_parameters->{cache_base_url} = $config->{cache}->{base_url}; - $template_parameters->{modified_at} = time::time_to_datetime( time() ); - if ( ( defined $params->{template} ) - && ( $params->{template} =~ /(\.xml)/ ) ) - { - $template_parameters->{modified_at_datetime_utc} = - time::datetime_to_utc_datetime( $template_parameters->{modified_at}, $config->{date}->{time_zone} ); - } + # $template_parameters->{print} =1 if ($params->{print} eq '1'); + $template_parameters->{base_url} = $config->{locations}->{base_url}; + $template_parameters->{base_domain} = $config->{locations}->{base_domain}; + $template_parameters->{cache_base_url} = $config->{cache}->{base_url}; + $template_parameters->{modified_at} = time::time_to_datetime( time() ); + if ( ( defined $params->{template} ) + && ( $params->{template} =~ /(\.xml)/ ) ) + { + $template_parameters->{modified_at_datetime_utc} = + time::datetime_to_utc_datetime( $template_parameters->{modified_at}, $config->{date}->{time_zone} ); + } - #$template_parameters->{tags} = $tags; + #$template_parameters->{tags} = $tags; - if ( @$results == 0 ) { - if ( ( $params->{search} ne '' ) - || ( $params->{category} ne '' ) - || ( $params->{series_name} ne '' ) ) - { - $template_parameters->{no_search_result} = '1'; - } else { - $template_parameters->{no_result} = '1'; - } - } else { - if ( ( !defined $params->{event_id} ) || ( $params->{event_id} eq '' ) ) { - $template_parameters->{event_count} = @$results . ''; - $template_parameters->{first_of_list} = $results->[0]->{event_id}; - } - my $start = $results->[0]->{start_datetime} || ''; - if ( $start =~ /(\d{4}\-\d{2})/ ) { - $template_parameters->{month} = $1; - } - } + if ( scalar @$results == 0 ) { + if ( ( $params->{search} ne '' ) + || ( $params->{category} ne '' ) + || ( $params->{series_name} ne '' ) ) + { + $template_parameters->{no_search_result} = '1'; + } else { + $template_parameters->{no_result} = '1'; + } + } else { + if ( ( !defined $params->{event_id} ) || ( $params->{event_id} eq '' ) ) { + $template_parameters->{event_count} = scalar @$results . ''; + $template_parameters->{first_of_list} = $results->[0]->{event_id}; + } + my $start = $results->[0]->{start_datetime} || ''; + if ( $start =~ /(\d{4}\-\d{2})/ ) { + $template_parameters->{month} = $1; + } + } - my $time_diff = time::utc_offset( $config->{date}->{time_zone} ); - $time_diff =~ s/(\d\d)(\d\d)/$1\:$2/g; - $template_parameters->{time_zone} = $config->{date}->{time_zone}; - $template_parameters->{utc_offset} = $time_diff; + my $time_diff = time::utc_offset( $config->{date}->{time_zone} ); + $time_diff =~ s/(\d\d)(\d\d)/$1\:$2/g; + $template_parameters->{time_zone} = $config->{date}->{time_zone}; + $template_parameters->{utc_offset} = $time_diff; - if ( $params->{template} =~ /\.atom\.xml/ ) { - $template_parameters->{modified_at} =~ s/ /T/gi; - $template_parameters->{modified_at} .= $time_diff; - } elsif ( $params->{template} =~ /\.rss\.xml/ ) { - $template_parameters->{modified_at} = - time::datetime_to_rfc822( $template_parameters->{modified_at} ); - } elsif ( $params->{template} =~ /\.txt/ ) { - $template_parameters->{modified_at_utc} = - time::datetime_to_utc( $template_parameters->{modified_at}, $config->{date}->{time_zone} ); - } + if ( $params->{template} =~ /\.atom\.xml/ ) { + $template_parameters->{modified_at} =~ s/ /T/gi; + $template_parameters->{modified_at} .= $time_diff; + } elsif ( $params->{template} =~ /\.rss\.xml/ ) { + $template_parameters->{modified_at} = + time::datetime_to_rfc822( $template_parameters->{modified_at} ); + } elsif ( $params->{template} =~ /\.txt/ ) { + $template_parameters->{modified_at_utc} = + time::datetime_to_utc( $template_parameters->{modified_at}, $config->{date}->{time_zone} ); + } - my $project = $params->{default_project}; - foreach my $key ( keys %$project ) { - $template_parameters->{ 'project_' . $key } = $project->{$key}; - } - $template_parameters->{ 'project_' . $project->{name} } = 1 - if ( $project->{name} ne '' ); + my $project = $params->{default_project}; + foreach my $key ( keys %$project ) { + $template_parameters->{ 'project_' . $key } = $project->{$key}; + } + $template_parameters->{ 'project_' . $project->{name} } = 1 + if ( $project->{name} ne '' ); - $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->{controllers} = $config->{controllers}; + $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"; - template::process( $_[0], $params->{template}, $template_parameters ); + # use Data::Dumper;print STDERR Dumper($template_parameters)."\n"; + template::process( $_[0], $params->{template}, $template_parameters ); - return $_[0]; + return $_[0]; } sub get_running_event_id { - my $dbh = shift; + my $dbh = shift; - my $query = qq{ + my $query = qq{ select id event_id, start, title from calcms_events where @@ -1254,106 +1251,106 @@ sub get_running_event_id { limit 1 }; - my $running_events = db::get( $dbh, $query ); - my @running_events = @$running_events; + my $running_events = db::get( $dbh, $query ); + my @running_events = @$running_events; - return $running_events->[0]->{event_id} if ( scalar @running_events > 0 ); - return 0; + return $running_events->[0]->{event_id} if ( scalar @running_events > 0 ); + return 0; } # add filters to query sub setDefaultEventConditions { - my $config = shift; - my $conditions = $_[0]; - my $bind_values = $_[1]; - my $options = $_[2]; - $options = {} unless defined $options; + my $config = shift; + my $conditions = $_[0]; + my $bind_values = $_[1]; + my $options = $_[2]; + $options = {} unless defined $options; - # exclude projects - if ( ( defined $options->{exclude_projects} ) - && ( $options->{exclude_projects} == 1 ) - && ( defined $config->{filter} ) - && ( defined $config->{filter}->{projects_to_exclude} ) ) - { - my @projects_to_exclude = - split( /,/, $config->{filter}->{projects_to_exclude} ); - push @$conditions, 'project not in (' . join( ",", map { '?' } @projects_to_exclude ) . ')'; - for my $project (@projects_to_exclude) { - push @$bind_values, $project; - } - } + # exclude projects + if ( ( defined $options->{exclude_projects} ) + && ( $options->{exclude_projects} == 1 ) + && ( defined $config->{filter} ) + && ( defined $config->{filter}->{projects_to_exclude} ) ) + { + my @projects_to_exclude = + split( /,/, $config->{filter}->{projects_to_exclude} ); + push @$conditions, 'project not in (' . join( ",", map { '?' } @projects_to_exclude ) . ')'; + for my $project (@projects_to_exclude) { + push @$bind_values, $project; + } + } - # exclude locations - if ( ( defined $options->{exclude_locations} ) - && ( $options->{exclude_locations} == 1 ) - && ( defined $config->{filter} ) - && ( defined $config->{filter}->{locations_to_exclude} ) ) - { - my @locations_to_exclude = - split( /,/, $config->{filter}->{locations_to_exclude} ); - push @$conditions, 'location not in (' . join( ",", map { '?' } @locations_to_exclude ) . ')'; - for my $location (@locations_to_exclude) { - push @$bind_values, $location; - } - } + # exclude locations + if ( ( defined $options->{exclude_locations} ) + && ( $options->{exclude_locations} == 1 ) + && ( defined $config->{filter} ) + && ( defined $config->{filter}->{locations_to_exclude} ) ) + { + my @locations_to_exclude = + split( /,/, $config->{filter}->{locations_to_exclude} ); + push @$conditions, 'location not in (' . join( ",", map { '?' } @locations_to_exclude ) . ')'; + for my $location (@locations_to_exclude) { + push @$bind_values, $location; + } + } } # for local use only or add support for exclude_projects and exclude_locations sub getEventById { - my $dbh = shift; - my $config = shift; - my $event_id = shift; - my $options = shift; + my $dbh = shift; + my $config = shift; + my $event_id = shift; + my $options = shift; - $dbh = db::connect($config) unless defined $dbh; + $dbh = db::connect($config) unless defined $dbh; - my $conditions = []; - my $bind_values = []; + my $conditions = []; + my $bind_values = []; - push @$conditions, "id=?"; - push @$bind_values, $event_id; + push @$conditions, "id=?"; + push @$bind_values, $event_id; - setDefaultEventConditions( $config, $conditions, $bind_values, $options ); - $conditions = join( ' and ', @$conditions ); + setDefaultEventConditions( $config, $conditions, $bind_values, $options ); + $conditions = join( ' and ', @$conditions ); - my $query = qq{ + my $query = qq{ select * from calcms_events where $conditions }; - my $events = db::get( $dbh, $query, $bind_values ); - return $events; + my $events = db::get( $dbh, $query, $bind_values ); + return $events; } sub get_next_event_of_series { - my $dbh = shift; - my $config = shift; - my $options = shift; + my $dbh = shift; + my $config = shift; + my $options = shift; - my $eventId = $options->{event_id}; - return undef unless defined $eventId; + my $eventId = $options->{event_id}; + return undef unless defined $eventId; - $dbh = db::connect($config) unless defined $dbh; + $dbh = db::connect($config) unless defined $dbh; - my $events = getEventById( $dbh, $config, $eventId, $options ); - return undef unless scalar(@$events) == 1; - my $event = $events->[0]; + my $events = getEventById( $dbh, $config, $eventId, $options ); + return undef unless scalar(@$events) == 1; + my $event = $events->[0]; - my $conditions = []; - my $bind_values = []; + my $conditions = []; + my $bind_values = []; - push @$conditions, "start>?"; - push @$bind_values, $event->{start}; + push @$conditions, "start>?"; + push @$bind_values, $event->{start}; - push @$conditions, "series_name=?"; - push @$bind_values, $event->{series_name}; + push @$conditions, "series_name=?"; + push @$bind_values, $event->{series_name}; - setDefaultEventConditions( $config, $conditions, $bind_values, $options ); - $conditions = join( ' and ', @$conditions ); + setDefaultEventConditions( $config, $conditions, $bind_values, $options ); + $conditions = join( ' and ', @$conditions ); - my $query = qq{ + my $query = qq{ select id from calcms_events where $conditions @@ -1361,509 +1358,509 @@ sub get_next_event_of_series { limit 1 }; - $events = db::get( $dbh, $query, $bind_values ); - return undef unless @$events == 1; + $events = db::get( $dbh, $query, $bind_values ); + return undef unless scalar @$events == 1; - return $events->[0]->{id}; + return $events->[0]->{id}; } sub get_previous_event_of_series { - my $dbh = shift; - my $config = shift; - my $options = shift; + my $dbh = shift; + my $config = shift; + my $options = shift; - my $eventId = $options->{event_id}; - return undef unless defined $eventId; + my $eventId = $options->{event_id}; + return undef unless defined $eventId; - $dbh = db::connect($config) unless defined $dbh; - my $events = getEventById( $dbh, $config, $eventId, $options ); - return undef unless scalar(@$events) == 1; - my $event = $events->[0]; + $dbh = db::connect($config) unless defined $dbh; + my $events = getEventById( $dbh, $config, $eventId, $options ); + return undef unless scalar(@$events) == 1; + my $event = $events->[0]; - my $conditions = []; - my $bind_values = []; + my $conditions = []; + my $bind_values = []; - push @$conditions, "start{start}; + push @$conditions, "start{start}; - push @$conditions, "series_name=?"; - push @$bind_values, $event->{series_name}; + push @$conditions, "series_name=?"; + push @$bind_values, $event->{series_name}; - setDefaultEventConditions( $config, $conditions, $bind_values, $options ); - $conditions = join( ' and ', @$conditions ); + setDefaultEventConditions( $config, $conditions, $bind_values, $options ); + $conditions = join( ' and ', @$conditions ); - my $query = qq{ + my $query = qq{ select id from calcms_events where $conditions order by start desc limit 1 }; - $events = db::get( $dbh, $query, $bind_values ); + $events = db::get( $dbh, $query, $bind_values ); - return undef unless scalar(@$events) == 1; - return $events->[0]->{id}; + return undef unless scalar(@$events) == 1; + return $events->[0]->{id}; } # used by calendar sub get_by_date_range { - my $dbh = shift; - my $config = shift; - my $start_date = shift; - my $end_date = shift; - my $options = shift; + my $dbh = shift; + my $config = shift; + my $start_date = shift; + my $end_date = shift; + my $options = shift; - my $day_starting_hour = $config->{date}->{day_starting_hour}; + my $day_starting_hour = $config->{date}->{day_starting_hour}; - my $start = time::datetime_cond( $start_date . 'T00:00:00' ); - $start = time::add_hours_to_datetime( $start, $day_starting_hour ); + my $start = time::datetime_cond( $start_date . 'T00:00:00' ); + $start = time::add_hours_to_datetime( $start, $day_starting_hour ); - my $end = time::datetime_cond( $end_date . 'T00:00:00' ); - $end = time::add_hours_to_datetime( $end, $day_starting_hour ); + my $end = time::datetime_cond( $end_date . 'T00:00:00' ); + $end = time::add_hours_to_datetime( $end, $day_starting_hour ); - my $conditions = []; - push @$conditions, 'start between ? and ?'; - my $bind_values = [ $start, $end ]; + my $conditions = []; + push @$conditions, 'start between ? and ?'; + my $bind_values = [ $start, $end ]; - setDefaultEventConditions( $config, $conditions, $bind_values, $options ); + setDefaultEventConditions( $config, $conditions, $bind_values, $options ); - $conditions = join( ' and ', @$conditions ); + $conditions = join( ' and ', @$conditions ); - my $select = qq{distinct date(start) 'start_date'}; - $select = qq{distinct date(DATE_SUB(start, INTERVAL $day_starting_hour HOUR)) 'start_date'} if defined $day_starting_hour; + my $select = qq{distinct date(start) 'start_date'}; + $select = qq{distinct date(DATE_SUB(start, INTERVAL $day_starting_hour HOUR)) 'start_date'} if defined $day_starting_hour; - my $query = qq{ + my $query = qq{ select $select from calcms_events where $conditions }; - #print STDERR Dumper($query); + #print STDERR Dumper($query); - my $events = db::get( $dbh, $query, $bind_values ); + my $events = db::get( $dbh, $query, $bind_values ); - return $events; + return $events; } sub get_by_image { - my $dbh = shift; - my $config = shift; - my $filename = shift; + my $dbh = shift; + my $config = shift; + my $filename = shift; - #$filename=$dbh->quote('%'.$filename.'%'); + #$filename=$dbh->quote('%'.$filename.'%'); - #$filename='%'.$filename.'%'; - my $query = qq{ + #$filename='%'.$filename.'%'; + my $query = qq{ select * from calcms_events where content like ? order by start desc limit 1 }; - my $bind_values = [ '%' . $filename . '%' ]; + my $bind_values = [ '%' . $filename . '%' ]; - my $events = db::get( $dbh, $query, $bind_values ); + my $events = db::get( $dbh, $query, $bind_values ); - # print STDERR Dumper($events); - return undef if ( @$events == 0 ); - return $events->[0]; + # print STDERR Dumper($events); + return undef if scalar @$events == 0; + return $events->[0]; } sub delete { - return; - my $request = shift; - my $config = shift; - my $event_id = shift; + return; + my $request = shift; + my $config = shift; + my $event_id = shift; - my $params = $request->{params}->{checked}; - my $debug = $config->{system}->{debug}; + my $params = $request->{params}->{checked}; + my $debug = $config->{system}->{debug}; - my $dbh = db::connect($config); + my $dbh = db::connect($config); - my $query = 'delete from calcms_events where id=?'; - db::put( $dbh, $query, [$event_id] ); + my $query = 'delete from calcms_events where id=?'; + db::put( $dbh, $query, [$event_id] ); - $query = 'delete from calcms_categories where id=?'; - db::put( $dbh, $query, [$event_id] ); + $query = 'delete from calcms_categories where id=?'; + db::put( $dbh, $query, [$event_id] ); - $query = 'delete from calcms_tags where id=?'; - db::put( $dbh, $query, [$event_id] ); + $query = 'delete from calcms_tags where id=?'; + db::put( $dbh, $query, [$event_id] ); - $query = 'delete from calcms_series_events where event_id=?'; - db::put( $dbh, $query, [$event_id] ); + $query = 'delete from calcms_series_events where event_id=?'; + db::put( $dbh, $query, [$event_id] ); } #TODO: add location to cache map sub configure_cache { - my $config = shift; - my $debug = $config->{system}->{debug}; + my $config = shift; + my $debug = $config->{system}->{debug}; - my $date_pattern = $cache::date_pattern; - my $controllers = $config->{controllers}; + my $date_pattern = $cache::date_pattern; + my $controllers = $config->{controllers}; - cache::init(); - cache::add_map( '', 'index.html' ); + cache::init(); + cache::add_map( '', 'index.html' ); - my $name = ''; - my $extension = ''; - my $templates = $config->{templates}->{events}; + my $name = ''; + my $extension = ''; + my $templates = $config->{templates}->{events}; - for my $template (@$templates) { - if ( $template =~ /^(.+)\.([^\.]+)$/ ) { - $name = $1; - $extension = $2; - } - cache::add_map( 'template=' . $template, $controllers->{events} . '/' . $name . '.' . $extension ); - cache::add_map( 'template=' . $template . '&date=today', $controllers->{events} . '/' . $template . '_today.' . $extension ); - cache::add_map( 'template=' . $template . '&date=' . $date_pattern, - $controllers->{events} . '/' . $name . '_date_$1-$2-$3.' . $extension ); - cache::add_map( 'template=' . $template . '&time=now', $controllers->{events} . '/' . $template . '_now.html' ); - cache::add_map( 'template=' . $template . '&time=now&limit=(\d+)', - $controllers->{events} . '/' . $name . '_now_limit_$1.' . $extension ); - cache::add_map( - 'template=' . $template . '&from_date=' . $date_pattern . '&till_date=' . $date_pattern, - $controllers->{events} . '/' . $name . '_from_$1-$2-$3_till_$4_$5_$6.' . $extension - ); - cache::add_map( - 'template=' . $template . '&from_date=' . $date_pattern . '&till_date=' . $date_pattern . '&weekday=(\d)', - $controllers->{events} . '/' . $template . '_from_$1-$2-$3_till_$4_$5_$6_weekday_$7.' . $extension - ); - cache::add_map( - 'template=' . $template . '&from_date=' . $date_pattern . '&till_date=' . $date_pattern . '&limit=(\d)', - $controllers->{events} . '/' . $template . '_from_$1-$2-$3_till_$4_$5_$6_limit_$7.' . $extension - ); - cache::add_map( 'template=' . $template . '&weekday=(\d)', $controllers->{events} . '/' . $name . '_weekday_$1.' . $extension ); - cache::add_map( 'template=' . $template . '&event_id=(\d+)', $controllers->{event} . '/' . $name . '_page_$1.' . $extension ); - } + for my $template (@$templates) { + if ( $template =~ /^(.+)\.([^\.]+)$/ ) { + $name = $1; + $extension = $2; + } + cache::add_map( 'template=' . $template, $controllers->{events} . '/' . $name . '.' . $extension ); + cache::add_map( 'template=' . $template . '&date=today', $controllers->{events} . '/' . $template . '_today.' . $extension ); + cache::add_map( 'template=' . $template . '&date=' . $date_pattern, + $controllers->{events} . '/' . $name . '_date_$1-$2-$3.' . $extension ); + cache::add_map( 'template=' . $template . '&time=now', $controllers->{events} . '/' . $template . '_now.html' ); + cache::add_map( 'template=' . $template . '&time=now&limit=(\d+)', + $controllers->{events} . '/' . $name . '_now_limit_$1.' . $extension ); + cache::add_map( + 'template=' . $template . '&from_date=' . $date_pattern . '&till_date=' . $date_pattern, + $controllers->{events} . '/' . $name . '_from_$1-$2-$3_till_$4_$5_$6.' . $extension + ); + cache::add_map( + 'template=' . $template . '&from_date=' . $date_pattern . '&till_date=' . $date_pattern . '&weekday=(\d)', + $controllers->{events} . '/' . $template . '_from_$1-$2-$3_till_$4_$5_$6_weekday_$7.' . $extension + ); + cache::add_map( + 'template=' . $template . '&from_date=' . $date_pattern . '&till_date=' . $date_pattern . '&limit=(\d)', + $controllers->{events} . '/' . $template . '_from_$1-$2-$3_till_$4_$5_$6_limit_$7.' . $extension + ); + cache::add_map( 'template=' . $template . '&weekday=(\d)', $controllers->{events} . '/' . $name . '_weekday_$1.' . $extension ); + cache::add_map( 'template=' . $template . '&event_id=(\d+)', $controllers->{event} . '/' . $name . '_page_$1.' . $extension ); + } } sub get_duration { - my $config = shift; - my $event = shift; - my $timezone = $config->{date}->{time_zone}; - my $start = time::get_datetime( $event->{start}, $timezone ); - my $end = time::get_datetime( $event->{end}, $timezone ); + my $config = shift; + my $event = shift; + my $timezone = $config->{date}->{time_zone}; + my $start = time::get_datetime( $event->{start}, $timezone ); + my $end = time::get_datetime( $event->{end}, $timezone ); - #my $seconds = $end->subtract($start)->in_units("minutes"); - #return $seconds; - return undef unless defined $start; - return undef unless defined $end; - my $duration = $end->epoch() - $start->epoch(); + #my $seconds = $end->subtract($start)->in_units("minutes"); + #return $seconds; + return undef unless defined $start; + return undef unless defined $end; + my $duration = $end->epoch() - $start->epoch(); - #print STDERR "duration=$duration, end=".$end->datetime()." start=".$start->datetime()."\n"; - return $duration / 60; + #print STDERR "duration=$duration, end=".$end->datetime()." start=".$start->datetime()."\n"; + return $duration / 60; } sub check_params { - my $config = shift; - my $params = shift; + my $config = shift; + my $params = shift; - #define running at - my $running_at = $params->{running_at} || ''; - if ( ( defined $running_at ) && ( $running_at ne '' ) ) { - my $run_date = time::check_date($running_at); - my $run_time = time::check_time($running_at); - if ( ( $run_date ne '' ) && ( $run_time ne '' ) ) { - $params->{till_date} = $run_date; - $params->{till_time} = $run_time; - $params->{order} = 'asc'; - $params->{limit} = 1; - $params->{archive} = 'all'; - } - } + #define running at + my $running_at = $params->{running_at} || ''; + if ( ( defined $running_at ) && ( $running_at ne '' ) ) { + my $run_date = time::check_date($running_at); + my $run_time = time::check_time($running_at); + if ( ( $run_date ne '' ) && ( $run_time ne '' ) ) { + $params->{till_date} = $run_date; + $params->{till_time} = $run_time; + $params->{order} = 'asc'; + $params->{limit} = 1; + $params->{archive} = 'all'; + } + } - #set time - my $time = time::check_time( $params->{time} ); - my $from_time = time::check_time( $params->{from_time} ); - my $till_time = time::check_time( $params->{till_time} ); + #set time + my $time = time::check_time( $params->{time} ); + my $from_time = time::check_time( $params->{from_time} ); + my $till_time = time::check_time( $params->{till_time} ); - #set date - my $date = ''; - my $from_date = time::check_date( $params->{from_date} ); - my $till_date = time::check_date( $params->{till_date} ); - if ( ( $from_date eq '' ) && ( $till_date eq '' ) ) { - $date = time::check_date( $params->{date} ); - } + #set date + my $date = ''; + my $from_date = time::check_date( $params->{from_date} ); + my $till_date = time::check_date( $params->{till_date} ); + if ( ( $from_date eq '' ) && ( $till_date eq '' ) ) { + $date = time::check_date( $params->{date} ); + } - #set date interval (including) - my $date_range_include = 0; - $date_range_include = 1 - if ( defined $params->{date_range_include} ) - && ( $params->{date_range_include} eq '1' ); + #set date interval (including) + my $date_range_include = 0; + $date_range_include = 1 + if ( defined $params->{date_range_include} ) + && ( $params->{date_range_include} eq '1' ); - my $order = ''; - if ( defined $params->{order} ) { - $order = 'desc' if ( $params->{order} eq 'desc' ); - $order = 'asc' if ( $params->{order} eq 'asc' ); - } + my $order = ''; + if ( defined $params->{order} ) { + $order = 'desc' if ( $params->{order} eq 'desc' ); + $order = 'asc' if ( $params->{order} eq 'asc' ); + } - my $weekday = $params->{weekday} || ''; + my $weekday = $params->{weekday} || ''; - if ( ( defined $weekday ) && ( $weekday ne '' ) ) { - if ( $weekday =~ /\d/ ) { - $weekday = int($weekday); - log::error( $config, 'invalid weekday' ) - if ( $weekday < 1 || $weekday > 7 ); - } else { - log::error( $config, 'invalid weekday' ); - } - } + if ( ( defined $weekday ) && ( $weekday ne '' ) ) { + if ( $weekday =~ /\d/ ) { + $weekday = int($weekday); + log::error( $config, 'invalid weekday' ) + if ( $weekday < 1 || $weekday > 7 ); + } else { + log::error( $config, 'invalid weekday' ); + } + } - my $time_of_day = $params->{time_of_day} || ''; - my $found = 0; - if ( defined $time_of_day ) { - for my $key ( 'night', 'morning', 'noon', 'afternoon', 'evening' ) { - $found = 1 if ( $key eq $time_of_day ); - } - log::error( $config, 'invalid time_of_day' ) - if ( ( $time_of_day ne '' ) && ( $found == 0 ) ); - } + my $time_of_day = $params->{time_of_day} || ''; + my $found = 0; + if ( defined $time_of_day ) { + for my $key ( 'night', 'morning', 'noon', 'afternoon', 'evening' ) { + $found = 1 if ( $key eq $time_of_day ); + } + log::error( $config, 'invalid time_of_day' ) + if ( $time_of_day ne '' ) && ( $found == 0 ); + } - my $tag = $params->{tag} || ''; - if ( ( defined $tag ) && ( $tag ne '' ) ) { - log::error( $config, "invalid tag" ) if ( $tag =~ /\s/ ); - log::error( $config, "invalid tag" ) if ( $tag =~ /\;/ ); - $tag =~ s/\'//gi; - } + my $tag = $params->{tag} || ''; + if ( ( defined $tag ) && ( $tag ne '' ) ) { + log::error( $config, "invalid tag" ) if ( $tag =~ /\s/ ); + log::error( $config, "invalid tag" ) if ( $tag =~ /\;/ ); + $tag =~ s/\'//gi; + } - my $category = $params->{category} || ''; - if ( ( defined $category ) && ( $category ne '' ) ) { - log::error( $config, "invalid category" ) if ( $category =~ /\;/ ); - $category =~ s/^\s+//gi; - $category =~ s/\s+$//gi; - $category =~ s/\'//gi; - } + my $category = $params->{category} || ''; + if ( ( defined $category ) && ( $category ne '' ) ) { + log::error( $config, "invalid category" ) if ( $category =~ /\;/ ); + $category =~ s/^\s+//gi; + $category =~ s/\s+$//gi; + $category =~ s/\'//gi; + } - my $series_name = $params->{series_name} || ''; - if ( ( defined $series_name ) && ( $series_name ne '' ) ) { - log::error( $config, "invalid series_name" ) - if ( $series_name =~ /\;/ ); - $series_name =~ s/^\s+//gi; - $series_name =~ s/\s+$//gi; - $series_name =~ s/\'//gi; - } + my $series_name = $params->{series_name} || ''; + if ( ( defined $series_name ) && ( $series_name ne '' ) ) { + log::error( $config, "invalid series_name" ) + if ( $series_name =~ /\;/ ); + $series_name =~ s/^\s+//gi; + $series_name =~ s/\s+$//gi; + $series_name =~ s/\'//gi; + } - my $title = $params->{title} || ''; - if ( ( defined $title ) && ( $title ne '' ) ) { - log::error( $config, "invalid title" ) if ( $title =~ /\;/ ); - $title =~ s/^\s+//gi; - $title =~ s/\s+$//gi; - $title =~ s/\'//gi; - } + my $title = $params->{title} || ''; + if ( ( defined $title ) && ( $title ne '' ) ) { + log::error( $config, "invalid title" ) if ( $title =~ /\;/ ); + $title =~ s/^\s+//gi; + $title =~ s/\s+$//gi; + $title =~ s/\'//gi; + } - my $location = $params->{location} || ''; - if ( ( defined $location ) && ( $location ne '' ) ) { - log::error( $config, "invalid location" ) if ( $location =~ /\;/ ); - $location =~ s/^\s+//gi; - $location =~ s/\s+$//gi; - $location =~ s/\'//gi; - } + my $location = $params->{location} || ''; + if ( ( defined $location ) && ( $location ne '' ) ) { + log::error( $config, "invalid location" ) if ( $location =~ /\;/ ); + $location =~ s/^\s+//gi; + $location =~ s/\s+$//gi; + $location =~ s/\'//gi; + } - #if no location is set, use exclude location filter from default config - my $locations_to_exclude = ''; - if ( ( $location eq '' ) - && ( defined $config->{filter} ) - && ( defined $config->{filter}->{locations_to_exclude} ) ) - { - $locations_to_exclude = $config->{filter}->{locations_to_exclude} || ''; - $locations_to_exclude =~ s/\s+/ /g; - } + #if no location is set, use exclude location filter from default config + my $locations_to_exclude = ''; + if ( ( $location eq '' ) + && ( defined $config->{filter} ) + && ( defined $config->{filter}->{locations_to_exclude} ) ) + { + $locations_to_exclude = $config->{filter}->{locations_to_exclude} || ''; + $locations_to_exclude =~ s/\s+/ /g; + } - my $projects_to_exclude = ''; - if ( ( defined $config->{filter} ) - && ( defined $config->{filter}->{projects_to_exclude} ) ) - { - $projects_to_exclude = $config->{filter}->{projects_to_exclude} || ''; - $projects_to_exclude =~ s/\s+/ /g; - } + my $projects_to_exclude = ''; + if ( ( defined $config->{filter} ) + && ( defined $config->{filter}->{projects_to_exclude} ) ) + { + $projects_to_exclude = $config->{filter}->{projects_to_exclude} || ''; + $projects_to_exclude =~ s/\s+/ /g; + } - #enable exclude locations filter - my $exclude_locations = 0; - $exclude_locations = 1 if ( defined $params->{exclude_locations} ) && ( $params->{exclude_locations} eq '1' ); + #enable exclude locations filter + my $exclude_locations = 0; + $exclude_locations = 1 if ( defined $params->{exclude_locations} ) && ( $params->{exclude_locations} eq '1' ); - my $exclude_projects = 0; - $exclude_projects = 1 if ( defined $params->{exclude_projects} ) && ( $params->{exclude_projects} eq '1' ); + my $exclude_projects = 0; + $exclude_projects = 1 if ( defined $params->{exclude_projects} ) && ( $params->{exclude_projects} eq '1' ); - my $exclude_event_images = 0; - $exclude_event_images = 1 if ( defined $params->{exclude_event_images} ) && ( $params->{exclude_event_images} eq '1' ); + my $exclude_event_images = 0; + $exclude_event_images = 1 if ( defined $params->{exclude_event_images} ) && ( $params->{exclude_event_images} eq '1' ); - #show future events by default - my $archive = 'future'; - if ( defined $params->{archive} ) { - $archive = 'all' if ( $params->{archive} eq 'all' ); - $archive = 'past' if ( $params->{archive} eq 'gone' ); - $archive = 'future' if ( $params->{archive} eq 'coming' ); - } + #show future events by default + my $archive = 'future'; + if ( defined $params->{archive} ) { + $archive = 'all' if ( $params->{archive} eq 'all' ); + $archive = 'past' if ( $params->{archive} eq 'gone' ); + $archive = 'future' if ( $params->{archive} eq 'coming' ); + } - my $disable_event_sync = ''; - if ( ( defined $params->{disable_event_sync} ) - && ( $params->{disable_event_sync} =~ /([01])/ ) ) - { - $disable_event_sync = $1; - } + my $disable_event_sync = ''; + if ( ( defined $params->{disable_event_sync} ) + && ( $params->{disable_event_sync} =~ /([01])/ ) ) + { + $disable_event_sync = $1; + } - #show all on defined timespans - if ( ( $from_date ne '' ) && ( $till_date ne '' ) ) { - $archive = 'all'; - } + #show all on defined timespans + if ( ( $from_date ne '' ) && ( $till_date ne '' ) ) { + $archive = 'all'; + } - my $event_id = $params->{event_id} || ''; - if ( ( defined $event_id ) && ( $event_id ne '' ) ) { - if ( $event_id =~ /(\d+)/ ) { - $event_id = $1; - } else { - log::error( $config, "invalid event_id" ); - } - } + my $event_id = $params->{event_id} || ''; + if ( ( defined $event_id ) && ( $event_id ne '' ) ) { + if ( $event_id =~ /(\d+)/ ) { + $event_id = $1; + } else { + log::error( $config, "invalid event_id" ); + } + } - my $get = 'all'; - $get = 'no_content' - if ( ( defined $params->{get} ) && ( $params->{get} eq 'no_content' ) ); + my $get = 'all'; + $get = 'no_content' + if ( defined $params->{get} ) && ( $params->{get} eq 'no_content' ); - my $search = $params->{search} || ''; - if ( ( defined $search ) && ( $search ne '' ) ) { - $search = substr( $search, 0, 100 ); - $search =~ s/^\s+//gi; - $search =~ s/\s+$//gi; - } + my $search = $params->{search} || ''; + if ( ( defined $search ) && ( $search ne '' ) ) { + $search = substr( $search, 0, 100 ); + $search =~ s/^\s+//gi; + $search =~ s/\s+$//gi; + } - #print STDERR $params->{template}."\n"; - my $template = '.html'; - if ( ( defined $params->{template} ) && ( $params->{template} eq 'no' ) ) { - $template = 'no'; - } else { - $template = template::check( $params->{template}, 'event_list.html' ); - } + #print STDERR $params->{template}."\n"; + my $template = '.html'; + if ( ( defined $params->{template} ) && ( $params->{template} eq 'no' ) ) { + $template = 'no'; + } else { + $template = template::check( $params->{template}, 'event_list.html' ); + } - my $limit_config = $config->{permissions}->{result_limit} || 100; - my $limit = $params->{limit} || $limit_config; - log::error( $config, 'invalid limit!' ) if ( $limit =~ /\D/ ); - $limit = $limit_config if ( $limit_config < $limit ); + my $limit_config = $config->{permissions}->{result_limit} || 100; + my $limit = $params->{limit} || $limit_config; + log::error( $config, 'invalid limit!' ) if ( $limit =~ /\D/ ); + $limit = $limit_config if ( $limit_config < $limit ); - #read project from configuration file - my $project_name = $config->{project} || ''; - log::error( $config, 'no default project configured' ) - if ( $project_name eq '' ); + #read project from configuration file + my $project_name = $config->{project} || ''; + log::error( $config, 'no default project configured' ) + if ( $project_name eq '' ); - #get default project - my $default_project = undef; - my $projects = project::get( $config, { name => $project_name } ); - log::error( $config, "no configuration found for project '$project_name'" ) - unless ( scalar(@$projects) == 1 ); - $default_project = $projects->[0]; + #get default project + my $default_project = undef; + my $projects = project::get( $config, { name => $project_name } ); + log::error( $config, "no configuration found for project '$project_name'" ) + unless ( scalar(@$projects) == 1 ); + $default_project = $projects->[0]; - # get project from parameter (by name) - my $project = ''; - if ( ( defined $params->{project} ) - && ( $params->{project} =~ /\w+/ ) - && ( $params->{project} ne 'all' ) ) - { - my $project_name = $params->{project}; - my $projects = project::get( $config, { name => $project_name } ); - log::error( $config, 'invalid project ' . $project_name ) - unless scalar(@$projects) == 1; - $project = $projects->[0]; - } + # get project from parameter (by name) + my $project = ''; + if ( ( defined $params->{project} ) + && ( $params->{project} =~ /\w+/ ) + && ( $params->{project} ne 'all' ) ) + { + my $project_name = $params->{project}; + my $projects = project::get( $config, { name => $project_name } ); + log::error( $config, 'invalid project ' . $project_name ) + unless scalar(@$projects) == 1; + $project = $projects->[0]; + } - $project_name = $params->{project_name} || ''; - my $studio_name = $params->{studio_name} || ''; + $project_name = $params->{project_name} || ''; + my $studio_name = $params->{studio_name} || ''; - my $project_id = $params->{project_id} || ''; - my $studio_id = $params->{studio_id} || ''; + my $project_id = $params->{project_id} || ''; + my $studio_id = $params->{studio_id} || ''; - my $debug = $params->{debug} || ''; - if ( $debug =~ /([a-z\_\,]+)/ ) { - $debug = $1; - } + my $debug = $params->{debug} || ''; + if ( $debug =~ /([a-z\_\,]+)/ ) { + $debug = $1; + } - my $json_callback = $params->{json_callback} || ''; - if ( $json_callback ne '' ) { - $json_callback =~ s/[^a-zA-Z0-9\_]//g; - } + my $json_callback = $params->{json_callback} || ''; + if ( $json_callback ne '' ) { + $json_callback =~ s/[^a-zA-Z0-9\_]//g; + } - # use relative links - my $extern = 0; - $extern = 1 if ( defined $params->{extern} ) && ( $params->{extern} eq '1' ); + # use relative links + my $extern = 0; + $extern = 1 if ( defined $params->{extern} ) && ( $params->{extern} eq '1' ); - my $recordings = 0; - $recordings = 1 if ( defined $params->{recordings} ) && ( $params->{recordings} eq '1' ); + my $recordings = 0; + $recordings = 1 if ( defined $params->{recordings} ) && ( $params->{recordings} eq '1' ); - my $checked = { - date => $date, - time => $time, - time_of_day => $time_of_day, - from_date => $from_date, - till_date => $till_date, - date_range_include => $date_range_include, - from_time => $from_time, - till_time => $till_time, - weekday => $weekday, - limit => $limit, - template => $template, - location => $location, - category => $category, - series_name => $series_name, - tag => $tag, - title => $title, - event_id => $event_id, - search => $search, - debug => $debug, - archive => $archive, - order => $order, - project => $project, - default_project => $default_project, - project_name => $project_name, - project_id => $project_id, - studio_name => $studio_name, - studio_id => $studio_id, - json_callback => $json_callback, - get => $get, - locations_to_exclude => $locations_to_exclude, - projects_to_exclude => $projects_to_exclude, - exclude_locations => $exclude_locations, - exclude_projects => $exclude_projects, - exclude_event_images => $exclude_event_images, - disable_event_sync => $disable_event_sync, - extern => $extern, - recordings => $recordings, - }; + my $checked = { + date => $date, + time => $time, + time_of_day => $time_of_day, + from_date => $from_date, + till_date => $till_date, + date_range_include => $date_range_include, + from_time => $from_time, + till_time => $till_time, + weekday => $weekday, + limit => $limit, + template => $template, + location => $location, + category => $category, + series_name => $series_name, + tag => $tag, + title => $title, + event_id => $event_id, + search => $search, + debug => $debug, + archive => $archive, + order => $order, + project => $project, + default_project => $default_project, + project_name => $project_name, + project_id => $project_id, + studio_name => $studio_name, + studio_id => $studio_id, + json_callback => $json_callback, + get => $get, + locations_to_exclude => $locations_to_exclude, + projects_to_exclude => $projects_to_exclude, + exclude_locations => $exclude_locations, + exclude_projects => $exclude_projects, + exclude_event_images => $exclude_event_images, + disable_event_sync => $disable_event_sync, + extern => $extern, + recordings => $recordings, + }; - #print STDERR Dumper($checked); - return $checked; + #print STDERR Dumper($checked); + return $checked; } sub get_keys { - my $event = shift; + my $event = shift; - my $program = $event->{program} || ''; - my $series_name = $event->{series_name} || ''; - my $title = $event->{title} || ''; - my $user_title = $event->{user_title} || ''; - my $episode = $event->{episode} || ''; - my $recurrence_count_alpha = $event->{recurrence_count_alpha} || ''; + my $program = $event->{program} || ''; + my $series_name = $event->{series_name} || ''; + my $title = $event->{title} || ''; + my $user_title = $event->{user_title} || ''; + my $episode = $event->{episode} || ''; + my $recurrence_count_alpha = $event->{recurrence_count_alpha} || ''; - my $skey = ''; - $skey .= $series_name if $series_name ne ''; - $skey .= ' - ' - if ( $series_name ne '' ) - && ( ( $title ne '' ) || ( $user_title ne '' ) ); + my $skey = ''; + $skey .= $series_name if $series_name ne ''; + $skey .= ' - ' + if ( $series_name ne '' ) + && ( ( $title ne '' ) || ( $user_title ne '' ) ); - my $tkey = ''; - $tkey = $title if $title ne ''; - $tkey .= ': ' if ( $title ne '' ) && ( $user_title ne '' ); - $tkey .= $user_title if $user_title ne ''; - $tkey .= ' #' . $episode if $episode ne ''; - $tkey .= $recurrence_count_alpha if $recurrence_count_alpha ne ''; + my $tkey = ''; + $tkey = $title if $title ne ''; + $tkey .= ': ' if ( $title ne '' ) && ( $user_title ne '' ); + $tkey .= $user_title if $user_title ne ''; + $tkey .= ' #' . $episode if $episode ne ''; + $tkey .= $recurrence_count_alpha if $recurrence_count_alpha ne ''; - my $pkey = ''; - $pkey = ' (' . $program . ')' if $program ne ''; + my $pkey = ''; + $pkey = ' (' . $program . ')' if $program ne ''; - return { - key => $skey . $tkey . $pkey, - full_title => $skey . $tkey . $pkey, - full_title_no_series => $tkey . $pkey, - full_title_no_program => $skey . $tkey, - full_title_no_program_no_series => $tkey - }; + return { + key => $skey . $tkey . $pkey, + full_title => $skey . $tkey . $pkey, + full_title_no_series => $tkey . $pkey, + full_title_no_program => $skey . $tkey, + full_title_no_program_no_series => $tkey + }; } #do not delete last line! diff --git a/lib/calcms/mail.pm b/lib/calcms/mail.pm index e945285..d89e278 100644 --- a/lib/calcms/mail.pm +++ b/lib/calcms/mail.pm @@ -14,11 +14,7 @@ sub send { 'Data' => $mail->{'Data'}, ); - #print '
';
 	$msg->print( \*STDERR );
-
-	#print '
'; - $msg->send; } diff --git a/lib/calcms/playout.pm b/lib/calcms/playout.pm index bdc53ba..d08dc5a 100644 --- a/lib/calcms/playout.pm +++ b/lib/calcms/playout.pm @@ -145,9 +145,6 @@ sub sync { my $studio_id = $options->{studio_id}; my $updates = $options->{events}; - #print STDERR "sync\n"; - #print STDERR Dumper($updates); - # get new entries by date my $update_by_date = {}; for my $entry (@$updates) { diff --git a/lib/calcms/project.pm b/lib/calcms/project.pm index 8ba35c2..4862dbe 100644 --- a/lib/calcms/project.pm +++ b/lib/calcms/project.pm @@ -30,46 +30,46 @@ sub debug; # get project columns sub get_columns { - my $config = shift; + my $config = shift; - my $dbh = db::connect($config); - my $cols = db::get_columns( $dbh, 'calcms_projects' ); - my $columns = {}; - for my $col (@$cols) { - $columns->{$col} = 1; - } - return $columns; + my $dbh = db::connect($config); + my $cols = db::get_columns( $dbh, 'calcms_projects' ); + my $columns = {}; + for my $col (@$cols) { + $columns->{$col} = 1; + } + return $columns; } # get projects sub get { - my $config = shift; - my $condition = shift; + my $config = shift; + my $condition = shift; - my $dbh = db::connect($config); + my $dbh = db::connect($config); - my @conditions = (); - my @bind_values = (); + my @conditions = (); + my @bind_values = (); - if ( ( defined $condition->{project_id} ) && ( $condition->{project_id} ne '' ) ) { - push @conditions, 'project_id=?'; - push @bind_values, $condition->{project_id}; - } + if ( ( defined $condition->{project_id} ) && ( $condition->{project_id} ne '' ) ) { + push @conditions, 'project_id=?'; + push @bind_values, $condition->{project_id}; + } - if ( ( defined $condition->{name} ) && ( $condition->{name} ne '' ) ) { - push @conditions, 'name=?'; - push @bind_values, $condition->{name}; - } + if ( ( defined $condition->{name} ) && ( $condition->{name} ne '' ) ) { + push @conditions, 'name=?'; + push @bind_values, $condition->{name}; + } - my $limit = ''; - if ( ( defined $condition->{limit} ) && ( $condition->{limit} ne '' ) ) { - $limit = 'limit ' . $condition->{limit}; - } + my $limit = ''; + if ( ( defined $condition->{limit} ) && ( $condition->{limit} ne '' ) ) { + $limit = 'limit ' . $condition->{limit}; + } - my $conditions = ''; - $conditions = " where " . join( " and ", @conditions ) if ( @conditions > 0 ); + my $conditions = ''; + $conditions = " where " . join( " and ", @conditions ) if ( @conditions > 0 ); - my $query = qq{ + my $query = qq{ select * from calcms_projects $conditions @@ -77,423 +77,423 @@ sub get { $limit }; - #print STDERR Dumper($query).Dumper(\@bind_values); + #print STDERR Dumper($query).Dumper(\@bind_values); - my $projects = db::get( $dbh, $query, \@bind_values ); - return $projects; + my $projects = db::get( $dbh, $query, \@bind_values ); + return $projects; } # requires at least project_id sub getImageById { - my $config = shift; - my $conditions = shift; + my $config = shift; + my $conditions = shift; - return undef unless defined $conditions->{project_id}; - my $projects = project::get( $config, $conditions ); - return undef if scalar(@$projects) != 1; - return $projects->[0]->{image}; + return undef unless defined $conditions->{project_id}; + my $projects = project::get( $config, $conditions ); + return undef if scalar(@$projects) != 1; + return $projects->[0]->{image}; } sub get_date_range { - my $config = shift; - my $query = qq{ + my $config = shift; + my $query = qq{ select min(start_date) start_date, max(end_date) end_date from calcms_projects }; - my $dbh = db::connect($config); + my $dbh = db::connect($config); - my $projects = db::get( $dbh, $query ); - return $projects->[0]; + my $projects = db::get( $dbh, $query ); + return $projects->[0]; } # insert project sub insert { - my $config = shift; - my $entry = shift; + my $config = shift; + my $entry = shift; - my $columns = get_columns($config); - my $project = {}; - for my $column ( keys %$columns ) { - $project->{$column} = $entry->{$column} if defined $entry->{$column}; - } + my $columns = get_columns($config); + my $project = {}; + for my $column ( keys %$columns ) { + $project->{$column} = $entry->{$column} if defined $entry->{$column}; + } - $project->{image} = images::normalizeName( $project->{image} ) if defined $project->{image}; + $project->{image} = images::normalizeName( $project->{image} ) if defined $project->{image}; - my $dbh = db::connect($config); - my $id = db::insert( $dbh, 'calcms_projects', $project ); - return $id; + my $dbh = db::connect($config); + my $id = db::insert( $dbh, 'calcms_projects', $project ); + return $id; } # update project sub update { - my $config = shift; - my $project = shift; + my $config = shift; + my $project = shift; - my $columns = project::get_columns($config); - my $entry = {}; - for my $column ( keys %$columns ) { - $entry->{$column} = $project->{$column} if defined $project->{$column}; - } + my $columns = project::get_columns($config); + my $entry = {}; + for my $column ( keys %$columns ) { + $entry->{$column} = $project->{$column} if defined $project->{$column}; + } - $entry->{image} = images::normalizeName( $entry->{image} ) if defined $entry->{image}; + $entry->{image} = images::normalizeName( $entry->{image} ) if defined $entry->{image}; - my $values = join( ",", map { $_ . '=?' } ( keys %$entry ) ); - my @bind_values = map { $entry->{$_} } ( keys %$entry ); - push @bind_values, $entry->{project_id}; + my $values = join( ",", map { $_ . '=?' } ( keys %$entry ) ); + my @bind_values = map { $entry->{$_} } ( keys %$entry ); + push @bind_values, $entry->{project_id}; - my $query = qq{ + my $query = qq{ update calcms_projects set $values where project_id=? }; - #print STDERR Dumper($query).Dumper(\@bind_values); - my $dbh = db::connect($config); - db::put( $dbh, $query, \@bind_values ); + #print STDERR Dumper($query).Dumper(\@bind_values); + my $dbh = db::connect($config); + db::put( $dbh, $query, \@bind_values ); } # delete project sub delete { - my $config = shift; - my $entry = shift; + my $config = shift; + my $entry = shift; - my $dbh = db::connect($config); - db::put( $dbh, 'delete from calcms_projects where project_id=?', [ $entry->{project_id} ] ); + my $dbh = db::connect($config); + db::put( $dbh, 'delete from calcms_projects where project_id=?', [ $entry->{project_id} ] ); } # get studios of a project sub get_studios { - my $config = shift; - my $options = shift; + my $config = shift; + my $options = shift; - return undef unless defined $options->{project_id}; - my $project_id = $options->{project_id}; + return undef unless defined $options->{project_id}; + my $project_id = $options->{project_id}; - my $query = qq{ + my $query = qq{ select * from calcms_project_studios where project_id=? }; - my $dbh = db::connect($config); - my $project_studios = db::get( $dbh, $query, [$project_id] ); + my $dbh = db::connect($config); + my $project_studios = db::get( $dbh, $query, [$project_id] ); - return $project_studios; + return $project_studios; } sub get_studio_assignments { - my $config = shift; - my $options = shift; + my $config = shift; + my $options = shift; - my @conditions = (); - my @bind_values = (); + my @conditions = (); + my @bind_values = (); - if ( ( defined $options->{project_id} ) && ( $options->{project_id} ne '' ) ) { - push @conditions, 'project_id=?'; - push @bind_values, $options->{project_id}; - } + if ( ( defined $options->{project_id} ) && ( $options->{project_id} ne '' ) ) { + push @conditions, 'project_id=?'; + push @bind_values, $options->{project_id}; + } - if ( ( defined $options->{studio_id} ) && ( $options->{studio_id} ne '' ) ) { - push @conditions, 'studio_id=?'; - push @bind_values, $options->{studio_id}; - } + if ( ( defined $options->{studio_id} ) && ( $options->{studio_id} ne '' ) ) { + push @conditions, 'studio_id=?'; + push @bind_values, $options->{studio_id}; + } - my $conditions = ''; - $conditions = " where " . join( " and ", @conditions ) if ( @conditions > 0 ); + my $conditions = ''; + $conditions = " where " . join( " and ", @conditions ) if ( @conditions > 0 ); - my $query = qq{ + my $query = qq{ select * from calcms_project_studios $conditions }; - my $dbh = db::connect($config); - my $results = db::get( $dbh, $query, \@bind_values ); + my $dbh = db::connect($config); + my $results = db::get( $dbh, $query, \@bind_values ); - return $results; + return $results; } # is studio assigned to project sub is_studio_assigned { - my $config = shift; - my $entry = shift; + my $config = shift; + my $entry = shift; - return 0 unless defined $entry->{project_id}; - return 0 unless defined $entry->{studio_id}; + return 0 unless defined $entry->{project_id}; + return 0 unless defined $entry->{studio_id}; - my $project_id = $entry->{project_id}; - my $studio_id = $entry->{studio_id}; + my $project_id = $entry->{project_id}; + my $studio_id = $entry->{studio_id}; - my $query = qq{ + my $query = qq{ select * from calcms_project_studios where project_id=? and studio_id=? }; - my $bind_values = [ $project_id, $studio_id ]; + my $bind_values = [ $project_id, $studio_id ]; - my $dbh = db::connect($config); - my $project_studios = db::get( $dbh, $query, $bind_values ); - return 1 if @$project_studios == 1; - return 0; + my $dbh = db::connect($config); + my $project_studios = db::get( $dbh, $query, $bind_values ); + return 1 if scalar @$project_studios == 1; + return 0; } # assign studio to project sub assign_studio { - my $config = shift; - my $entry = shift; + my $config = shift; + my $entry = shift; - return undef unless defined $entry->{project_id}; - return undef unless defined $entry->{studio_id}; - my $project_id = $entry->{project_id}; - my $studio_id = $entry->{studio_id}; + return undef unless defined $entry->{project_id}; + return undef unless defined $entry->{studio_id}; + my $project_id = $entry->{project_id}; + my $studio_id = $entry->{studio_id}; - if ( is_studio_assigned($entry) ) { - print STDERR "studio $entry->{studio_id} already assigned to project $entry->{project_id}\n"; - return 1; - } - my $dbh = db::connect($config); - my $id = db::insert( $dbh, 'calcms_project_studios', $entry ); - return $id; + if ( is_studio_assigned($entry) ) { + print STDERR "studio $entry->{studio_id} already assigned to project $entry->{project_id}\n"; + return 1; + } + my $dbh = db::connect($config); + my $id = db::insert( $dbh, 'calcms_project_studios', $entry ); + return $id; } # unassign studio from project sub unassign_studio { - my $config = shift; - my $entry = shift; + my $config = shift; + my $entry = shift; - return undef unless defined $entry->{project_id}; - return undef unless defined $entry->{studio_id}; - my $project_id = $entry->{project_id}; - my $studio_id = $entry->{studio_id}; + return undef unless defined $entry->{project_id}; + return undef unless defined $entry->{studio_id}; + my $project_id = $entry->{project_id}; + my $studio_id = $entry->{studio_id}; - my $sql = 'delete from calcms_project_studios where project_id=? and studio_id=?'; - my $bind_values = [ $project_id, $studio_id ]; - my $dbh = db::connect($config); - return db::put( $dbh, $sql, $bind_values ); + my $sql = 'delete from calcms_project_studios where project_id=? and studio_id=?'; + my $bind_values = [ $project_id, $studio_id ]; + my $dbh = db::connect($config); + return db::put( $dbh, $sql, $bind_values ); } # get series by project and studio sub get_series { - my $config = shift; - my $options = shift; + my $config = shift; + my $options = shift; - return undef unless defined $options->{project_id}; - return undef unless defined $options->{studio_id}; - my $project_id = $options->{project_id}; - my $studio_id = $options->{studio_id}; + return undef unless defined $options->{project_id}; + return undef unless defined $options->{studio_id}; + my $project_id = $options->{project_id}; + my $studio_id = $options->{studio_id}; - my $query = qq{ + my $query = qq{ select * from calcms_project_series where project_id=? and studio_id=? }; - my $bind_values = [ $project_id, $studio_id ]; - my $dbh = db::connect($config); - my $project_series = db::get( $dbh, $query, $bind_values ); + my $bind_values = [ $project_id, $studio_id ]; + my $dbh = db::connect($config); + my $project_series = db::get( $dbh, $query, $bind_values ); - return $project_series; + return $project_series; } sub get_series_assignments { - my $config = shift; - my $options = shift; + my $config = shift; + my $options = shift; - my @conditions = (); - my @bind_values = (); + my @conditions = (); + my @bind_values = (); - if ( ( defined $options->{project_id} ) && ( $options->{project_id} ne '' ) ) { - push @conditions, 'project_id=?'; - push @bind_values, $options->{project_id}; - } + if ( ( defined $options->{project_id} ) && ( $options->{project_id} ne '' ) ) { + push @conditions, 'project_id=?'; + push @bind_values, $options->{project_id}; + } - if ( ( defined $options->{studio_id} ) && ( $options->{studio_id} ne '' ) ) { - push @conditions, 'studio_id=?'; - push @bind_values, $options->{studio_id}; - } + if ( ( defined $options->{studio_id} ) && ( $options->{studio_id} ne '' ) ) { + push @conditions, 'studio_id=?'; + push @bind_values, $options->{studio_id}; + } - if ( ( defined $options->{series_id} ) && ( $options->{series_id} ne '' ) ) { - push @conditions, 'series_id=?'; - push @bind_values, $options->{series_id}; - } + if ( ( defined $options->{series_id} ) && ( $options->{series_id} ne '' ) ) { + push @conditions, 'series_id=?'; + push @bind_values, $options->{series_id}; + } - my $conditions = ''; - $conditions = " where " . join( " and ", @conditions ) if ( @conditions > 0 ); + my $conditions = ''; + $conditions = " where " . join( " and ", @conditions ) if ( @conditions > 0 ); - my $query = qq{ + my $query = qq{ select * from calcms_project_series $conditions }; - my $dbh = db::connect($config); - my $results = db::get( $dbh, $query, \@bind_values ); + my $dbh = db::connect($config); + my $results = db::get( $dbh, $query, \@bind_values ); - return $results; + return $results; } # is series assigned to project and studio sub is_series_assigned { - my $config = shift; - my $entry = shift; + my $config = shift; + my $entry = shift; - return 0 unless defined $entry->{project_id}; - return 0 unless defined $entry->{studio_id}; - return 0 unless defined $entry->{series_id}; + return 0 unless defined $entry->{project_id}; + return 0 unless defined $entry->{studio_id}; + return 0 unless defined $entry->{series_id}; - my $project_id = $entry->{project_id}; - my $studio_id = $entry->{studio_id}; - my $series_id = $entry->{series_id}; + my $project_id = $entry->{project_id}; + my $studio_id = $entry->{studio_id}; + my $series_id = $entry->{series_id}; - my $query = qq{ + my $query = qq{ select * from calcms_project_series where project_id=? and studio_id=? and series_id=? }; - my $bind_values = [ $project_id, $studio_id, $series_id ]; + my $bind_values = [ $project_id, $studio_id, $series_id ]; - my $dbh = db::connect($config); - my $project_series = db::get( $dbh, $query, $bind_values ); - return 1 if @$project_series == 1; - return 0; + my $dbh = db::connect($config); + my $project_series = db::get( $dbh, $query, $bind_values ); + return 1 if scalar @$project_series == 1; + return 0; } # assign series to project and studio sub assign_series { - my $config = shift; - my $entry = shift; + my $config = shift; + my $entry = shift; - return undef unless defined $entry->{project_id}; - return undef unless defined $entry->{studio_id}; - return undef unless defined $entry->{series_id}; + return undef unless defined $entry->{project_id}; + return undef unless defined $entry->{studio_id}; + return undef unless defined $entry->{series_id}; - my $project_id = $entry->{project_id}; - my $studio_id = $entry->{studio_id}; - my $series_id = $entry->{series_id}; + my $project_id = $entry->{project_id}; + my $studio_id = $entry->{studio_id}; + my $series_id = $entry->{series_id}; - if ( is_series_assigned($entry) ) { - print STDERR "series $series_id already assigned to project $project_id and studio $studio_id\n"; - return return undef; - } - my $dbh = db::connect($config); - my $id = db::insert( $dbh, 'calcms_project_series', $entry ); - print STDERR "assigned series $series_id to project $project_id and studio $studio_id\n"; - return $id; + if ( is_series_assigned($entry) ) { + print STDERR "series $series_id already assigned to project $project_id and studio $studio_id\n"; + return return undef; + } + my $dbh = db::connect($config); + my $id = db::insert( $dbh, 'calcms_project_series', $entry ); + print STDERR "assigned series $series_id to project $project_id and studio $studio_id\n"; + return $id; } # unassign series from project # TODO: remove series _single_ if no event is assigned to sub unassign_series { - my $config = shift; - my $entry = shift; + my $config = shift; + my $entry = shift; - return undef unless defined $entry->{project_id}; - return undef unless defined $entry->{studio_id}; - return undef unless defined $entry->{series_id}; + return undef unless defined $entry->{project_id}; + return undef unless defined $entry->{studio_id}; + return undef unless defined $entry->{series_id}; - my $project_id = $entry->{project_id}; - my $studio_id = $entry->{studio_id}; - my $series_id = $entry->{series_id}; + my $project_id = $entry->{project_id}; + my $studio_id = $entry->{studio_id}; + my $series_id = $entry->{series_id}; - my $sql = 'delete from calcms_project_series where project_id=? and studio_id=? and series_id=?'; - my $bind_values = [ $project_id, $studio_id, $series_id ]; - my $dbh = db::connect($config); - return db::put( $dbh, $sql, $bind_values ); + my $sql = 'delete from calcms_project_series where project_id=? and studio_id=? and series_id=?'; + my $bind_values = [ $project_id, $studio_id, $series_id ]; + my $dbh = db::connect($config); + return db::put( $dbh, $sql, $bind_values ); } sub get_with_dates { - my $config = shift; - my $options = shift; + my $config = shift; + my $options = shift; - my $language = $config->{date}->{language} || 'en'; - my $projects = project::get( $config, {} ); + my $language = $config->{date}->{language} || 'en'; + my $projects = project::get( $config, {} ); - foreach my $project ( reverse sort { $a->{end_date} cmp $b->{end_date} } (@$projects) ) { - $project->{months} = get_months( $config, $project, $language ); - $project->{user} = $ENV{REMOTE_USER}; - $project->{current} = 1 if ( $project->{name} eq $config::config->{project} ); - } + foreach my $project ( reverse sort { $a->{end_date} cmp $b->{end_date} } (@$projects) ) { + $project->{months} = get_months( $config, $project, $language ); + $project->{user} = $ENV{REMOTE_USER}; + $project->{current} = 1 if ( $project->{name} eq $config::config->{project} ); + } - return $projects; + return $projects; } #TODO: add config sub get_sorted { - my $config = shift; - my $projects = project::get( $config, {} ); - my @projects = reverse sort { $a->{end_date} cmp $b->{end_date} } (@$projects); + my $config = shift; + my $projects = project::get( $config, {} ); + my @projects = reverse sort { $a->{end_date} cmp $b->{end_date} } (@$projects); - unshift @projects, - { - name => 'all', - title => 'alle', - priority => '0', - start_date => $projects[-1]->{start_date}, - end_date => $projects[0]->{end_date}, - }; - return \@projects; + unshift @projects, + { + name => 'all', + title => 'alle', + priority => '0', + start_date => $projects[-1]->{start_date}, + end_date => $projects[0]->{end_date}, + }; + return \@projects; } # internal sub get_months { - my $config = shift; - my $project = shift; - my $language = shift || $config->{date}->{language} || 'en'; + my $config = shift; + my $project = shift; + my $language = shift || $config->{date}->{language} || 'en'; - my $start = $project->{start_date}; - my $end = $project->{end_date}; + my $start = $project->{start_date}; + my $end = $project->{end_date}; - ( my $start_year, my $start_month, my $start_day ) = split( /\-/, $start ); - my $last_day = Date::Calc::Days_in_Month( $start_year, $start_month ); - $start_day = 1 if ( $start_day < 1 ); - $start_day = $last_day if ( $start_day gt $last_day ); + ( my $start_year, my $start_month, my $start_day ) = split( /\-/, $start ); + my $last_day = Date::Calc::Days_in_Month( $start_year, $start_month ); + $start_day = 1 if ( $start_day < 1 ); + $start_day = $last_day if ( $start_day gt $last_day ); - ( my $end_year, my $end_month, my $end_day ) = split( /\-/, $end ); - $last_day = Date::Calc::Days_in_Month( $end_year, $end_month ); - $end_day = 1 if ( $end_day < 1 ); - $end_day = $last_day if ( $end_day gt $last_day ); + ( my $end_year, my $end_month, my $end_day ) = split( /\-/, $end ); + $last_day = Date::Calc::Days_in_Month( $end_year, $end_month ); + $end_day = 1 if ( $end_day < 1 ); + $end_day = $last_day if ( $end_day gt $last_day ); - my @months = (); - for my $year ( $start_year .. $end_year ) { - my $m1 = 1; - my $m2 = 12; - $m1 = $start_month if $year eq $start_year; - $m2 = $end_month if $year eq $end_year; + my @months = (); + for my $year ( $start_year .. $end_year ) { + my $m1 = 1; + my $m2 = 12; + $m1 = $start_month if $year eq $start_year; + $m2 = $end_month if $year eq $end_year; - for my $month ( $m1 .. $m2 ) { - my $d1 = 1; - my $d2 = Date::Calc::Days_in_Month( $year, $month ); - $d1 = $start_day if $month eq $start_month; - $d2 = $end_day if $month eq $end_month; - push @months, - { - start => time::array_to_date( $year, $month, $d1 ), - end => time::array_to_date( $year, $month, $d2 ), - year => $year, - month => $month, - month_name => $time::names->{$language}->{months_abbr}->[ $month - 1 ], - title => $project->{title}, - user => $ENV{REMOTE_USER} - }; - } - } - @months = reverse @months; - return \@months; + for my $month ( $m1 .. $m2 ) { + my $d1 = 1; + my $d2 = Date::Calc::Days_in_Month( $year, $month ); + $d1 = $start_day if $month eq $start_month; + $d2 = $end_day if $month eq $end_month; + push @months, + { + start => time::array_to_date( $year, $month, $d1 ), + end => time::array_to_date( $year, $month, $d2 ), + year => $year, + month => $month, + month_name => $time::names->{$language}->{months_abbr}->[ $month - 1 ], + title => $project->{title}, + user => $ENV{REMOTE_USER} + }; + } + } + @months = reverse @months; + return \@months; } # check project_id sub check { - my $config = shift; - my $options = shift; - return "missing project_id at checking project" unless defined $options->{project_id}; - return "Please select a project" if ( $options->{project_id} eq '-1' ); - return "Please select a project" if ( $options->{project_id} eq '' ); - my $projects = project::get( $config, { project_id => $options->{project_id} } ); - return "Sorry. unknown project" unless defined $projects; - return 1; + my $config = shift; + my $options = shift; + return "missing project_id at checking project" unless defined $options->{project_id}; + return "Please select a project" if ( $options->{project_id} eq '-1' ); + return "Please select a project" if ( $options->{project_id} eq '' ); + my $projects = project::get( $config, { project_id => $options->{project_id} } ); + return "Sorry. unknown project" unless defined $projects; + return 1; } sub error { - my $msg = shift; - print "ERROR: $msg
\n"; + my $msg = shift; + print "ERROR: $msg
\n"; } #do not delete last line! diff --git a/lib/calcms/projects.pm b/lib/calcms/projects.pm deleted file mode 100644 index 4c94729..0000000 --- a/lib/calcms/projects.pm +++ /dev/null @@ -1,9 +0,0 @@ -use warnings "all"; -use strict; - -package projects; - -print STDERR "projects.pm is not used anymore!\n"; - -#do not delete last line! -1; diff --git a/lib/calcms/series.pm b/lib/calcms/series.pm index 316b81e..06b97f1 100644 --- a/lib/calcms/series.pm +++ b/lib/calcms/series.pm @@ -31,503 +31,503 @@ sub debug; # get series columns sub get_columns { - my $config = shift; + my $config = shift; - my $dbh = db::connect($config); - my $cols = db::get_columns( $dbh, 'calcms_series' ); - my $columns = {}; - for my $col (@$cols) { - $columns->{$col} = 1; - } - return $columns; + my $dbh = db::connect($config); + my $cols = db::get_columns( $dbh, 'calcms_series' ); + my $columns = {}; + for my $col (@$cols) { + $columns->{$col} = 1; + } + return $columns; } # get series content sub get { - my $config = shift; - my $condition = shift; + my $config = shift; + my $condition = shift; - my @conditions = (); - my @bind_values = (); + my @conditions = (); + my @bind_values = (); - if ( ( defined $condition->{series_id} ) && ( $condition->{series_id} ne '' ) ) { - push @conditions, 'id=?'; - push @bind_values, $condition->{series_id}; - } + if ( ( defined $condition->{series_id} ) && ( $condition->{series_id} ne '' ) ) { + push @conditions, 'id=?'; + push @bind_values, $condition->{series_id}; + } - if ( ( defined $condition->{series_name} ) && ( $condition->{series_name} ne '' ) ) { - push @conditions, 'series_name=?'; - push @bind_values, $condition->{series_name}; - } + if ( ( defined $condition->{series_name} ) && ( $condition->{series_name} ne '' ) ) { + push @conditions, 'series_name=?'; + push @bind_values, $condition->{series_name}; + } - if ( ( defined $condition->{title} ) && ( $condition->{title} ne '' ) ) { - push @conditions, 'title=?'; - push @bind_values, $condition->{title}; - } + if ( ( defined $condition->{title} ) && ( $condition->{title} ne '' ) ) { + push @conditions, 'title=?'; + push @bind_values, $condition->{title}; + } - if ( ( defined $condition->{has_single_events} ) && ( $condition->{has_single_events} ne '' ) ) { - push @conditions, 'has_single_events=?'; - push @bind_values, $condition->{has_single_events}; - } + if ( ( defined $condition->{has_single_events} ) && ( $condition->{has_single_events} ne '' ) ) { + push @conditions, 'has_single_events=?'; + push @bind_values, $condition->{has_single_events}; + } - my $search_cond = ''; - if ( ( defined $condition->{search} ) && ( $condition->{search} ne '' ) ) { - my $search = lc $condition->{search}; - $search =~ s/[^a-z0-9\_\.\-\:\!öäüßÖÄÜ \&]/%/; - $search =~ s/\%+/\%/; - $search =~ s/^[\%\s]+//; - $search =~ s/[\%\s]+$//; - if ( $search ne '' ) { - $search = '%' . $search . '%'; - my @attr = ( 'title', 'series_name', 'excerpt', 'category', 'content' ); - push @conditions, "(" . join( " or ", map { 'lower(' . $_ . ') like ?' } @attr ) . ")"; - for my $attr (@attr) { - push @bind_values, $search; - } - } - } + my $search_cond = ''; + if ( ( defined $condition->{search} ) && ( $condition->{search} ne '' ) ) { + my $search = lc $condition->{search}; + $search =~ s/[^a-z0-9\_\.\-\:\!öäüßÖÄÜ \&]/%/; + $search =~ s/\%+/\%/; + $search =~ s/^[\%\s]+//; + $search =~ s/[\%\s]+$//; + if ( $search ne '' ) { + $search = '%' . $search . '%'; + my @attr = ( 'title', 'series_name', 'excerpt', 'category', 'content' ); + push @conditions, "(" . join( " or ", map { 'lower(' . $_ . ') like ?' } @attr ) . ")"; + for my $attr (@attr) { + push @bind_values, $search; + } + } + } - my $query = ''; - my $conditions = ''; + my $query = ''; + my $conditions = ''; - if ( ( defined $condition->{project_id} ) || ( defined $condition->{studio_id} ) ) { - if ( ( defined $condition->{project_id} ) && ( $condition->{project_id} ne '' ) ) { - push @conditions, 'ps.project_id=?'; - push @bind_values, $condition->{project_id}; - } + if ( ( defined $condition->{project_id} ) || ( defined $condition->{studio_id} ) ) { + if ( ( defined $condition->{project_id} ) && ( $condition->{project_id} ne '' ) ) { + push @conditions, 'ps.project_id=?'; + push @bind_values, $condition->{project_id}; + } - if ( ( defined $condition->{studio_id} ) && ( $condition->{studio_id} ne '' ) ) { - push @conditions, 'ps.studio_id=?'; - push @bind_values, $condition->{studio_id}; - } - push @conditions, 'ps.series_id=s.id'; - $conditions = " where " . join( " and ", @conditions ) if ( @conditions > 0 ); - $query = qq{ + if ( ( defined $condition->{studio_id} ) && ( $condition->{studio_id} ne '' ) ) { + push @conditions, 'ps.studio_id=?'; + push @bind_values, $condition->{studio_id}; + } + push @conditions, 'ps.series_id=s.id'; + $conditions = " where " . join( " and ", @conditions ) if ( @conditions > 0 ); + $query = qq{ select * from calcms_series s, calcms_project_series ps $conditions order by has_single_events desc, series_name, title }; - } else { + } else { - # simple query - $conditions = " where " . join( " and ", @conditions ) if ( @conditions > 0 ); - $query = qq{ + # simple query + $conditions = " where " . join( " and ", @conditions ) if ( @conditions > 0 ); + $query = qq{ select * from calcms_series $conditions order by has_single_events desc, series_name, title }; - } + } - my $dbh = db::connect($config); - my $series = db::get( $dbh, $query, \@bind_values ); + my $dbh = db::connect($config); + my $series = db::get( $dbh, $query, \@bind_values ); - #print STDERR Dumper(time()); - for my $serie (@$series) { - $serie->{series_id} = $serie->{id}; - delete $serie->{id}; - } + #print STDERR Dumper(time()); + for my $serie (@$series) { + $serie->{series_id} = $serie->{id}; + delete $serie->{id}; + } - #print STDERR Dumper($series); - return $series; + #print STDERR Dumper($series); + return $series; } # insert series sub insert { - my $config = shift; - my $series = shift; + my $config = shift; + my $series = shift; - #print STDERR Dumper($series); - return undef unless defined $series->{project_id}; - return undef unless defined $series->{studio_id}; + #print STDERR Dumper($series); + return undef unless defined $series->{project_id}; + return undef unless defined $series->{studio_id}; - my $project_id = $series->{project_id}; - my $studio_id = $series->{studio_id}; + my $project_id = $series->{project_id}; + my $studio_id = $series->{studio_id}; - my $columns = series::get_columns($config); + my $columns = series::get_columns($config); - my $entry = {}; - for my $column ( keys %$columns ) { - $entry->{$column} = $series->{$column} if defined $series->{$column}; - } - $entry->{image} = images::normalizeName( $entry->{image} ) if defined $entry->{image}; + my $entry = {}; + for my $column ( keys %$columns ) { + $entry->{$column} = $series->{$column} if defined $series->{$column}; + } + $entry->{image} = images::normalizeName( $entry->{image} ) if defined $entry->{image}; - $entry->{created_at} = time::time_to_datetime( time() ); - $entry->{modified_at} = time::time_to_datetime( time() ); + $entry->{created_at} = time::time_to_datetime( time() ); + $entry->{modified_at} = time::time_to_datetime( time() ); - #print STDERR Dumper($entry); + #print STDERR Dumper($entry); - my $dbh = db::connect($config); - my $series_id = db::insert( $dbh, 'calcms_series', $entry ); + my $dbh = db::connect($config); + my $series_id = db::insert( $dbh, 'calcms_series', $entry ); - return undef unless defined $series_id; + return undef unless defined $series_id; - my $result = project::assign_series( - $config, - { - project_id => $project_id, - studio_id => $studio_id, - series_id => $series_id - } - ); - return undef unless defined $result; - return $series_id; + my $result = project::assign_series( + $config, + { + project_id => $project_id, + studio_id => $studio_id, + series_id => $series_id + } + ); + return undef unless defined $result; + return $series_id; } # update series sub update { - my $config = shift; - my $series = shift; + my $config = shift; + my $series = shift; - return undef unless defined $series->{project_id}; - return undef unless defined $series->{studio_id}; - return undef unless defined $series->{series_id}; + return undef unless defined $series->{project_id}; + return undef unless defined $series->{studio_id}; + return undef unless defined $series->{series_id}; - my $columns = series::get_columns($config); + my $columns = series::get_columns($config); - my $entry = {}; - for my $column ( keys %$columns ) { - $entry->{$column} = $series->{$column} if defined $series->{$column}; - } - $entry->{image} = images::normalizeName( $entry->{image} ) if defined $entry->{image}; - $entry->{id} = $series->{series_id}; - $entry->{modified_at} = time::time_to_datetime( time() ); + my $entry = {}; + for my $column ( keys %$columns ) { + $entry->{$column} = $series->{$column} if defined $series->{$column}; + } + $entry->{image} = images::normalizeName( $entry->{image} ) if defined $entry->{image}; + $entry->{id} = $series->{series_id}; + $entry->{modified_at} = time::time_to_datetime( time() ); - my $values = join( ",", map { $_ . '=?' } ( keys %$entry ) ); - my @bind_values = map { $entry->{$_} } ( keys %$entry ); - push @bind_values, $entry->{id}; + my $values = join( ",", map { $_ . '=?' } ( keys %$entry ) ); + my @bind_values = map { $entry->{$_} } ( keys %$entry ); + push @bind_values, $entry->{id}; - my $query = qq{ + my $query = qq{ update calcms_series set $values where id=? }; - #print STDERR Dumper($query).Dumper(\@bind_values); + #print STDERR Dumper($query).Dumper(\@bind_values); - my $dbh = db::connect($config); - return db::put( $dbh, $query, \@bind_values ); + my $dbh = db::connect($config); + return db::put( $dbh, $query, \@bind_values ); } # delete series, its schedules and series dates # unassign its users and events sub delete { - my $config = shift; - my $series = shift; + my $config = shift; + my $series = shift; - return undef unless defined $series->{project_id}; - return undef unless defined $series->{studio_id}; - return undef unless defined $series->{series_id}; + return undef unless defined $series->{project_id}; + return undef unless defined $series->{studio_id}; + return undef unless defined $series->{series_id}; - my $project_id = $series->{project_id}; - my $studio_id = $series->{studio_id}; - my $series_id = $series->{series_id}; + my $project_id = $series->{project_id}; + my $studio_id = $series->{studio_id}; + my $series_id = $series->{series_id}; - unless ( project::is_series_assigned( $config, $series ) == 1 ) { - print STDERR "series is not assigned to project $project_id and studio $studio_id\n"; - return undef; - } + unless ( project::is_series_assigned( $config, $series ) == 1 ) { + print STDERR "series is not assigned to project $project_id and studio $studio_id\n"; + return undef; + } - my $query = undef; - my $bind_values = undef; - my $dbh = db::connect($config); + my $query = undef; + my $bind_values = undef; + my $dbh = db::connect($config); - $bind_values = [ $project_id, $studio_id, $series_id ]; + $bind_values = [ $project_id, $studio_id, $series_id ]; - #delete schedules - $query = qq{ + #delete schedules + $query = qq{ delete from calcms_series_schedule where project_id=? and studio_id=? and series_id=? }; - db::put( $dbh, $query, $bind_values ); + db::put( $dbh, $query, $bind_values ); - #delete series dates - $query = qq{ + #delete series dates + $query = qq{ delete from calcms_series_dates where project_id=? and studio_id=? and series_id=? }; - db::put( $dbh, $query, $bind_values ); + db::put( $dbh, $query, $bind_values ); - #unassign users - series::remove_user( - $config, - { - project_id => $project_id, - studio_id => $studio_id, - series_id => $series_id - } - ); + #unassign users + series::remove_user( + $config, + { + project_id => $project_id, + studio_id => $studio_id, + series_id => $series_id + } + ); - #unassign events - $bind_values = [ $project_id, $studio_id, $series_id ]; - $query = qq{ + #unassign events + $bind_values = [ $project_id, $studio_id, $series_id ]; + $query = qq{ delete from calcms_series_events where project_id=? and studio_id=? and series_id=? }; - #print '
$query'.$query.Dumper($bind_values).'
'; - db::put( $dbh, $query, $bind_values ); + #print '
$query'.$query.Dumper($bind_values).'
'; + db::put( $dbh, $query, $bind_values ); - project::unassign_series( - $config, - { - project_id => $project_id, - studio_id => $studio_id, - series_id => $series_id - } - ); + project::unassign_series( + $config, + { + project_id => $project_id, + studio_id => $studio_id, + series_id => $series_id + } + ); - #delete series + #delete series - my $series_assignments = project::get_series_assignments( - $config, - { - series_id => $series_id - } - ); - if ( @$series_assignments > 1 ) { - print STDERR "do not delete series, due to assigned to other project or studio"; - return; - } + my $series_assignments = project::get_series_assignments( + $config, + { + series_id => $series_id + } + ); + if ( scalar @$series_assignments > 1 ) { + print STDERR "do not delete series, due to assigned to other project or studio"; + return; + } - $bind_values = [$series_id]; - $query = qq{ + $bind_values = [$series_id]; + $query = qq{ delete from calcms_series where id=? }; - #print STDERR $query.$query.Dumper($bind_values); - db::put( $dbh, $query, $bind_values ); + #print STDERR $query.$query.Dumper($bind_values); + db::put( $dbh, $query, $bind_values ); } # get users directly assigned to project, studio, series (editors) sub get_users { - my $config = shift; - my $condition = shift; + my $config = shift; + my $condition = shift; - my @conditions = (); - my @bind_values = (); + my @conditions = (); + my @bind_values = (); - if ( ( defined $condition->{project_id} ) && ( $condition->{project_id} ne '' ) ) { - push @conditions, 'us.project_id=?'; - push @bind_values, $condition->{project_id}; - } + if ( ( defined $condition->{project_id} ) && ( $condition->{project_id} ne '' ) ) { + push @conditions, 'us.project_id=?'; + push @bind_values, $condition->{project_id}; + } - if ( ( defined $condition->{studio_id} ) && ( $condition->{studio_id} ne '' ) ) { - push @conditions, 'us.studio_id=?'; - push @bind_values, $condition->{studio_id}; - } + if ( ( defined $condition->{studio_id} ) && ( $condition->{studio_id} ne '' ) ) { + push @conditions, 'us.studio_id=?'; + push @bind_values, $condition->{studio_id}; + } - if ( ( defined $condition->{series_id} ) && ( $condition->{series_id} ne '' ) ) { - push @conditions, 'us.series_id=?'; - push @bind_values, $condition->{series_id}; - } + if ( ( defined $condition->{series_id} ) && ( $condition->{series_id} ne '' ) ) { + push @conditions, 'us.series_id=?'; + push @bind_values, $condition->{series_id}; + } - if ( ( defined $condition->{name} ) && ( $condition->{name} ne '' ) ) { - push @conditions, 'u.name=?'; - push @bind_values, $condition->{name}; - } + if ( ( defined $condition->{name} ) && ( $condition->{name} ne '' ) ) { + push @conditions, 'u.name=?'; + push @bind_values, $condition->{name}; + } - my $conditions = ''; - $conditions = " and " . join( " and ", @conditions ) if ( @conditions > 0 ); + my $conditions = ''; + $conditions = " and " . join( " and ", @conditions ) if ( @conditions > 0 ); - my $query = qq{ + my $query = qq{ select u.id, u.name, u.full_name, u.email, us.modified_by, us.modified_at from calcms_users u, calcms_user_series us where us.user_id=u.id $conditions }; - #print STDERR $query." ".Dumper(\@bind_values)."\n"; - my $dbh = db::connect($config); - my $result = db::get( $dbh, $query, \@bind_values ); + #print STDERR $query." ".Dumper(\@bind_values)."\n"; + my $dbh = db::connect($config); + my $result = db::get( $dbh, $query, \@bind_values ); - #print STDERR $query." ".Dumper($result)."\n"; - return $result; + #print STDERR $query." ".Dumper($result)."\n"; + return $result; } # assign user to series sub add_user { - my $config = shift; - my $entry = shift; + my $config = shift; + my $entry = shift; - return unless defined $entry->{project_id}; - return unless defined $entry->{studio_id}; - return unless defined $entry->{series_id}; - return unless defined $entry->{user_id}; - return unless defined $entry->{user}; + return unless defined $entry->{project_id}; + return unless defined $entry->{studio_id}; + return unless defined $entry->{series_id}; + return unless defined $entry->{user_id}; + return unless defined $entry->{user}; - my $query = qq{ + my $query = qq{ select id from calcms_user_series where project_id=? and studio_id=? and series_id=? and user_id=? }; - my $bind_values = [ $entry->{project_id}, $entry->{studio_id}, $entry->{series_id}, $entry->{user_id} ]; + my $bind_values = [ $entry->{project_id}, $entry->{studio_id}, $entry->{series_id}, $entry->{user_id} ]; - my $dbh = db::connect($config); - my $results = db::get( $dbh, $query, $bind_values ); - return unless ( @$results == 0 ); + my $dbh = db::connect($config); + my $results = db::get( $dbh, $query, $bind_values ); + return unless scalar @$results == 0; - $query = qq{ + $query = qq{ insert calcms_user_series set project_id=?, studio_id=?, series_id=?, user_id=?, modified_by=?, modified_at=now() }; - $bind_values = [ $entry->{project_id}, $entry->{studio_id}, $entry->{series_id}, $entry->{user_id}, $entry->{user} ]; - db::put( $dbh, $query, $bind_values ); + $bind_values = [ $entry->{project_id}, $entry->{studio_id}, $entry->{series_id}, $entry->{user_id}, $entry->{user} ]; + db::put( $dbh, $query, $bind_values ); } # remove user(s) from series. sub remove_user { - my $config = shift; - my $condition = shift; + my $config = shift; + my $condition = shift; - return unless ( defined $condition->{project_id} ); - return unless ( defined $condition->{studio_id} ); - return unless ( defined $condition->{series_id} ); + return unless ( defined $condition->{project_id} ); + return unless ( defined $condition->{studio_id} ); + return unless ( defined $condition->{series_id} ); - my @conditions = (); - my @bind_values = (); + my @conditions = (); + my @bind_values = (); - if ( ( defined $condition->{project_id} ) && ( $condition->{project_id} ne '' ) ) { - push @conditions, 'project_id=?'; - push @bind_values, $condition->{project_id}; - } + if ( ( defined $condition->{project_id} ) && ( $condition->{project_id} ne '' ) ) { + push @conditions, 'project_id=?'; + push @bind_values, $condition->{project_id}; + } - if ( ( defined $condition->{studio_id} ) && ( $condition->{studio_id} ne '' ) ) { - push @conditions, 'studio_id=?'; - push @bind_values, $condition->{studio_id}; - } + if ( ( defined $condition->{studio_id} ) && ( $condition->{studio_id} ne '' ) ) { + push @conditions, 'studio_id=?'; + push @bind_values, $condition->{studio_id}; + } - if ( ( defined $condition->{series_id} ) && ( $condition->{series_id} ne '' ) ) { - push @conditions, 'series_id=?'; - push @bind_values, $condition->{series_id}; - } + if ( ( defined $condition->{series_id} ) && ( $condition->{series_id} ne '' ) ) { + push @conditions, 'series_id=?'; + push @bind_values, $condition->{series_id}; + } - if ( ( defined $condition->{user_id} ) && ( $condition->{user_id} ne '' ) ) { - push @conditions, 'user_id=?'; - push @bind_values, $condition->{user_id}; - } + if ( ( defined $condition->{user_id} ) && ( $condition->{user_id} ne '' ) ) { + push @conditions, 'user_id=?'; + push @bind_values, $condition->{user_id}; + } - my $conditions = ''; - $conditions = join( " and ", @conditions ) if ( @conditions > 0 ); + my $conditions = ''; + $conditions = join( " and ", @conditions ) if ( @conditions > 0 ); - my $query = qq{ + my $query = qq{ delete from calcms_user_series where $conditions }; - my $dbh = db::connect($config); - db::put( $dbh, $query, \@bind_values ); + my $dbh = db::connect($config); + db::put( $dbh, $query, \@bind_values ); } #search events by series_name and title (for events not assigned yet) #TODO: add location sub search_events { - my $config = shift; - my $request = shift; - my $options = shift; + my $config = shift; + my $request = shift; + my $options = shift; - my $series_name = $options->{series_name} || ''; - my $title = $options->{title} || ''; - return undef if ( ( $series_name eq '' ) && ( $title eq '' ) ); + my $series_name = $options->{series_name} || ''; + my $title = $options->{title} || ''; + return undef if ( $series_name eq '' ) && ( $title eq '' ); - $series_name =~ s/[^a-zA-Z0-9 \-]+/\?/g; - $title =~ s/[^a-zA-Z0-9 \-]+/\?/g; + $series_name =~ s/[^a-zA-Z0-9 \-]+/\?/g; + $title =~ s/[^a-zA-Z0-9 \-]+/\?/g; - $series_name =~ s/\?+/\?/g; - $title =~ s/\?+/\?/g; + $series_name =~ s/\?+/\?/g; + $title =~ s/\?+/\?/g; - my $params = { - series_name => $series_name, - title => $title, - template => 'no' - }; - if ( defined $options ) { - $params->{from_date} = $options->{from_date} if ( defined $options->{from_date} ); - $params->{till_date} = $options->{till_date} if ( defined $options->{till_date} ); - $params->{location} = $options->{location} if ( defined $options->{location} ); - $params->{limit} = $options->{limit} if ( defined $options->{limit} ); - $params->{archive} = $options->{archive} if ( defined $options->{archive} ); - $params->{get} = $options->{get} if ( defined $options->{get} ); - } + my $params = { + series_name => $series_name, + title => $title, + template => 'no' + }; + if ( defined $options ) { + $params->{from_date} = $options->{from_date} if ( defined $options->{from_date} ); + $params->{till_date} = $options->{till_date} if ( defined $options->{till_date} ); + $params->{location} = $options->{location} if ( defined $options->{location} ); + $params->{limit} = $options->{limit} if ( defined $options->{limit} ); + $params->{archive} = $options->{archive} if ( defined $options->{archive} ); + $params->{get} = $options->{get} if ( defined $options->{get} ); + } - my $checked_params = events::check_params( $config, $params ); + my $checked_params = events::check_params( $config, $params ); - #print STDERR '
'.Dumper($checked_params).'
'; - my $request2 = { - params => { - checked => $checked_params - }, - config => $request->{config}, - permissions => $request->{permissions} - }; + #print STDERR '
'.Dumper($checked_params).'
'; + my $request2 = { + params => { + checked => $checked_params + }, + config => $request->{config}, + permissions => $request->{permissions} + }; - #my $debug=1; - #print STDERR Dumper($request2->{params}); - my $events = events::get( $config, $request2 ); + #my $debug=1; + #print STDERR Dumper($request2->{params}); + my $events = events::get( $config, $request2 ); - #print Dumper($events); - return $events; + #print Dumper($events); + return $events; } #get events (only assigned ones) by project_id,studio_id,series_id, sub get_events { - my $config = shift; - my $options = shift; + my $config = shift; + my $options = shift; - #print STDERR Dumper($options); - return [] if defined( $options->{series_id} ) && ( $options->{series_id} <= 0 ); + #print STDERR Dumper($options); + return [] if defined( $options->{series_id} ) && ( $options->{series_id} <= 0 ); - my @conditions = (); - my @bind_values = (); + my @conditions = (); + my @bind_values = (); - if ( defined $options->{project_id} ) { - push @conditions, 'se.project_id = ?'; - push @bind_values, $options->{project_id}; - } - if ( defined $options->{studio_id} ) { - push @conditions, 'se.studio_id = ?'; - push @bind_values, $options->{studio_id}; - } - if ( ( defined $options->{series_id} ) && ( $options->{series_id} =~ /(\d+)/ ) ) { - push @bind_values, $1; - push @conditions, 'se.series_id = ?'; - } + if ( defined $options->{project_id} ) { + push @conditions, 'se.project_id = ?'; + push @bind_values, $options->{project_id}; + } + if ( defined $options->{studio_id} ) { + push @conditions, 'se.studio_id = ?'; + push @bind_values, $options->{studio_id}; + } + if ( ( defined $options->{series_id} ) && ( $options->{series_id} =~ /(\d+)/ ) ) { + push @bind_values, $1; + push @conditions, 'se.series_id = ?'; + } - if ( defined $options->{event_id} ) { - push @bind_values, $options->{event_id}; - push @conditions, 'e.id = ?'; - } + if ( defined $options->{event_id} ) { + push @bind_values, $options->{event_id}; + push @conditions, 'e.id = ?'; + } - if ( ( defined $options->{from_date} ) && ( $options->{from_date} =~ /(\d\d\d\d\-\d\d\-\d\d)/ ) ) { - push @bind_values, $1; - push @conditions, 'e.start_date >= ?'; - } - if ( ( defined $options->{till_date} ) && ( $options->{till_date} =~ /(\d\d\d\d\-\d\d\-\d\d)/ ) ) { - push @bind_values, $1; - push @conditions, 'e.start_date <= ?'; - } - if ( defined $options->{location} ) { - push @conditions, 'e.location = ?'; - push @bind_values, $options->{location}; - } + if ( ( defined $options->{from_date} ) && ( $options->{from_date} =~ /(\d\d\d\d\-\d\d\-\d\d)/ ) ) { + push @bind_values, $1; + push @conditions, 'e.start_date >= ?'; + } + if ( ( defined $options->{till_date} ) && ( $options->{till_date} =~ /(\d\d\d\d\-\d\d\-\d\d)/ ) ) { + push @bind_values, $1; + push @conditions, 'e.start_date <= ?'; + } + if ( defined $options->{location} ) { + push @conditions, 'e.location = ?'; + push @bind_values, $options->{location}; + } - if ( defined $options->{draft} ) { - push @conditions, 'e.draft = ?'; - push @bind_values, $options->{draft}; - } + if ( defined $options->{draft} ) { + push @conditions, 'e.draft = ?'; + push @bind_values, $options->{draft}; + } - my $conditions = ''; - if ( @conditions > 0 ) { - $conditions = ' and ' . join( ' and ', @conditions ); - } + my $conditions = ''; + if ( @conditions > 0 ) { + $conditions = ' and ' . join( ' and ', @conditions ); + } - my $limit = ''; - if ( ( defined $options->{limit} ) && ( $limit =~ /(\d+)/ ) ) { - $limit = 'limit ' . $1; - } + my $limit = ''; + if ( ( defined $options->{limit} ) && ( $limit =~ /(\d+)/ ) ) { + $limit = 'limit ' . $1; + } - my $query = qq{ + my $query = qq{ select * ,date(start) start_date ,date(end) end_date @@ -544,145 +544,145 @@ sub get_events { $limit }; - #print STDERR '
'.$query.Dumper(\@bind_values).'
'; + #print STDERR '
'.$query.Dumper(\@bind_values).'
'; - my $dbh = db::connect($config); - my $results = db::get( $dbh, $query, \@bind_values ); + my $dbh = db::connect($config); + my $results = db::get( $dbh, $query, \@bind_values ); - #print STDERR Dumper($results); + #print STDERR Dumper($results); - $results = events::modify_results( - $dbh, $config, - { - base_url => '', - params => { - checked => { - template => '' - } - } - }, - $results - ); + $results = events::modify_results( + $dbh, $config, + { + base_url => '', + params => { + checked => { + template => '' + } + } + }, + $results + ); - #add studio id to events - my $studios = studios::get( $config, $options ); + #add studio id to events + my $studios = studios::get( $config, $options ); - my $studio_id_by_location = {}; - for my $studio (@$studios) { - $studio_id_by_location->{ $studio->{location} } = $studio->{id}; - } - for my $result (@$results) { - $result->{studio_id} = $studio_id_by_location->{ $result->{location} }; - } + my $studio_id_by_location = {}; + for my $studio (@$studios) { + $studio_id_by_location->{ $studio->{location} } = $studio->{id}; + } + for my $result (@$results) { + $result->{studio_id} = $studio_id_by_location->{ $result->{location} }; + } - #print STDERR Dumper($results); - return $results; + #print STDERR Dumper($results); + return $results; } # load event given by studio_id, series_id and event_id # helper for gui - errors are written to gui output # return undef on error sub get_event { - my $config = shift; - my $options = shift; + my $config = shift; + my $options = shift; - my $project_id = $options->{project_id} || ''; - my $studio_id = $options->{studio_id} || ''; - my $series_id = $options->{series_id} || ''; - my $event_id = $options->{event_id} || ''; - my $draft = $options->{draft} || ''; + my $project_id = $options->{project_id} || ''; + my $studio_id = $options->{studio_id} || ''; + my $series_id = $options->{series_id} || ''; + my $event_id = $options->{event_id} || ''; + my $draft = $options->{draft} || ''; - unless ( defined $options->{allow_any} ) { - if ( $project_id eq '' ) { - uac::print_error("missing project_id"); - return undef; - } - if ( $studio_id eq '' ) { - uac::print_error("missing studio_id"); - return undef; - } - if ( $series_id eq '' ) { - uac::print_error("missing series_id"); - return undef; - } - } + unless ( defined $options->{allow_any} ) { + if ( $project_id eq '' ) { + uac::print_error("missing project_id"); + return undef; + } + if ( $studio_id eq '' ) { + uac::print_error("missing studio_id"); + return undef; + } + if ( $series_id eq '' ) { + uac::print_error("missing series_id"); + return undef; + } + } - if ( $event_id eq '' ) { - uac::print_error("missing event_id"); - return undef; - } + if ( $event_id eq '' ) { + uac::print_error("missing event_id"); + return undef; + } - my $queryOptions = {}; - $queryOptions->{project_id} = $project_id if $project_id ne ''; - $queryOptions->{studio_id} = $studio_id if $studio_id ne ''; - $queryOptions->{series_id} = $series_id if $series_id ne ''; - $queryOptions->{event_id} = $event_id if $event_id ne ''; - $queryOptions->{draft} = $draft if $draft ne ''; + my $queryOptions = {}; + $queryOptions->{project_id} = $project_id if $project_id ne ''; + $queryOptions->{studio_id} = $studio_id if $studio_id ne ''; + $queryOptions->{series_id} = $series_id if $series_id ne ''; + $queryOptions->{event_id} = $event_id if $event_id ne ''; + $queryOptions->{draft} = $draft if $draft ne ''; - my $events = series::get_events( $config, $queryOptions ); + my $events = series::get_events( $config, $queryOptions ); - unless ( defined $events ) { - uac::print_error("error on loading event"); - return undef; - } + unless ( defined $events ) { + uac::print_error("error on loading event"); + return undef; + } - if ( scalar(@$events) == 0 ) { - uac::print_error("event not found"); - return undef; - } + if ( scalar(@$events) == 0 ) { + uac::print_error("event not found"); + return undef; + } - if ( scalar(@$events) > 1 ) { - print STDERR q{multiple assignments found for } - . q{project_id=} - . $options->{project_id} - . q{, studio_id=} - . $options->{studio_id} - . q{, series_id=} - . $options->{series_id} - . q{, event_id=} - . $options->{event_id} . "\n"; - } - my $event = $events->[0]; - return $event; + if ( scalar(@$events) > 1 ) { + print STDERR q{multiple assignments found for } + . q{project_id=} + . $options->{project_id} + . q{, studio_id=} + . $options->{studio_id} + . q{, series_id=} + . $options->{series_id} + . q{, event_id=} + . $options->{event_id} . "\n"; + } + my $event = $events->[0]; + return $event; } # get name and title of series and age in days ('days_over') sub get_event_age { - my $config = shift; - my $options = shift; + my $config = shift; + my $options = shift; - #print STDERR Dumper($options); - return undef unless defined $options->{project_id}; - return undef unless defined $options->{studio_id}; + #print STDERR Dumper($options); + return undef unless defined $options->{project_id}; + return undef unless defined $options->{studio_id}; - my @conditions = (); - my @bind_values = (); + my @conditions = (); + my @bind_values = (); - if ( ( defined $options->{project_id} ) && ( $options->{project_id} =~ /(\d+)/ ) ) { - push @bind_values, $1; - push @conditions, 'ps.project_id = ?'; - } + if ( ( defined $options->{project_id} ) && ( $options->{project_id} =~ /(\d+)/ ) ) { + push @bind_values, $1; + push @conditions, 'ps.project_id = ?'; + } - if ( ( defined $options->{studio_id} ) && ( $options->{studio_id} =~ /(\d+)/ ) ) { - push @bind_values, $1; - push @conditions, 'ps.studio_id = ?'; - } + if ( ( defined $options->{studio_id} ) && ( $options->{studio_id} =~ /(\d+)/ ) ) { + push @bind_values, $1; + push @conditions, 'ps.studio_id = ?'; + } - if ( ( defined $options->{series_id} ) && ( $options->{series_id} =~ /(\d+)/ ) ) { - push @bind_values, $1; - push @conditions, 's.id = ?'; - } + if ( ( defined $options->{series_id} ) && ( $options->{series_id} =~ /(\d+)/ ) ) { + push @bind_values, $1; + push @conditions, 's.id = ?'; + } - if ( ( defined $options->{event_id} ) && ( $options->{event_id} =~ /(\d+)/ ) ) { - push @bind_values, $1; - push @conditions, 'e.id = ?'; - } + if ( ( defined $options->{event_id} ) && ( $options->{event_id} =~ /(\d+)/ ) ) { + push @bind_values, $1; + push @conditions, 'e.id = ?'; + } - my $conditions = ''; - if ( @conditions > 0 ) { - $conditions = join( ' and ', @conditions ); - } - my $query = qq{ + my $conditions = ''; + if ( @conditions > 0 ) { + $conditions = join( ' and ', @conditions ); + } + my $query = qq{ select s.id series_id, s.series_name, s.title, s.has_single_events has_single_events, (to_days(now())-to_days(max(e.start))) days_over from calcms_project_series ps left join calcms_series s on ps.series_id=s.id @@ -693,141 +693,141 @@ sub get_event_age { order by has_single_events desc, days_over }; - #print STDERR $query." ".Dumper(\@bind_values); - my $dbh = db::connect($config); - my $results = db::get( $dbh, $query, \@bind_values ); + #print STDERR $query." ".Dumper(\@bind_values); + my $dbh = db::connect($config); + my $results = db::get( $dbh, $query, \@bind_values ); - for my $result (@$results) { - $result->{days_over} = 0 unless defined $result->{days_over}; - } - return $results; + for my $result (@$results) { + $result->{days_over} = 0 unless defined $result->{days_over}; + } + return $results; } # is event older than max_age days sub is_event_older_than_days { - my $config = shift; - my $options = shift; + my $config = shift; + my $options = shift; - #print STDERR Dumper($options); + #print STDERR Dumper($options); - return 1 unless defined $options->{project_id}; - return 1 unless defined $options->{studio_id}; - return 1 unless defined $options->{series_id}; - return 1 unless defined $options->{event_id}; - return 1 unless defined $options->{max_age}; + return 1 unless defined $options->{project_id}; + return 1 unless defined $options->{studio_id}; + return 1 unless defined $options->{series_id}; + return 1 unless defined $options->{event_id}; + return 1 unless defined $options->{max_age}; - my $events = series::get_event_age( - $config, - { - project_id => $options->{project_id}, - studio_id => $options->{studio_id}, - series_id => $options->{series_id}, - event_id => $options->{event_id} - } - ); + my $events = series::get_event_age( + $config, + { + project_id => $options->{project_id}, + studio_id => $options->{studio_id}, + series_id => $options->{series_id}, + event_id => $options->{event_id} + } + ); - if ( scalar(@$events) == 0 ) { - print STDERR + if ( scalar(@$events) == 0 ) { + print STDERR "series_events::event_over_in_days: event $options->{event_id} is not assigned to project $options->{project_id}, studio $options->{studio_id}, series $options->{series_id}\n"; - return 1; - } - my $event = $events->[0]; + return 1; + } + my $event = $events->[0]; - #print STDERR Dumper($event); - return 1 if $event->{days_over} > $options->{max_age}; - return 0; + #print STDERR Dumper($event); + return 1 if $event->{days_over} > $options->{max_age}; + return 0; } sub get_next_episode { - my $config = shift; - my $options = shift; + my $config = shift; + my $options = shift; - return 0 unless defined $options->{project_id}; - return 0 unless defined $options->{studio_id}; - return 0 unless defined $options->{series_id}; + return 0 unless defined $options->{project_id}; + return 0 unless defined $options->{studio_id}; + return 0 unless defined $options->{series_id}; - #return if episodes should not be counted for this series - my $query = q{ + #return if episodes should not be counted for this series + my $query = q{ select count_episodes from calcms_series where id=? }; - my $bind_values = [ $options->{series_id} ]; - my $dbh = db::connect($config); - my $results = db::get( $dbh, $query, $bind_values ); - return 0 if ( @$results != 1 ); - return 0 if ( $results->[0]->{count_episodes} eq '0' ); + my $bind_values = [ $options->{series_id} ]; + my $dbh = db::connect($config); + my $results = db::get( $dbh, $query, $bind_values ); + return 0 if ( @$results != 1 ); + return 0 if ( $results->[0]->{count_episodes} eq '0' ); - #print STDERR Dumper($results); + #print STDERR Dumper($results); - #get all - $query = q{ + #get all + $query = q{ select title,episode from calcms_events e, calcms_series_events se where se.project_id=? and se.studio_id=? and se.series_id=? and se.event_id=e.id }; - $bind_values = [ $options->{project_id}, $options->{studio_id}, $options->{series_id} ]; - $results = db::get( $dbh, $query, $bind_values ); + $bind_values = [ $options->{project_id}, $options->{studio_id}, $options->{series_id} ]; + $results = db::get( $dbh, $query, $bind_values ); - my $max = 0; - for my $result (@$results) { - if ( $result->{title} =~ /\#(\d+)/ ) { - my $value = $1; - $max = $value if $value > $max; - } - next unless defined $result->{episode}; - $max = $result->{episode} if $result->{episode} > $max; - } - return $max + 1; + my $max = 0; + for my $result (@$results) { + if ( $result->{title} =~ /\#(\d+)/ ) { + my $value = $1; + $max = $value if $value > $max; + } + next unless defined $result->{episode}; + $max = $result->{episode} if $result->{episode} > $max; + } + return $max + 1; } sub get_images { - my $config = shift; - my $options = shift; + my $config = shift; + my $options = shift; - return undef unless defined $options->{project_id}; - return undef unless defined $options->{studio_id}; - return undef unless defined $options->{series_id}; + return undef unless defined $options->{project_id}; + return undef unless defined $options->{studio_id}; + return undef unless defined $options->{series_id}; - #get images from all events of the series - my $dbh = db::connect($config); - my $events = series::get_events( - $config, - { - project_id => $options->{project_id}, - studio_id => $options->{studio_id}, - series_id => $options->{series_id} - } - ); - my $images = {}; - my $found = 0; - for my $event (@$events) { - my $image = $event->{image}; - $image =~ s/.*\///; - $images->{$image} = 1; - $found++; - } + #get images from all events of the series + my $dbh = db::connect($config); + my $events = series::get_events( + $config, + { + project_id => $options->{project_id}, + studio_id => $options->{studio_id}, + series_id => $options->{series_id} + } + ); + my $images = {}; + my $found = 0; + for my $event (@$events) { + my $image = $event->{image}; + $image =~ s/.*\///; + $images->{$image} = 1; + $found++; + } - return undef if $found == 0; + return undef if $found == 0; - # get all images from database - my @cond = (); - my $bind_values = []; - for my $image ( keys %$images ) { - push @cond, 'filename=?'; - push @$bind_values, $image; - } + # get all images from database + my @cond = (); + my $bind_values = []; + for my $image ( keys %$images ) { + push @cond, 'filename=?'; + push @$bind_values, $image; + } - my $where = ''; - if ( @cond > 0 ) { - $where = 'where ' . join( ' or ', @cond ); - } + my $where = ''; + if ( @cond > 0 ) { + $where = 'where ' . join( ' or ', @cond ); + } - my $limit = ''; - if ( ( defined $options->{limit} ) && ( $options->{limit} =~ /(\d+)/ ) ) { - $limit = ' limit ' . $1; - } + my $limit = ''; + if ( ( defined $options->{limit} ) && ( $options->{limit} =~ /(\d+)/ ) ) { + $limit = ' limit ' . $1; + } - my $query = qq{ + my $query = qq{ select * from calcms_images $where @@ -835,460 +835,460 @@ sub get_images { $limit }; - #print STDERR Dumper($query).Dumper($bind_values); - my $results = db::get( $dbh, $query, $bind_values ); + #print STDERR Dumper($query).Dumper($bind_values); + my $results = db::get( $dbh, $query, $bind_values ); - #print STDERR @$results."\n"; - return $results; + #print STDERR @$results."\n"; + return $results; } #assign event to series #TODO: manual assign needs to update automatic one sub assign_event { - my $config = shift; - my $entry = shift; + my $config = shift; + my $entry = shift; - #print STDERR Dumper($entry); - return undef unless defined $entry->{project_id}; - return undef unless defined $entry->{studio_id}; - return undef unless defined $entry->{series_id}; - return undef unless defined $entry->{event_id}; - $entry->{manual} = 0 unless ( ( defined $entry->{manual} ) && ( $entry->{manual} eq '1' ) ); + #print STDERR Dumper($entry); + return undef unless defined $entry->{project_id}; + return undef unless defined $entry->{studio_id}; + return undef unless defined $entry->{series_id}; + return undef unless defined $entry->{event_id}; + $entry->{manual} = 0 unless ( defined $entry->{manual} ) && ( $entry->{manual} eq '1' ); - my $conditions = ''; - $conditions = 'and manual=1' if ( $entry->{manual} eq '1' ); + my $conditions = ''; + $conditions = 'and manual=1' if $entry->{manual} eq '1'; - my $query = qq{ + my $query = qq{ select * from calcms_series_events where project_id=? and studio_id=? and series_id=? and event_id=? $conditions }; - my $bind_values = [ $entry->{project_id}, $entry->{studio_id}, $entry->{series_id}, $entry->{event_id} ]; - my $dbh = db::connect($config); - my $results = db::get( $dbh, $query, $bind_values ); + my $bind_values = [ $entry->{project_id}, $entry->{studio_id}, $entry->{series_id}, $entry->{event_id} ]; + my $dbh = db::connect($config); + my $results = db::get( $dbh, $query, $bind_values ); - if ( @$results > 1 ) { - print STDERR + if ( scalar @$results > 1 ) { + print STDERR "multiple assignments of project_id=$entry->{project_id}, studio_id=$entry->{studio_id}, series_id=$entry->{series_id}, event_id=$entry->{event_id}\n"; - return; - } - if ( @$results == 1 ) { - print STDERR + return; + } + if ( scalar @$results == 1 ) { + print STDERR "already assigned: project_id=$entry->{project_id}, studio_id=$entry->{studio_id}, series_id=$entry->{series_id}, event_id=$entry->{event_id}\n"; - return; - } + return; + } - $query = qq{ + $query = qq{ insert into calcms_series_events (project_id, studio_id, series_id, event_id, manual) values (?,?,?,?,?) }; - $bind_values = [ $entry->{project_id}, $entry->{studio_id}, $entry->{series_id}, $entry->{event_id}, $entry->{manual} ]; + $bind_values = [ $entry->{project_id}, $entry->{studio_id}, $entry->{series_id}, $entry->{event_id}, $entry->{manual} ]; - #print STDERR '
'.$query.Dumper($bind_values).'
'; - return db::put( $dbh, $query, $bind_values ); + #print STDERR '
'.$query.Dumper($bind_values).'
'; + return db::put( $dbh, $query, $bind_values ); } #unassign event from series sub unassign_event { - my $config = shift; - my $entry = shift; + my $config = shift; + my $entry = shift; - return unless defined $entry->{project_id}; - return unless defined $entry->{studio_id}; - return unless defined $entry->{series_id}; - return unless defined $entry->{event_id}; + return unless defined $entry->{project_id}; + return unless defined $entry->{studio_id}; + return unless defined $entry->{series_id}; + return unless defined $entry->{event_id}; - my $conditions = ''; - $conditions = 'and manual=1' if ( ( defined $entry->{manual} ) && ( $entry->{manual} eq '1' ) ); + my $conditions = ''; + $conditions = 'and manual=1' if ( defined $entry->{manual} ) && ( $entry->{manual} eq '1' ); - my $query = qq{ + my $query = qq{ delete from calcms_series_events where project_id=? and studio_id=? and series_id=? and event_id=? $conditions }; - my $bind_values = [ $entry->{project_id}, $entry->{studio_id}, $entry->{series_id}, $entry->{event_id} ]; + my $bind_values = [ $entry->{project_id}, $entry->{studio_id}, $entry->{series_id}, $entry->{event_id} ]; - #print STDERR '
'.$query.Dumper($bind_values).'
'; - my $dbh = db::connect($config); - return db::put( $dbh, $query, $bind_values ); + #print STDERR '
'.$query.Dumper($bind_values).'
'; + my $dbh = db::connect($config); + return db::put( $dbh, $query, $bind_values ); } # put series id to given events (for legacy handling) # used by calendar # TODO: optionally add project_id and studio_id to conditions sub add_series_ids_to_events { - my $config = shift; - my $events = shift; + my $config = shift; + my $events = shift; - #get event ids from given events - my @event_ids = (); - for my $event (@$events) { - push @event_ids, $event->{event_id}; - } + #get event ids from given events + my @event_ids = (); + for my $event (@$events) { + push @event_ids, $event->{event_id}; + } - return if ( @event_ids == 0 ); + return if scalar @event_ids == 0; - my @bind_values = @event_ids; - my $event_ids = join( ',', map { '?' } @event_ids ); + my @bind_values = @event_ids; + my $event_ids = join( ',', map { '?' } @event_ids ); - #query series ids - my $dbh = db::connect($config); - my $query = qq{ + #query series ids + my $dbh = db::connect($config); + my $query = qq{ select project_id, studio_id, series_id, event_id from calcms_series_events where event_id in ($event_ids) }; - my $results = db::get( $dbh, $query, \@bind_values ); - my @results = @$results; - return [] unless ( @results > 0 ); + my $results = db::get( $dbh, $query, \@bind_values ); + my @results = @$results; + return [] unless scalar @results > 0; - #build hash of series ids by event ids - my $assignments_by_event_id = {}; - for my $entry (@$results) { - my $event_id = $entry->{event_id}; - $assignments_by_event_id->{$event_id} = $entry; - } + #build hash of series ids by event ids + my $assignments_by_event_id = {}; + for my $entry (@$results) { + my $event_id = $entry->{event_id}; + $assignments_by_event_id->{$event_id} = $entry; + } - #fill in ids into events - for my $event (@$events) { - my $event_id = $event->{event_id}; - my $assignment = $assignments_by_event_id->{$event_id}; - if ( defined $assignment ) { - $event->{project_id} = $assignment->{project_id}; - $event->{studio_id} = $assignment->{studio_id}; - $event->{series_id} = $assignment->{series_id}; - } - } + #fill in ids into events + for my $event (@$events) { + my $event_id = $event->{event_id}; + my $assignment = $assignments_by_event_id->{$event_id}; + if ( defined $assignment ) { + $event->{project_id} = $assignment->{project_id}; + $event->{studio_id} = $assignment->{studio_id}; + $event->{series_id} = $assignment->{series_id}; + } + } } # add event_ids to series and remove all event ids from series, not given event_ids # for scan only, used at series sub set_event_ids { - my $config = shift; - my $project_id = shift; - my $studio_id = shift; - my $serie = shift; - my $event_ids = shift; + my $config = shift; + my $project_id = shift; + my $studio_id = shift; + my $serie = shift; + my $event_ids = shift; - my $serie_id = $serie->{series_id}; - return unless defined $project_id; - return unless defined $studio_id; - return unless defined $serie_id; - return unless defined $event_ids; + my $serie_id = $serie->{series_id}; + return unless defined $project_id; + return unless defined $studio_id; + return unless defined $serie_id; + return unless defined $event_ids; - #make lookup table from events - my $event_id_hash = {}; - for my $event_id (@$event_ids) { - $event_id_hash->{$event_id} = 1; - } + #make lookup table from events + my $event_id_hash = {}; + for my $event_id (@$event_ids) { + $event_id_hash->{$event_id} = 1; + } - #get series_entries from db - #my $bind_names=join(',', (map { '?' } @$event_ids)); - my $query = qq{ + #get series_entries from db + #my $bind_names=join(',', (map { '?' } @$event_ids)); + my $query = qq{ select event_id from calcms_series_events where project_id=? and studio_id=? and series_id=? }; - my $bind_values = [ $project_id, $studio_id, $serie_id ]; + my $bind_values = [ $project_id, $studio_id, $serie_id ]; - my $dbh = db::connect($config); - my $results = db::get( $dbh, $query, $bind_values ); + my $dbh = db::connect($config); + my $results = db::get( $dbh, $query, $bind_values ); - my $found = {}; + my $found = {}; - #mark events found assigned to series - my $i = 1; - for my $event (@$results) { + #mark events found assigned to series + my $i = 1; + for my $event (@$results) { - #print "found event $i: $event->{event_id}\n"; - $found->{ $event->{event_id} } = 1; - $i++; - } + #print "found event $i: $event->{event_id}\n"; + $found->{ $event->{event_id} } = 1; + $i++; + } - #insert events from list, not found in db - for my $event_id (@$event_ids) { + #insert events from list, not found in db + for my $event_id (@$event_ids) { - #print "insert event_id $event_id\n"; - series::assign_event( - $config, - { - project_id => $project_id, - studio_id => $studio_id, - series_id => $serie_id, - event_id => $event_id - } - ) unless ( $found->{$event_id} ); - } + #print "insert event_id $event_id\n"; + series::assign_event( + $config, + { + project_id => $project_id, + studio_id => $studio_id, + series_id => $serie_id, + event_id => $event_id + } + ) unless ( $found->{$event_id} ); + } - #delete events found in db, but not in list - for my $event_id ( keys %$found ) { + #delete events found in db, but not in list + for my $event_id ( keys %$found ) { - #print "delete event_id $event_id\n"; - series::unassign_event( - $config, - { - project_id => $project_id, - studio_id => $studio_id, - series_id => $serie_id, - event_id => $event_id, - manual => 0 - } - ) unless ( defined $event_id_hash->{$event_id} ); - } + #print "delete event_id $event_id\n"; + series::unassign_event( + $config, + { + project_id => $project_id, + studio_id => $studio_id, + series_id => $serie_id, + event_id => $event_id, + manual => 0 + } + ) unless defined $event_id_hash->{$event_id}; + } } # check if user allowed to update series events # evaluate permissions and consider editors directly assigned to series sub can_user_update_events { - my $request = shift; - my $options = shift; + my $request = shift; + my $options = shift; - my $config = $request->{config}; - my $permissions = $request->{permissions}; + my $config = $request->{config}; + my $permissions = $request->{permissions}; - return 0 unless defined $request->{user}; - return 0 unless defined $options->{project_id}; - return 0 unless defined $options->{studio_id}; - return 0 unless defined $options->{series_id}; + return 0 unless defined $request->{user}; + return 0 unless defined $options->{project_id}; + return 0 unless defined $options->{studio_id}; + return 0 unless defined $options->{series_id}; - return 1 if ( ( defined $permissions->{update_event_of_others} ) && ( $permissions->{update_event_of_others} eq '1' ) ); - return 1 if ( ( defined $permissions->{is_admin} ) && ( $permissions->{is_admin} eq '1' ) ); - return 0 if ( $permissions->{update_event_of_series} ne '1' ); + return 1 if ( defined $permissions->{update_event_of_others} ) && ( $permissions->{update_event_of_others} eq '1' ); + return 1 if ( defined $permissions->{is_admin} ) && ( $permissions->{is_admin} eq '1' ); + return 0 if $permissions->{update_event_of_series} ne '1'; - return is_series_assigned_to_user( $request, $options ); + return is_series_assigned_to_user( $request, $options ); } # check if user allowed to create series events # evaluate permissions and consider editors directly assigned to series sub can_user_create_events { - my $request = shift; - my $options = shift; + my $request = shift; + my $options = shift; - my $config = $request->{config}; - my $permissions = $request->{permissions}; + my $config = $request->{config}; + my $permissions = $request->{permissions}; - return 0 unless defined $request->{user}; - return 0 unless defined $options->{project_id}; - return 0 unless defined $options->{studio_id}; - return 0 unless defined $options->{series_id}; + return 0 unless defined $request->{user}; + return 0 unless defined $options->{project_id}; + return 0 unless defined $options->{studio_id}; + return 0 unless defined $options->{series_id}; - return 1 if ( ( defined $permissions->{create_event} ) && ( $permissions->{create_event} eq '1' ) ); - return 1 if ( ( defined $permissions->{is_admin} ) && ( $permissions->{is_admin} eq '1' ) ); - return 0 if ( $permissions->{create_event_of_series} ne '1' ); + return 1 if ( defined $permissions->{create_event} ) && ( $permissions->{create_event} eq '1' ); + return 1 if ( defined $permissions->{is_admin} ) && ( $permissions->{is_admin} eq '1' ); + return 0 if $permissions->{create_event_of_series} ne '1'; - return is_series_assigned_to_user( $request, $options ); + return is_series_assigned_to_user( $request, $options ); } sub is_series_assigned_to_user { - my $request = shift; - my $options = shift; + my $request = shift; + my $options = shift; - my $config = $request->{config}; - my $permissions = $request->{permissions}; + my $config = $request->{config}; + my $permissions = $request->{permissions}; - return 0 unless defined $options->{project_id}; - return 0 unless defined $options->{studio_id}; - return 0 unless defined $options->{series_id}; - return 0 unless defined $request->{user}; + return 0 unless defined $options->{project_id}; + return 0 unless defined $options->{studio_id}; + return 0 unless defined $options->{series_id}; + return 0 unless defined $request->{user}; - my $series_users = series::get_users( - $config, - { - project_id => $options->{project_id}, - studio_id => $options->{studio_id}, - series_id => $options->{series_id}, - name => $request->{user} - } - ); - return 0 if ( @$series_users == 0 ); - return 1; + my $series_users = series::get_users( + $config, + { + project_id => $options->{project_id}, + studio_id => $options->{studio_id}, + series_id => $options->{series_id}, + name => $request->{user} + } + ); + return 0 if scalar @$series_users == 0; + return 1; } # check if user is assigned to studio where location matchs to event # return 1 on success or error text sub is_event_assigned_to_user { - my $request = shift; - my $options = shift; + my $request = shift; + my $options = shift; - my $config = $request->{config}; + my $config = $request->{config}; - return "missing user" unless defined $request->{user}; - return "missing project_id" unless defined $options->{project_id}; - return "missing studio_id" unless defined $options->{studio_id}; - return "missing series_id" unless defined $options->{series_id}; - return "missing event_id" unless defined $options->{event_id}; + return "missing user" unless defined $request->{user}; + return "missing project_id" unless defined $options->{project_id}; + return "missing studio_id" unless defined $options->{studio_id}; + return "missing series_id" unless defined $options->{series_id}; + return "missing event_id" unless defined $options->{event_id}; - #check roles - my $user_studios = uac::get_studios_by_user( - $config, - { - project_id => $options->{project_id}, - studio_id => $options->{studio_id}, - user => $request->{user}, - } - ); - return "user is not assigned to studio" if @$user_studios == 0; - my $studio = $user_studios->[0]; - my $location = $studio->{location}; - return "could not get studio location" if $location eq ''; + #check roles + my $user_studios = uac::get_studios_by_user( + $config, + { + project_id => $options->{project_id}, + studio_id => $options->{studio_id}, + user => $request->{user}, + } + ); + return "user is not assigned to studio" if scalar @$user_studios == 0; + my $studio = $user_studios->[0]; + my $location = $studio->{location}; + return "could not get studio location" if $location eq ''; - #TODO: replace legacy support - my $events = series::get_events( - $config, - { - project_id => $options->{project_id}, - studio_id => $options->{studio_id}, - series_id => $options->{series_id}, - event_id => $options->{event_id}, - location => $location, - limit => 1 - } - ); + #TODO: replace legacy support + my $events = series::get_events( + $config, + { + project_id => $options->{project_id}, + studio_id => $options->{studio_id}, + series_id => $options->{series_id}, + event_id => $options->{event_id}, + location => $location, + limit => 1 + } + ); - #print STDERR Dumper(@$events); - return - "no event found for" - . " project $options->{project_id}," - . " studio $options->{studio_id}," - . " location $location," - . " series $options->{series_id}" - . " and event $options->{event_id}" - if @$events == 0; - return 1; + #print STDERR Dumper(@$events); + return + "no event found for" + . " project $options->{project_id}," + . " studio $options->{studio_id}," + . " location $location," + . " series $options->{series_id}" + . " and event $options->{event_id}" + if scalar @$events == 0; + return 1; } # to find multiple recurrences this does not include the recurrence_count # use events::get_key to add the recurrence sub get_event_key { - my $event = shift; + my $event = shift; - my $program = $event->{program} || ''; - my $series_name = $event->{series_name} || ''; - my $title = $event->{title} || ''; - my $user_title = $event->{user_title} || ''; - my $episode = $event->{episode} || ''; + my $program = $event->{program} || ''; + my $series_name = $event->{series_name} || ''; + my $title = $event->{title} || ''; + my $user_title = $event->{user_title} || ''; + my $episode = $event->{episode} || ''; - my $key = ''; - $key .= $series_name if $series_name ne ''; - $key .= ' - ' if ( $series_name ne '' ) && ( $title ne '' ); - $key .= $title if $title ne ''; - $key .= ': ' if ( $title ne '' ) && ( $user_title ne '' ); - $key .= $user_title if $user_title ne ''; - $key .= ' #' . $episode if $episode ne ''; - return $key; + my $key = ''; + $key .= $series_name if $series_name ne ''; + $key .= ' - ' if ( $series_name ne '' ) && ( $title ne '' ); + $key .= $title if $title ne ''; + $key .= ': ' if ( $title ne '' ) && ( $user_title ne '' ); + $key .= $user_title if $user_title ne ''; + $key .= ' #' . $episode if $episode ne ''; + return $key; } sub update_recurring_events { - my $config = shift; - my $options = shift; + my $config = shift; + my $options = shift; - return "missing project_id" unless defined $options->{project_id}; - return "missing studio_id" unless defined $options->{studio_id}; - return "missing series_id" unless defined $options->{series_id}; - return "missing event_id" unless defined $options->{event_id}; + return "missing project_id" unless defined $options->{project_id}; + return "missing studio_id" unless defined $options->{studio_id}; + return "missing series_id" unless defined $options->{series_id}; + return "missing event_id" unless defined $options->{event_id}; - my $events = series::get_events( - $config, - { - project_id => $options->{project_id}, - studio_id => $options->{studio_id}, - series_id => $options->{series_id}, - draft => 0 - } - ); - @$events = sort { $a->{start} cmp $b->{start} } @$events; + my $events = series::get_events( + $config, + { + project_id => $options->{project_id}, + studio_id => $options->{studio_id}, + series_id => $options->{series_id}, + draft => 0 + } + ); + @$events = sort { $a->{start} cmp $b->{start} } @$events; - # store events with recurrences by key (series_name, title, user_title, episode) - my $events_by_key = {}; - for my $event (@$events) { - my $key = get_event_key($event); - next unless $key =~ /\#\d+$/; - $event->{key} = $key; - push @{ $events_by_key->{$key} }, $event; - } + # store events with recurrences by key (series_name, title, user_title, episode) + my $events_by_key = {}; + for my $event (@$events) { + my $key = get_event_key($event); + next unless $key =~ /\#\d+$/; + $event->{key} = $key; + push @{ $events_by_key->{$key} }, $event; + } - # handle all events with the same key - for my $key ( keys %$events_by_key ) { - my $events = $events_by_key->{$key}; - next unless scalar @$events > 0; + # handle all events with the same key + for my $key ( keys %$events_by_key ) { + my $events = $events_by_key->{$key}; + next unless scalar @$events > 0; - if ( scalar @$events == 1 ) { + if ( scalar @$events == 1 ) { - # one event found -> check if recurrence is to be removed - my $event = $events->[0]; - next if $event->{recurrence} == 0; - next if $event->{recurrence_count} == 0; - print STDERR - "remove recurrence\t'$event->{event_id}'\t'$event->{start}'\t'$event->{rerun}'\t'$event->{recurrence}'\t'$event->{key}'\n"; - $event->{recurrence} = 0; - $event->{recurrence_count} = 0; - $event->{rerun} = 0; - series::update_recurring_event( $config, $event ); + # one event found -> check if recurrence is to be removed + my $event = $events->[0]; + next if $event->{recurrence} == 0; + next if $event->{recurrence_count} == 0; + print STDERR + "remove recurrence\t'$event->{event_id}'\t'$event->{start}'\t'$event->{rerun}'\t'$event->{recurrence}'\t'$event->{key}'\n"; + $event->{recurrence} = 0; + $event->{recurrence_count} = 0; + $event->{rerun} = 0; + series::update_recurring_event( $config, $event ); - } elsif ( scalar @$events > 1 ) { + } elsif ( scalar @$events > 1 ) { - # multiple events found with same key - # first event is the original - my $event = $events->[0]; - my $originalId = $event->{event_id}; - print STDERR + # multiple events found with same key + # first event is the original + my $event = $events->[0]; + my $originalId = $event->{event_id}; + print STDERR "0\t'$event->{recurrence_count}'\t'$event->{event_id}'\t'$event->{start}'\t'$event->{rerun}'\t'$event->{recurrence}'\t'$event->{key}'\n"; - # succeeding events are reruns - for ( my $c = 1 ; $c < scalar(@$events) ; $c++ ) { - my $event = $events->[$c]; - print STDERR + # succeeding events are reruns + for ( my $c = 1 ; $c < scalar(@$events) ; $c++ ) { + my $event = $events->[$c]; + print STDERR "$c\t'$event->{recurrence_count}'\t'$event->{event_id}'\t'$event->{start}'\t'$event->{rerun}'\t'$event->{recurrence}'\t'$event->{key}'\n"; - my $update = 0; - $update = 1 if $event->{recurrence} ne $originalId; - $update = 1 if $event->{rerun} ne '1'; - $update = 1 if $event->{recurrence_count} ne $c; - next if $update == 0; + my $update = 0; + $update = 1 if $event->{recurrence} ne $originalId; + $update = 1 if $event->{rerun} ne '1'; + $update = 1 if $event->{recurrence_count} ne $c; + next if $update == 0; - $event->{recurrence} = $originalId; - $event->{recurrence_count} = $c; - $event->{rerun} = 1; - series::update_recurring_event( $config, $event ); - } - } - } + $event->{recurrence} = $originalId; + $event->{recurrence_count} = $c; + $event->{rerun} = 1; + series::update_recurring_event( $config, $event ); + } + } + } } sub update_recurring_event { - my $config = shift; - my $event = shift; + my $config = shift; + my $event = shift; - return undef unless defined $event->{event_id}; - return undef unless defined $event->{recurrence}; - return undef unless defined $event->{recurrence_count}; - return undef unless defined $event->{rerun}; + return undef unless defined $event->{event_id}; + return undef unless defined $event->{recurrence}; + return undef unless defined $event->{recurrence_count}; + return undef unless defined $event->{rerun}; - return unless $event->{event_id} =~ /^\d+$/; - return unless $event->{recurrence} =~ /^\d+$/; - return unless $event->{recurrence_count} =~ /^\d+$/; - return unless $event->{rerun} =~ /^\d+$/; + return unless $event->{event_id} =~ /^\d+$/; + return unless $event->{recurrence} =~ /^\d+$/; + return unless $event->{recurrence_count} =~ /^\d+$/; + return unless $event->{rerun} =~ /^\d+$/; - my $bind_values = []; - push @$bind_values, $event->{recurrence}; - push @$bind_values, $event->{recurrence_count}; - push @$bind_values, $event->{rerun}; - push @$bind_values, $event->{id}; + my $bind_values = []; + push @$bind_values, $event->{recurrence}; + push @$bind_values, $event->{recurrence_count}; + push @$bind_values, $event->{rerun}; + push @$bind_values, $event->{id}; - my $update_sql = qq{ + my $update_sql = qq{ update calcms_events set recurrence=?, recurrence_count=?, rerun=? where id=? }; - #print STDERR $update_sql."\n".Dumper($bind_values)."\n"; - my $dbh = db::connect($config); - db::put( $dbh, $update_sql, $bind_values ); + #print STDERR $update_sql."\n".Dumper($bind_values)."\n"; + my $dbh = db::connect($config); + db::put( $dbh, $update_sql, $bind_values ); } sub error { - my $msg = shift; - print "ERROR: $msg
\n"; + my $msg = shift; + print "ERROR: $msg
\n"; } #do not delete last line! diff --git a/lib/calcms/series_dates.pm b/lib/calcms/series_dates.pm index 89f5f10..dab893f 100644 --- a/lib/calcms/series_dates.pm +++ b/lib/calcms/series_dates.pm @@ -22,72 +22,72 @@ our %EXPORT_TAGS = ( 'all' => [@EXPORT_OK] ); sub debug; sub get_columns { - my $config = shift; + my $config = shift; - my $dbh = db::connect($config); - my $cols = db::get_columns( $dbh, 'calcms_series_dates' ); - my $columns = {}; - for my $col (@$cols) { - $columns->{$col} = 1; - } - return $columns; + my $dbh = db::connect($config); + my $cols = db::get_columns( $dbh, 'calcms_series_dates' ); + my $columns = {}; + for my $col (@$cols) { + $columns->{$col} = 1; + } + return $columns; } # get all series_dates for studio_id and series_id within given time range # calculate start_date, end_date, weeday, day from start and end(datetime) sub get { - my $config = shift; - my $condition = shift; + my $config = shift; + my $condition = shift; - my $dbh = db::connect($config); + my $dbh = db::connect($config); - my @conditions = (); - my @bind_values = (); + my @conditions = (); + my @bind_values = (); - if ( ( defined $condition->{project_id} ) && ( $condition->{project_id} ne '' ) ) { - push @conditions, 'project_id=?'; - push @bind_values, $condition->{project_id}; - } + if ( ( defined $condition->{project_id} ) && ( $condition->{project_id} ne '' ) ) { + push @conditions, 'project_id=?'; + push @bind_values, $condition->{project_id}; + } - if ( ( defined $condition->{studio_id} ) && ( $condition->{studio_id} ne '' ) ) { - push @conditions, 'studio_id=?'; - push @bind_values, $condition->{studio_id}; - } + if ( ( defined $condition->{studio_id} ) && ( $condition->{studio_id} ne '' ) ) { + push @conditions, 'studio_id=?'; + push @bind_values, $condition->{studio_id}; + } - if ( ( defined $condition->{series_id} ) && ( $condition->{series_id} ne '' ) ) { - push @conditions, 'series_id=?'; - push @bind_values, $condition->{series_id}; - } + if ( ( defined $condition->{series_id} ) && ( $condition->{series_id} ne '' ) ) { + push @conditions, 'series_id=?'; + push @bind_values, $condition->{series_id}; + } - if ( ( defined $condition->{start_at} ) && ( $condition->{start_at} ne '' ) ) { - push @conditions, 'start=?'; - push @bind_values, $condition->{start_at}; - } + if ( ( defined $condition->{start_at} ) && ( $condition->{start_at} ne '' ) ) { + push @conditions, 'start=?'; + push @bind_values, $condition->{start_at}; + } - if ( ( defined $condition->{from} ) && ( $condition->{from} ne '' ) ) { - push @conditions, 'start_date>=?'; - push @bind_values, $condition->{from}; - } + if ( ( defined $condition->{from} ) && ( $condition->{from} ne '' ) ) { + push @conditions, 'start_date>=?'; + push @bind_values, $condition->{from}; + } - if ( ( defined $condition->{till} ) && ( $condition->{till} ne '' ) ) { - push @conditions, 'end_date{till}; - } + if ( ( defined $condition->{till} ) && ( $condition->{till} ne '' ) ) { + push @conditions, 'end_date{till}; + } - if ( ( defined $condition->{schedule_id} ) && ( $condition->{schedule_id} ne '' ) ) { - push @conditions, 'id=?'; - push @bind_values, $condition->{schedule_id}; - } + if ( ( defined $condition->{schedule_id} ) && ( $condition->{schedule_id} ne '' ) ) { + push @conditions, 'id=?'; + push @bind_values, $condition->{schedule_id}; + } - if ( ( defined $condition->{exclude} ) && ( $condition->{exclude} ne '' ) ) { - push @conditions, 'exclude=?'; - push @bind_values, $condition->{exclude}; - } + if ( ( defined $condition->{exclude} ) && ( $condition->{exclude} ne '' ) ) { + push @conditions, 'exclude=?'; + push @bind_values, $condition->{exclude}; + } - my $conditions = ''; - $conditions = " where " . join( " and ", @conditions ) if ( @conditions > 0 ); + my $conditions = ''; + $conditions = " where " . join( " and ", @conditions ) if ( @conditions > 0 ); - my $query = qq{ + my $query = qq{ select date(start) start_date ,date(end) end_date ,dayname(start) weekday @@ -105,129 +105,129 @@ sub get { order by start }; - #print STDERR $query."\n"; - #print STDERR Dumper(\@bind_values); + #print STDERR $query."\n"; + #print STDERR Dumper(\@bind_values); - my $entries = db::get( $dbh, $query, \@bind_values ); - for my $entry (@$entries) { - $entry->{weekday} = substr( $entry->{weekday}, 0, 2 ); - } + my $entries = db::get( $dbh, $query, \@bind_values ); + for my $entry (@$entries) { + $entry->{weekday} = substr( $entry->{weekday}, 0, 2 ); + } - return $entries; + return $entries; } #check if event is scheduled (on permission check) sub is_event_scheduled { - my $request = shift; - my $options = shift; + my $request = shift; + my $options = shift; - return 0 unless defined $options->{project_id}; - return 0 unless defined $options->{studio_id}; - return 0 unless defined $options->{series_id}; - return 0 unless defined $options->{start_at}; + return 0 unless defined $options->{project_id}; + return 0 unless defined $options->{studio_id}; + return 0 unless defined $options->{series_id}; + return 0 unless defined $options->{start_at}; - my $config = $request->{config}; - my $schedules = series_dates::get( - $config, - { - project_id => $options->{project_id}, - studio_id => $options->{studio_id}, - series_id => $options->{series_id}, - start_at => $options->{start_at} - } - ); - return 0 if ( @$schedules != 1 ); - return 1; + my $config = $request->{config}; + my $schedules = series_dates::get( + $config, + { + project_id => $options->{project_id}, + studio_id => $options->{studio_id}, + series_id => $options->{series_id}, + start_at => $options->{start_at} + } + ); + return 0 if scalar @$schedules != 1; + return 1; } #get all series for given studio_id, time range and search sub get_series { - my $config = shift; - my $condition = shift; + my $config = shift; + my $condition = shift; - my $date_range_include = 0; - $date_range_include = 1 if ( defined $condition->{date_range_include} ) && ( $condition->{date_range_include} == 1 ); + my $date_range_include = 0; + $date_range_include = 1 if ( defined $condition->{date_range_include} ) && ( $condition->{date_range_include} == 1 ); - my $dbh = db::connect($config); + my $dbh = db::connect($config); - my @conditions = (); - my @bind_values = (); + my @conditions = (); + my @bind_values = (); - push @conditions, 'd.series_id=s.id'; + push @conditions, 'd.series_id=s.id'; - # push @conditions, 'd.studio_id=s.studio_id'; + # push @conditions, 'd.studio_id=s.studio_id'; - if ( ( defined $condition->{project_id} ) && ( $condition->{project_id} ne '' ) ) { - push @conditions, 'd.project_id=?'; - push @bind_values, $condition->{project_id}; - } + if ( ( defined $condition->{project_id} ) && ( $condition->{project_id} ne '' ) ) { + push @conditions, 'd.project_id=?'; + push @bind_values, $condition->{project_id}; + } - if ( ( defined $condition->{studio_id} ) && ( $condition->{studio_id} ne '' ) ) { - push @conditions, 'd.studio_id=?'; - push @bind_values, $condition->{studio_id}; - } + if ( ( defined $condition->{studio_id} ) && ( $condition->{studio_id} ne '' ) ) { + push @conditions, 'd.studio_id=?'; + push @bind_values, $condition->{studio_id}; + } - if ( ( defined $condition->{series_id} ) && ( $condition->{series_id} ne '' ) ) { - push @conditions, 'd.series_id=?'; - push @bind_values, $condition->{series_id}; - } + if ( ( defined $condition->{series_id} ) && ( $condition->{series_id} ne '' ) ) { + push @conditions, 'd.series_id=?'; + push @bind_values, $condition->{series_id}; + } - if ( ( defined $condition->{start_at} ) && ( $condition->{start_at} ne '' ) ) { - push @conditions, 'd.start=?'; - push @bind_values, $condition->{start_at}; - } + if ( ( defined $condition->{start_at} ) && ( $condition->{start_at} ne '' ) ) { + push @conditions, 'd.start=?'; + push @bind_values, $condition->{start_at}; + } - if ( ( defined $condition->{from} ) && ( $condition->{from} ne '' ) ) { - if ( $date_range_include == 1 ) { - push @conditions, 'd.end_date>=?'; - push @bind_values, $condition->{from}; - } else { - push @conditions, 'd.start_date>=?'; - push @bind_values, $condition->{from}; - } - } + if ( ( defined $condition->{from} ) && ( $condition->{from} ne '' ) ) { + if ( $date_range_include == 1 ) { + push @conditions, 'd.end_date>=?'; + push @bind_values, $condition->{from}; + } else { + push @conditions, 'd.start_date>=?'; + push @bind_values, $condition->{from}; + } + } - if ( ( defined $condition->{till} ) && ( $condition->{till} ne '' ) ) { - if ( $date_range_include == 1 ) { - push @conditions, 'd.start_date<=?'; - push @bind_values, $condition->{till}; - } else { - push @conditions, 'd.end_date{till}; - } - } + if ( ( defined $condition->{till} ) && ( $condition->{till} ne '' ) ) { + if ( $date_range_include == 1 ) { + push @conditions, 'd.start_date<=?'; + push @bind_values, $condition->{till}; + } else { + push @conditions, 'd.end_date{till}; + } + } - if ( ( defined $condition->{schedule_id} ) && ( $condition->{schedule_id} ne '' ) ) { - push @conditions, 'd.id=?'; - push @bind_values, $condition->{schedule_id}; - } + if ( ( defined $condition->{schedule_id} ) && ( $condition->{schedule_id} ne '' ) ) { + push @conditions, 'd.id=?'; + push @bind_values, $condition->{schedule_id}; + } - if ( ( defined $condition->{exclude} ) && ( $condition->{exclude} ne '' ) ) { - push @conditions, 'd.exclude=?'; - push @bind_values, $condition->{exclude}; - } + if ( ( defined $condition->{exclude} ) && ( $condition->{exclude} ne '' ) ) { + push @conditions, 'd.exclude=?'; + push @bind_values, $condition->{exclude}; + } - my $search_cond = ''; - if ( ( defined $condition->{search} ) && ( $condition->{search} ne '' ) ) { - my $search = lc $condition->{search}; - $search =~ s/[^a-z0-9\_\.\-\:\!öäüßÖÄÜ \&]/%/; - $search =~ s/\%+/\%/; - $search =~ s/^[\%\s]+//; - $search =~ s/[\%\s]+$//; - if ( $search ne '' ) { - $search = '%' . $search . '%'; - my @attr = ( 's.title', 's.series_name', 's.excerpt', 's.category', 's.content' ); - push @conditions, "(" . join( " or ", map { 'lower(' . $_ . ') like ?' } @attr ) . ")"; - for my $attr (@attr) { - push @bind_values, $search; - } - } - } + my $search_cond = ''; + if ( ( defined $condition->{search} ) && ( $condition->{search} ne '' ) ) { + my $search = lc $condition->{search}; + $search =~ s/[^a-z0-9\_\.\-\:\!öäüßÖÄÜ \&]/%/; + $search =~ s/\%+/\%/; + $search =~ s/^[\%\s]+//; + $search =~ s/[\%\s]+$//; + if ( $search ne '' ) { + $search = '%' . $search . '%'; + my @attr = ( 's.title', 's.series_name', 's.excerpt', 's.category', 's.content' ); + push @conditions, "(" . join( " or ", map { 'lower(' . $_ . ') like ?' } @attr ) . ")"; + for my $attr (@attr) { + push @bind_values, $search; + } + } + } - my $conditions = ''; - $conditions = " where " . join( " and ", @conditions ) if ( @conditions > 0 ); + my $conditions = ''; + $conditions = " where " . join( " and ", @conditions ) if ( @conditions > 0 ); - my $query = qq{ + my $query = qq{ select date(d.start) start_date ,date(d.end) end_date ,dayname(d.start) weekday @@ -248,333 +248,333 @@ sub get_series { order by start }; - my $entries = db::get( $dbh, $query, \@bind_values ); + my $entries = db::get( $dbh, $query, \@bind_values ); - for my $entry (@$entries) { - $entry->{weekday} = substr( $entry->{weekday}, 0, 2 ); - } + for my $entry (@$entries) { + $entry->{weekday} = substr( $entry->{weekday}, 0, 2 ); + } - # add series schedule - $entries = series_dates::addSeriesScheduleAttributes( $config, $entries ); + # add series schedule + $entries = series_dates::addSeriesScheduleAttributes( $config, $entries ); - return $entries; + return $entries; } sub addSeriesScheduleAttributes { - my $config = shift; - my $entries = shift; + my $config = shift; + my $entries = shift; - my $scheduleIds = {}; + my $scheduleIds = {}; - # get series schedule ids used at entries - for my $entry (@$entries) { - $scheduleIds->{ $entry->{series_schedule_id} } = 1; - } - my @scheduleIds = keys %$scheduleIds; - return $entries if scalar(@scheduleIds) == 0; + # get series schedule ids used at entries + for my $entry (@$entries) { + $scheduleIds->{ $entry->{series_schedule_id} } = 1; + } + my @scheduleIds = keys %$scheduleIds; + return $entries if scalar(@scheduleIds) == 0; - # get schedules with schedule ids - my $schedules = series_schedule::get( - $config, - { - schedule_ids => \@scheduleIds - } - ); + # get schedules with schedule ids + my $schedules = series_schedule::get( + $config, + { + schedule_ids => \@scheduleIds + } + ); - # get schedules by id - my $scheduleById = {}; - for my $schedule (@$schedules) { - $scheduleById->{ $schedule->{schedule_id} } = $schedule; - } + # get schedules by id + my $scheduleById = {}; + for my $schedule (@$schedules) { + $scheduleById->{ $schedule->{schedule_id} } = $schedule; + } - for my $entry (@$entries) { - $entry->{frequency} = $scheduleById->{ $entry->{series_schedule_id} }->{frequency}; - $entry->{period_type} = $scheduleById->{ $entry->{series_schedule_id} }->{period_type}; - } + for my $entry (@$entries) { + $entry->{frequency} = $scheduleById->{ $entry->{series_schedule_id} }->{frequency}; + $entry->{period_type} = $scheduleById->{ $entry->{series_schedule_id} }->{period_type}; + } - return $entries; + return $entries; } #update series dates for all schedules of a series and studio_id sub update { - my $config = shift; - my $entry = shift; + my $config = shift; + my $entry = shift; - return undef unless defined $entry->{project_id}; - return undef unless defined $entry->{studio_id}; - return undef unless defined $entry->{series_id}; + return undef unless defined $entry->{project_id}; + return undef unless defined $entry->{studio_id}; + return undef unless defined $entry->{series_id}; - my $dbh = db::connect($config); + my $dbh = db::connect($config); - #delete all dates for series (by studio and series id) - series_dates::delete( $config, $entry ); + #delete all dates for series (by studio and series id) + series_dates::delete( $config, $entry ); - my $day_start = $config->{date}->{day_starting_hour}; + my $day_start = $config->{date}->{day_starting_hour}; - #get all schedules for series ordered by exclude, date - my $schedules = series_schedule::get( - $config, - { - project_id => $entry->{project_id}, - studio_id => $entry->{studio_id}, - series_id => $entry->{series_id}, - } - ); + #get all schedules for series ordered by exclude, date + my $schedules = series_schedule::get( + $config, + { + project_id => $entry->{project_id}, + studio_id => $entry->{studio_id}, + series_id => $entry->{series_id}, + } + ); - #add scheduled series dates and remove exluded dates - my $series_dates = {}; + #add scheduled series dates and remove exluded dates + my $series_dates = {}; - #TODO:set schedules exclude to 0 if not 1 - #insert all normal dates (not excludes) - for my $schedule (@$schedules) { - my $dates = get_schedule_dates( $schedule, { exclude => 0 } ); - for my $date (@$dates) { - $date->{exclude} = 0; - $series_dates->{ $date->{start} } = $date; + #TODO:set schedules exclude to 0 if not 1 + #insert all normal dates (not excludes) + for my $schedule (@$schedules) { + my $dates = get_schedule_dates( $schedule, { exclude => 0 } ); + for my $date (@$dates) { + $date->{exclude} = 0; + $series_dates->{ $date->{start} } = $date; - #print STDERR Dumper($date)."\n" if ($date->{start} eq'2014-02-05 19:00:00'); - } - } + #print STDERR Dumper($date)."\n" if ($date->{start} eq'2014-02-05 19:00:00'); + } + } - #insert / overwrite all exlude dates - for my $schedule (@$schedules) { - my $dates = get_schedule_dates( $schedule, { exclude => 1 } ); - for my $date (@$dates) { - $date->{exclude} = 1; - $series_dates->{ $date->{start} } = $date; + #insert / overwrite all exlude dates + for my $schedule (@$schedules) { + my $dates = get_schedule_dates( $schedule, { exclude => 1 } ); + for my $date (@$dates) { + $date->{exclude} = 1; + $series_dates->{ $date->{start} } = $date; - #print STDERR Dumper($date)."\n" if ($date->{start} eq'2014-02-05 19:00:00'); - } - } + #print STDERR Dumper($date)."\n" if ($date->{start} eq'2014-02-05 19:00:00'); + } + } - #print STDERR Dumper($series_dates->{'2014-02-05 19:00:00'}); + #print STDERR Dumper($series_dates->{'2014-02-05 19:00:00'}); - my $request = { config => $config }; + my $request = { config => $config }; - my $i = 0; - my $j = 0; - for my $date ( keys %$series_dates ) { - my $series_date = $series_dates->{$date}; + my $i = 0; + my $j = 0; + for my $date ( keys %$series_dates ) { + my $series_date = $series_dates->{$date}; - #insert date - my $entry = { - project_id => $entry->{project_id}, - studio_id => $entry->{studio_id}, - series_id => $entry->{series_id}, - series_schedule_id => $series_date->{series_schedule_id}, - start => $series_date->{start}, - end => $series_date->{end}, - exclude => $series_date->{exclude}, - }; - if ( studio_timeslot_dates::can_studio_edit_events( $config, $entry ) == 1 ) { # by studio_id, start, end - $entry->{start_date} = time::add_hours_to_datetime( $entry->{start}, -$day_start ); - $entry->{end_date} = time::add_hours_to_datetime( $entry->{end}, -$day_start ); - db::insert( $dbh, 'calcms_series_dates', $entry ); + #insert date + my $entry = { + project_id => $entry->{project_id}, + studio_id => $entry->{studio_id}, + series_id => $entry->{series_id}, + series_schedule_id => $series_date->{series_schedule_id}, + start => $series_date->{start}, + end => $series_date->{end}, + exclude => $series_date->{exclude}, + }; + if ( studio_timeslot_dates::can_studio_edit_events( $config, $entry ) == 1 ) { # by studio_id, start, end + $entry->{start_date} = time::add_hours_to_datetime( $entry->{start}, -$day_start ); + $entry->{end_date} = time::add_hours_to_datetime( $entry->{end}, -$day_start ); + db::insert( $dbh, 'calcms_series_dates', $entry ); - #print STDERR "$entry->{start_date}\n"; - $i++; - } else { - $j++; + #print STDERR "$entry->{start_date}\n"; + $i++; + } else { + $j++; - #print STDERR Dumper($entry); - } - } + #print STDERR Dumper($entry); + } + } - #print STDERR "$i series_dates updates\n"; - return $j . " dates out of studio times, " . $i; + #print STDERR "$i series_dates updates\n"; + return $j . " dates out of studio times, " . $i; } sub get_schedule_dates { - my $schedule = shift; - my $options = shift; + my $schedule = shift; + my $options = shift; - my $is_exclude = $options->{exclude} || 0; - my $dates = []; - return $dates if ( ( $is_exclude eq '1' ) && ( $schedule->{exclude} ne '1' ) ); - return $dates if ( ( $is_exclude eq '0' ) && ( $schedule->{exclude} eq '1' ) ); + my $is_exclude = $options->{exclude} || 0; + my $dates = []; + return $dates if ( $is_exclude eq '1' ) && ( $schedule->{exclude} ne '1' ); + return $dates if ( $is_exclude eq '0' ) && ( $schedule->{exclude} eq '1' ); - if ( $schedule->{period_type} eq 'single' ) { - $dates = get_single_date( $schedule->{start}, $schedule->{duration} ); - } elsif ( $schedule->{period_type} eq 'days' ) { - $dates = get_dates( $schedule->{start}, $schedule->{end}, $schedule->{duration}, $schedule->{frequency} ); - } elsif ( $schedule->{period_type} eq 'week_of_month' ) { - $dates = get_week_of_month_dates( - $schedule->{start}, $schedule->{end}, $schedule->{duration}, $schedule->{week_of_month}, - $schedule->{weekday}, $schedule->{month}, $schedule->{nextDay} - ); - } else { - print STDERR "unknown schedule period_type\n"; - } + if ( $schedule->{period_type} eq 'single' ) { + $dates = get_single_date( $schedule->{start}, $schedule->{duration} ); + } elsif ( $schedule->{period_type} eq 'days' ) { + $dates = get_dates( $schedule->{start}, $schedule->{end}, $schedule->{duration}, $schedule->{frequency} ); + } elsif ( $schedule->{period_type} eq 'week_of_month' ) { + $dates = get_week_of_month_dates( + $schedule->{start}, $schedule->{end}, $schedule->{duration}, $schedule->{week_of_month}, + $schedule->{weekday}, $schedule->{month}, $schedule->{nextDay} + ); + } else { + print STDERR "unknown schedule period_type\n"; + } - # set series schedule id - for my $date (@$dates) { - $date->{series_schedule_id} = $schedule->{schedule_id}; - } - return $dates; + # set series schedule id + for my $date (@$dates) { + $date->{series_schedule_id} = $schedule->{schedule_id}; + } + return $dates; } sub get_week_of_month_dates { - my $start = shift; # datetime string - my $end = shift; # datetime string - my $duration = shift; # in minutes - my $week = shift; # every nth week of month - my $weekday = shift; # weekday [1..7] - my $frequency = shift; # every 1st,2nd,3th time - my $nextDay = shift; # add 24 hours to start, (for night hours at last weekday of month) + my $start = shift; # datetime string + my $end = shift; # datetime string + my $duration = shift; # in minutes + my $week = shift; # every nth week of month + my $weekday = shift; # weekday [1..7] + my $frequency = shift; # every 1st,2nd,3th time + my $nextDay = shift; # add 24 hours to start, (for night hours at last weekday of month) - return undef if $start eq ''; - return undef if $end eq ''; - return undef if $duration eq ''; - return undef if $week eq ''; - return undef if $weekday eq ''; - return undef if $frequency eq ''; - return undef if $frequency == 0; + return undef if $start eq ''; + return undef if $end eq ''; + return undef if $duration eq ''; + return undef if $week eq ''; + return undef if $weekday eq ''; + return undef if $frequency eq ''; + return undef if $frequency == 0; - my $start_dates = time::get_nth_weekday_in_month( $start, $end, $week, $weekday ); + my $start_dates = time::get_nth_weekday_in_month( $start, $end, $week, $weekday ); - if ( ( defined $nextDay ) && ( $nextDay > 0 ) ) { - for ( my $i = 0 ; $i < @$start_dates ; $i++ ) { - $start_dates->[$i] = time::add_hours_to_datetime( $start_dates->[$i], 24 ); - } - } + if ( ( defined $nextDay ) && ( $nextDay > 0 ) ) { + for ( my $i = 0 ; $i < @$start_dates ; $i++ ) { + $start_dates->[$i] = time::add_hours_to_datetime( $start_dates->[$i], 24 ); + } + } - my $results = []; + my $results = []; - my $c = -1; - for my $start_datetime (@$start_dates) { - $c++; - my @start = @{ time::datetime_to_array($start_datetime) }; - next unless @start >= 6; - next if ( ( $c % $frequency ) != 0 ); + my $c = -1; + for my $start_datetime (@$start_dates) { + $c++; + my @start = @{ time::datetime_to_array($start_datetime) }; + next unless @start >= 6; + next if ( $c % $frequency ) != 0; - my @end_datetime = Date::Calc::Add_Delta_DHMS( - $start[0], $start[1], $start[2], # start date - $start[3], $start[4], $start[5], # start time - 0, 0, $duration, 0 # delta days, hours, minutes, seconds - ); - my $end_datetime = time::array_to_datetime( \@end_datetime ); + my @end_datetime = Date::Calc::Add_Delta_DHMS( + $start[0], $start[1], $start[2], # start date + $start[3], $start[4], $start[5], # start time + 0, 0, $duration, 0 # delta days, hours, minutes, seconds + ); + my $end_datetime = time::array_to_datetime( \@end_datetime ); - push @$results, - { - start => $start_datetime, - end => $end_datetime - }; - } - return $results; + push @$results, + { + start => $start_datetime, + end => $end_datetime + }; + } + return $results; } #add duration to a single date sub get_single_date { - my $start_datetime = shift; - my $duration = shift; + my $start_datetime = shift; + my $duration = shift; - my @start = @{ time::datetime_to_array($start_datetime) }; - return unless @start >= 6; + my @start = @{ time::datetime_to_array($start_datetime) }; + return unless @start >= 6; - my @end_datetime = Date::Calc::Add_Delta_DHMS( - $start[0], $start[1], $start[2], # start date - $start[3], $start[4], $start[5], # start time - 0, 0, $duration, 0 # delta days, hours, minutes, seconds - ); - my $date = { - start => $start_datetime, - end => time::array_to_datetime( \@end_datetime ) - }; - return [$date]; + my @end_datetime = Date::Calc::Add_Delta_DHMS( + $start[0], $start[1], $start[2], # start date + $start[3], $start[4], $start[5], # start time + 0, 0, $duration, 0 # delta days, hours, minutes, seconds + ); + my $date = { + start => $start_datetime, + end => time::array_to_datetime( \@end_datetime ) + }; + return [$date]; } #calculate all dates between start_datetime and end_date with duration(minutes) and frequency(days) sub get_dates { - my $start_datetime = shift; - my $end_date = shift; - my $duration = shift; # in minutes - my $frequency = shift; # in days - #print "start_datetime:$start_datetime end_date:$end_date duration:$duration frequency:$frequency\n"; + my $start_datetime = shift; + my $end_date = shift; + my $duration = shift; # in minutes + my $frequency = shift; # in days + #print "start_datetime:$start_datetime end_date:$end_date duration:$duration frequency:$frequency\n"; - my @start = @{ time::datetime_to_array($start_datetime) }; - return unless @start >= 6; - my @start_date = ( $start[0], $start[1], $start[2] ); - my $start_time = sprintf( '%02d:%02d:%02d', $start[3], $start[4], $start[5] ); + my @start = @{ time::datetime_to_array($start_datetime) }; + return unless @start >= 6; + my @start_date = ( $start[0], $start[1], $start[2] ); + my $start_time = sprintf( '%02d:%02d:%02d', $start[3], $start[4], $start[5] ); - #print STDERR "$start_datetime,$end_date,$duration,$frequency\n"; + #print STDERR "$start_datetime,$end_date,$duration,$frequency\n"; - #return on single date - my $date = {}; - $date->{start} = sprintf( "%04d-%02d-%02d", @start_date ) . ' ' . $start_time; - return undef if $duration eq ''; + #return on single date + my $date = {}; + $date->{start} = sprintf( "%04d-%02d-%02d", @start_date ) . ' ' . $start_time; + return undef if $duration eq ''; - return undef if ( ( $frequency eq '' ) || ( $end_date eq '' ) ); + return undef if ( $frequency eq '' ) || ( $end_date eq '' ); - #continue on recurring date - my @end = @{ time::datetime_to_array($end_date) }; - return unless @end >= 3; - my @end_date = ( $end[0], $end[1], $end[2] ); + #continue on recurring date + my @end = @{ time::datetime_to_array($end_date) }; + return unless scalar @end >= 3; + my @end_date = ( $end[0], $end[1], $end[2] ); - my $today = time::time_to_date(); - my ( $year, $month, $day ) = split( /\-/, $today ); + my $today = time::time_to_date(); + my ( $year, $month, $day ) = split( /\-/, $today ); - my $dates = []; - return $dates if ( $end_date lt $today ); - return $dates if ( $frequency < 1 ); + my $dates = []; + return $dates if $end_date lt $today; + return $dates if $frequency < 1; - my $j = Date::Calc::Delta_Days( @start_date, @end_date ); - my $c = 0; - for ( my $i = 0 ; $i <= $j ; $i += $frequency ) { - my @date = Date::Calc::Add_Delta_Days( $start[0], $start[1], $start[2], $i ); - my $date = {}; - $date->{start} = sprintf( "%04d-%02d-%02d", @date ) . ' ' . $start_time; + my $j = Date::Calc::Delta_Days( @start_date, @end_date ); + my $c = 0; + for ( my $i = 0 ; $i <= $j ; $i += $frequency ) { + my @date = Date::Calc::Add_Delta_Days( $start[0], $start[1], $start[2], $i ); + my $date = {}; + $date->{start} = sprintf( "%04d-%02d-%02d", @date ) . ' ' . $start_time; - #if($date->{start} gt $today){ - my @end_datetime = Date::Calc::Add_Delta_DHMS( - $date[0], $date[1], $date[2], # start date - $start[3], $start[4], $start[5], # start time - 0, 0, $duration, 0 # delta days, hours, minutes, seconds - ); - $date->{end} = time::array_to_datetime( \@end_datetime ); - push @$dates, $date; + #if($date->{start} gt $today){ + my @end_datetime = Date::Calc::Add_Delta_DHMS( + $date[0], $date[1], $date[2], # start date + $start[3], $start[4], $start[5], # start time + 0, 0, $duration, 0 # delta days, hours, minutes, seconds + ); + $date->{end} = time::array_to_datetime( \@end_datetime ); + push @$dates, $date; - #} - last if ( $c > 200 ); - $c++; - } - return $dates; + #} + last if ( $c > 200 ); + $c++; + } + return $dates; } #remove all series_dates for studio_id and series_id sub delete { - my $config = shift; - my $entry = shift; + my $config = shift; + my $entry = shift; - return unless defined $entry->{project_id}; - return unless defined $entry->{studio_id}; - return unless defined $entry->{series_id}; + return unless defined $entry->{project_id}; + return unless defined $entry->{studio_id}; + return unless defined $entry->{series_id}; - my $dbh = db::connect($config); + my $dbh = db::connect($config); - my $query = qq{ + my $query = qq{ delete from calcms_series_dates where project_id=? and studio_id=? and series_id=? }; - my $bind_values = [ $entry->{project_id}, $entry->{studio_id}, $entry->{series_id} ]; + my $bind_values = [ $entry->{project_id}, $entry->{studio_id}, $entry->{series_id} ]; - #print '
$query'.$query.Dumper($bind_values).'
'; - db::put( $dbh, $query, $bind_values ); + #print '
$query'.$query.Dumper($bind_values).'
'; + db::put( $dbh, $query, $bind_values ); } # get all series dates where no event has been created for sub getDatesWithoutEvent { - my $config = shift; - my $options = shift; + my $config = shift; + my $options = shift; - return unless defined $options->{project_id}; - return unless defined $options->{studio_id}; - return unless defined $options->{from}; - return unless defined $options->{till}; + return unless defined $options->{project_id}; + return unless defined $options->{studio_id}; + return unless defined $options->{from}; + return unless defined $options->{till}; - my $dbh = db::connect($config); + my $dbh = db::connect($config); - my $query = q{ + my $query = q{ SELECT sd.* FROM calcms_series_dates sd LEFT JOIN calcms_events e on (sd.start = e.start) @@ -587,15 +587,15 @@ sub getDatesWithoutEvent { order by sd.start }; - my $bind_values = [ $options->{project_id}, $options->{studio_id}, $options->{from}, $options->{till} ]; - my $entries = db::get( $dbh, $query, $bind_values ); - return $entries; + my $bind_values = [ $options->{project_id}, $options->{studio_id}, $options->{from}, $options->{till} ]; + my $entries = db::get( $dbh, $query, $bind_values ); + return $entries; } sub error { - my $msg = shift; - print "ERROR: $msg
\n"; + my $msg = shift; + print "ERROR: $msg
\n"; } #do not delete last line! diff --git a/lib/calcms/series_events.pm b/lib/calcms/series_events.pm index f975660..9e303fb 100644 --- a/lib/calcms/series_events.pm +++ b/lib/calcms/series_events.pm @@ -39,147 +39,147 @@ sub debug; # do not check for project,studio,series # all changed columns are returned for history handling sub save_content { - my $config = shift; - my $entry = shift; + my $config = shift; + my $entry = shift; - my $dbh = db::connect($config); + my $dbh = db::connect($config); - return undef unless ( defined $entry->{id} ); + return undef unless ( defined $entry->{id} ); - for my $attr ( keys %$entry ) { - next unless defined $entry->{$attr}; - $entry->{$attr} =~ s/^\s+//g; - $entry->{$attr} =~ s/\s+$//g; - } + for my $attr ( keys %$entry ) { + next unless defined $entry->{$attr}; + $entry->{$attr} =~ s/^\s+//g; + $entry->{$attr} =~ s/\s+$//g; + } - for my $attr ( 'image', 'series_image' ) { - $entry->{$attr} = images::normalizeName( $entry->{$attr} ) if defined $entry->{$attr}; - } + for my $attr ( 'image', 'series_image' ) { + $entry->{$attr} = images::normalizeName( $entry->{$attr} ) if defined $entry->{$attr}; + } - #print STDERR Dumper(\$entry->{content}); - for my $attr ( 'content', 'topic' ) { - if ( defined $entry->{$attr} ) { - $entry->{ 'html_' . $attr } = markup::creole_to_html( $entry->{$attr} ); + #print STDERR Dumper(\$entry->{content}); + for my $attr ( 'content', 'topic' ) { + if ( defined $entry->{$attr} ) { + $entry->{ 'html_' . $attr } = markup::creole_to_html( $entry->{$attr} ); - #$entry->{'html_'.$attr}=~s/([^\>])\n+([^\<])/$1$2/g; - #$entry->{'html_'.$attr}=~s/^\s*(

)?//g; - #$entry->{'html_'.$attr}=~s/(<\/p>)?\s*$//g; - } - } + #$entry->{'html_'.$attr}=~s/([^\>])\n+([^\<])/$1$2/g; + #$entry->{'html_'.$attr}=~s/^\s*(

)?//g; + #$entry->{'html_'.$attr}=~s/(<\/p>)?\s*$//g; + } + } - #print STDERR Dumper(\$entry->{series_image}); - #print STDERR "ok2\n"; - #return; - $entry->{modified_at} = time::time_to_datetime( time() ); + #print STDERR Dumper(\$entry->{series_image}); + #print STDERR "ok2\n"; + #return; + $entry->{modified_at} = time::time_to_datetime( time() ); - #return; - #update only existing atributes + #return; + #update only existing atributes - #TODO: double check series_name (needed for reassignment but not for editing...) - my @keys = (); - for my $key ( - 'series_name', 'title', 'excerpt', 'content', - 'html_content', 'user_title', 'user_excerpt', 'topic', - 'html_topic', 'episode', 'image', 'image_label', - 'series_image', 'series_image_label', 'podcast_url', 'archive_url', - 'live', 'published', 'playout', 'archived', - 'rerun', 'draft', 'disable_event_sync', 'modified_by' - ) - { - push @keys, $key if defined $entry->{$key}; - } - $entry->{episode} = undef if ( defined $entry->{episode} ) && ( $entry->{episode} eq '0' ); - $entry->{published} = 0 if ( defined $entry->{draft} ) && ( $entry->{draft} eq '1' ); + #TODO: double check series_name (needed for reassignment but not for editing...) + my @keys = (); + for my $key ( + 'series_name', 'title', 'excerpt', 'content', + 'html_content', 'user_title', 'user_excerpt', 'topic', + 'html_topic', 'episode', 'image', 'image_label', + 'series_image', 'series_image_label', 'podcast_url', 'archive_url', + 'live', 'published', 'playout', 'archived', + 'rerun', 'draft', 'disable_event_sync', 'modified_by' + ) + { + push @keys, $key if defined $entry->{$key}; + } + $entry->{episode} = undef if ( defined $entry->{episode} ) && ( $entry->{episode} eq '0' ); + $entry->{published} = 0 if ( defined $entry->{draft} ) && ( $entry->{draft} eq '1' ); - my $values = join( ",", map { $_ . '=?' } (@keys) ); - my @bind_values = map { $entry->{$_} } (@keys); + my $values = join( ",", map { $_ . '=?' } (@keys) ); + my @bind_values = map { $entry->{$_} } (@keys); - push @bind_values, $entry->{id}; - my $query = qq{ + push @bind_values, $entry->{id}; + my $query = qq{ update calcms_events set $values where id=? }; - #print STDERR "update:".$query.Dumper(\@bind_values); - my $result = db::put( $dbh, $query, \@bind_values ); - unless ( defined $result ) { - print STDERR "error on updating event\n"; - return undef; - } + #print STDERR "update:".$query.Dumper(\@bind_values); + my $result = db::put( $dbh, $query, \@bind_values ); + unless ( defined $result ) { + print STDERR "error on updating event\n"; + return undef; + } - #print STDERR "result=$result\n"; - #print STDERR "entr after update".Dumper($entry); - return $entry; + #print STDERR "result=$result\n"; + #print STDERR "entr after update".Dumper($entry); + return $entry; } # save event time by id # do not check project, studio, series # for history handling all changed columns are returned sub save_event_time { - my $config = shift; - my $entry = shift; + my $config = shift; + my $entry = shift; - return undef unless ( defined $entry->{id} ); - return undef unless ( defined $entry->{duration} ); - return undef unless ( defined $entry->{start_date} ); + return undef unless ( defined $entry->{id} ); + return undef unless ( defined $entry->{duration} ); + return undef unless ( defined $entry->{start_date} ); - my $dbh = db::connect($config); - my $event = { - id => $entry->{id}, - start => $entry->{start_date}, - end => time::add_minutes_to_datetime( $entry->{start_date}, $entry->{duration} ) - }; + my $dbh = db::connect($config); + my $event = { + id => $entry->{id}, + start => $entry->{start_date}, + end => time::add_minutes_to_datetime( $entry->{start_date}, $entry->{duration} ) + }; - my $day_start = $config->{date}->{day_starting_hour}; - my $event_hour = int( ( split( /[\-\:\sT]/, $event->{start} ) )[3] ); + my $day_start = $config->{date}->{day_starting_hour}; + my $event_hour = int( ( split( /[\-\:\sT]/, $event->{start} ) )[3] ); - my @update_columns = (); - my $bind_values = []; - push @update_columns, 'start=?'; - push @$bind_values, $event->{start}; + my @update_columns = (); + my $bind_values = []; + push @update_columns, 'start=?'; + push @$bind_values, $event->{start}; - push @update_columns, 'end=?'; - push @$bind_values, $event->{end}; + push @update_columns, 'end=?'; + push @$bind_values, $event->{end}; - # add start date - my $start_date = time::add_hours_to_datetime( $event->{start}, -$day_start ); - push @update_columns, 'start_date=?'; - push @$bind_values, $start_date; - $event->{start_date} = $start_date; + # add start date + my $start_date = time::add_hours_to_datetime( $event->{start}, -$day_start ); + push @update_columns, 'start_date=?'; + push @$bind_values, $start_date; + $event->{start_date} = $start_date; - # add end date - my $end_date = time::add_hours_to_datetime( $event->{end}, -$day_start ); - push @update_columns, 'end_date=?'; - push @$bind_values, $end_date; - $event->{end_date} = $end_date; + # add end date + my $end_date = time::add_hours_to_datetime( $event->{end}, -$day_start ); + push @update_columns, 'end_date=?'; + push @$bind_values, $end_date; + $event->{end_date} = $end_date; - my $update_columns = join( ",\n", @update_columns ); - my $update_sql = qq{ + my $update_columns = join( ",\n", @update_columns ); + my $update_sql = qq{ update calcms_events set $update_columns where id=? }; - push @$bind_values, $event->{id}; + push @$bind_values, $event->{id}; - #print STDERR $update_sql."\n".Dumper($bind_values)."\n"; - db::put( $dbh, $update_sql, $bind_values ); - return $event; + #print STDERR $update_sql."\n".Dumper($bind_values)."\n"; + db::put( $dbh, $update_sql, $bind_values ); + return $event; } sub set_playout_status { - my $config = shift; - my $entry = shift; + my $config = shift; + my $entry = shift; - return undef unless defined $entry->{project_id}; - return undef unless defined $entry->{studio_id}; - return undef unless defined $entry->{start}; - return undef unless defined $entry->{playout}; + return undef unless defined $entry->{project_id}; + return undef unless defined $entry->{studio_id}; + return undef unless defined $entry->{start}; + return undef unless defined $entry->{playout}; - my $dbh = db::connect($config); + my $dbh = db::connect($config); - # check if event is assigned to project and studio - my $sql = qq{ + # check if event is assigned to project and studio + my $sql = qq{ select se.event_id event_id from calcms_series_events se, calcms_events e where @@ -188,82 +188,82 @@ sub set_playout_status { and se.project_id=? and se.studio_id=? }; - my $bind_values = [ $entry->{start}, $entry->{project_id}, $entry->{studio_id} ]; + my $bind_values = [ $entry->{start}, $entry->{project_id}, $entry->{studio_id} ]; - #print STDERR Dumper($sql).Dumper($bind_values); - my $events = db::get( $dbh, $sql, $bind_values ); + #print STDERR Dumper($sql).Dumper($bind_values); + my $events = db::get( $dbh, $sql, $bind_values ); - #print STDERR Dumper($events); - return undef if scalar(@$events) != 1; - my $event_id = $events->[0]->{event_id}; - $sql = qq{ + #print STDERR Dumper($events); + return undef if scalar(@$events) != 1; + my $event_id = $events->[0]->{event_id}; + $sql = qq{ update calcms_events set playout=? where id=? and start=? }; - $bind_values = [ $entry->{playout}, $event_id, $entry->{start} ]; + $bind_values = [ $entry->{playout}, $event_id, $entry->{start} ]; - #print STDERR $sql."\n".Dumper($bind_values)."\n"; - my $result = db::put( $dbh, $sql, $bind_values ); - return $result; + #print STDERR $sql."\n".Dumper($bind_values)."\n"; + my $result = db::put( $dbh, $sql, $bind_values ); + return $result; } # is event assigned to project, studio and series? sub is_event_assigned { - my $config = shift; - my $entry = shift; + my $config = shift; + my $entry = shift; - return 0 unless defined $entry->{project_id}; - return 0 unless defined $entry->{studio_id}; - return 0 unless defined $entry->{series_id}; - return 0 unless defined $entry->{event_id}; + return 0 unless defined $entry->{project_id}; + return 0 unless defined $entry->{studio_id}; + return 0 unless defined $entry->{series_id}; + return 0 unless defined $entry->{event_id}; - my $dbh = db::connect($config); + my $dbh = db::connect($config); - my $sql = q{ + my $sql = q{ select * from calcms_series_events where project_id=? and studio_id=? and series_id=? and event_id=? }; - my $bind_values = [ $entry->{project_id}, $entry->{studio_id}, $entry->{series_id}, $entry->{event_id} ]; - my $results = db::get( $dbh, $sql, $bind_values ); + my $bind_values = [ $entry->{project_id}, $entry->{studio_id}, $entry->{series_id}, $entry->{event_id} ]; + my $results = db::get( $dbh, $sql, $bind_values ); - return 1 if @$results >= 1; - return 0; + return 1 if scalar @$results >= 1; + return 0; } sub delete_event { - my $config = shift; - my $entry = shift; + my $config = shift; + my $entry = shift; - return undef unless defined $entry->{project_id}; - return undef unless defined $entry->{studio_id}; - return undef unless defined $entry->{series_id}; - return undef unless defined $entry->{event_id}; - return undef unless defined $entry->{user}; + return undef unless defined $entry->{project_id}; + return undef unless defined $entry->{studio_id}; + return undef unless defined $entry->{series_id}; + return undef unless defined $entry->{event_id}; + return undef unless defined $entry->{user}; - #is event assigned to project, studio and series? - unless ( is_event_assigned( $config, $entry ) == 1 ) { - print STDERR + #is event assigned to project, studio and series? + unless ( is_event_assigned( $config, $entry ) == 1 ) { + print STDERR "cannot delete event with project_id=$entry->{project_id}, studio_id=$entry->{studio_id}, series_id=$entry->{series_id}, event_id=$entry->{event_id}"; - return 0; - } + return 0; + } - event_history::insert_by_event_id( $config, $entry ); + event_history::insert_by_event_id( $config, $entry ); - #delete the association - series::unassign_event( $config, $entry ); + #delete the association + series::unassign_event( $config, $entry ); - # delete the event - my $dbh = db::connect($config); - my $sql = q{ + # delete the event + my $dbh = db::connect($config); + my $sql = q{ delete from calcms_events where id=? }; - my $bind_values = [ $entry->{event_id} ]; - db::put( $dbh, $sql, $bind_values ); + my $bind_values = [ $entry->{event_id} ]; + db::put( $dbh, $sql, $bind_values ); - return 1; + return 1; } #check permissions @@ -272,141 +272,141 @@ sub delete_event { # key check_for: user, studio, series, events, schedule # return error text or 1 if okay sub check_permission { - my $request = shift; - my $options = shift; + my $request = shift; + my $options = shift; - return "missing permission at check" unless defined $options->{permission}; - return "missing check_for at check" unless defined $options->{check_for}; - return "missing user at check" unless defined $request->{user}; - return "missing project_id at check" unless defined $options->{project_id}; - return "missing studio_id at check" unless defined $options->{studio_id}; - return "missing series_id at check" unless defined $options->{series_id}; + return "missing permission at check" unless defined $options->{permission}; + return "missing check_for at check" unless defined $options->{check_for}; + return "missing user at check" unless defined $request->{user}; + return "missing project_id at check" unless defined $options->{project_id}; + return "missing studio_id at check" unless defined $options->{studio_id}; + return "missing series_id at check" unless defined $options->{series_id}; - my $permissions = $request->{permissions}; - my $config = $request->{config}; + my $permissions = $request->{permissions}; + my $config = $request->{config}; - my $studio_check = studios::check( $config, $options ); - return $studio_check if ( $studio_check ne '1' ); - print STDERR "check studio ok\n"; + my $studio_check = studios::check( $config, $options ); + return $studio_check if ( $studio_check ne '1' ); + print STDERR "check studio ok\n"; - my $project_check = project::check( $config, $options ); - return $project_check if ( $project_check ne '1' ); - print STDERR "check project ok\n"; + my $project_check = project::check( $config, $options ); + return $project_check if ( $project_check ne '1' ); + print STDERR "check project ok\n"; - #check if permissions are set (like create_event) - my $found = 0; - for my $permission ( split /\,/, $options->{permission} ) { - $found = 1 if ( ( defined $permissions->{$permission} ) && ( $permissions->{$permission} ) eq '1' ); - } - return 'missing permission to ' . $options->{permission} if $found == 0; - delete $options->{permission}; + #check if permissions are set (like create_event) + my $found = 0; + for my $permission ( split /\,/, $options->{permission} ) { + $found = 1 if ( defined $permissions->{$permission} ) && ( $permissions->{$permission} ) eq '1' ; + } + return 'missing permission to ' . $options->{permission} if $found == 0; + delete $options->{permission}; - #convert check list to hash - my $check = {}; - for my $permission ( @{ $options->{check_for} } ) { - $check->{$permission} = 1; - } - delete $options->{check_for}; + #convert check list to hash + my $check = {}; + for my $permission ( @{ $options->{check_for} } ) { + $check->{$permission} = 1; + } + delete $options->{check_for}; - # is project assigned to studio - return "studio is not assigned to project" unless project::is_studio_assigned( $config, $options ) == 1; + # is project assigned to studio + return "studio is not assigned to project" unless project::is_studio_assigned( $config, $options ) == 1; - #get studio names - my $studios = studios::get( - $config, - { - project_id => $options->{project_id}, - studio_id => $options->{studio_id} - } - ); - return "unknown studio" unless defined $studios; - return "unknown studio" unless ( @$studios == 1 ); - my $studio = $studios->[0]; - my $studio_name = $studio->{name} || ''; + #get studio names + my $studios = studios::get( + $config, + { + project_id => $options->{project_id}, + studio_id => $options->{studio_id} + } + ); + return "unknown studio" unless defined $studios; + return "unknown studio" unless scalar @$studios == 1; + my $studio = $studios->[0]; + my $studio_name = $studio->{name} || ''; - #get series names - my $series = series::get( - $config, - { - project_id => $options->{project_id}, - studio_id => $options->{studio_id}, - series_id => $options->{series_id} - } - ); - my $series_name = $series->[0]->{series_name} || ''; - $series_name .= ' - ' . $series->[0]->{title} if $series->[0]->{series_name} ne ''; + #get series names + my $series = series::get( + $config, + { + project_id => $options->{project_id}, + studio_id => $options->{studio_id}, + series_id => $options->{series_id} + } + ); + my $series_name = $series->[0]->{series_name} || ''; + $series_name .= ' - ' . $series->[0]->{title} if $series->[0]->{series_name} ne ''; - my $draft = 0; - $draft = 1 if ( defined $options->{draft} ) && ( $options->{draft} == 1 ); + my $draft = 0; + $draft = 1 if ( defined $options->{draft} ) && ( $options->{draft} == 1 ); - #check all items from checklist - if ( ( defined $check->{user} ) && ( uac::is_user_assigned_to_studio( $request, $options ) == 0 ) ) { - return "User '$request->{user}' is not assigned to studio $studio_name ($options->{studio_id})"; - } + #check all items from checklist + if ( ( defined $check->{user} ) && ( uac::is_user_assigned_to_studio( $request, $options ) == 0 ) ) { + return "User '$request->{user}' is not assigned to studio $studio_name ($options->{studio_id})"; + } - if ( ( defined $check->{studio} ) && ( project::is_series_assigned( $config, $options ) == 0 ) ) { - return "Series '$series_name' ($options->{series_id}) is not assigned to studio '$studio_name' ($options->{studio_id})"; - } + if ( ( defined $check->{studio} ) && ( project::is_series_assigned( $config, $options ) == 0 ) ) { + return "Series '$series_name' ($options->{series_id}) is not assigned to studio '$studio_name' ($options->{studio_id})"; + } - # check series and can user update events - if ( ( defined $check->{series} ) && ( series::can_user_update_events( $request, $options ) == 0 ) ) { - return "unknown series" unless defined $series; - return "User $request->{user} cannot update events for series '$series_name' ($options->{series_id})"; - } + # check series and can user update events + if ( ( defined $check->{series} ) && ( series::can_user_update_events( $request, $options ) == 0 ) ) { + return "unknown series" unless defined $series; + return "User $request->{user} cannot update events for series '$series_name' ($options->{series_id})"; + } - # check series and can user create events - if ( ( defined $check->{create_events} ) && ( series::can_user_create_events( $request, $options ) == 0 ) ) { - return "unknown series" unless defined $series; - return "User $request->{user} cannot create events for series '$series_name' ($options->{series_id})"; - } + # check series and can user create events + if ( ( defined $check->{create_events} ) && ( series::can_user_create_events( $request, $options ) == 0 ) ) { + return "unknown series" unless defined $series; + return "User $request->{user} cannot create events for series '$series_name' ($options->{series_id})"; + } - if ( ( $draft == 0 ) - && ( defined $check->{studio_timeslots} ) - && ( studio_timeslot_dates::can_studio_edit_events( $config, $options ) == 0 ) ) - { - return "requested time is not assigned to studio '$studio_name' ($options->{studio_id})"; - } + if ( ( $draft == 0 ) + && ( defined $check->{studio_timeslots} ) + && ( studio_timeslot_dates::can_studio_edit_events( $config, $options ) == 0 ) ) + { + return "requested time is not assigned to studio '$studio_name' ($options->{studio_id})"; + } - #check if event is assigned to user,project,studio,series,location - if ( defined $check->{events} ) { - return "missing event_id" unless defined $options->{event_id}; - my $result = series::is_event_assigned_to_user( $request, $options ); - return $result if $result ne '1'; - } + #check if event is assigned to user,project,studio,series,location + if ( defined $check->{events} ) { + return "missing event_id" unless defined $options->{event_id}; + my $result = series::is_event_assigned_to_user( $request, $options ); + return $result if $result ne '1'; + } - # prevent editing events that are over for more than 14 days - if ( ( $draft == 0 ) && ( defined $check->{event_age} ) ) { - if ( - series::is_event_older_than_days( - $config, - { - project_id => $options->{project_id}, - studio_id => $options->{studio_id}, - series_id => $options->{series_id}, - event_id => $options->{event_id}, - max_age => 14 - } - ) == 1 - ) - { - return "show is over for more than 2 weeks" - unless ( ( defined $permissions->{update_event_after_week} ) - && ( $permissions->{update_event_after_week} eq '1' ) ); - } - } + # prevent editing events that are over for more than 14 days + if ( ( $draft == 0 ) && ( defined $check->{event_age} ) ) { + if ( + series::is_event_older_than_days( + $config, + { + project_id => $options->{project_id}, + studio_id => $options->{studio_id}, + series_id => $options->{series_id}, + event_id => $options->{event_id}, + max_age => 14 + } + ) == 1 + ) + { + return "show is over for more than 2 weeks" + unless ( ( defined $permissions->{update_event_after_week} ) + && ( $permissions->{update_event_after_week} eq '1' ) ); + } + } - #check if schedule event exists for given date - if ( ( $draft == 0 ) && ( defined $check->{schedule} ) ) { - return "unknown series" unless defined $series; - return "missing start_at at check_permission" unless defined $options->{start_date}; + #check if schedule event exists for given date + if ( ( $draft == 0 ) && ( defined $check->{schedule} ) ) { + return "unknown series" unless defined $series; + return "missing start_at at check_permission" unless defined $options->{start_date}; - #TODO: check "is_event_scheduled" if start_at could be moved to start_date - $options->{start_at} = $options->{start_date}; - return "No event scheduled for series '$series_name' ($options->{series_id})" - if ( series_dates::is_event_scheduled( $request, $options ) == 0 ); - } + #TODO: check "is_event_scheduled" if start_at could be moved to start_date + $options->{start_at} = $options->{start_date}; + return "No event scheduled for series '$series_name' ($options->{series_id})" + if ( series_dates::is_event_scheduled( $request, $options ) == 0 ); + } - return '1'; + return '1'; } #not handled, yet: @@ -415,126 +415,126 @@ sub check_permission { #insert event sub insert_event { - my $config = shift; - my $options = shift; + my $config = shift; + my $options = shift; - my $project_id = $options->{project_id}; - my $studio = $options->{studio}; - my $serie = $options->{serie}; - my $params = $options->{event}; - my $user = $options->{user}; + my $project_id = $options->{project_id}; + my $studio = $options->{studio}; + my $serie = $options->{serie}; + my $params = $options->{event}; + my $user = $options->{user}; - return 0 unless defined $studio; - return 0 unless defined $serie; - return 0 unless defined $params; - return 0 unless defined $user; - return 0 unless defined $studio->{location}; + return 0 unless defined $studio; + return 0 unless defined $serie; + return 0 unless defined $params; + return 0 unless defined $user; + return 0 unless defined $studio->{location}; - my $projects = project::get( $config, { project_id => $project_id } ); - if ( @$projects == 0 ) { - print STDERR "project not found at insert event\n"; - return 0; - } - my $projectName = $projects->[0]->{name}; - my $event = { - project => $projectName, - location => $studio->{location}, # location from studio - }; + my $projects = project::get( $config, { project_id => $project_id } ); + if ( scalar @$projects == 0 ) { + print STDERR "project not found at insert event\n"; + return 0; + } + my $projectName = $projects->[0]->{name}; + my $event = { + project => $projectName, + location => $studio->{location}, # location from studio + }; - #print '

';
-	$event = series_events::add_event_dates( $config, $event, $params );
+    #print '
';
+    $event = series_events::add_event_dates( $config, $event, $params );
 
-	#get event content from series
-	for my $attr ( 'program', 'series_name', 'title', 'excerpt', 'content', 'topic', 'image', 'episode', 'podcast_url', 'archive_url' ) {
-		$event->{$attr} = $serie->{$attr} if defined $serie->{$attr};
-	}
-	$event->{series_image}       = $serie->{image}   if defined $serie->{image};
-	$event->{series_image_label} = $serie->{licence} if defined $serie->{licence};
+    #get event content from series
+    for my $attr ( 'program', 'series_name', 'title', 'excerpt', 'content', 'topic', 'image', 'episode', 'podcast_url', 'archive_url' ) {
+        $event->{$attr} = $serie->{$attr} if defined $serie->{$attr};
+    }
+    $event->{series_image}       = $serie->{image}   if defined $serie->{image};
+    $event->{series_image_label} = $serie->{licence} if defined $serie->{licence};
 
-	#overwrite series values from parameters
-	for my $attr (
-		'program', 'series_name', 'title', 'user_title', 'excerpt',     'user_except',
-		'content', 'topic',       'image', 'episode',    'podcast_url', 'archive_url'
-	  )
-	{
-		$event->{$attr} = $params->{$attr} if defined $params->{$attr};
-	}
-	$event->{'html_content'} = markup::creole_to_html( $event->{'content'} ) if defined $event->{'content'};
-	$event->{'html_topic'}   = markup::creole_to_html( $event->{'topic'} )   if defined $event->{'topic'};
+    #overwrite series values from parameters
+    for my $attr (
+        'program', 'series_name', 'title', 'user_title', 'excerpt',     'user_except',
+        'content', 'topic',       'image', 'episode',    'podcast_url', 'archive_url'
+      )
+    {
+        $event->{$attr} = $params->{$attr} if defined $params->{$attr};
+    }
+    $event->{'html_content'} = markup::creole_to_html( $event->{'content'} ) if defined $event->{'content'};
+    $event->{'html_topic'}   = markup::creole_to_html( $event->{'topic'} )   if defined $event->{'topic'};
 
-	#add event status
-	for my $attr ( 'live', 'published', 'playout', 'archived', 'rerun', 'draft', 'disable_event_sync' ) {
-		$event->{$attr} = $params->{$attr} || 0;
-	}
+    #add event status
+    for my $attr ( 'live', 'published', 'playout', 'archived', 'rerun', 'draft', 'disable_event_sync' ) {
+        $event->{$attr} = $params->{$attr} || 0;
+    }
 
-	if ( $serie->{has_single_events} eq '1' ) {
-		delete $event->{series_name};
-		delete $event->{episode};
-	}
+    if ( $serie->{has_single_events} eq '1' ) {
+        delete $event->{series_name};
+        delete $event->{episode};
+    }
 
-	$event->{modified_at} = time::time_to_datetime( time() );
-	$event->{created_at}  = time::time_to_datetime( time() );
-	$event->{modified_by} = $user;
+    $event->{modified_at} = time::time_to_datetime( time() );
+    $event->{created_at}  = time::time_to_datetime( time() );
+    $event->{modified_by} = $user;
 
-	#print STDERR Dumper($event);
-	my $dbh = db::connect($config);
-	my $event_id = db::insert( $dbh, 'calcms_events', $event );
+    #print STDERR Dumper($event);
+    my $dbh = db::connect($config);
+    my $event_id = db::insert( $dbh, 'calcms_events', $event );
 
-	#add to history
-	$event->{project_id} = $project_id;
-	$event->{studio_id}  = $studio->{id};
-	$event->{series_id}  = $serie->{series_id};
-	$event->{event_id}   = $event_id;
-	event_history::insert( $config, $event );
-	return $event_id;
+    #add to history
+    $event->{project_id} = $project_id;
+    $event->{studio_id}  = $studio->{id};
+    $event->{series_id}  = $serie->{series_id};
+    $event->{event_id}   = $event_id;
+    event_history::insert( $config, $event );
+    return $event_id;
 }
 
 #set start, end, start-date, end_date to an event
 sub add_event_dates {
-	my $config = shift;
-	my $event  = shift;
-	my $params = shift;
+    my $config = shift;
+    my $event  = shift;
+    my $params = shift;
 
-	#start and end datetime
-	$event->{start} = $params->{start_date};
-	$event->{end} = time::add_minutes_to_datetime( $params->{start_date}, $params->{duration} );
+    #start and end datetime
+    $event->{start} = $params->{start_date};
+    $event->{end} = time::add_minutes_to_datetime( $params->{start_date}, $params->{duration} );
 
-	#set program days
-	my $day_start = $config->{date}->{day_starting_hour};
-	$event->{start_date} = time::date_cond( time::add_hours_to_datetime( $event->{start}, -$day_start ) );
-	$event->{end_date}   = time::date_cond( time::add_hours_to_datetime( $event->{end},   -$day_start ) );
-	return $event;
+    #set program days
+    my $day_start = $config->{date}->{day_starting_hour};
+    $event->{start_date} = time::date_cond( time::add_hours_to_datetime( $event->{start}, -$day_start ) );
+    $event->{end_date}   = time::date_cond( time::add_hours_to_datetime( $event->{end},   -$day_start ) );
+    return $event;
 }
 
 sub update_series_images {
-	my $config  = shift;
-	my $options = shift;
+    my $config  = shift;
+    my $options = shift;
 
-	return "missing project_id"   unless defined $options->{project_id};
-	return "missing studio_id"    unless defined $options->{studio_id};
-	return "missing series_id"    unless defined $options->{series_id};
-	return "missing series_image" unless defined $options->{series_image};
+    return "missing project_id"   unless defined $options->{project_id};
+    return "missing studio_id"    unless defined $options->{studio_id};
+    return "missing series_id"    unless defined $options->{series_id};
+    return "missing series_image" unless defined $options->{series_image};
 
-	#print "save $options->{series_image}\n";
+    #print "save $options->{series_image}\n";
 
-	my $events = series::get_events(
-		$config,
-		{
-			project_id => $options->{project_id},
-			studio_id  => $options->{studio_id},
-			series_id  => $options->{series_id},
-		}
-	);
+    my $events = series::get_events(
+        $config,
+        {
+            project_id => $options->{project_id},
+            studio_id  => $options->{studio_id},
+            series_id  => $options->{series_id},
+        }
+    );
 
-	for my $event (@$events) {
-		$event->{series_image} = $options->{series_image};
-		series_events::save_content( $config, $event );
-	}
+    for my $event (@$events) {
+        $event->{series_image} = $options->{series_image};
+        series_events::save_content( $config, $event );
+    }
 }
 
 sub error {
-	my $msg = shift;
-	print "ERROR: $msg
\n"; + my $msg = shift; + print "ERROR: $msg
\n"; } #do not delete last line! diff --git a/lib/calcms/startup.pl b/lib/calcms/startup.pl index 55a6121..8daa587 100644 --- a/lib/calcms/startup.pl +++ b/lib/calcms/startup.pl @@ -2,6 +2,7 @@ use lib qw(/home/radio/calcms/calcms/); use Data::Dumper; use Apache::DBI(); + #$Apache::DBI::DEBUG = 2; use Time::Local(); diff --git a/lib/calcms/studios.pm b/lib/calcms/studios.pm index 490c119..f9efaac 100644 --- a/lib/calcms/studios.pm +++ b/lib/calcms/studios.pm @@ -16,157 +16,157 @@ our %EXPORT_TAGS = ( 'all' => [@EXPORT_OK] ); sub debug; sub get_columns { - my $config = shift; + my $config = shift; - my $dbh = db::connect($config); - my $cols = db::get_columns( $dbh, 'calcms_studios' ); - my $columns = {}; - for my $col (@$cols) { - $columns->{$col} = 1; - } - return $columns; + my $dbh = db::connect($config); + my $cols = db::get_columns( $dbh, 'calcms_studios' ); + my $columns = {}; + for my $col (@$cols) { + $columns->{$col} = 1; + } + return $columns; } sub get { - my $config = shift; - my $condition = shift || {}; + my $config = shift; + my $condition = shift || {}; - my @conditions = (); - my @bind_values = (); + my @conditions = (); + my @bind_values = (); - if ( ( defined $condition->{studio_id} ) && ( $condition->{studio_id} ne '' ) ) { - push @conditions, 's.id=?'; - push @bind_values, $condition->{studio_id}; - } + if ( ( defined $condition->{studio_id} ) && ( $condition->{studio_id} ne '' ) ) { + push @conditions, 's.id=?'; + push @bind_values, $condition->{studio_id}; + } - if ( ( defined $condition->{name} ) && ( $condition->{name} ne '' ) ) { - push @conditions, 's.name=?'; - push @bind_values, $condition->{name}; - } + if ( ( defined $condition->{name} ) && ( $condition->{name} ne '' ) ) { + push @conditions, 's.name=?'; + push @bind_values, $condition->{name}; + } - if ( ( defined $condition->{location} ) && ( $condition->{location} ne '' ) ) { - push @conditions, 's.location=?'; - push @bind_values, $condition->{location}; - } + if ( ( defined $condition->{location} ) && ( $condition->{location} ne '' ) ) { + push @conditions, 's.location=?'; + push @bind_values, $condition->{location}; + } - my $limit = ''; - if ( ( defined $condition->{limit} ) && ( $condition->{limit} ne '' ) ) { - $limit = 'limit ' . $condition->{limit}; - } + my $limit = ''; + if ( ( defined $condition->{limit} ) && ( $condition->{limit} ne '' ) ) { + $limit = 'limit ' . $condition->{limit}; + } - my $query = ''; - unless ( ( defined $condition->{project_id} ) && ( $condition->{project_id} ne '' ) ) { - my $conditions = ''; - $conditions = " where " . join( " and ", @conditions ) if ( @conditions > 0 ); - $query = qq{ + my $query = ''; + unless ( ( defined $condition->{project_id} ) && ( $condition->{project_id} ne '' ) ) { + my $conditions = ''; + $conditions = " where " . join( " and ", @conditions ) if ( @conditions > 0 ); + $query = qq{ select * from calcms_studios s $conditions $limit }; - } else { - push @conditions, 's.id=ps.studio_id'; + } else { + push @conditions, 's.id=ps.studio_id'; - push @conditions, 'ps.project_id=?'; - push @bind_values, $condition->{project_id}; - my $conditions = " where " . join( " and ", @conditions ) if ( @conditions > 0 ); - $query = qq{ + push @conditions, 'ps.project_id=?'; + push @bind_values, $condition->{project_id}; + my $conditions = " where " . join( " and ", @conditions ) if ( @conditions > 0 ); + $query = qq{ select * from calcms_studios s, calcms_project_studios ps $conditions $limit }; - } - my $dbh = db::connect($config); + } + my $dbh = db::connect($config); - #print STDERR Dumper($query).Dumper(\@bind_values); - my $studios = db::get( $dbh, $query, \@bind_values ); - return $studios; + #print STDERR Dumper($query).Dumper(\@bind_values); + my $studios = db::get( $dbh, $query, \@bind_values ); + return $studios; } sub getImageById { - my $config = shift; - my $conditions = shift; + my $config = shift; + my $conditions = shift; - return undef unless defined $conditions->{project_id}; - return undef unless defined $conditions->{studio_id}; - my $studios = studios::get( $config, $conditions ); - return undef if scalar(@$studios) != 1; - return $studios->[0]->{image}; + return undef unless defined $conditions->{project_id}; + return undef unless defined $conditions->{studio_id}; + my $studios = studios::get( $config, $conditions ); + return undef if scalar(@$studios) != 1; + return $studios->[0]->{image}; } sub insert { - my $config = shift; - my $entry = shift; + my $config = shift; + my $entry = shift; - $entry->{created_at} = time::time_to_datetime( time() ); - $entry->{modified_at} = time::time_to_datetime( time() ); - $entry->{image} = images::normalizeName( $entry->{image} ) if defined $entry->{image}; + $entry->{created_at} = time::time_to_datetime( time() ); + $entry->{modified_at} = time::time_to_datetime( time() ); + $entry->{image} = images::normalizeName( $entry->{image} ) if defined $entry->{image}; - my $dbh = db::connect($config); - my $id = db::insert( $dbh, 'calcms_studios', $entry ); - return $id; + my $dbh = db::connect($config); + my $id = db::insert( $dbh, 'calcms_studios', $entry ); + return $id; } sub update { - my $config = shift; - my $studio = shift; + my $config = shift; + my $studio = shift; - $studio->{modified_at} = time::time_to_datetime( time() ); + $studio->{modified_at} = time::time_to_datetime( time() ); - my $columns = get_columns($config); - my $entry = {}; - for my $column ( keys %$columns ) { - $entry->{$column} = $studio->{$column} if defined $studio->{$column}; - } - $entry->{image} = images::normalizeName( $entry->{image} ) if defined $entry->{image}; + my $columns = get_columns($config); + my $entry = {}; + for my $column ( keys %$columns ) { + $entry->{$column} = $studio->{$column} if defined $studio->{$column}; + } + $entry->{image} = images::normalizeName( $entry->{image} ) if defined $entry->{image}; - my $values = join( ",", map { $_ . '=?' } ( keys %$entry ) ); - my @bind_values = map { $entry->{$_} } ( keys %$entry ); - push @bind_values, $entry->{id}; + my $values = join( ",", map { $_ . '=?' } ( keys %$entry ) ); + my @bind_values = map { $entry->{$_} } ( keys %$entry ); + push @bind_values, $entry->{id}; - my $query = qq{ + my $query = qq{ update calcms_studios set $values where id=? }; - my $dbh = db::connect($config); + my $dbh = db::connect($config); - #print STDERR Dumper($query)." ".Dumper(\@bind_values); - db::put( $dbh, $query, \@bind_values ); + #print STDERR Dumper($query)." ".Dumper(\@bind_values); + db::put( $dbh, $query, \@bind_values ); } sub delete { - my $config = shift; - my $studio = shift; + my $config = shift; + my $studio = shift; - my $dbh = db::connect($config); - db::put( $dbh, 'delete from calcms_studios where id=?', [ $studio->{id} ] ); + my $dbh = db::connect($config); + db::put( $dbh, 'delete from calcms_studios where id=?', [ $studio->{id} ] ); } #TODO rename to check sub check_studio { - my $config = shift; - my $options = shift; - return check( $config, $options ); + my $config = shift; + my $options = shift; + return check( $config, $options ); } sub check { - my $config = shift; - my $options = shift; - return "missing studio_id" unless defined $options->{studio_id}; - return "Please select a studio" if ( $options->{studio_id} eq '-1' ); - return "Please select a studio" if ( $options->{studio_id} eq '' ); - my $studios = studios::get( $config, { studio_id => $options->{studio_id} } ); - return "Sorry. unknown studio" unless defined $studios; - return "Sorry. unknown studio" unless @$studios == 1; - return 1; + my $config = shift; + my $options = shift; + return "missing studio_id" unless defined $options->{studio_id}; + return "Please select a studio" if ( $options->{studio_id} eq '-1' ); + return "Please select a studio" if ( $options->{studio_id} eq '' ); + my $studios = studios::get( $config, { studio_id => $options->{studio_id} } ); + return "Sorry. unknown studio" unless defined $studios; + return "Sorry. unknown studio" unless scalar @$studios == 1; + return 1; } sub error { - my $msg = shift; - print "ERROR: $msg
\n"; + my $msg = shift; + print "ERROR: $msg
\n"; } #do not delete last line! diff --git a/lib/calcms/template.pm b/lib/calcms/template.pm index b296e5d..35d1061 100644 --- a/lib/calcms/template.pm +++ b/lib/calcms/template.pm @@ -23,205 +23,205 @@ our %EXPORT_TAGS = ( 'all' => [@EXPORT_OK] ); sub process { - # my $output=$_[0]; - my $filename = $_[1]; - my $params = $_[2]; + # my $output=$_[0]; + my $filename = $_[1]; + my $params = $_[2]; - my $config = $config::config; - for my $key ( keys %{ $config::config->{locations} } ) { - $params->{$key} = $config::config->{locations}->{$key} if ( $key =~ /\_url$/ ); - } + my $config = $config::config; + for my $key ( keys %{ $config::config->{locations} } ) { + $params->{$key} = $config::config->{locations}->{$key} if ( $key =~ /\_url$/ ); + } - # add current project - unless ( defined $params->{project_title} ) { - my $projects = project::get_with_dates( $config, { name => $config->{project} } ); - if ( @$projects == 1 ) { - my $project = $projects->[0]; - foreach my $key ( keys %$project ) { - $params->{ 'project_' . $key } = $project->{$key}; - } - } - } + # add current project + unless ( defined $params->{project_title} ) { + my $projects = project::get_with_dates( $config, { name => $config->{project} } ); + if ( scalar @$projects == 1 ) { + my $project = $projects->[0]; + foreach my $key ( keys %$project ) { + $params->{ 'project_' . $key } = $project->{$key}; + } + } + } - $params->{user} = $ENV{REMOTE_USER} unless defined $params->{user}; + $params->{user} = $ENV{REMOTE_USER} unless defined $params->{user}; - my $user_permissions = roles::get_user_permissions(); - for my $permission ( keys %$user_permissions ) { - $params->{$permission} = $user_permissions->{$permission} if ( $user_permissions->{$permission} eq '1' ); - } + my $user_permissions = roles::get_user_permissions(); + for my $permission ( keys %$user_permissions ) { + $params->{$permission} = $user_permissions->{$permission} if ( $user_permissions->{$permission} eq '1' ); + } - $params->{jobs} = roles::get_user_jobs(); - if ( ( $filename =~ /json\-p/ ) || (params::isJson) ) { - my $header = "Content-type:application/json; charset=utf-8\n\n"; - my $json = JSON::to_json( $params, { pretty => 1 } ); + $params->{jobs} = roles::get_user_jobs(); + if ( ( $filename =~ /json\-p/ ) || (params::isJson) ) { + my $header = "Content-type:application/json; charset=utf-8\n\n"; + my $json = JSON::to_json( $params, { pretty => 1 } ); - # $json=$header.$params->{json_callback}.'['.$json.']'; - $json = $header . $params->{json_callback} . $json; - if ( ( defined $_[0] ) && ( $_[0] eq 'print' ) ) { - print $json. "\n"; - } else { - $_[0] = $json . "\n"; - } - return; - } + # $json=$header.$params->{json_callback}.'['.$json.']'; + $json = $header . $params->{json_callback} . $json; + if ( ( defined $_[0] ) && ( $_[0] eq 'print' ) ) { + print $json. "\n"; + } else { + $_[0] = $json . "\n"; + } + return; + } - #print STDERR $filename."\n"; - log::error( $config, "cannot find template $filename " ) unless -e $filename; - log::error( $config, "cannot read template $filename " ) unless -r $filename; + #print STDERR $filename."\n"; + log::error( $config, "cannot find template $filename " ) unless -e $filename; + log::error( $config, "cannot read template $filename " ) unless -r $filename; - my $default_escape = '0'; - $default_escape = 'JS' if ( $filename =~ /\.js$/ ); - $default_escape = 'JS' if ( $filename =~ /\.json$/ ); - $default_escape = 'HTML_ALL' if ( $filename =~ /\.html$/ ); + my $default_escape = '0'; + $default_escape = 'JS' if ( $filename =~ /\.js$/ ); + $default_escape = 'JS' if ( $filename =~ /\.json$/ ); + $default_escape = 'HTML_ALL' if ( $filename =~ /\.html$/ ); - my $html_template = undef; + my $html_template = undef; - unless ( $filename =~ /\.xml$/ ) { - $html_template = HTML::Template::Compiled->new( - filename => $filename, - die_on_bad_params => 0, - case_sensitive => 0, - loop_context_vars => 0, - global_vars => 0, - tagstyle => '-asp -comment', - default_escape => $default_escape, - cache => 0, - utf8 => 1, - ); - } else { - $html_template = HTML::Template::Compiled->new( - filename => $filename, - die_on_bad_params => 0, - case_sensitive => 1, - loop_context_vars => 0, - global_vars => 0, - tagstyle => '-asp -comment', - default_escape => 'XML', - plugin => [qw(HTML::Template::Compiled::Plugin::XMLEscape)], - utf8 => 1 - ); - } + unless ( $filename =~ /\.xml$/ ) { + $html_template = HTML::Template::Compiled->new( + filename => $filename, + die_on_bad_params => 0, + case_sensitive => 0, + loop_context_vars => 0, + global_vars => 0, + tagstyle => '-asp -comment', + default_escape => $default_escape, + cache => 0, + utf8 => 1, + ); + } else { + $html_template = HTML::Template::Compiled->new( + filename => $filename, + die_on_bad_params => 0, + case_sensitive => 1, + loop_context_vars => 0, + global_vars => 0, + tagstyle => '-asp -comment', + default_escape => 'XML', + plugin => [qw(HTML::Template::Compiled::Plugin::XMLEscape)], + utf8 => 1 + ); + } - #$params= - setRelativeUrls( $params, 0 ) unless ( defined $params->{extern} ) && ( $params->{extern} eq '1' ); + #$params= + setRelativeUrls( $params, 0 ) unless ( defined $params->{extern} ) && ( $params->{extern} eq '1' ); - # HTML::Template::Compiled->preload($cache_dir); - $html_template->param($params); - if ( ( defined $_[0] ) && ( $_[0] eq 'print' ) ) { - print $html_template->output; - } else { - $_[0] = $html_template->output; - } + # HTML::Template::Compiled->preload($cache_dir); + $html_template->param($params); + if ( ( defined $_[0] ) && ( $_[0] eq 'print' ) ) { + print $html_template->output; + } else { + $_[0] = $html_template->output; + } } # set relative urls in nested params structure sub setRelativeUrls { - my $params = shift; - my $depth = shift || 0; + my $params = shift; + my $depth = shift || 0; - #print STDERR "setRelativeUrls depth:$depth ".ref($params)."\n"; + #print STDERR "setRelativeUrls depth:$depth ".ref($params)."\n"; - return unless defined $params; + return unless defined $params; - if ( $depth > 10 ) { - print STDERR "prevent deep recursion in template::setRelativeUrls()\n"; - return; - } + if ( $depth > 10 ) { + print STDERR "prevent deep recursion in template::setRelativeUrls()\n"; + return; + } - # set recursive for hash - if ( ref($params) eq 'HASH' ) { - for my $key ( keys %$params ) { + # set recursive for hash + if ( ref($params) eq 'HASH' ) { + for my $key ( keys %$params ) { - #next unless ($key eq 'icon') || ($key eq 'thumb'); - my $val = $params->{$key}; - next unless defined $val; - if ( ref($val) eq '' ) { + #next unless ($key eq 'icon') || ($key eq 'thumb'); + my $val = $params->{$key}; + next unless defined $val; + if ( ref($val) eq '' ) { - # make link relative - $params->{$key} =~ s/^https?\:(\/\/[^\/]+)/$1/; - } elsif ( ( ref($val) eq 'HASH' ) || ( ref($val) eq 'ARRAY' ) ) { - setRelativeUrls( $params->{$key}, $depth + 1 ); - } - } - return $params; - } + # make link relative + $params->{$key} =~ s/^https?\:(\/\/[^\/]+)/$1/; + } elsif ( ( ref($val) eq 'HASH' ) || ( ref($val) eq 'ARRAY' ) ) { + setRelativeUrls( $params->{$key}, $depth + 1 ); + } + } + return $params; + } - # set recursive for arrays - if ( ref($params) eq 'ARRAY' ) { - for my $i ( 0 .. @$params ) { - my $val = $params->[$i]; - next unless defined $val; - if ( ( ref($val) eq 'HASH' ) || ( ref($val) eq 'ARRAY' ) ) { - setRelativeUrls( $params->[$i], $depth + 1 ); - } - } - return $params; - } + # set recursive for arrays + if ( ref($params) eq 'ARRAY' ) { + for my $i ( 0 .. @$params ) { + my $val = $params->[$i]; + next unless defined $val; + if ( ( ref($val) eq 'HASH' ) || ( ref($val) eq 'ARRAY' ) ) { + setRelativeUrls( $params->[$i], $depth + 1 ); + } + } + return $params; + } - return $params; + return $params; } #requires read config sub check { - my $template = shift || ''; - my $default = shift; + my $template = shift || ''; + my $default = shift; - if ( $template =~ /json\-p/ ) { - $template =~ s/[^a-zA-Z0-9\-\_\.]//g; - $template =~ s/\.{2,99}/\./g; - return $template; - } + if ( $template =~ /json\-p/ ) { + $template =~ s/[^a-zA-Z0-9\-\_\.]//g; + $template =~ s/\.{2,99}/\./g; + return $template; + } - my $config = $config::config; - if ( $template eq '' ) { - $template = $default; - } else { - $template =~ s/^\.\///gi; + my $config = $config::config; + if ( $template eq '' ) { + $template = $default; + } else { + $template =~ s/^\.\///gi; - #template does use ';' in filename - log::error( $config, 'invalid template!' ) if ( $template =~ /;/ ); + #template does use ';' in filename + log::error( $config, 'invalid template!' ) if ( $template =~ /;/ ); - #template does use '..' in filename - log::error( $config, 'invalid template!' ) if ( $template =~ /\.\./ ); - } + #template does use '..' in filename + log::error( $config, 'invalid template!' ) if ( $template =~ /\.\./ ); + } - #print STDERR $config::config->{cache}->{compress}."<.compres default:$template\n"; - $template = ( split( /\//, $template ) )[-1]; - my $cwd = Cwd::getcwd(); + #print STDERR $config::config->{cache}->{compress}."<.compres default:$template\n"; + $template = ( split( /\//, $template ) )[-1]; + my $cwd = Cwd::getcwd(); - $template .= '.html' unless ( $template =~ /\./ ); - if ( ( $config::config->{cache}->{compress} eq '1' ) && ( -e $cwd . '/templates/compressed/' . $template ) ) { - $template = $cwd . '/templates/compressed/' . $template; - } elsif ( -e $cwd . '/templates/' . $template ) { - $template = $cwd . '/templates/' . $template; - } else { - log::error( $config, "template not found: '$cwd/$template'" ); + $template .= '.html' unless ( $template =~ /\./ ); + if ( ( $config::config->{cache}->{compress} eq '1' ) && ( -e $cwd . '/templates/compressed/' . $template ) ) { + $template = $cwd . '/templates/compressed/' . $template; + } elsif ( -e $cwd . '/templates/' . $template ) { + $template = $cwd . '/templates/' . $template; + } else { + log::error( $config, "template not found: '$cwd/$template'" ); - } + } - log::error( $config, "missing permission to read template '$template'" ) unless ( -r $template ); - return $template; + log::error( $config, "missing permission to read template '$template'" ) unless ( -r $template ); + return $template; } #deprecated (for old admin only) sub exit_on_missing_permission { - my $permission = shift; - my $user_permissions = roles::get_user_permissions(); - if ( $user_permissions->{$permission} ne '1' ) { - print STDERR "missing permission to $permission\n"; - template::process( 'print', template::check('default.html'), { error => 'sorry, missing permission!' } ); - die(); + my $permission = shift; + my $user_permissions = roles::get_user_permissions(); + if ( $user_permissions->{$permission} ne '1' ) { + print STDERR "missing permission to $permission\n"; + template::process( 'print', template::check('default.html'), { error => 'sorry, missing permission!' } ); + die(); - #exit; - } + #exit; + } } sub clear_cache { - HTML::Template::Compiled->clear_cache(); + HTML::Template::Compiled->clear_cache(); - # return; - # my $html_template = HTML::Template::Compiled->new(); - # $html_template->clear_cache(); + # return; + # my $html_template = HTML::Template::Compiled->new(); + # $html_template->clear_cache(); } #do not delete last line! diff --git a/lib/calcms/time.pm b/lib/calcms/time.pm index 6182ef4..04b8394 100644 --- a/lib/calcms/time.pm +++ b/lib/calcms/time.pm @@ -32,584 +32,584 @@ our @EXPORT_OK = qw( our %EXPORT_TAGS = ( 'all' => [@EXPORT_OK] ); our $names = { - 'de' => { - months => - [ 'Januar', 'Februar', 'März', 'April', 'Mai', 'Juni', 'Juli', 'August', 'September', 'Oktober', 'November', 'Dezember' ], - months_abbr => [ 'Jan', 'Feb', 'Mär', 'Apr', 'Mai', 'Jun', 'Jul', 'Aug', 'Sep', 'Okt', 'Nov', 'Dez' ], - weekdays => [ 'Montag', 'Dienstag', 'Mittwoch', 'Donnerstag', 'Freitag', 'Samstag', 'Sonntag' ], - weekdays_abbr => [ 'Mo', 'Di', 'Mi', 'Do', 'Fr', 'Sa', 'So' ], - }, - 'en' => { - months => - [ 'January', 'February', 'March', 'April', 'May', 'June', 'Jule', 'August', 'September', 'October', 'November', 'December' ], - months_abbr => [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ], - weekdays => [ 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday' ], - weekdays_abbr => [ 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa', 'Su' ], - }, + 'de' => { + months => + [ 'Januar', 'Februar', 'März', 'April', 'Mai', 'Juni', 'Juli', 'August', 'September', 'Oktober', 'November', 'Dezember' ], + months_abbr => [ 'Jan', 'Feb', 'Mär', 'Apr', 'Mai', 'Jun', 'Jul', 'Aug', 'Sep', 'Okt', 'Nov', 'Dez' ], + weekdays => [ 'Montag', 'Dienstag', 'Mittwoch', 'Donnerstag', 'Freitag', 'Samstag', 'Sonntag' ], + weekdays_abbr => [ 'Mo', 'Di', 'Mi', 'Do', 'Fr', 'Sa', 'So' ], + }, + 'en' => { + months => + [ 'January', 'February', 'March', 'April', 'May', 'June', 'Jule', 'August', 'September', 'October', 'November', 'December' ], + months_abbr => [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ], + weekdays => [ 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday' ], + weekdays_abbr => [ 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa', 'Su' ], + }, }; our $durations = [ - 0, 5, 10, 15, 20, 30, 40, 45, 50, 60, 70, 75, 80, 90, 100, 105, 110, 115, - 120, 135, 150, 165, 180, 195, 210, 225, 240, 300, 360, 420, 480, 540, 600, 660, 720, 1440 + 0, 5, 10, 15, 20, 30, 40, 45, 50, 60, 70, 75, 80, 90, 100, 105, 110, 115, + 120, 135, 150, 165, 180, 195, 210, 225, 240, 300, 360, 420, 480, 540, 600, 660, 720, 1440 ]; sub get_names { - my $language = shift || 'en'; - return $time::names->{$language}; + my $language = shift || 'en'; + return $time::names->{$language}; } sub get_all_names { - return $time::names; + return $time::names; } sub get_durations { - return $time::durations; + return $time::durations; } #TODO: build from datenames our $weekday_index = { - '0' => 0, - '1' => 1, - '2' => 2, - '3' => 3, - '4' => 4, - '5' => 5, - '6' => 6, - 'Mo' => 0, - 'Tu' => 1, - 'Di' => 1, - 'We' => 2, - 'Mi' => 2, - 'Th' => 3, - 'Do' => 3, - 'Fr' => 4, - 'Sa' => 5, - 'Su' => 6, - 'So' => 6 + '0' => 0, + '1' => 1, + '2' => 2, + '3' => 3, + '4' => 4, + '5' => 5, + '6' => 6, + 'Mo' => 0, + 'Tu' => 1, + 'Di' => 1, + 'We' => 2, + 'Mi' => 2, + 'Th' => 3, + 'Do' => 3, + 'Fr' => 4, + 'Sa' => 5, + 'Su' => 6, + 'So' => 6 }; sub get_weekdays { - return { - 1 => 1, - 2 => 2, - 3 => 3, - 4 => 4, - 5 => 5, - 6 => 6, - 7 => 7, - 'Mo' => 1, - 'Tu' => 2, - 'Di' => 2, - 'We' => 3, - 'Mi' => 3, - 'Th' => 4, - 'Do' => 4, - 'Fr' => 5, - 'Sa' => 6, - 'Su' => 7, - 'So' => 7 - }; + return { + 1 => 1, + 2 => 2, + 3 => 3, + 4 => 4, + 5 => 5, + 6 => 6, + 7 => 7, + 'Mo' => 1, + 'Tu' => 2, + 'Di' => 2, + 'We' => 3, + 'Mi' => 3, + 'Th' => 4, + 'Do' => 4, + 'Fr' => 5, + 'Sa' => 6, + 'Su' => 7, + 'So' => 7 + }; } #deprecated, for wordpress sync sub format_datetime { - my $datetime = shift; - return $datetime if ( $datetime eq '' ); - return add_hours_to_datetime( $datetime, 0 ); + my $datetime = shift; + return $datetime if ( $datetime eq '' ); + return add_hours_to_datetime( $datetime, 0 ); } #deprecated sub format_time { - my $t = $_[0]; + my $t = $_[0]; - my $year = $t->[5] + 1900; - my $month = $t->[4] + 1; - $month = '0' . $month if ( length($month) == 1 ); + my $year = $t->[5] + 1900; + my $month = $t->[4] + 1; + $month = '0' . $month if ( length($month) == 1 ); - my $day = $t->[3]; - $day = '0' . $day if ( length($day) == 1 ); + my $day = $t->[3]; + $day = '0' . $day if ( length($day) == 1 ); - my $hour = $t->[2]; - $hour = '0' . $hour if ( length($hour) == 1 ); + my $hour = $t->[2]; + $hour = '0' . $hour if ( length($hour) == 1 ); - my $minute = $t->[1]; - $minute = '0' . $minute if ( length($minute) == 1 ); + my $minute = $t->[1]; + $minute = '0' . $minute if ( length($minute) == 1 ); - return [ $day, $month, $year, $hour, $minute ]; + return [ $day, $month, $year, $hour, $minute ]; } # convert datetime to unix time sub datetime_to_time { - my $datetime = $_[0]; + my $datetime = $_[0]; - # print $datetime."\n"; - if ( $datetime =~ /(\d\d\d\d)\-(\d+)\-(\d+)[T\s](\d+)\:(\d+)(\:(\d+))?/ ) { - my $year = $1; - my $month = $2 - 1; - my $day = $3; - my $hour = $4; - my $minute = $5; - my $second = $8 || 0; - return Time::Local::timelocal( $second, $minute, $hour, $day, $month, $year ); + # print $datetime."\n"; + if ( $datetime =~ /(\d\d\d\d)\-(\d+)\-(\d+)[T\s](\d+)\:(\d+)(\:(\d+))?/ ) { + my $year = $1; + my $month = $2 - 1; + my $day = $3; + my $hour = $4; + my $minute = $5; + my $second = $8 || 0; + return Time::Local::timelocal( $second, $minute, $hour, $day, $month, $year ); - } else { - print STDERR "datetime_to_time: no valid date time found! ($datetime )\n"; - return -1; - } + } else { + print STDERR "datetime_to_time: no valid date time found! ($datetime )\n"; + return -1; + } } #get rfc822 datetime string from datetime string sub datetime_to_rfc822 { - my $datetime = $_[0]; - my $time = datetime_to_time($datetime); - return POSIX::strftime( "%a, %d %b %Y %H:%M:%S %z", localtime($time) ); + my $datetime = $_[0]; + my $time = datetime_to_time($datetime); + return POSIX::strftime( "%a, %d %b %Y %H:%M:%S %z", localtime($time) ); } #get seconds from epoch sub datetime_to_utc { - my $datetime = shift; - my $time_zone = shift; - $datetime = get_datetime( $datetime, $time_zone ); - return $datetime->epoch(); + my $datetime = shift; + my $time_zone = shift; + $datetime = get_datetime( $datetime, $time_zone ); + return $datetime->epoch(); } # get full utc datetime including timezone offset sub datetime_to_utc_datetime { - my $datetime = shift; - my $time_zone = shift; - $datetime = get_datetime( $datetime, $time_zone ); - return $datetime->format_cldr("yyyy-MM-ddTHH:mm:ssZZZZZ"); + my $datetime = shift; + my $time_zone = shift; + $datetime = get_datetime( $datetime, $time_zone ); + return $datetime->format_cldr("yyyy-MM-ddTHH:mm:ssZZZZZ"); } #add hours to datetime string sub add_hours_to_datetime { - my $datetime = shift; - my $hours = shift; - $hours = 0 unless defined $hours; - return time_to_datetime( datetime_to_time($datetime) + ( 3600 * $hours ) ); + my $datetime = shift; + my $hours = shift; + $hours = 0 unless defined $hours; + return time_to_datetime( datetime_to_time($datetime) + ( 3600 * $hours ) ); } #add minutes to datetime string sub add_minutes_to_datetime { - my $datetime = shift; - my $minutes = shift; - $minutes = 0 unless defined $minutes; - return time_to_datetime( datetime_to_time($datetime) + ( 60 * $minutes ) ); + my $datetime = shift; + my $minutes = shift; + $minutes = 0 unless defined $minutes; + return time_to_datetime( datetime_to_time($datetime) + ( 60 * $minutes ) ); } #add days to datetime string sub add_days_to_datetime { - my $datetime = shift; - my $days = shift; - $days = 0 unless defined $days; - my $time = datetime_to_array($datetime); + my $datetime = shift; + my $days = shift; + $days = 0 unless defined $days; + my $time = datetime_to_array($datetime); - #print STDERR Dumper($time); - ( $time->[0], $time->[1], $time->[2] ) = Date::Calc::Add_Delta_Days( $time->[0] + 0, $time->[1] + 0, $time->[2] + 0, $days ); - return array_to_datetime($time); + #print STDERR Dumper($time); + ( $time->[0], $time->[1], $time->[2] ) = Date::Calc::Add_Delta_Days( $time->[0] + 0, $time->[1] + 0, $time->[2] + 0, $days ); + return array_to_datetime($time); } sub add_days_to_date { - my $datetime = shift; - my $days = shift; - $days = 0 unless defined $days; - my $date = date_to_array($datetime); - ( $date->[0], $date->[1], $date->[2] ) = Date::Calc::Add_Delta_Days( $date->[0] + 0, $date->[1] + 0, $date->[2] + 0, $days ); - return array_to_date($date); + my $datetime = shift; + my $days = shift; + $days = 0 unless defined $days; + my $date = date_to_array($datetime); + ( $date->[0], $date->[1], $date->[2] ) = Date::Calc::Add_Delta_Days( $date->[0] + 0, $date->[1] + 0, $date->[2] + 0, $days ); + return array_to_date($date); } # convert unix time to datetime format sub time_to_datetime { - my $time = shift; - $time = time() unless ( ( defined $time ) && ( $time ne '' ) ); - my @t = localtime($time); - return sprintf( '%04d-%02d-%02d %02d:%02d:%02d', $t[5] + 1900, $t[4] + 1, $t[3], $t[2], $t[1], $t[0] ); + my $time = shift; + $time = time() unless ( defined $time ) && ( $time ne '' ); + my @t = localtime($time); + return sprintf( '%04d-%02d-%02d %02d:%02d:%02d', $t[5] + 1900, $t[4] + 1, $t[3], $t[2], $t[1], $t[0] ); } # convert unix time to date format sub time_to_date { - my $time = shift; - $time = time() unless ( ( defined $time ) && ( $time ne '' ) ); - my @t = localtime($time); - return sprintf( '%04d-%02d-%02d', $t[5] + 1900, $t[4] + 1, $t[3] ); + my $time = shift; + $time = time() unless ( defined $time ) && ( $time ne '' ); + my @t = localtime($time); + return sprintf( '%04d-%02d-%02d', $t[5] + 1900, $t[4] + 1, $t[3] ); } # convert datetime to a array of date/time values sub datetime_to_array { - my $datetime = $_[0] || ''; - if ( $datetime =~ /(\d\d\d\d)\-(\d+)\-(\d+)([T\s]+(\d+)\:(\d+)(\:(\d+))?)?/ ) { - my $year = $1; - my $month = $2; - my $day = $3; - my $hour = $5 || '00'; - my $minute = $6 || '00'; - my $second = $8 || '00'; - return [ $year, $month, $day, $hour, $minute, $second ]; - } - return undef; + my $datetime = $_[0] || ''; + if ( $datetime =~ /(\d\d\d\d)\-(\d+)\-(\d+)([T\s]+(\d+)\:(\d+)(\:(\d+))?)?/ ) { + my $year = $1; + my $month = $2; + my $day = $3; + my $hour = $5 || '00'; + my $minute = $6 || '00'; + my $second = $8 || '00'; + return [ $year, $month, $day, $hour, $minute, $second ]; + } + return undef; } # convert datetime to date sub datetime_to_date { - my $datetime = $_[0] || ''; - if ( $datetime =~ /(\d\d\d\d)\-(\d+)\-(\d+)/ ) { - my $year = $1; - my $month = $2; - my $day = $3; - return sprintf( "%04d-%02d-%02d", $year, $month, $day ); - } - return undef; + my $datetime = $_[0] || ''; + if ( $datetime =~ /(\d\d\d\d)\-(\d+)\-(\d+)/ ) { + my $year = $1; + my $month = $2; + my $day = $3; + return sprintf( "%04d-%02d-%02d", $year, $month, $day ); + } + return undef; } #convert datetime array or single value to datetime string sub array_to_datetime { - my $date = shift; - if ( ref($date) eq 'ARRAY' ) { - return sprintf( "%04d-%02d-%02d %02d:%02d:%02d", $date->[0], $date->[1], $date->[2], $date->[3], $date->[4], $date->[5] ); - } - my $month = shift; - my $day = shift; - my $hour = shift || '0'; - my $minute = shift || '0'; - my $second = shift || '0'; - return sprintf( "%04d-%02d-%02d %02d:%02d:%02d", $date, $month, $day, $hour, $minute, $second ); + my $date = shift; + if ( ref($date) eq 'ARRAY' ) { + return sprintf( "%04d-%02d-%02d %02d:%02d:%02d", $date->[0], $date->[1], $date->[2], $date->[3], $date->[4], $date->[5] ); + } + my $month = shift; + my $day = shift; + my $hour = shift || '0'; + my $minute = shift || '0'; + my $second = shift || '0'; + return sprintf( "%04d-%02d-%02d %02d:%02d:%02d", $date, $month, $day, $hour, $minute, $second ); } #convert date array or single values to date string sub array_to_date { - my $date = shift; - if ( ref($date) eq 'ARRAY' ) { - return sprintf( "%04d-%02d-%02d", $date->[0], $date->[1], $date->[2] ); - } - my $month = shift; - my $day = shift; - return sprintf( "%04d-%02d-%02d", $date, $month, $day ); + my $date = shift; + if ( ref($date) eq 'ARRAY' ) { + return sprintf( "%04d-%02d-%02d", $date->[0], $date->[1], $date->[2] ); + } + my $month = shift; + my $day = shift; + return sprintf( "%04d-%02d-%02d", $date, $month, $day ); } sub array_to_time { - my $date = shift; - if ( ref($date) eq 'ARRAY' ) { - return sprintf( "%02d:%02d:%02d", $date->[3], $date->[4], $date->[5] ); - } - my $minute = shift || 0; - my $second = shift || 0; - return sprintf( "%02d:%02d:%02d", $date, $minute, $second ); + my $date = shift; + if ( ref($date) eq 'ARRAY' ) { + return sprintf( "%02d:%02d:%02d", $date->[3], $date->[4], $date->[5] ); + } + my $minute = shift || 0; + my $second = shift || 0; + return sprintf( "%02d:%02d:%02d", $date, $minute, $second ); } sub array_to_time_hm { - my $date = shift; - if ( ref($date) eq 'ARRAY' ) { - return sprintf( "%02d:%02d", $date->[3], $date->[4] ); - } - my $minute = shift || 0; - return sprintf( "%02d:%02d", $date, $minute ); + my $date = shift; + if ( ref($date) eq 'ARRAY' ) { + return sprintf( "%02d:%02d", $date->[3], $date->[4] ); + } + my $minute = shift || 0; + return sprintf( "%02d:%02d", $date, $minute ); } # get number of days between two days sub days_between { - my $today = $_[0]; - my $date = $_[1]; - my $delta_days = eval { Date::Calc::Delta_Days( $today->[0], $today->[1], $today->[2], $date->[0], $date->[1], $date->[2] ) }; - return $delta_days; + my $today = $_[0]; + my $date = $_[1]; + my $delta_days = eval { Date::Calc::Delta_Days( $today->[0], $today->[1], $today->[2], $date->[0], $date->[1], $date->[2] ) }; + return $delta_days; } sub dayOfYear { - my $datetime = $_[0]; - if ( $datetime =~ /(\d\d\d\d)\-(\d+)\-(\d+)/ ) { - my $year = $1; - my $month = $2; - my $day = $3; - return Date::Calc::Day_of_Year( $year, $month, $day ); - } - return undef; + my $datetime = $_[0]; + if ( $datetime =~ /(\d\d\d\d)\-(\d+)\-(\d+)/ ) { + my $year = $1; + my $month = $2; + my $day = $3; + return Date::Calc::Day_of_Year( $year, $month, $day ); + } + return undef; } # get duration in minutes sub get_duration { - my $start = shift; - my $end = shift; - my $timezone = shift; - $start = time::get_datetime( $start, $timezone ); - $end = time::get_datetime( $end, $timezone ); - my $duration = $end->epoch() - $start->epoch(); - return $duration / 60; + my $start = shift; + my $end = shift; + my $timezone = shift; + $start = time::get_datetime( $start, $timezone ); + $end = time::get_datetime( $end, $timezone ); + my $duration = $end->epoch() - $start->epoch(); + return $duration / 60; } # get duration in seconds sub get_duration_seconds { - my $start = shift; - my $end = shift; - my $timezone = shift || 'UTC'; + my $start = shift; + my $end = shift; + my $timezone = shift || 'UTC'; - unless ( defined $start ) { - print STDERR "time::get_duration_seconds(): start is missing\n"; - return 0; - } - unless ( defined $end ) { - print STDERR "time::get_duration_seconds(): end is missing\n"; - return 0; - } + unless ( defined $start ) { + print STDERR "time::get_duration_seconds(): start is missing\n"; + return 0; + } + unless ( defined $end ) { + print STDERR "time::get_duration_seconds(): end is missing\n"; + return 0; + } - $start = time::get_datetime( $start, $timezone ); - $end = time::get_datetime( $end, $timezone ); - unless ( defined $start ) { - print STDERR "time::get_duration_seconds(): invalid start\n"; - return 0; - } - unless ( defined $end ) { - print STDERR "time::get_duration_seconds(): invalid end\n"; - return 0; - } - my $duration = $end->epoch() - $start->epoch(); - return $duration; + $start = time::get_datetime( $start, $timezone ); + $end = time::get_datetime( $end, $timezone ); + unless ( defined $start ) { + print STDERR "time::get_duration_seconds(): invalid start\n"; + return 0; + } + unless ( defined $end ) { + print STDERR "time::get_duration_seconds(): invalid end\n"; + return 0; + } + my $duration = $end->epoch() - $start->epoch(); + return $duration; } # convert date string to a array of date values sub date_to_array { - my $datetime = $_[0]; - if ( $datetime =~ /(\d\d\d\d)\-(\d+)\-(\d+)/ ) { - my $year = $1; - my $month = $2; - my $day = $3; - return [ $year, $month, $day ]; - } - return undef; + my $datetime = $_[0]; + if ( $datetime =~ /(\d\d\d\d)\-(\d+)\-(\d+)/ ) { + my $year = $1; + my $month = $2; + my $day = $3; + return [ $year, $month, $day ]; + } + return undef; } # parse date string and return date string # pass 'today', return '' on parse error sub date_cond { - my $date = shift; + my $date = shift; - return '' if ( $date eq '' ); - if ( $date =~ /(\d\d\d\d)\-(\d\d?)\-(\d\d?)/ ) { - my $year = $1; - my $month = $2; - my $day = $3; - return sprintf( "%04d-%02d-%02d", $year, $month, $day ); - } - return 'today' if ( $date eq 'today' ); - return ''; + return '' if ( $date eq '' ); + if ( $date =~ /(\d\d\d\d)\-(\d\d?)\-(\d\d?)/ ) { + my $year = $1; + my $month = $2; + my $day = $3; + return sprintf( "%04d-%02d-%02d", $year, $month, $day ); + } + return 'today' if ( $date eq 'today' ); + return ''; } #parse time and return time string hh:mm:ss #return hh:00 if time is 'now' sub time_cond { - my $time = shift; + my $time = shift; - return '' if ( $time eq '' ); - if ( $time =~ /(\d\d?)\:(\d\d?)(\:(\d\d))?/ ) { - my $hour = $1; - my $minute = $2; - my $second = $4 || '00'; - return sprintf( "%02d:%02d:%02d", $hour, $minute, $second ); - } - if ( $time eq 'now' ) { - my $date = datetime_to_array( time_to_datetime( time() ) ); - my $hour = $date->[3] - 2; - $hour = 0 if ( $hour < 0 ); - $time = sprintf( "%02d:00", $hour ); - return $time; - } - return ''; + return '' if ( $time eq '' ); + if ( $time =~ /(\d\d?)\:(\d\d?)(\:(\d\d))?/ ) { + my $hour = $1; + my $minute = $2; + my $second = $4 || '00'; + return sprintf( "%02d:%02d:%02d", $hour, $minute, $second ); + } + if ( $time eq 'now' ) { + my $date = datetime_to_array( time_to_datetime( time() ) ); + my $hour = $date->[3] - 2; + $hour = 0 if ( $hour < 0 ); + $time = sprintf( "%02d:00", $hour ); + return $time; + } + return ''; } #parse date and time string and return yyyy-mm-ddThh:mm:ss sub datetime_cond { - my $datetime = shift; + my $datetime = shift; - return '' if ( $datetime eq '' ); - ( my $date, my $time ) = split /[ T]/, $datetime; - $date = time::date_cond($date); - return '' if ( $date eq '' ); - $time = time::time_cond($time); - return '' if ( $time eq '' ); + return '' if ( $datetime eq '' ); + ( my $date, my $time ) = split /[ T]/, $datetime; + $date = time::date_cond($date); + return '' if ( $date eq '' ); + $time = time::time_cond($time); + return '' if ( $time eq '' ); - return $date . 'T' . $time; + return $date . 'T' . $time; } sub check_date { - my $date = shift; + my $date = shift; - return "" if ( ( !defined $date ) || ( $date eq '' ) ); - if ( $date =~ /(\d\d\d\d)\-(\d\d?)\-(\d\d?)/ ) { - return $1 . '-' . $2 . '-' . $3; - } elsif ( $date =~ /(\d\d?)\.(\d\d?)\.(\d\d\d\d)/ ) { - return $3 . '-' . $2 . '-' . $1; - } - return $date if ( $date eq 'today' || $date eq 'tomorrow' || $date eq 'yesterday' ); - return -1; + return "" if ( !defined $date ) || ( $date eq '' ); + if ( $date =~ /(\d\d\d\d)\-(\d\d?)\-(\d\d?)/ ) { + return $1 . '-' . $2 . '-' . $3; + } elsif ( $date =~ /(\d\d?)\.(\d\d?)\.(\d\d\d\d)/ ) { + return $3 . '-' . $2 . '-' . $1; + } + return $date if ( $date eq 'today' || $date eq 'tomorrow' || $date eq 'yesterday' ); + return -1; - #error("no valid date format given!"); + #error("no valid date format given!"); } sub check_time { - my $time = shift; - return "" if ( ( !defined $time ) || ( $time eq '' ) ); - return $time if ( ( $time eq 'now' ) || ( $time eq 'future' ) ); - if ( $time =~ /(\d\d?)\:(\d\d?)/ ) { - return $1 . ':' . $2; - } - return -1; + my $time = shift; + return "" if ( !defined $time ) || ( $time eq '' ); + return $time if ( $time eq 'now' ) || ( $time eq 'future' ); + if ( $time =~ /(\d\d?)\:(\d\d?)/ ) { + return $1 . ':' . $2; + } + return -1; } sub check_datetime { - my $date = shift; + my $date = shift; - return "" if ( ( !defined $date ) || ( $date eq '' ) ); - if ( $date =~ /(\d\d\d\d)\-(\d\d?)\-(\d\d?)[T ](\d\d?)\:(\d\d?)/ ) { - return sprintf( "%04d-%02d-%02dT%02d:%02d", $1, $2, $3, $4, $5 ); - } - return -1; + return "" if ( !defined $date ) || ( $date eq '' ); + if ( $date =~ /(\d\d\d\d)\-(\d\d?)\-(\d\d?)[T ](\d\d?)\:(\d\d?)/ ) { + return sprintf( "%04d-%02d-%02dT%02d:%02d", $1, $2, $3, $4, $5 ); + } + return -1; } sub check_year_month { - my $date = shift; - return -1 unless ( defined $date ); - return $date if ( $date eq '' ); - if ( $date =~ /(\d\d\d\d)\-(\d\d?)/ ) { - return $1 . '-' . $2 . '-' . $3; - } - return -1; + my $date = shift; + return -1 unless ( defined $date ); + return $date if ( $date eq '' ); + if ( $date =~ /(\d\d\d\d)\-(\d\d?)/ ) { + return $1 . '-' . $2 . '-' . $3; + } + return -1; } #TODO: remove config dependency sub date_time_format { - my $datetime = shift; - my $language = shift || $config::config->{date}->{language} || 'en'; - if ( defined $datetime && $datetime =~ /(\d\d\d\d)\-(\d\d?)\-(\d\d?)[\sT](\d\d?\:\d\d?)/ ) { - my $time = $4; - my $day = $3; - my $month = $2; - my $year = $1; + my $datetime = shift; + my $language = shift || $config::config->{date}->{language} || 'en'; + if ( defined $datetime && $datetime =~ /(\d\d\d\d)\-(\d\d?)\-(\d\d?)[\sT](\d\d?\:\d\d?)/ ) { + my $time = $4; + my $day = $3; + my $month = $2; + my $year = $1; - $month = $time::names->{$language}->{months_abbr}->[ $month - 1 ] || ''; - return "$day. $month $year $time"; - } - return $datetime; + $month = $time::names->{$language}->{months_abbr}->[ $month - 1 ] || ''; + return "$day. $month $year $time"; + } + return $datetime; } #format datetime to date string #TODO: remove config dependency sub date_format { - my $datetime = shift; - my $language = shift || $config::config->{date}->{language} || 'en'; + my $datetime = shift; + my $language = shift || $config::config->{date}->{language} || 'en'; - if ( defined $datetime && $datetime =~ /(\d\d\d\d)\-(\d\d?)\-(\d\d?)/ ) { - my $day = $3; - my $month = $2; - my $year = $1; - $month = $time::names->{$language}->{months_abbr}->[ $month - 1 ] || ''; - return "$day. $month $year"; - } - return $datetime; + if ( defined $datetime && $datetime =~ /(\d\d\d\d)\-(\d\d?)\-(\d\d?)/ ) { + my $day = $3; + my $month = $2; + my $year = $1; + $month = $time::names->{$language}->{months_abbr}->[ $month - 1 ] || ''; + return "$day. $month $year"; + } + return $datetime; } #format datetime to time string sub time_format { - my $datetime = shift; - if ( defined $datetime && $datetime =~ /(\d\d?\:\d\d?)/ ) { - return $1; - } - return $datetime; + my $datetime = shift; + if ( defined $datetime && $datetime =~ /(\d\d?\:\d\d?)/ ) { + return $1; + } + return $datetime; } #get offset from given time_zone sub utc_offset { - my $time_zone = shift; + my $time_zone = shift; - $a = DateTime->now(); - $a->set_time_zone($time_zone); - return $a->strftime("%z"); + $a = DateTime->now(); + $a->set_time_zone($time_zone); + return $a->strftime("%z"); } #get weekday from (yyyy,mm,dd) sub weekday { - my ( $year, $month, $day ) = @_; - my $time = Time::Local::timelocal( 0, 0, 0, $day, $month - 1, $year ); - return ( localtime($time) )[6]; + my ( $year, $month, $day ) = @_; + my $time = Time::Local::timelocal( 0, 0, 0, $day, $month - 1, $year ); + return ( localtime($time) )[6]; } #get current date, related to starting day_starting_hour #TODO: remove config dependency sub get_event_date { - my $config = shift; - $config = $config::config unless defined $config; + my $config = shift; + $config = $config::config unless defined $config; - my $datetime = time::time_to_datetime( time() ); - my $hour = ( time::datetime_to_array($datetime) )->[3]; + my $datetime = time::time_to_datetime( time() ); + my $hour = ( time::datetime_to_array($datetime) )->[3]; - #print STDERR "datetime=$datetime hour=$hour\n"; - #today: between 0:00 and starting_hour show last day - if ( $hour < $config->{date}->{day_starting_hour} ) { - my $date = time::datetime_to_array( time::add_days_to_datetime( $datetime, -1 ) ); - return join( '-', ( $date->[0], $date->[1], $date->[2] ) ); - } else { + #print STDERR "datetime=$datetime hour=$hour\n"; + #today: between 0:00 and starting_hour show last day + if ( $hour < $config->{date}->{day_starting_hour} ) { + my $date = time::datetime_to_array( time::add_days_to_datetime( $datetime, -1 ) ); + return join( '-', ( $date->[0], $date->[1], $date->[2] ) ); + } else { - #today: between starting_hour and end of day show current day - my $date = time::datetime_to_array( time::time_to_datetime( time() ) ); - return join( '-', ( $date->[0], $date->[1], $date->[2] ) ); - } + #today: between starting_hour and end of day show current day + my $date = time::datetime_to_array( time::time_to_datetime( time() ) ); + return join( '-', ( $date->[0], $date->[1], $date->[2] ) ); + } } #get datetime object from datetime string sub get_datetime { - my $datetime = shift; - my $timezone = shift; + my $datetime = shift; + my $timezone = shift; - return unless defined $datetime; - return if $datetime eq ''; - my @l = @{ time::datetime_to_array($datetime) }; - return undef if scalar(@l) == 0; + return unless defined $datetime; + return if $datetime eq ''; + my @l = @{ time::datetime_to_array($datetime) }; + return undef if scalar(@l) == 0; - # catch invalid datees - $datetime = undef; - eval { - $datetime = DateTime->new( - year => $l[0], - month => $l[1], - day => $l[2], - hour => $l[3], - minute => $l[4], - second => $l[5], - time_zone => $timezone - ); - }; - return undef unless defined $datetime; - $datetime->set_locale('de_DE'); - return $datetime; + # catch invalid datees + $datetime = undef; + eval { + $datetime = DateTime->new( + year => $l[0], + month => $l[1], + day => $l[2], + hour => $l[3], + minute => $l[4], + second => $l[5], + time_zone => $timezone + ); + }; + return undef unless defined $datetime; + $datetime->set_locale('de_DE'); + return $datetime; } #get list of nth weekday in month from start to end sub get_nth_weekday_in_month { - my $start = shift; # datetime string - my $end = shift; # datetime string - my $nth = shift; # every nth week of month - my $weekday = shift; # weekday [1..7,'Mo'-'Su','Mo'-'Fr'] + my $start = shift; # datetime string + my $end = shift; # datetime string + my $nth = shift; # every nth week of month + my $weekday = shift; # weekday [1..7,'Mo'-'Su','Mo'-'Fr'] - return [] unless defined $start; - return [] unless defined $end; - return [] unless defined $nth; - return [] unless defined $weekday; + return [] unless defined $start; + return [] unless defined $end; + return [] unless defined $nth; + return [] unless defined $weekday; - my $weekdays = time::get_weekdays(); - return [] unless defined $weekdays->{$weekday}; - $weekday = $weekdays->{$weekday}; + my $weekdays = time::get_weekdays(); + return [] unless defined $weekdays->{$weekday}; + $weekday = $weekdays->{$weekday}; - my $dates = []; + my $dates = []; - if ( $start =~ /(\d\d\d\d)-(\d\d)-(\d\d)[ T](\d\d)\:(\d\d)/ ) { - my $hour = int($4); - my $min = int($5); - my $sec = 0; - my @date = Date::Manip::ParseRecur( "0:1*$nth:$weekday:$hour:$min:$sec", "", $start, $end ); - for my $date (@date) { - if ( $date =~ /(\d\d\d\d)(\d\d)(\d\d)(\d\d)\:(\d\d)\:(\d\d)/ ) { - push @$dates, "$1-$2-$3 $4:$5:$6"; - } - } - } - return $dates; + if ( $start =~ /(\d\d\d\d)-(\d\d)-(\d\d)[ T](\d\d)\:(\d\d)/ ) { + my $hour = int($4); + my $min = int($5); + my $sec = 0; + my @date = Date::Manip::ParseRecur( "0:1*$nth:$weekday:$hour:$min:$sec", "", $start, $end ); + for my $date (@date) { + if ( $date =~ /(\d\d\d\d)(\d\d)(\d\d)(\d\d)\:(\d\d)\:(\d\d)/ ) { + push @$dates, "$1-$2-$3 $4:$5:$6"; + } + } + } + return $dates; } #do not delete last line! diff --git a/lib/calcms/uac.pm b/lib/calcms/uac.pm index 2b558d0..f203092 100644 --- a/lib/calcms/uac.pm +++ b/lib/calcms/uac.pm @@ -31,53 +31,53 @@ sub debug; # get user by name sub get_user { - my $config = shift; - my $user = shift; + my $config = shift; + my $user = shift; - my $query = qq{ + my $query = qq{ select id, name, full_name, email, disabled, modified_at, created_at from calcms_users where name=? }; - my $bind_values = [$user]; + my $bind_values = [$user]; - my $dbh = db::connect($config); - my $users = db::get( $dbh, $query, $bind_values ); - if ( @$users != 1 ) { - print STDERR "cannot find user '$user'\n"; - return undef; - } - return $users->[0]; + my $dbh = db::connect($config); + my $users = db::get( $dbh, $query, $bind_values ); + if ( scalar @$users != 1 ) { + print STDERR "cannot find user '$user'\n"; + return undef; + } + return $users->[0]; } # get all users sub get_users { - my $config = shift; - my $condition = shift; + my $config = shift; + my $condition = shift; - my @conditions = (); - my @bind_values = (); + my @conditions = (); + my @bind_values = (); - for my $key ( 'name', 'email' ) { - my $value = $condition->{$key}; - next unless defined $value; - next if $value eq ''; - push @conditions, $key . '=?'; - push @bind_values, $value; - } + for my $key ( 'name', 'email' ) { + my $value = $condition->{$key}; + next unless defined $value; + next if $value eq ''; + push @conditions, $key . '=?'; + push @bind_values, $value; + } - my $conditions = ''; - $conditions = " where " . join( " and ", @conditions ) if ( scalar @conditions > 0 ); + my $conditions = ''; + $conditions = " where " . join( " and ", @conditions ) if ( scalar @conditions > 0 ); - my $query = qq{ + my $query = qq{ select id, name, full_name, email, disabled, modified_at, created_at from calcms_users $conditions }; - my $dbh = db::connect($config); - my $users = db::get( $dbh, $query, \@bind_values ); - return $users; + my $dbh = db::connect($config); + my $users = db::get( $dbh, $query, \@bind_values ); + return $users; } #TODO: get_users_by_project @@ -85,376 +85,376 @@ sub get_users { # get all users of a given studio id # used at series (previously named get_studio_users) sub get_users_by_studio { - my $config = shift; - my $condition = shift; + my $config = shift; + my $condition = shift; - return unless ( defined $condition->{studio_id} ); + return unless ( defined $condition->{studio_id} ); - my @conditions = (); - my @bind_values = (); + my @conditions = (); + my @bind_values = (); - if ( ( defined $condition->{project_id} ) && ( $condition->{project_id} ne '' ) ) { - push @conditions, 'ur.project_id=?'; - push @bind_values, $condition->{project_id}; - } + if ( ( defined $condition->{project_id} ) && ( $condition->{project_id} ne '' ) ) { + push @conditions, 'ur.project_id=?'; + push @bind_values, $condition->{project_id}; + } - if ( ( defined $condition->{studio_id} ) && ( $condition->{studio_id} ne '' ) ) { - push @conditions, 'ur.studio_id=?'; - push @bind_values, $condition->{studio_id}; - } + if ( ( defined $condition->{studio_id} ) && ( $condition->{studio_id} ne '' ) ) { + push @conditions, 'ur.studio_id=?'; + push @bind_values, $condition->{studio_id}; + } - my $conditions = ''; - $conditions = " and " . join( " and ", @conditions ) if ( scalar @conditions > 0 ); + my $conditions = ''; + $conditions = " and " . join( " and ", @conditions ) if ( scalar @conditions > 0 ); - my $query = qq{ + my $query = qq{ select distinct(u.id), u.name, u.full_name from calcms_user_roles ur, calcms_users u where ur.user_id=u.id $conditions }; - my $dbh = db::connect($config); - my $users = db::get( $dbh, $query, \@bind_values ); - return $users; + my $dbh = db::connect($config); + my $users = db::get( $dbh, $query, \@bind_values ); + return $users; } # get projects a user is assigned by name sub get_projects_by_user { - my $config = shift; - my $condition = shift; + my $config = shift; + my $condition = shift; - my @conditions = (); - my @bind_values = (); + my @conditions = (); + my @bind_values = (); - if ( ( defined $condition->{project_id} ) && ( $condition->{project_id} ne '' ) ) { - push @conditions, 'ur.project_id=?'; - push @bind_values, $condition->{project_id}; - } + if ( ( defined $condition->{project_id} ) && ( $condition->{project_id} ne '' ) ) { + push @conditions, 'ur.project_id=?'; + push @bind_values, $condition->{project_id}; + } - if ( ( defined $condition->{studio_id} ) && ( $condition->{studio_id} ne '' ) ) { - push @conditions, 'ur.studio_id=?'; - push @bind_values, $condition->{studio_id}; - } + if ( ( defined $condition->{studio_id} ) && ( $condition->{studio_id} ne '' ) ) { + push @conditions, 'ur.studio_id=?'; + push @bind_values, $condition->{studio_id}; + } - if ( ( defined $condition->{user} ) && ( $condition->{user} ne '' ) ) { - push @conditions, 'u.name=?'; - push @bind_values, $condition->{user}; - } + if ( ( defined $condition->{user} ) && ( $condition->{user} ne '' ) ) { + push @conditions, 'u.name=?'; + push @bind_values, $condition->{user}; + } - my $conditions = ''; - $conditions = " and " . join( " and ", @conditions ) if ( @conditions > 0 ); + my $conditions = ''; + $conditions = " and " . join( " and ", @conditions ) if ( @conditions > 0 ); - my $query = qq{ + my $query = qq{ select distinct p.*, ur.project_id project_id from calcms_user_roles ur, calcms_users u, calcms_projects p where ur.user_id=u.id and p.project_id=ur.project_id $conditions }; - my $dbh = db::connect($config); - my $users = db::get( $dbh, $query, \@bind_values ); - return $users; + my $dbh = db::connect($config); + my $users = db::get( $dbh, $query, \@bind_values ); + return $users; } # get all studios a user is assigned to by role # used at series (previously named get_user_studios) sub get_studios_by_user { - my $config = shift; - my $condition = shift; + my $config = shift; + my $condition = shift; - my @conditions = (); - my @bind_values = (); + my @conditions = (); + my @bind_values = (); - if ( ( defined $condition->{project_id} ) && ( $condition->{project_id} ne '' ) ) { - push @conditions, 'ur.project_id=?'; - push @bind_values, $condition->{project_id}; - } + if ( ( defined $condition->{project_id} ) && ( $condition->{project_id} ne '' ) ) { + push @conditions, 'ur.project_id=?'; + push @bind_values, $condition->{project_id}; + } - if ( ( defined $condition->{studio_id} ) && ( $condition->{studio_id} ne '' ) ) { - push @conditions, 'ur.studio_id=?'; - push @bind_values, $condition->{studio_id}; - } + if ( ( defined $condition->{studio_id} ) && ( $condition->{studio_id} ne '' ) ) { + push @conditions, 'ur.studio_id=?'; + push @bind_values, $condition->{studio_id}; + } - if ( ( defined $condition->{user} ) && ( $condition->{user} ne '' ) ) { - push @conditions, 'u.name=?'; - push @bind_values, $condition->{user}; - } + if ( ( defined $condition->{user} ) && ( $condition->{user} ne '' ) ) { + push @conditions, 'u.name=?'; + push @bind_values, $condition->{user}; + } - my $conditions = ''; - $conditions = " and " . join( " and ", @conditions ) if ( @conditions > 0 ); + my $conditions = ''; + $conditions = " and " . join( " and ", @conditions ) if ( @conditions > 0 ); - my $query = qq{ + my $query = qq{ select distinct s.*, ur.project_id project_id from calcms_user_roles ur, calcms_users u, calcms_studios s where ur.user_id=u.id and s.id=ur.studio_id $conditions }; - my $dbh = db::connect($config); - my $users = db::get( $dbh, $query, \@bind_values ); - return $users; + my $dbh = db::connect($config); + my $users = db::get( $dbh, $query, \@bind_values ); + return $users; } sub insert_user { - my $config = shift; - my $entry = shift; + my $config = shift; + my $entry = shift; - $entry->{created_at} = time::time_to_datetime( time() ); - $entry->{modified_at} = time::time_to_datetime( time() ); + $entry->{created_at} = time::time_to_datetime( time() ); + $entry->{modified_at} = time::time_to_datetime( time() ); - my $dbh = db::connect($config); - db::insert( $dbh, 'calcms_users', $entry ); + my $dbh = db::connect($config); + db::insert( $dbh, 'calcms_users', $entry ); } sub update_user { - my $config = shift; - my $entry = shift; + my $config = shift; + my $entry = shift; - $entry->{modified_at} = time::time_to_datetime( time() ); + $entry->{modified_at} = time::time_to_datetime( time() ); - my $values = join( ",", map { $_ . '=?' } ( keys %$entry ) ); - my @bind_values = map { $entry->{$_} } ( keys %$entry ); - push @bind_values, $entry->{id}; + my $values = join( ",", map { $_ . '=?' } ( keys %$entry ) ); + my @bind_values = map { $entry->{$_} } ( keys %$entry ); + push @bind_values, $entry->{id}; - my $query = qq{ + my $query = qq{ update calcms_users set $values where id=? }; - my $dbh = db::connect($config); - db::put( $dbh, $query, \@bind_values ); + my $dbh = db::connect($config); + db::put( $dbh, $query, \@bind_values ); } sub delete_user { - my $config = shift; - my $id = shift; - return unless ( defined $id && ( $id =~ /^\d+$/ ) ); + my $config = shift; + my $id = shift; + return unless ( defined $id && ( $id =~ /^\d+$/ ) ); - my $query = qq{ + my $query = qq{ delete from calcms_users where id=? }; - my $dbh = db::connect($config); - db::put( $dbh, $query, [$id] ); + my $dbh = db::connect($config); + db::put( $dbh, $query, [$id] ); } # get all roles used by all users of a studio # available conditions: project_id, studio_id sub get_studio_roles { - my $config = shift; - my $condition = shift; + my $config = shift; + my $condition = shift; - return [] if ( $condition->{studio_id} eq '' ); + return [] if ( $condition->{studio_id} eq '' ); - my @conditions = (); - my @bind_values = (); + my @conditions = (); + my @bind_values = (); - if ( ( defined $condition->{project_id} ) && ( $condition->{project_id} ne '' ) ) { - push @conditions, 'ur.project_id=?'; - push @bind_values, $condition->{project_id}; - } + if ( ( defined $condition->{project_id} ) && ( $condition->{project_id} ne '' ) ) { + push @conditions, 'ur.project_id=?'; + push @bind_values, $condition->{project_id}; + } - if ( ( defined $condition->{studio_id} ) && ( $condition->{studio_id} ne '' ) ) { - push @conditions, 'ur.studio_id=?'; - push @bind_values, $condition->{studio_id}; - } + if ( ( defined $condition->{studio_id} ) && ( $condition->{studio_id} ne '' ) ) { + push @conditions, 'ur.studio_id=?'; + push @bind_values, $condition->{studio_id}; + } - my $conditions = ''; - $conditions = " and " . join( " and ", @conditions ) if ( @conditions > 0 ); + my $conditions = ''; + $conditions = " and " . join( " and ", @conditions ) if ( @conditions > 0 ); - my $query = qq{ + my $query = qq{ select r.*, ur.studio_id, ur.project_id from calcms_roles r, calcms_user_roles ur where r.id=ur.role_id $conditions }; - my $dbh = db::connect($config); - my $roles = db::get( $dbh, $query, \@bind_values ); - return $roles; + my $dbh = db::connect($config); + my $roles = db::get( $dbh, $query, \@bind_values ); + return $roles; } # get role columns (for external use only) sub get_role_columns { - my $config = shift; - my $dbh = db::connect($config); - my $columns = db::get_columns_hash( $dbh, 'calcms_roles' ); - return $columns; + my $config = shift; + my $dbh = db::connect($config); + my $columns = db::get_columns_hash( $dbh, 'calcms_roles' ); + return $columns; } # get roles # filter: studio_id project_id sub get_roles { - my $config = shift; - my $condition = shift; + my $config = shift; + my $condition = shift; - my @conditions = (); - my @bind_values = (); + my @conditions = (); + my @bind_values = (); - my $dbh = db::connect($config); - my $columns = db::get_columns_hash( $dbh, 'calcms_roles' ); + my $dbh = db::connect($config); + my $columns = db::get_columns_hash( $dbh, 'calcms_roles' ); - for my $column ( keys %$columns ) { - if ( defined $condition->{$column} ) { - push @conditions, $column . '=?'; - push @bind_values, $condition->{$column}; - } - } - my $conditions = ''; - $conditions = ' where ' . join( ' and ', @conditions ) if ( @conditions > 0 ); + for my $column ( keys %$columns ) { + if ( defined $condition->{$column} ) { + push @conditions, $column . '=?'; + push @bind_values, $condition->{$column}; + } + } + my $conditions = ''; + $conditions = ' where ' . join( ' and ', @conditions ) if ( @conditions > 0 ); - my $query = qq{ + my $query = qq{ select r.* from calcms_roles r $conditions }; - my $roles = db::get( $dbh, $query, \@bind_values ); + my $roles = db::get( $dbh, $query, \@bind_values ); - return $roles; + return $roles; } #insert role to database, set created_at and modified_at sub insert_role { - my $config = shift; - my $entry = shift; + my $config = shift; + my $entry = shift; - $entry->{created_at} = time::time_to_datetime( time() ); - $entry->{modified_at} = time::time_to_datetime( time() ); + $entry->{created_at} = time::time_to_datetime( time() ); + $entry->{modified_at} = time::time_to_datetime( time() ); - my $dbh = db::connect($config); - my $columns = db::get_columns_hash( $dbh, 'calcms_roles' ); - my $role = {}; - for my $column ( keys %$columns ) { - $role->{$column} = $entry->{$column} if defined $entry->{$column}; - } - db::insert( $dbh, 'calcms_roles', $role ); + my $dbh = db::connect($config); + my $columns = db::get_columns_hash( $dbh, 'calcms_roles' ); + my $role = {}; + for my $column ( keys %$columns ) { + $role->{$column} = $entry->{$column} if defined $entry->{$column}; + } + db::insert( $dbh, 'calcms_roles', $role ); } #update role, set modified_at sub update_role { - my $config = shift; - my $entry = shift; + my $config = shift; + my $entry = shift; - $entry->{modified_at} = time::time_to_datetime( time() ); + $entry->{modified_at} = time::time_to_datetime( time() ); - my $dbh = db::connect($config); - my $columns = db::get_columns_hash( $dbh, 'calcms_roles' ); - my $values = join( ",", map { $_ . '=?' } ( keys %$columns ) ); - my @bind_values = map { $entry->{$_} } ( keys %$columns ); - push @bind_values, $entry->{id}; + my $dbh = db::connect($config); + my $columns = db::get_columns_hash( $dbh, 'calcms_roles' ); + my $values = join( ",", map { $_ . '=?' } ( keys %$columns ) ); + my @bind_values = map { $entry->{$_} } ( keys %$columns ); + push @bind_values, $entry->{id}; - my $query = qq{ + my $query = qq{ update calcms_roles set $values where id=? }; - # print $query."
\n".Dumper(\@bind_values)."
\ņ"; + # print $query."
\n".Dumper(\@bind_values)."
\ņ"; - db::put( $dbh, $query, \@bind_values ); + db::put( $dbh, $query, \@bind_values ); } # delete role from database sub delete_role { - my $config = shift; - my $id = shift; + my $config = shift; + my $id = shift; - return unless ( defined $id && ( $id =~ /^\d+$/ ) ); + return unless ( defined $id && ( $id =~ /^\d+$/ ) ); - my $query = qq{ + my $query = qq{ delete from calcms_roles where id=? }; - my $dbh = db::connect($config); - db::put( $dbh, $query, [$id] ); + my $dbh = db::connect($config); + db::put( $dbh, $query, [$id] ); } # get all roles for given conditions: project_id, studio_id, user_id, name # includes global admin user role sub get_user_roles { - my $config = shift; - my $condition = shift; + my $config = shift; + my $condition = shift; - my @conditions = (); - my @bind_values = (); + my @conditions = (); + my @bind_values = (); - if ( defined $condition->{user} ) { - push @conditions, 'u.name=?'; - push @bind_values, $condition->{user}; - } - if ( defined $condition->{user_id} ) { - push @conditions, 'ur.user_id=?'; - push @bind_values, $condition->{user_id}; - } - if ( defined $condition->{studio_id} ) { - push @conditions, 'ur.studio_id=?'; - push @bind_values, $condition->{studio_id}; - } - if ( defined $condition->{project_id} ) { - push @conditions, 'ur.project_id=?'; - push @bind_values, $condition->{project_id}; - } + if ( defined $condition->{user} ) { + push @conditions, 'u.name=?'; + push @bind_values, $condition->{user}; + } + if ( defined $condition->{user_id} ) { + push @conditions, 'ur.user_id=?'; + push @bind_values, $condition->{user_id}; + } + if ( defined $condition->{studio_id} ) { + push @conditions, 'ur.studio_id=?'; + push @bind_values, $condition->{studio_id}; + } + if ( defined $condition->{project_id} ) { + push @conditions, 'ur.project_id=?'; + push @bind_values, $condition->{project_id}; + } - my $conditions = ''; - $conditions = " and " . join( " and ", @conditions ) if ( @conditions > 0 ); + my $conditions = ''; + $conditions = " and " . join( " and ", @conditions ) if ( @conditions > 0 ); - my $query = qq{ + my $query = qq{ select distinct r.* from calcms_users u, calcms_user_roles ur, calcms_roles r where ur.user_id=u.id and ur.role_id=r.id $conditions }; - my $dbh = db::connect($config); - my $user_roles = db::get( $dbh, $query, \@bind_values ); + my $dbh = db::connect($config); + my $user_roles = db::get( $dbh, $query, \@bind_values ); - #return roles, if the contain an admin role - for my $role (@$user_roles) { - return $user_roles if $role->{role} eq 'Admin'; - } + #return roles, if the contain an admin role + for my $role (@$user_roles) { + return $user_roles if $role->{role} eq 'Admin'; + } - #get all admin roles - delete $condition->{studio_id} if defined $condition->{studio_id}; - delete $condition->{project_id} if defined $condition->{project_id}; - my $admin_roles = get_admin_user_roles( $config, $condition ); + #get all admin roles + delete $condition->{studio_id} if defined $condition->{studio_id}; + delete $condition->{project_id} if defined $condition->{project_id}; + my $admin_roles = get_admin_user_roles( $config, $condition ); - #add admin roles to user roles - my @user_roles = ( @$admin_roles, @$user_roles ); - $user_roles = \@user_roles; + #add admin roles to user roles + my @user_roles = ( @$admin_roles, @$user_roles ); + $user_roles = \@user_roles; - return $user_roles; + return $user_roles; } #return admin user roles for given conditions: project_id, studio_id, user, user_id sub get_admin_user_roles { - my $config = shift; - my $condition = shift; + my $config = shift; + my $condition = shift; - my @conditions = (); - my @bind_values = (); + my @conditions = (); + my @bind_values = (); - if ( ( defined $condition->{user} ) && ( $condition->{user} ne '' ) ) { - push @conditions, 'u.name=?'; - push @bind_values, $condition->{user}; - } - if ( ( defined $condition->{user_id} ) && ( $condition->{user_id} ne '' ) ) { - push @conditions, 'ur.user_id=?'; - push @bind_values, $condition->{user_id}; - } - if ( ( defined $condition->{studio_id} ) && ( $condition->{studio_id} ne '' ) ) { - push @conditions, 'ur.studio_id=?'; - push @bind_values, $condition->{studio_id}; - } - if ( ( defined $condition->{project_id} ) && ( $condition->{project_id} ne '' ) ) { - push @conditions, 'ur.project_id=?'; - push @bind_values, $condition->{project_id}; - } + if ( ( defined $condition->{user} ) && ( $condition->{user} ne '' ) ) { + push @conditions, 'u.name=?'; + push @bind_values, $condition->{user}; + } + if ( ( defined $condition->{user_id} ) && ( $condition->{user_id} ne '' ) ) { + push @conditions, 'ur.user_id=?'; + push @bind_values, $condition->{user_id}; + } + if ( ( defined $condition->{studio_id} ) && ( $condition->{studio_id} ne '' ) ) { + push @conditions, 'ur.studio_id=?'; + push @bind_values, $condition->{studio_id}; + } + if ( ( defined $condition->{project_id} ) && ( $condition->{project_id} ne '' ) ) { + push @conditions, 'ur.project_id=?'; + push @bind_values, $condition->{project_id}; + } - my $conditions = ''; - $conditions = " and " . join( " and ", @conditions ) if ( @conditions > 0 ); + my $conditions = ''; + $conditions = " and " . join( " and ", @conditions ) if ( @conditions > 0 ); - my $query = qq{ + my $query = qq{ select distinct r.*, ur.studio_id, ur.project_id from calcms_users u, calcms_user_roles ur, calcms_roles r where ur.user_id=u.id and ur.role_id=r.id and r.role='Admin' @@ -462,371 +462,371 @@ sub get_admin_user_roles { limit 1 }; - my $dbh = db::connect($config); - my $user_roles = db::get( $dbh, $query, \@bind_values ); - return $user_roles; + my $dbh = db::connect($config); + my $user_roles = db::get( $dbh, $query, \@bind_values ); + return $user_roles; } # read permissions for given conditions and add to user_permissions # return user_permissions # studio_id, user_id, name sub get_user_permissions { - my $config = shift; - my $conditions = shift; - my $user_permissions = shift; + my $config = shift; + my $conditions = shift; + my $user_permissions = shift; - my $user_roles = get_user_roles( $config, $conditions ); - my $admin_roles = get_admin_user_roles( $config, $conditions ); - my @user_roles = ( @$admin_roles, @$user_roles ); + my $user_roles = get_user_roles( $config, $conditions ); + my $admin_roles = get_admin_user_roles( $config, $conditions ); + my @user_roles = ( @$admin_roles, @$user_roles ); - #set default permissions - $user_permissions = {} unless ( defined $user_permissions ); - $user_permissions->{is_admin} = 1 if ( scalar @$admin_roles > 0 ); + #set default permissions + $user_permissions = {} unless ( defined $user_permissions ); + $user_permissions->{is_admin} = 1 if ( scalar @$admin_roles > 0 ); - my $max_level = 0; + my $max_level = 0; - # aggregate max permissions - # should be limited by project and studio - for my $user_role (@user_roles) { - if ( $user_role->{level} > $max_level ) { - $user_permissions->{level} = $user_role->{level}; - $user_permissions->{id} = $user_role->{id}; - $user_permissions->{role} = $user_role->{role}; - $user_permissions->{studio_id} = $user_role->{studio_id}; - $user_permissions->{project_id} = $user_role->{project_id}; - $max_level = $user_role->{level}; - } - for my $permission ( keys %$user_role ) { - if ( ( $permission ne 'level' ) - && ( $permission ne 'id' ) - && ( $permission ne 'role' ) - && ( $permission ne 'studio_id' ) - && ( $permission ne 'project_id' ) ) - { - $user_permissions->{$permission} = 1 if ( ( defined $user_role->{$permission} ) && ( $user_role->{$permission} ne '0' ) ); - } - } - } - return $user_permissions; + # aggregate max permissions + # should be limited by project and studio + for my $user_role (@user_roles) { + if ( $user_role->{level} > $max_level ) { + $user_permissions->{level} = $user_role->{level}; + $user_permissions->{id} = $user_role->{id}; + $user_permissions->{role} = $user_role->{role}; + $user_permissions->{studio_id} = $user_role->{studio_id}; + $user_permissions->{project_id} = $user_role->{project_id}; + $max_level = $user_role->{level}; + } + for my $permission ( keys %$user_role ) { + if ( ( $permission ne 'level' ) + && ( $permission ne 'id' ) + && ( $permission ne 'role' ) + && ( $permission ne 'studio_id' ) + && ( $permission ne 'project_id' ) ) + { + $user_permissions->{$permission} = 1 if ( defined $user_role->{$permission} ) && ( $user_role->{$permission} ne '0' ); + } + } + } + return $user_permissions; } #get user id by user name sub get_user_id { - my $config = shift; - my $user = shift; + my $config = shift; + my $user = shift; - return undef unless ( defined $user ); + return undef unless ( defined $user ); - my $query = qq{ + my $query = qq{ select id from calcms_users where binary name=? }; - my $dbh = db::connect($config); - my $users = db::get( $dbh, $query, [$user] ); - return undef if ( @$users == 0 ); - return $users->[0]->{id}; + my $dbh = db::connect($config); + my $users = db::get( $dbh, $query, [$user] ); + return undef if scalar @$users == 0; + return $users->[0]->{id}; } #get role id by role name sub get_role_id { - my $config = shift; - my $role = shift; + my $config = shift; + my $role = shift; - return undef unless ( defined $role ); + return undef unless ( defined $role ); - my $query = qq{ + my $query = qq{ select id from calcms_roles where role=? }; - my $dbh = db::connect($config); - my $roles = db::get( $dbh, $query, [$role] ); - return undef if ( @$roles == 0 ); - return $roles->[0]->{id}; + my $dbh = db::connect($config); + my $roles = db::get( $dbh, $query, [$role] ); + return undef if scalar @$roles == 0; + return $roles->[0]->{id}; } # assign a role to an user (for a studio) sub assign_user_role { - my $config = shift; - my $options = shift; + my $config = shift; + my $options = shift; - #print STDERR Dumper($options); - return undef unless defined $options->{project_id}; - return undef unless defined $options->{studio_id}; - return undef unless defined $options->{user_id}; - return undef unless defined $options->{role_id}; + #print STDERR Dumper($options); + return undef unless defined $options->{project_id}; + return undef unless defined $options->{studio_id}; + return undef unless defined $options->{user_id}; + return undef unless defined $options->{role_id}; - #return if already exists - my $query = qq{ + #return if already exists + my $query = qq{ select * from calcms_user_roles where project_id=? and studio_id=? and user_id=? and role_id=? }; - my $dbh = db::connect($config); - my $user_roles = db::get( $dbh, $query, [ $options->{project_id}, $options->{studio_id}, $options->{user_id}, $options->{role_id} ] ); - return undef if ( @$user_roles > 0 ); + my $dbh = db::connect($config); + my $user_roles = db::get( $dbh, $query, [ $options->{project_id}, $options->{studio_id}, $options->{user_id}, $options->{role_id} ] ); + return undef if scalar @$user_roles > 0; - #insert entry - my $entry = { - project_id => $options->{project_id}, - studio_id => $options->{studio_id}, - user_id => $options->{user_id}, - role_id => $options->{role_id}, - created_at => time::time_to_datetime( time() ) - }; + #insert entry + my $entry = { + project_id => $options->{project_id}, + studio_id => $options->{studio_id}, + user_id => $options->{user_id}, + role_id => $options->{role_id}, + created_at => time::time_to_datetime( time() ) + }; - return db::insert( $dbh, 'calcms_user_roles', $entry ); + return db::insert( $dbh, 'calcms_user_roles', $entry ); } # unassign a user from a role of (for a studio) sub remove_user_role { - my $config = shift; - my $options = shift; + my $config = shift; + my $options = shift; - return undef unless defined $options->{project_id}; - return undef unless defined $options->{studio_id}; - return undef unless defined $options->{user_id}; - return undef unless defined $options->{role_id}; + return undef unless defined $options->{project_id}; + return undef unless defined $options->{studio_id}; + return undef unless defined $options->{user_id}; + return undef unless defined $options->{role_id}; - my $query = qq{ + my $query = qq{ delete from calcms_user_roles where project_id=? and studio_id=? and user_id=? and role_id=? }; - my $bind_values = [ $options->{project_id}, $options->{studio_id}, $options->{user_id}, $options->{role_id} ]; + my $bind_values = [ $options->{project_id}, $options->{studio_id}, $options->{user_id}, $options->{role_id} ]; - #print STDERR Dumper($query).Dumper($bind_values); - my $dbh = db::connect($config); - my $result = db::put( $dbh, $query, $bind_values ); + #print STDERR Dumper($query).Dumper($bind_values); + my $dbh = db::connect($config); + my $result = db::put( $dbh, $query, $bind_values ); - # successfully return even if no entry exists - return 1; + # successfully return even if no entry exists + return 1; } #checks sub is_user_assigned_to_studio { - my $request = shift; - my $options = shift; + my $request = shift; + my $options = shift; - my $config = $request->{config}; + my $config = $request->{config}; - return 0 unless defined $request->{user}; - return 0 unless defined $options->{studio_id}; - return 0 unless defined $options->{project_id}; + return 0 unless defined $request->{user}; + return 0 unless defined $options->{studio_id}; + return 0 unless defined $options->{project_id}; - my $options2 = { - user => $request->{user}, - studio_id => $options->{studio_id}, - project_id => $options->{project_id} - }; + my $options2 = { + user => $request->{user}, + studio_id => $options->{studio_id}, + project_id => $options->{project_id} + }; - my $user_studios = uac::get_studios_by_user( $config, $options2 ); - return 1 if ( @$user_studios == 1 ); - return 0; + my $user_studios = uac::get_studios_by_user( $config, $options2 ); + return 1 if scalar @$user_studios == 1; + return 0; } # print errors at get_user_presets and check for project id and studio id # call after header is printed sub check { - my $config = shift; - my $params = shift; - my $user_presets = shift; + my $config = shift; + my $params = shift; + my $user_presets = shift; - if ( defined $user_presets->{error} ) { - uac::print_error( $user_presets->{error} ); - return undef; - } + if ( defined $user_presets->{error} ) { + uac::print_error( $user_presets->{error} ); + return undef; + } - my $project_check = project::check( $config, { project_id => $params->{project_id} } ); - if ( $project_check ne '1' ) { - uac::print_error($project_check); - return undef; - } + my $project_check = project::check( $config, { project_id => $params->{project_id} } ); + if ( $project_check ne '1' ) { + uac::print_error($project_check); + return undef; + } - my $studio_check = studios::check( $config, { studio_id => $params->{studio_id} } ); - if ( $studio_check ne '1' ) { - uac::print_error($studio_check); - return undef; - } - return 1; + my $studio_check = studios::check( $config, { studio_id => $params->{studio_id} } ); + if ( $studio_check ne '1' ) { + uac::print_error($studio_check); + return undef; + } + return 1; } # get user, projects and studios user is assigned to for selected values from params # set permissions for selected project and studio # return request sub get_user_presets { - my $config = shift; - my $options = shift; + my $config = shift; + my $options = shift; - my $user = $options->{user} || ''; - my $error = undef; - return { error => "no user selected" } if ( $user eq '' ); + my $user = $options->{user} || ''; + my $error = undef; + return { error => "no user selected" } if ( $user eq '' ); - my $project_id = $options->{project_id} || ''; - my $studio_id = $options->{studio_id} || ''; - $config->{access}->{write} = 0; + my $project_id = $options->{project_id} || ''; + my $studio_id = $options->{studio_id} || ''; + $config->{access}->{write} = 0; - #get - my $admin_roles = get_admin_user_roles( $config, { user => $user } ); + #get + my $admin_roles = get_admin_user_roles( $config, { user => $user } ); - #get all projects by user - my $projects = uac::get_projects_by_user( $config, { user => $user } ); - return { error => "no project is assigned to user" } if ( @$projects == 0 ); + #get all projects by user + my $projects = uac::get_projects_by_user( $config, { user => $user } ); + return { error => "no project is assigned to user" } if scalar @$projects == 0; - $projects = project::get($config) if ( @$admin_roles > 0 ); - my @projects = reverse sort { $a->{end_date} cmp $b->{end_date} } (@$projects); - $projects = \@projects; + $projects = project::get($config) if ( @$admin_roles > 0 ); + my @projects = reverse sort { $a->{end_date} cmp $b->{end_date} } (@$projects); + $projects = \@projects; - if ( $project_id ne '' && $project_id ne '-1' ) { - my $projectFound = 0; - for my $project (@$projects) { - if ( $project->{project_id} eq $project_id ) { - $projectFound = 1; - last; - } - } - return { error => "project is not assigned to user" } if ( $projectFound == 0 ); - } else { - $project_id = $projects->[0]->{project_id}; - } + if ( $project_id ne '' && $project_id ne '-1' ) { + my $projectFound = 0; + for my $project (@$projects) { + if ( $project->{project_id} eq $project_id ) { + $projectFound = 1; + last; + } + } + return { error => "project is not assigned to user" } if ( $projectFound == 0 ); + } else { + $project_id = $projects->[0]->{project_id}; + } - #print STDERR "project:$project_id\n"; + #print STDERR "project:$project_id\n"; - #check if studios are assigned to project - my $studios = project::get_studios( $config, { project_id => $project_id } ); - $error = "no studio is assigned to project" if ( @$studios == 0 ); + #check if studios are assigned to project + my $studios = project::get_studios( $config, { project_id => $project_id } ); + $error = "no studio is assigned to project" if scalar @$studios == 0; - if ( @$admin_roles == 0 ) { + if ( scalar @$admin_roles == 0 ) { - #get all studios by user - $studios = uac::get_studios_by_user( $config, { user => $user, project_id => $project_id } ); - $error = "no studio is assigned to user" if ( @$studios == 0 ); - if ( ( $studio_id ne '' ) && ( $studio_id ne '-1' ) ) { - my $studioFound = 0; - for my $studio (@$studios) { - if ( $studio->{id} eq $studio_id ) { - $studioFound = 1; - last; - } - } - $error = "studio is not assigned to user" if ( $studioFound == 0 ); - } else { - $studio_id = $studios->[0]->{id}; - } - } else { + #get all studios by user + $studios = uac::get_studios_by_user( $config, { user => $user, project_id => $project_id } ); + $error = "no studio is assigned to user" if scalar @$studios == 0; + if ( ( $studio_id ne '' ) && ( $studio_id ne '-1' ) ) { + my $studioFound = 0; + for my $studio (@$studios) { + if ( $studio->{id} eq $studio_id ) { + $studioFound = 1; + last; + } + } + $error = "studio is not assigned to user" if ( $studioFound == 0 ); + } else { + $studio_id = $studios->[0]->{id}; + } + } else { - #for admin get studios by project - $studios = studios::get( $config, { project_id => $project_id } ); - if ( ( $studio_id ne '' ) && ( $studio_id ne '-1' ) ) { - my $studioFound = 0; - for my $studio (@$studios) { - if ( $studio->{id} eq $studio_id ) { - $studioFound = 1; - last; - } - } - $error = "studio is not assigned to project" if ( $studioFound == 0 ); - } else { - $studio_id = $studios->[0]->{id}; - } - } + #for admin get studios by project + $studios = studios::get( $config, { project_id => $project_id } ); + if ( ( $studio_id ne '' ) && ( $studio_id ne '-1' ) ) { + my $studioFound = 0; + for my $studio (@$studios) { + if ( $studio->{id} eq $studio_id ) { + $studioFound = 1; + last; + } + } + $error = "studio is not assigned to project" if ( $studioFound == 0 ); + } else { + $studio_id = $studios->[0]->{id}; + } + } - my $permissions = uac::get_user_permissions( $config, { user => $user, project_id => $project_id, studio_id => $studio_id } ); + my $permissions = uac::get_user_permissions( $config, { user => $user, project_id => $project_id, studio_id => $studio_id } ); - #only admin is allowed to select all projects - # if($permissions->{is_admin}==1){ - # $projects=project::get($config); - # } + #only admin is allowed to select all projects + # if($permissions->{is_admin}==1){ + # $projects=project::get($config); + # } - #set studios and projects as selected, TODO:do in JS - my $selectedProject = {}; - for my $project (@$projects) { - if ( $project_id eq $project->{project_id} ) { - $project->{selected} = 'selected="selected"'; - $selectedProject = $project; - last; - } - } + #set studios and projects as selected, TODO:do in JS + my $selectedProject = {}; + for my $project (@$projects) { + if ( $project_id eq $project->{project_id} ) { + $project->{selected} = 'selected="selected"'; + $selectedProject = $project; + last; + } + } - my $selectedStudio = {}; - for my $studio (@$studios) { - if ( $studio_id eq $studio->{id} ) { - $studio->{selected} = 'selected="selected"'; - $selectedStudio = $studio; - last; - } - } + my $selectedStudio = {}; + for my $studio (@$studios) { + if ( $studio_id eq $studio->{id} ) { + $studio->{selected} = 'selected="selected"'; + $selectedStudio = $studio; + last; + } + } - my $logout_url = ( split( /\//, $0 ) )[-1]; + my $logout_url = ( split( /\//, $0 ) )[-1]; - #print STDERR "ok\n"; - my $result = { - user => $user, - logout_url => $logout_url, + #print STDERR "ok\n"; + my $result = { + user => $user, + logout_url => $logout_url, - project_id => $project_id, # from parameter or default - projects => $projects, - project => $selectedProject, + project_id => $project_id, # from parameter or default + projects => $projects, + project => $selectedProject, - studio_id => $studio_id, # from parameter or default - studios => $studios, - studio => $selectedStudio, + studio_id => $studio_id, # from parameter or default + studios => $studios, + studio => $selectedStudio, - permissions => $permissions, # from parameter or default - config => $config - }; - $result->{error} = $error if defined $error; - return $result; + permissions => $permissions, # from parameter or default + config => $config + }; + $result->{error} = $error if defined $error; + return $result; } #set user preset properties to request sub prepare_request { - my $request = shift; - my $user_presets = shift; + my $request = shift; + my $user_presets = shift; - for my $key ( keys %$user_presets ) { - $request->{$key} = $user_presets->{$key}; - } + for my $key ( keys %$user_presets ) { + $request->{$key} = $user_presets->{$key}; + } - #enrich menu parameters - for my $key ( 'studio_id', 'project_id', 'studio', 'project', 'studios', 'projects', 'user', 'logout_url' ) { - $request->{params}->{checked}->{presets}->{$key} = $user_presets->{$key}; - } - return $request; + #enrich menu parameters + for my $key ( 'studio_id', 'project_id', 'studio', 'project', 'studios', 'projects', 'user', 'logout_url' ) { + $request->{params}->{checked}->{presets}->{$key} = $user_presets->{$key}; + } + return $request; } #TODO: shift to permissions sub entry sub set_template_permissions { - my $permissions = shift; - my $params = shift; + my $permissions = shift; + my $params = shift; - for my $usecase ( keys %$permissions ) { - $params->{'allow'}->{$usecase} = 1 if ( $permissions->{$usecase} eq '1' ); - } - return $params; + for my $usecase ( keys %$permissions ) { + $params->{'allow'}->{$usecase} = 1 if ( $permissions->{$usecase} eq '1' ); + } + return $params; } #print error message sub permissions_denied { - my $message = shift; - $message =~ s/_/ /g; - print '
Sorry! Missing permissions to ' . $message . '
' . "\n"; - print STDERR 'Sorry! Missing permissions to ' . $message . "\n"; + my $message = shift; + $message =~ s/_/ /g; + print '
Sorry! Missing permissions to ' . $message . '
' . "\n"; + print STDERR 'Sorry! Missing permissions to ' . $message . "\n"; } sub print_info { - print '
' . ' ' . $_[0] . '
' . "\n"; + print '
' . ' ' . $_[0] . '
' . "\n"; } sub print_warn { - print '
' . ' ' . $_[0] . '
' . "\n"; + print '
' . ' ' . $_[0] . '
' . "\n"; } sub print_error { - my $message = shift; - print STDERR "ERROR:" . $message . "\n"; - print '
' . ' ' . $message . '
' . "\n"; + my $message = shift; + print STDERR "ERROR:" . $message . "\n"; + print '
' . ' ' . $message . '
' . "\n"; } #do not delete last line! diff --git a/lib/calcms/user_settings.pm b/lib/calcms/user_settings.pm index 07b06bb..5b0aa68 100644 --- a/lib/calcms/user_settings.pm +++ b/lib/calcms/user_settings.pm @@ -14,218 +14,218 @@ our %EXPORT_TAGS = ( 'all' => [@EXPORT_OK] ); sub debug; our $defaultColors = [ - { - name => 'color_event', - css => '#content .event', - color => '#c5e1a5' - }, - { - name => 'color_draft', - css => '#content .draft', - color => '#eeeeee', - }, - { - name => 'color_schedule', - css => '#content .schedule', - color => '#dde4e6', - }, - { - name => 'color_published', - css => '#content .event.published', - color => '#a5d6a7', - }, - { - name => 'color_no_series', - css => '#content .event.no_series', - color => '#fff59d', - }, - { - name => 'color_marked', - css => '#content .event.marked', - color => '#81d4fa', - }, - { - name => 'color_event_error', - css => '#content.conflicts .event.error', - color => '#ffab91', - }, - { - name => 'color_schedule_error', - css => '#content.conflicts .schedule.error', - color => '#ffcc80' - }, - { - name => 'color_work', - css => '#content .work', - color => '#b39ddb' - }, - { - name => 'color_playout', - css => '#content .play', - color => '#90caf9' - } + { + name => 'color_event', + css => '#content .event', + color => '#c5e1a5' + }, + { + name => 'color_draft', + css => '#content .draft', + color => '#eeeeee', + }, + { + name => 'color_schedule', + css => '#content .schedule', + color => '#dde4e6', + }, + { + name => 'color_published', + css => '#content .event.published', + color => '#a5d6a7', + }, + { + name => 'color_no_series', + css => '#content .event.no_series', + color => '#fff59d', + }, + { + name => 'color_marked', + css => '#content .event.marked', + color => '#81d4fa', + }, + { + name => 'color_event_error', + css => '#content.conflicts .event.error', + color => '#ffab91', + }, + { + name => 'color_schedule_error', + css => '#content.conflicts .schedule.error', + color => '#ffcc80' + }, + { + name => 'color_work', + css => '#content .work', + color => '#b39ddb' + }, + { + name => 'color_playout', + css => '#content .play', + color => '#90caf9' + } ]; sub getColors { - my $config = shift; - my $conditions = shift; - return unless defined $conditions->{user}; - my $user = $conditions->{user}; + my $config = shift; + my $conditions = shift; + return unless defined $conditions->{user}; + my $user = $conditions->{user}; - #get defaultColors - my $colors = []; - my $colorMap = {}; - for my $defaultColor (@$defaultColors) { - my $color = { - name => $defaultColor->{name}, - css => $defaultColor->{css}, - color => $defaultColor->{color}, - }; - push @$colors, $color; - $colorMap->{ $color->{css} } = $color; - } + #get defaultColors + my $colors = []; + my $colorMap = {}; + for my $defaultColor (@$defaultColors) { + my $color = { + name => $defaultColor->{name}, + css => $defaultColor->{css}, + color => $defaultColor->{color}, + }; + push @$colors, $color; + $colorMap->{ $color->{css} } = $color; + } - my $settings = user_settings::get( $config, { user => $user } ); - $settings->{colors} |= ''; + my $settings = user_settings::get( $config, { user => $user } ); + $settings->{colors} |= ''; - #overwrite colors from user settings - for my $line ( split( /\n+/, $settings->{colors} ) ) { - my ( $key, $value ) = split( /\=/, $line ); - $key =~ s/^\s+//; - $key =~ s/\s+$//; - $value =~ s/^\s+//; - $value =~ s/\s+$//; - $colorMap->{$key}->{color} = $value if ( ( $key ne '' ) && ( $value ne '' ) && ( defined $colorMap->{$key} ) ); - } - return $colors; + #overwrite colors from user settings + for my $line ( split( /\n+/, $settings->{colors} ) ) { + my ( $key, $value ) = split( /\=/, $line ); + $key =~ s/^\s+//; + $key =~ s/\s+$//; + $value =~ s/^\s+//; + $value =~ s/\s+$//; + $colorMap->{$key}->{color} = $value if ( $key ne '' ) && ( $value ne '' ) && ( defined $colorMap->{$key} ) ; + } + return $colors; } sub getColorCss { - my $config = shift; - my $conditions = shift; - return unless defined $conditions->{user}; + my $config = shift; + my $conditions = shift; + return unless defined $conditions->{user}; - my $shift = 20; - my $limit = 220; + my $shift = 20; + my $limit = 220; - my $colors = getColors( $config, $conditions ); - my $style = "\n"; - return $style; + my $colors = getColors( $config, $conditions ); + my $style = "\n"; + return $style; } sub get_columns { - my $config = shift; + my $config = shift; - my $dbh = db::connect($config); - my $cols = db::get_columns( $dbh, 'calcms_user_settings' ); - my $columns = {}; - for my $col (@$cols) { - $columns->{$col} = 1; - } - return $columns; + my $dbh = db::connect($config); + my $cols = db::get_columns( $dbh, 'calcms_user_settings' ); + my $columns = {}; + for my $col (@$cols) { + $columns->{$col} = 1; + } + return $columns; } sub get { - my $config = shift; - my $condition = shift; + my $config = shift; + my $condition = shift; - my $dbh = db::connect($config); + my $dbh = db::connect($config); - my @conditions = (); - my @bind_values = (); + my @conditions = (); + my @bind_values = (); - if ( ( defined $condition->{user} ) && ( $condition->{user} ne '' ) ) { - push @conditions, 'user=?'; - push @bind_values, $condition->{user}; - } + if ( ( defined $condition->{user} ) && ( $condition->{user} ne '' ) ) { + push @conditions, 'user=?'; + push @bind_values, $condition->{user}; + } - my $conditions = ''; - $conditions = " where " . join( " and ", @conditions ) if ( @conditions > 0 ); + my $conditions = ''; + $conditions = " where " . join( " and ", @conditions ) if ( @conditions > 0 ); - my $query = qq{ + my $query = qq{ select * from calcms_user_settings $conditions }; - #print $query."\n"; - #print Dumper(\@bind_values); + #print $query."\n"; + #print Dumper(\@bind_values); - my $entries = db::get( $dbh, $query, \@bind_values ); - return $entries->[0] || undef; + my $entries = db::get( $dbh, $query, \@bind_values ); + return $entries->[0] || undef; } sub insert { - my $config = shift; - my $entry = shift; + my $config = shift; + my $entry = shift; - return unless ( defined $entry->{user} ); - return unless ( defined $entry->{colors} ); - my $dbh = db::connect($config); - return db::insert( $dbh, 'calcms_user_settings', $entry ); + return unless ( defined $entry->{user} ); + return unless ( defined $entry->{colors} ); + my $dbh = db::connect($config); + return db::insert( $dbh, 'calcms_user_settings', $entry ); } sub update { - my $config = shift; - my $entry = shift; + my $config = shift; + my $entry = shift; - return unless ( defined $entry->{user} ); - return unless ( defined $entry->{colors} ); + return unless ( defined $entry->{user} ); + return unless ( defined $entry->{colors} ); - my $dbh = db::connect($config); - my $values = join( ",", map { $_ . '=?' } ( keys %$entry ) ); - my @bind_values = map { $entry->{$_} } ( keys %$entry ); - push @bind_values, $entry->{user}; + my $dbh = db::connect($config); + my $values = join( ",", map { $_ . '=?' } ( keys %$entry ) ); + my @bind_values = map { $entry->{$_} } ( keys %$entry ); + push @bind_values, $entry->{user}; - my $query = qq{ + my $query = qq{ update calcms_user_settings set $values where user=? }; - db::put( $dbh, $query, \@bind_values ); - print "done\n"; + db::put( $dbh, $query, \@bind_values ); + print "done\n"; } sub delete { - my $config = shift; - my $entry = shift; + my $config = shift; + my $entry = shift; - return unless ( defined $entry->{user} ); + return unless ( defined $entry->{user} ); - my $dbh = db::connect($config); + my $dbh = db::connect($config); - my $query = qq{ + my $query = qq{ delete from calcms_user_settings where user=? }; - my $bind_values = [ $entry->{user} ]; + my $bind_values = [ $entry->{user} ]; - #print '
$query'.$query.Dumper($bind_values).'
'; - db::put( $dbh, $query, $bind_values ); + #print '
$query'.$query.Dumper($bind_values).'
'; + db::put( $dbh, $query, $bind_values ); } sub error { - my $msg = shift; - print "ERROR: $msg
\n"; + my $msg = shift; + print "ERROR: $msg
\n"; } #do not delete last line! diff --git a/lib/calcms/user_stats.pm b/lib/calcms/user_stats.pm index 6f80503..8aace0d 100644 --- a/lib/calcms/user_stats.pm +++ b/lib/calcms/user_stats.pm @@ -13,55 +13,55 @@ our %EXPORT_TAGS = ( 'all' => [@EXPORT_OK] ); sub debug; sub get_columns { - my $config = shift; + my $config = shift; - my $dbh = db::connect($config); - my $cols = db::get_columns( $dbh, 'calcms_user_stats' ); - my $columns = {}; - for my $col (@$cols) { - $columns->{$col} = 1; - } - return $columns; + my $dbh = db::connect($config); + my $cols = db::get_columns( $dbh, 'calcms_user_stats' ); + my $columns = {}; + for my $col (@$cols) { + $columns->{$col} = 1; + } + return $columns; } sub get { - my $config = shift; - my $condition = shift; + my $config = shift; + my $condition = shift; - my $dbh = db::connect($config); + my $dbh = db::connect($config); - my @conditions = (); - my @bind_values = (); + my @conditions = (); + my @bind_values = (); - if ( ( defined $condition->{project_id} ) && ( $condition->{project_id} ne '' ) ) { - push @conditions, 'project_id=?'; - push @bind_values, $condition->{project_id}; - } + if ( ( defined $condition->{project_id} ) && ( $condition->{project_id} ne '' ) ) { + push @conditions, 'project_id=?'; + push @bind_values, $condition->{project_id}; + } - if ( ( defined $condition->{studio_id} ) && ( $condition->{studio_id} ne '' ) ) { - push @conditions, 'studio_id=?'; - push @bind_values, $condition->{studio_id}; - } + if ( ( defined $condition->{studio_id} ) && ( $condition->{studio_id} ne '' ) ) { + push @conditions, 'studio_id=?'; + push @bind_values, $condition->{studio_id}; + } - if ( ( defined $condition->{series_id} ) && ( $condition->{series_id} ne '' ) ) { - push @conditions, 'series_id=?'; - push @bind_values, $condition->{series_id}; - } + if ( ( defined $condition->{series_id} ) && ( $condition->{series_id} ne '' ) ) { + push @conditions, 'series_id=?'; + push @bind_values, $condition->{series_id}; + } - if ( ( defined $condition->{user} ) && ( $condition->{user} ne '' ) ) { - push @conditions, 'user=?'; - push @bind_values, $condition->{user}; - } + if ( ( defined $condition->{user} ) && ( $condition->{user} ne '' ) ) { + push @conditions, 'user=?'; + push @bind_values, $condition->{user}; + } - my $limit = ''; - if ( ( defined $condition->{limit} ) && ( $condition->{limit} ne '' ) ) { - $limit = 'limit ' . $condition->{limit}; - } + my $limit = ''; + if ( ( defined $condition->{limit} ) && ( $condition->{limit} ne '' ) ) { + $limit = 'limit ' . $condition->{limit}; + } - my $conditions = ''; - $conditions = " where " . join( " and ", @conditions ) if ( @conditions > 0 ); + my $conditions = ''; + $conditions = " where " . join( " and ", @conditions ) if ( @conditions > 0 ); - my $query = qq{ + my $query = qq{ select * from calcms_user_stats $conditions @@ -69,50 +69,50 @@ sub get { $limit }; - #print STDERR Dumper($query).Dumper(\@bind_values); + #print STDERR Dumper($query).Dumper(\@bind_values); - my $results = db::get( $dbh, $query, \@bind_values ); - return $results; + my $results = db::get( $dbh, $query, \@bind_values ); + return $results; } sub get_stats { - my $config = shift; - my $condition = shift; + my $config = shift; + my $condition = shift; - my $dbh = db::connect($config); + my $dbh = db::connect($config); - my @conditions = (); - my @bind_values = (); + my @conditions = (); + my @bind_values = (); - if ( ( defined $condition->{project_id} ) && ( $condition->{project_id} ne '' ) ) { - push @conditions, 'project_id=?'; - push @bind_values, $condition->{project_id}; - } + if ( ( defined $condition->{project_id} ) && ( $condition->{project_id} ne '' ) ) { + push @conditions, 'project_id=?'; + push @bind_values, $condition->{project_id}; + } - if ( ( defined $condition->{studio_id} ) && ( $condition->{studio_id} ne '' ) ) { - push @conditions, 'studio_id=?'; - push @bind_values, $condition->{studio_id}; - } + if ( ( defined $condition->{studio_id} ) && ( $condition->{studio_id} ne '' ) ) { + push @conditions, 'studio_id=?'; + push @bind_values, $condition->{studio_id}; + } - if ( ( defined $condition->{series_id} ) && ( $condition->{series_id} ne '' ) ) { - push @conditions, 'series_id=?'; - push @bind_values, $condition->{series_id}; - } + if ( ( defined $condition->{series_id} ) && ( $condition->{series_id} ne '' ) ) { + push @conditions, 'series_id=?'; + push @bind_values, $condition->{series_id}; + } - if ( ( defined $condition->{user} ) && ( $condition->{user} ne '' ) ) { - push @conditions, 'user=?'; - push @bind_values, $condition->{user}; - } + if ( ( defined $condition->{user} ) && ( $condition->{user} ne '' ) ) { + push @conditions, 'user=?'; + push @bind_values, $condition->{user}; + } - my $limit = ''; - if ( ( defined $condition->{limit} ) && ( $condition->{limit} ne '' ) ) { - $limit = 'limit ' . $condition->{limit}; - } + my $limit = ''; + if ( ( defined $condition->{limit} ) && ( $condition->{limit} ne '' ) ) { + $limit = 'limit ' . $condition->{limit}; + } - my $conditions = ''; - $conditions = " where " . join( " and ", @conditions ) if ( @conditions > 0 ); + my $conditions = ''; + $conditions = " where " . join( " and ", @conditions ) if ( @conditions > 0 ); - my $query = qq{ + my $query = qq{ select user, project_id, studio_id, max(modified_at) modified_at, sum(create_events) create_events, @@ -127,126 +127,126 @@ sub get_stats { $limit }; - #print STDERR Dumper($query).Dumper(\@bind_values); + #print STDERR Dumper($query).Dumper(\@bind_values); - my $results = db::get( $dbh, $query, \@bind_values ); - for my $result (@$results) { - $result->{score} = 0; - for my $column ( 'create_events', 'update_events', 'delete_events', 'create_series', 'update_series', 'delete_series' ) { - $result->{score} += $result->{$column}; - } - } - my @results = reverse sort { $a->{score} <=> $b->{score} } @$results; - return \@results; + my $results = db::get( $dbh, $query, \@bind_values ); + for my $result (@$results) { + $result->{score} = 0; + for my $column ( 'create_events', 'update_events', 'delete_events', 'create_series', 'update_series', 'delete_series' ) { + $result->{score} += $result->{$column}; + } + } + my @results = reverse sort { $a->{score} <=> $b->{score} } @$results; + return \@results; } sub insert { - my $config = shift; - my $stats = shift; + my $config = shift; + my $stats = shift; - return undef unless defined $stats->{project_id}; - return undef unless defined $stats->{studio_id}; - return undef unless defined $stats->{series_id}; - return undef unless defined $stats->{user}; + return undef unless defined $stats->{project_id}; + return undef unless defined $stats->{studio_id}; + return undef unless defined $stats->{series_id}; + return undef unless defined $stats->{user}; - #TODO:filter for existing attributes - my $columns = get_columns($config); - my $entry = {}; - for my $column ( keys %$columns ) { - $entry->{$column} = $stats->{$column} if defined $stats->{$column}; - } - $entry->{modified_at} = time::time_to_datetime( time() ); + #TODO:filter for existing attributes + my $columns = get_columns($config); + my $entry = {}; + for my $column ( keys %$columns ) { + $entry->{$column} = $stats->{$column} if defined $stats->{$column}; + } + $entry->{modified_at} = time::time_to_datetime( time() ); - my $dbh = db::connect($config); - my $id = db::insert( $dbh, 'calcms_user_stats', $entry ); - return $id; + my $dbh = db::connect($config); + my $id = db::insert( $dbh, 'calcms_user_stats', $entry ); + return $id; } # update project sub update { - my $config = shift; - my $stats = shift; + my $config = shift; + my $stats = shift; - return undef unless defined $stats->{project_id}; - return undef unless defined $stats->{studio_id}; - return undef unless defined $stats->{series_id}; - return undef unless defined $stats->{user}; + return undef unless defined $stats->{project_id}; + return undef unless defined $stats->{studio_id}; + return undef unless defined $stats->{series_id}; + return undef unless defined $stats->{user}; - my $columns = get_columns($config); - my $entry = {}; - for my $column ( keys %$columns ) { - $entry->{$column} = $stats->{$column} if defined $stats->{$column}; - } - $entry->{modified_at} = time::time_to_datetime( time() ); + my $columns = get_columns($config); + my $entry = {}; + for my $column ( keys %$columns ) { + $entry->{$column} = $stats->{$column} if defined $stats->{$column}; + } + $entry->{modified_at} = time::time_to_datetime( time() ); - my $values = join( ",", map { $_ . '=?' } ( keys %$entry ) ); - my @bind_values = map { $entry->{$_} } ( keys %$entry ); - push @bind_values, $entry->{user}; - push @bind_values, $entry->{project_id}; - push @bind_values, $entry->{studio_id}; - push @bind_values, $entry->{series_id}; + my $values = join( ",", map { $_ . '=?' } ( keys %$entry ) ); + my @bind_values = map { $entry->{$_} } ( keys %$entry ); + push @bind_values, $entry->{user}; + push @bind_values, $entry->{project_id}; + push @bind_values, $entry->{studio_id}; + push @bind_values, $entry->{series_id}; - my $query = qq{ + my $query = qq{ update calcms_user_stats set $values where user=? and project_id=? and studio_id=? and series_id=? }; - #print STDERR Dumper($query).Dumper(\@bind_values); - my $dbh = db::connect($config); - return db::put( $dbh, $query, \@bind_values ); + #print STDERR Dumper($query).Dumper(\@bind_values); + my $dbh = db::connect($config); + return db::put( $dbh, $query, \@bind_values ); } sub increase { - my $config = shift; - my $usecase = shift; - my $options = shift; + my $config = shift; + my $usecase = shift; + my $options = shift; - #print STDERR Dumper($usecase)." ".Dumper($options); + #print STDERR Dumper($usecase)." ".Dumper($options); - return undef unless defined $usecase; - return undef unless defined $options->{project_id}; - return undef unless defined $options->{studio_id}; - return undef unless defined $options->{series_id}; - return undef unless defined $options->{user}; + return undef unless defined $usecase; + return undef unless defined $options->{project_id}; + return undef unless defined $options->{studio_id}; + return undef unless defined $options->{series_id}; + return undef unless defined $options->{user}; - #print STDERR "ok\n"; + #print STDERR "ok\n"; - my $columns = get_columns($config); + my $columns = get_columns($config); - #print STDERR "columns:".Dumper($columns); - return undef unless defined $columns->{$usecase}; + #print STDERR "columns:".Dumper($columns); + return undef unless defined $columns->{$usecase}; - my $entries = get( $config, $options ); + my $entries = get( $config, $options ); - #print STDERR "exist:".Dumper($columns); + #print STDERR "exist:".Dumper($columns); - if ( @$entries == 0 ) { - my $entry = { - project_id => $options->{project_id}, - studio_id => $options->{studio_id}, - series_id => $options->{series_id}, - user => $options->{user}, - $usecase => 1, - }; + if ( scalar @$entries == 0 ) { + my $entry = { + project_id => $options->{project_id}, + studio_id => $options->{studio_id}, + series_id => $options->{series_id}, + user => $options->{user}, + $usecase => 1, + }; - #print STDERR "user_stats::insert\n"; - return insert( $config, $entry ); - } elsif ( @$entries == 1 ) { - my $entry = $entries->[0]; - $entry->{$usecase}++ if defined + #print STDERR "user_stats::insert\n"; + return insert( $config, $entry ); + } elsif ( scalar @$entries == 1 ) { + my $entry = $entries->[0]; + $entry->{$usecase}++ if defined - #print STDERR "user_stats::update\n"; - return update( $config, $entry ); - } else { - print STDERR "user_stats: to few options given: $usecase," . Dumper($options) . "\n"; - } + #print STDERR "user_stats::update\n"; + return update( $config, $entry ); + } else { + print STDERR "user_stats: to few options given: $usecase," . Dumper($options) . "\n"; + } } sub error { - my $msg = shift; - print "ERROR: $msg
\n"; + my $msg = shift; + print "ERROR: $msg
\n"; } #do not delete last line! diff --git a/lib/calcms/work_dates.pm b/lib/calcms/work_dates.pm index 7f8d09f..91e0132 100644 --- a/lib/calcms/work_dates.pm +++ b/lib/calcms/work_dates.pm @@ -22,80 +22,80 @@ our %EXPORT_TAGS = ( 'all' => [@EXPORT_OK] ); sub debug; sub get_columns { - my $config = shift; + my $config = shift; - my $dbh = db::connect($config); - my $cols = db::get_columns( $dbh, 'calcms_work_dates' ); - my $columns = {}; - for my $col (@$cols) { - $columns->{$col} = 1; - } - return $columns; + my $dbh = db::connect($config); + my $cols = db::get_columns( $dbh, 'calcms_work_dates' ); + my $columns = {}; + for my $col (@$cols) { + $columns->{$col} = 1; + } + return $columns; } # get all work_dates for studio_id and schedule_id within given time range # calculate start_date, end_date, weeday, day from start and end(datetime) sub get { - my $config = shift; - my $condition = shift; + my $config = shift; + my $condition = shift; - my $date_range_include = 0; - $date_range_include = 1 if ( defined $condition->{date_range_include} ) && ( $condition->{date_range_include} == 1 ); + my $date_range_include = 0; + $date_range_include = 1 if ( defined $condition->{date_range_include} ) && ( $condition->{date_range_include} == 1 ); - my $dbh = db::connect($config); + my $dbh = db::connect($config); - my @conditions = (); - my @bind_values = (); + my @conditions = (); + my @bind_values = (); - if ( ( defined $condition->{project_id} ) && ( $condition->{project_id} ne '' ) ) { - push @conditions, 'project_id=?'; - push @bind_values, $condition->{project_id}; - } + if ( ( defined $condition->{project_id} ) && ( $condition->{project_id} ne '' ) ) { + push @conditions, 'project_id=?'; + push @bind_values, $condition->{project_id}; + } - if ( ( defined $condition->{studio_id} ) && ( $condition->{studio_id} ne '' ) ) { - push @conditions, 'studio_id=?'; - push @bind_values, $condition->{studio_id}; - } + if ( ( defined $condition->{studio_id} ) && ( $condition->{studio_id} ne '' ) ) { + push @conditions, 'studio_id=?'; + push @bind_values, $condition->{studio_id}; + } - if ( ( defined $condition->{schedule_id} ) && ( $condition->{schedule_id} ne '' ) ) { - push @conditions, 'schedule_id=?'; - push @bind_values, $condition->{schedule_id}; - } + if ( ( defined $condition->{schedule_id} ) && ( $condition->{schedule_id} ne '' ) ) { + push @conditions, 'schedule_id=?'; + push @bind_values, $condition->{schedule_id}; + } - if ( ( defined $condition->{start_at} ) && ( $condition->{start_at} ne '' ) ) { - push @conditions, 'start=?'; - push @bind_values, $condition->{start_at}; - } + if ( ( defined $condition->{start_at} ) && ( $condition->{start_at} ne '' ) ) { + push @conditions, 'start=?'; + push @bind_values, $condition->{start_at}; + } - if ( ( defined $condition->{from} ) && ( $condition->{from} ne '' ) ) { - if ( $date_range_include == 1 ) { - push @conditions, 'end_date>=?'; - push @bind_values, $condition->{from}; - } else { - push @conditions, 'start_date>=?'; - push @bind_values, $condition->{from}; - } - } + if ( ( defined $condition->{from} ) && ( $condition->{from} ne '' ) ) { + if ( $date_range_include == 1 ) { + push @conditions, 'end_date>=?'; + push @bind_values, $condition->{from}; + } else { + push @conditions, 'start_date>=?'; + push @bind_values, $condition->{from}; + } + } - if ( ( defined $condition->{till} ) && ( $condition->{till} ne '' ) ) { - if ( $date_range_include == 1 ) { - push @conditions, 'start_date<=?'; - push @bind_values, $condition->{till}; - } else { - push @conditions, 'end_date<=?'; - push @bind_values, $condition->{till}; - } - } + if ( ( defined $condition->{till} ) && ( $condition->{till} ne '' ) ) { + if ( $date_range_include == 1 ) { + push @conditions, 'start_date<=?'; + push @bind_values, $condition->{till}; + } else { + push @conditions, 'end_date<=?'; + push @bind_values, $condition->{till}; + } + } - if ( ( defined $condition->{exclude} ) && ( $condition->{exclude} ne '' ) ) { - push @conditions, 'exclude=?'; - push @bind_values, $condition->{exclude}; - } + if ( ( defined $condition->{exclude} ) && ( $condition->{exclude} ne '' ) ) { + push @conditions, 'exclude=?'; + push @bind_values, $condition->{exclude}; + } - my $conditions = ''; - $conditions = " where " . join( " and ", @conditions ) if ( @conditions > 0 ); + my $conditions = ''; + $conditions = " where " . join( " and ", @conditions ) if ( @conditions > 0 ); - my $query = qq{ + my $query = qq{ select date(start) start_date ,date(end) end_date ,dayname(start) weekday @@ -114,281 +114,281 @@ sub get { order by start }; - #print STDERR $query."\n"; - #print STDERR Dumper(\@bind_values); + #print STDERR $query."\n"; + #print STDERR Dumper(\@bind_values); - my $entries = db::get( $dbh, $query, \@bind_values ); - for my $entry (@$entries) { - $entry->{weekday} = substr( $entry->{weekday}, 0, 2 ); - } + my $entries = db::get( $dbh, $query, \@bind_values ); + for my $entry (@$entries) { + $entry->{weekday} = substr( $entry->{weekday}, 0, 2 ); + } - return $entries; + return $entries; } #update work dates for all schedules of a work and studio_id sub update { - my $config = shift; - my $entry = shift; + my $config = shift; + my $entry = shift; - return undef unless defined $entry->{project_id}; - return undef unless defined $entry->{studio_id}; - return undef unless defined $entry->{schedule_id}; + return undef unless defined $entry->{project_id}; + return undef unless defined $entry->{studio_id}; + return undef unless defined $entry->{schedule_id}; - my $dbh = db::connect($config); + my $dbh = db::connect($config); - #delete all existing work dates (by project, studio and schedule id) - work_dates::delete( $config, $entry ); + #delete all existing work dates (by project, studio and schedule id) + work_dates::delete( $config, $entry ); - my $day_start = $config->{date}->{day_starting_hour}; + my $day_start = $config->{date}->{day_starting_hour}; - #get all schedules for schedule id ordered by exclude, date - my $schedules = work_schedule::get( - $config, - { - project_id => $entry->{project_id}, - studio_id => $entry->{studio_id}, - schedule_id => $entry->{schedule_id}, - } - ); + #get all schedules for schedule id ordered by exclude, date + my $schedules = work_schedule::get( + $config, + { + project_id => $entry->{project_id}, + studio_id => $entry->{studio_id}, + schedule_id => $entry->{schedule_id}, + } + ); - #add scheduled work dates and remove exluded dates - my $work_dates = {}; + #add scheduled work dates and remove exluded dates + my $work_dates = {}; - #TODO:set schedules exclude to 0 if not 1 - #insert all normal dates (not excludes) - for my $schedule (@$schedules) { - my $dates = get_schedule_dates( $schedule, { exclude => 0 } ); - for my $date (@$dates) { - $date->{exclude} = 0; - $work_dates->{ $date->{start} } = $date; + #TODO:set schedules exclude to 0 if not 1 + #insert all normal dates (not excludes) + for my $schedule (@$schedules) { + my $dates = get_schedule_dates( $schedule, { exclude => 0 } ); + for my $date (@$dates) { + $date->{exclude} = 0; + $work_dates->{ $date->{start} } = $date; - #print STDERR Dumper($date)."\n" if ($date->{start} eq'2014-02-05 19:00:00'); - } - } + #print STDERR Dumper($date)."\n" if ($date->{start} eq'2014-02-05 19:00:00'); + } + } - #insert / overwrite all exlude dates - for my $schedule (@$schedules) { - my $dates = get_schedule_dates( $schedule, { exclude => 1 } ); - for my $date (@$dates) { - $date->{exclude} = 1; - $work_dates->{ $date->{start} } = $date; + #insert / overwrite all exlude dates + for my $schedule (@$schedules) { + my $dates = get_schedule_dates( $schedule, { exclude => 1 } ); + for my $date (@$dates) { + $date->{exclude} = 1; + $work_dates->{ $date->{start} } = $date; - #print STDERR Dumper($date)."\n" if ($date->{start} eq'2014-02-05 19:00:00'); - } - } + #print STDERR Dumper($date)."\n" if ($date->{start} eq'2014-02-05 19:00:00'); + } + } - #print STDERR Dumper($work_dates->{'2014-02-05 19:00:00'}); + #print STDERR Dumper($work_dates->{'2014-02-05 19:00:00'}); - my $request = { config => $config }; + my $request = { config => $config }; - my $i = 0; - my $j = 0; - for my $date ( keys %$work_dates ) { - my $work_date = $work_dates->{$date}; + my $i = 0; + my $j = 0; + for my $date ( keys %$work_dates ) { + my $work_date = $work_dates->{$date}; - #insert date - my $entry = { - project_id => $entry->{project_id}, - studio_id => $entry->{studio_id}, - schedule_id => $entry->{schedule_id}, - title => $entry->{title}, - type => $entry->{type}, - schedule_id => $entry->{schedule_id}, - start => $work_date->{start}, - end => $work_date->{end}, - exclude => $work_date->{exclude} - }; - if ( studio_timeslot_dates::can_studio_edit_events( $config, $entry ) == 1 ) { # by studio_id, start, end - $entry->{start_date} = time::add_hours_to_datetime( $entry->{start}, -$day_start ); - $entry->{end_date} = time::add_hours_to_datetime( $entry->{end}, -$day_start ); - db::insert( $dbh, 'calcms_work_dates', $entry ); + #insert date + my $entry = { + project_id => $entry->{project_id}, + studio_id => $entry->{studio_id}, + schedule_id => $entry->{schedule_id}, + title => $entry->{title}, + type => $entry->{type}, + schedule_id => $entry->{schedule_id}, + start => $work_date->{start}, + end => $work_date->{end}, + exclude => $work_date->{exclude} + }; + if ( studio_timeslot_dates::can_studio_edit_events( $config, $entry ) == 1 ) { # by studio_id, start, end + $entry->{start_date} = time::add_hours_to_datetime( $entry->{start}, -$day_start ); + $entry->{end_date} = time::add_hours_to_datetime( $entry->{end}, -$day_start ); + db::insert( $dbh, 'calcms_work_dates', $entry ); - #print STDERR "$entry->{start_date}\n"; - $i++; - } else { - $j++; + #print STDERR "$entry->{start_date}\n"; + $i++; + } else { + $j++; - #print STDERR Dumper($entry); - } - } + #print STDERR Dumper($entry); + } + } - #print STDERR "$i work_dates updates\n"; - return $j . " dates out of studio times, " . $i; + #print STDERR "$i work_dates updates\n"; + return $j . " dates out of studio times, " . $i; } sub get_schedule_dates { - my $schedule = shift; - my $options = shift; + my $schedule = shift; + my $options = shift; - my $is_exclude = $options->{exclude} || 0; - my $dates = []; - return $dates if ( ( $is_exclude eq '1' ) && ( $schedule->{exclude} ne '1' ) ); - return $dates if ( ( $is_exclude eq '0' ) && ( $schedule->{exclude} eq '1' ) ); + my $is_exclude = $options->{exclude} || 0; + my $dates = []; + return $dates if ( $is_exclude eq '1' ) && ( $schedule->{exclude} ne '1' ); + return $dates if ( $is_exclude eq '0' ) && ( $schedule->{exclude} eq '1' ); - if ( $schedule->{period_type} eq 'single' ) { - $dates = get_single_date( $schedule->{start}, $schedule->{duration} ); - } elsif ( $schedule->{period_type} eq 'days' ) { - $dates = get_dates( $schedule->{start}, $schedule->{end}, $schedule->{duration}, $schedule->{frequency} ); - } elsif ( $schedule->{period_type} eq 'week_of_month' ) { - $dates = get_week_of_month_dates( - $schedule->{start}, $schedule->{end}, $schedule->{duration}, - $schedule->{week_of_month}, $schedule->{weekday}, $schedule->{month} - ); - } else { - print STDERR "unknown schedule period_type\n"; - } - return $dates; + if ( $schedule->{period_type} eq 'single' ) { + $dates = get_single_date( $schedule->{start}, $schedule->{duration} ); + } elsif ( $schedule->{period_type} eq 'days' ) { + $dates = get_dates( $schedule->{start}, $schedule->{end}, $schedule->{duration}, $schedule->{frequency} ); + } elsif ( $schedule->{period_type} eq 'week_of_month' ) { + $dates = get_week_of_month_dates( + $schedule->{start}, $schedule->{end}, $schedule->{duration}, + $schedule->{week_of_month}, $schedule->{weekday}, $schedule->{month} + ); + } else { + print STDERR "unknown schedule period_type\n"; + } + return $dates; } sub get_week_of_month_dates { - my $start = shift; # datetime string - my $end = shift; # datetime string - my $duration = shift; # in minutes - my $week = shift; # every nth week of month - my $weekday = shift; # weekday [1..7] - my $frequency = shift; # every 1st,2nd,3th time + my $start = shift; # datetime string + my $end = shift; # datetime string + my $duration = shift; # in minutes + my $week = shift; # every nth week of month + my $weekday = shift; # weekday [1..7] + my $frequency = shift; # every 1st,2nd,3th time - return undef if $start eq ''; - return undef if $end eq ''; - return undef if $duration eq ''; - return undef if $week eq ''; - return undef if $weekday eq ''; - return undef if $frequency eq ''; - return undef if $frequency == 0; + return undef if $start eq ''; + return undef if $end eq ''; + return undef if $duration eq ''; + return undef if $week eq ''; + return undef if $weekday eq ''; + return undef if $frequency eq ''; + return undef if $frequency == 0; - my $start_dates = time::get_nth_weekday_in_month( $start, $end, $week, $weekday ); + my $start_dates = time::get_nth_weekday_in_month( $start, $end, $week, $weekday ); - my $results = []; + my $results = []; - my $c = -1; - for my $start_datetime (@$start_dates) { - $c++; - my @start = @{ time::datetime_to_array($start_datetime) }; - next unless @start >= 6; - next if ( ( $c % $frequency ) != 0 ); + my $c = -1; + for my $start_datetime (@$start_dates) { + $c++; + my @start = @{ time::datetime_to_array($start_datetime) }; + next unless @start >= 6; + next if ( $c % $frequency ) != 0; - my @end_datetime = Date::Calc::Add_Delta_DHMS( - $start[0], $start[1], $start[2], # start date - $start[3], $start[4], $start[5], # start time - 0, 0, $duration, 0 # delta days, hours, minutes, seconds - ); - my $end_datetime = time::array_to_datetime( \@end_datetime ); + my @end_datetime = Date::Calc::Add_Delta_DHMS( + $start[0], $start[1], $start[2], # start date + $start[3], $start[4], $start[5], # start time + 0, 0, $duration, 0 # delta days, hours, minutes, seconds + ); + my $end_datetime = time::array_to_datetime( \@end_datetime ); - push @$results, - { - start => $start_datetime, - end => $end_datetime - }; - } - return $results; + push @$results, + { + start => $start_datetime, + end => $end_datetime + }; + } + return $results; } #add duration to a single date sub get_single_date { - my $start_datetime = shift; - my $duration = shift; + my $start_datetime = shift; + my $duration = shift; - my @start = @{ time::datetime_to_array($start_datetime) }; - return unless @start >= 6; + my @start = @{ time::datetime_to_array($start_datetime) }; + return unless @start >= 6; - my @end_datetime = Date::Calc::Add_Delta_DHMS( - $start[0], $start[1], $start[2], # start date - $start[3], $start[4], $start[5], # start time - 0, 0, $duration, 0 # delta days, hours, minutes, seconds - ); - my $date = { - start => $start_datetime, - end => time::array_to_datetime( \@end_datetime ) - }; - return [$date]; + my @end_datetime = Date::Calc::Add_Delta_DHMS( + $start[0], $start[1], $start[2], # start date + $start[3], $start[4], $start[5], # start time + 0, 0, $duration, 0 # delta days, hours, minutes, seconds + ); + my $date = { + start => $start_datetime, + end => time::array_to_datetime( \@end_datetime ) + }; + return [$date]; } #calculate all dates between start_datetime and end_date with duration(minutes) and frequency(days) sub get_dates { - my $start_datetime = shift; - my $end_date = shift; - my $duration = shift; # in minutes - my $frequency = shift; # in days - #print "start_datetime:$start_datetime end_date:$end_date duration:$duration frequency:$frequency\n"; + my $start_datetime = shift; + my $end_date = shift; + my $duration = shift; # in minutes + my $frequency = shift; # in days + #print "start_datetime:$start_datetime end_date:$end_date duration:$duration frequency:$frequency\n"; - my @start = @{ time::datetime_to_array($start_datetime) }; - return unless @start >= 6; - my @start_date = ( $start[0], $start[1], $start[2] ); - my $start_time = sprintf( '%02d:%02d:%02d', $start[3], $start[4], $start[5] ); + my @start = @{ time::datetime_to_array($start_datetime) }; + return unless @start >= 6; + my @start_date = ( $start[0], $start[1], $start[2] ); + my $start_time = sprintf( '%02d:%02d:%02d', $start[3], $start[4], $start[5] ); - #print STDERR "$start_datetime,$end_date,$duration,$frequency\n"; + #print STDERR "$start_datetime,$end_date,$duration,$frequency\n"; - #return on single date - my $date = {}; - $date->{start} = sprintf( "%04d-%02d-%02d", @start_date ) . ' ' . $start_time; - return undef if $duration eq ''; + #return on single date + my $date = {}; + $date->{start} = sprintf( "%04d-%02d-%02d", @start_date ) . ' ' . $start_time; + return undef if $duration eq ''; - return undef if ( ( $frequency eq '' ) || ( $end_date eq '' ) ); + return undef if ( $frequency eq '' ) || ( $end_date eq '' ); - #continue on recurring date - my @end = @{ time::datetime_to_array($end_date) }; - return unless @end >= 3; - my @end_date = ( $end[0], $end[1], $end[2] ); + #continue on recurring date + my @end = @{ time::datetime_to_array($end_date) }; + return unless @end >= 3; + my @end_date = ( $end[0], $end[1], $end[2] ); - my $today = time::time_to_date(); - my ( $year, $month, $day ) = split( /\-/, $today ); + my $today = time::time_to_date(); + my ( $year, $month, $day ) = split( /\-/, $today ); - #do not show dates one month back - my $not_before = sprintf( "%04d-%02d-%02d", Date::Calc::Add_Delta_Days( $year, $month, $day, -30 ) ); + #do not show dates one month back + my $not_before = sprintf( "%04d-%02d-%02d", Date::Calc::Add_Delta_Days( $year, $month, $day, -30 ) ); - my $dates = []; - return $dates if ( $end_date lt $today ); - return $dates if ( $frequency < 1 ); + my $dates = []; + return $dates if ( $end_date lt $today ); + return $dates if ( $frequency < 1 ); - my $j = Date::Calc::Delta_Days( @start_date, @end_date ); - my $c = 0; - for ( my $i = 0 ; $i <= $j ; $i += $frequency ) { - my @date = Date::Calc::Add_Delta_Days( $start[0], $start[1], $start[2], $i ); - my $date = {}; - $date->{start} = sprintf( "%04d-%02d-%02d", @date ) . ' ' . $start_time; + my $j = Date::Calc::Delta_Days( @start_date, @end_date ); + my $c = 0; + for ( my $i = 0 ; $i <= $j ; $i += $frequency ) { + my @date = Date::Calc::Add_Delta_Days( $start[0], $start[1], $start[2], $i ); + my $date = {}; + $date->{start} = sprintf( "%04d-%02d-%02d", @date ) . ' ' . $start_time; - my @end_datetime = Date::Calc::Add_Delta_DHMS( - $date[0], $date[1], $date[2], # start date - $start[3], $start[4], $start[5], # start time - 0, 0, $duration, 0 # delta days, hours, minutes, seconds - ); - $date->{end} = time::array_to_datetime( \@end_datetime ); + my @end_datetime = Date::Calc::Add_Delta_DHMS( + $date[0], $date[1], $date[2], # start date + $start[3], $start[4], $start[5], # start time + 0, 0, $duration, 0 # delta days, hours, minutes, seconds + ); + $date->{end} = time::array_to_datetime( \@end_datetime ); - last if ( $c > 200 ); - $c++; + last if ( $c > 200 ); + $c++; - next if $date->{end} lt $not_before; - push @$dates, $date; + next if $date->{end} lt $not_before; + push @$dates, $date; - } - return $dates; + } + return $dates; } #remove all work_dates for studio_id and schedule_id sub delete { - my $config = shift; - my $entry = shift; + my $config = shift; + my $entry = shift; - return undef unless defined $entry->{project_id}; - return undef unless defined $entry->{studio_id}; - return undef unless defined $entry->{schedule_id}; + return undef unless defined $entry->{project_id}; + return undef unless defined $entry->{studio_id}; + return undef unless defined $entry->{schedule_id}; - my $dbh = db::connect($config); + my $dbh = db::connect($config); - my $query = qq{ + my $query = qq{ delete from calcms_work_dates where project_id=? and studio_id=? and schedule_id=? }; - my $bind_values = [ $entry->{project_id}, $entry->{studio_id}, $entry->{schedule_id} ]; + my $bind_values = [ $entry->{project_id}, $entry->{studio_id}, $entry->{schedule_id} ]; - #print '
$query'.$query.Dumper($bind_values).'
'; - return db::put( $dbh, $query, $bind_values ); + #print '
$query'.$query.Dumper($bind_values).'
'; + return db::put( $dbh, $query, $bind_values ); } sub error { - my $msg = shift; - print "ERROR: $msg
\n"; + my $msg = shift; + print "ERROR: $msg
\n"; } #do not delete last line! diff --git a/website/agenda/planung/assign_series.cgi b/website/agenda/planung/assign_series.cgi index 1cb164c..406b813 100755 --- a/website/agenda/planung/assign_series.cgi +++ b/website/agenda/planung/assign_series.cgi @@ -2,7 +2,6 @@ use warnings "all"; use strict; - use Data::Dumper; use URI::Escape(); use Encode(); @@ -32,30 +31,30 @@ my $r = shift; my $config = config::get('../config/config.cgi'); my $debug = $config->{system}->{debug}; my ( $user, $expires ) = auth::get_user( $cgi, $config ); -return if ( ( !defined $user ) || ( $user eq '' ) ); +return if ( !defined $user ) || ( $user eq '' ); #print STDERR $params->{project_id}."\n"; my $user_presets = uac::get_user_presets( - $config, - { - project_id => $params->{project_id}, - studio_id => $params->{studio_id}, - user => $user - } + $config, + { + project_id => $params->{project_id}, + studio_id => $params->{studio_id}, + user => $user + } ); $params->{default_studio_id} = $user_presets->{studio_id}; $params->{studio_id} = $params->{default_studio_id} - if ( ( !( defined $params->{action} ) ) || ( $params->{action} eq '' ) || ( $params->{action} eq 'login' ) ); + if ( !( defined $params->{action} ) ) || ( $params->{action} eq '' ) || ( $params->{action} eq 'login' ); $params->{project_id} = $user_presets->{project_id} - if ( ( !( defined $params->{action} ) ) || ( $params->{action} eq '' ) || ( $params->{action} eq 'login' ) ); + if ( !( defined $params->{action} ) ) || ( $params->{action} eq '' ) || ( $params->{action} eq 'login' ); #print STDERR $params->{project_id}."\n"; my $request = { - url => $ENV{QUERY_STRING} || '', - params => { - original => $params, - checked => check_params($params), - }, + url => $ENV{QUERY_STRING} || '', + params => { + original => $params, + checked => check_params($params), + }, }; $request = uac::prepare_request( $request, $user_presets ); @@ -76,170 +75,170 @@ print q{ my $permissions = $request->{permissions}; unless ( $permissions->{scan_series_events} == 1 ) { - uac::permissions_denied('scan_series_events'); - return; + uac::permissions_denied('scan_series_events'); + return; } if ( defined $params->{action} ) { - assign_series( $config, $request ) if ( $params->{action} eq 'assign_series' ); + assign_series( $config, $request ) if $params->{action} eq 'assign_series'; } show_events( $config, $request ); sub show_events { - my $config = shift; - my $request = shift; + my $config = shift; + my $request = shift; - my $params = $request->{params}->{checked}; - my $permissions = $request->{permissions}; - unless ( $permissions->{assign_series_events} == 1 ) { - uac::permissions_denied('assign_series_events'); - return; - } + my $params = $request->{params}->{checked}; + my $permissions = $request->{permissions}; + unless ( $permissions->{assign_series_events} == 1 ) { + uac::permissions_denied('assign_series_events'); + return; + } - my $projects = project::get( $config, { project_id => $params->{project_id} } ); - my $project = $projects->[0]; - return unless ( @$projects == 1 ); + my $projects = project::get( $config, { project_id => $params->{project_id} } ); + my $project = $projects->[0]; + return unless scalar @$projects == 1 ; - my $studios = studios::get( $config, { project_id => $params->{project_id}, studio_id => $params->{studio_id} } ); - my $studio = $studios->[0]; - return unless ( @$studios == 1 ); + my $studios = studios::get( $config, { project_id => $params->{project_id}, studio_id => $params->{studio_id} } ); + my $studio = $studios->[0]; + return unless scalar @$studios == 1 ; - my $project_name = $project->{name}; - my $studio_name = $studio->{location}; + my $project_name = $project->{name}; + my $studio_name = $studio->{location}; - #get series_names - my $dbh = db::connect($config); - my $query = q{ + #get series_names + my $dbh = db::connect($config); + my $query = q{ select project_id, studio_id, series_id, series_name, title from calcms_series s, calcms_project_series ps where s.id=ps.series_id order by series_name, title }; - my $results = db::get( $dbh, $query ); + my $results = db::get( $dbh, $query ); - # get projects by id - my $projects_by_id = {}; - $projects = project::get($config); - for my $project (@$projects) { - $projects_by_id->{ $project->{project_id} } = $project; - } + # get projects by id + my $projects_by_id = {}; + $projects = project::get($config); + for my $project (@$projects) { + $projects_by_id->{ $project->{project_id} } = $project; + } - # get studios by id - my $studios_by_id = {}; - $studios = studios::get($config); - for my $studio (@$studios) { - $studios_by_id->{ $studio->{id} } = $studio; - } + # get studios by id + my $studios_by_id = {}; + $studios = studios::get($config); + for my $studio (@$studios) { + $studios_by_id->{ $studio->{id} } = $studio; + } - #add project and studio name to series - for my $result (@$results) { - $result->{project_name} = $projects_by_id->{ $result->{project_id} }->{name}; - $result->{studio_name} = $studios_by_id->{ $result->{studio_id} }->{location}; - $result->{series_name} = 'Einzelsendung' if $result->{series_name} eq '_single_'; - } - $params->{series} = $results; + #add project and studio name to series + for my $result (@$results) { + $result->{project_name} = $projects_by_id->{ $result->{project_id} }->{name}; + $result->{studio_name} = $studios_by_id->{ $result->{studio_id} }->{location}; + $result->{series_name} = 'Einzelsendung' if $result->{series_name} eq '_single_'; + } + $params->{series} = $results; - #fill template - $params->{project_name} = $project_name; - $params->{studio_name} = $studio_name; + #fill template + $params->{project_name} = $project_name; + $params->{studio_name} = $studio_name; - template::process( 'print', $params->{template}, $params ); + template::process( 'print', $params->{template}, $params ); } sub assign_series { - my $config = shift; - my $request = shift; + my $config = shift; + my $request = shift; - my $params = $request->{params}->{checked}; - my $permissions = $request->{permissions}; - unless ( $permissions->{assign_series_events} == 1 ) { - uac::permissions_denied('assign_series_events'); - return; - } + my $params = $request->{params}->{checked}; + my $permissions = $request->{permissions}; + unless ( $permissions->{assign_series_events} == 1 ) { + uac::permissions_denied('assign_series_events'); + return; + } - my $entry = {}; - for my $attr ( 'project_id', 'studio_id', 'series_id' ) { - if ( defined $params->{$attr} ) { - $entry->{$attr} = $params->{$attr}; - } else { - uac::print_error( $attr . ' not given!' ); - return; - } - } + my $entry = {}; + for my $attr ( 'project_id', 'studio_id', 'series_id' ) { + if ( defined $params->{$attr} ) { + $entry->{$attr} = $params->{$attr}; + } else { + uac::print_error( $attr . ' not given!' ); + return; + } + } - $config->{access}->{write} = 1; + $config->{access}->{write} = 1; - #check if series is assigned to project/studio - my $series = series::get( - $config, - { - project_id => $entry->{project_id}, - studio_id => $entry->{studio_id}, - series_id => $entry->{series_id}, - } - ); + #check if series is assigned to project/studio + my $series = series::get( + $config, + { + project_id => $entry->{project_id}, + studio_id => $entry->{studio_id}, + series_id => $entry->{series_id}, + } + ); - #print Dumper($series); - if ( @$series == 0 ) { + #print Dumper($series); + if ( @$series == 0 ) { - # assign series to project/studio - project::assign_series( - $config, - { - project_id => $entry->{project_id}, - studio_id => $entry->{studio_id}, - series_id => $entry->{series_id}, - } - ); + # assign series to project/studio + project::assign_series( + $config, + { + project_id => $entry->{project_id}, + studio_id => $entry->{studio_id}, + series_id => $entry->{series_id}, + } + ); - #print "assign\n"; - } else { - print STDERR + #print "assign\n"; + } else { + print STDERR "event $entry->{event_id} already assigned to project $entry->{project_id}, studio $entry->{studio_id}, series $entry->{series_id}\n"; - #print "is schon\n"; - } + #print "is schon\n"; + } - $config->{access}->{write} = 0; - uac::print_info("event successfully assigned to series"); + $config->{access}->{write} = 0; + uac::print_info("event successfully assigned to series"); } sub check_params { - my $params = shift; + my $params = shift; - my $checked = {}; + my $checked = {}; - my $debug = $params->{debug} || ''; - if ( $debug =~ /([a-z\_\,]+)/ ) { - $debug = $1; - } - $checked->{debug} = $debug; + my $debug = $params->{debug} || ''; + if ( $debug =~ /([a-z\_\,]+)/ ) { + $debug = $1; + } + $checked->{debug} = $debug; - #actions and roles - $checked->{action} = ''; - if ( defined $params->{action} ) { - if ( $params->{action} =~ /^(assign_series)$/ ) { - $checked->{action} = $params->{action}; - } - } + #actions and roles + $checked->{action} = ''; + if ( defined $params->{action} ) { + if ( $params->{action} =~ /^(assign_series)$/ ) { + $checked->{action} = $params->{action}; + } + } - #numeric values - $checked->{exclude} = 0; - for my $param ( 'id', 'project_id', 'studio_id', 'series_id' ) { - if ( ( defined $params->{$param} ) && ( $params->{$param} =~ /^\d+$/ ) ) { - $checked->{$param} = $params->{$param}; - } - } + #numeric values + $checked->{exclude} = 0; + for my $param ( 'id', 'project_id', 'studio_id', 'series_id' ) { + if ( ( defined $params->{$param} ) && ( $params->{$param} =~ /^\d+$/ ) ) { + $checked->{$param} = $params->{$param}; + } + } - if ( defined $checked->{studio_id} ) { - $checked->{default_studio_id} = $checked->{studio_id}; - } else { - $checked->{studio_id} = -1; - } + if ( defined $checked->{studio_id} ) { + $checked->{default_studio_id} = $checked->{studio_id}; + } else { + $checked->{studio_id} = -1; + } - $checked->{template} = template::check( $params->{template}, 'assign_series' ); + $checked->{template} = template::check( $params->{template}, 'assign_series' ); - return $checked; + return $checked; } __DATA__ diff --git a/website/agenda/planung/assignments.cgi b/website/agenda/planung/assignments.cgi index 967e954..73d8ccf 100755 --- a/website/agenda/planung/assignments.cgi +++ b/website/agenda/planung/assignments.cgi @@ -36,26 +36,26 @@ return if ( ( !defined $user ) || ( $user eq '' ) ); #print STDERR $params->{project_id}."\n"; my $user_presets = uac::get_user_presets( - $config, - { - project_id => $params->{project_id}, - studio_id => $params->{studio_id}, - user => $user - } + $config, + { + project_id => $params->{project_id}, + studio_id => $params->{studio_id}, + user => $user + } ); $params->{default_studio_id} = $user_presets->{studio_id}; $params->{studio_id} = $params->{default_studio_id} - if ( ( !( defined $params->{action} ) ) || ( $params->{action} eq '' ) || ( $params->{action} eq 'login' ) ); + if ( !( defined $params->{action} ) ) || ( $params->{action} eq '' ) || ( $params->{action} eq 'login' ); $params->{project_id} = $user_presets->{project_id} - if ( ( !( defined $params->{action} ) ) || ( $params->{action} eq '' ) || ( $params->{action} eq 'login' ) ); + if ( !( defined $params->{action} ) ) || ( $params->{action} eq '' ) || ( $params->{action} eq 'login' ); #print STDERR $params->{project_id}."\n"; my $request = { - url => $ENV{QUERY_STRING} || '', - params => { - original => $params, - checked => check_params($params), - }, + url => $ENV{QUERY_STRING} || '', + params => { + original => $params, + checked => check_params($params), + }, }; $request = uac::prepare_request( $request, $user_presets ); @@ -76,86 +76,86 @@ print q{ my $permissions = $request->{permissions}; unless ( $permissions->{scan_series_events} == 1 ) { - uac::permissions_denied('scan_series_events'); - return; + uac::permissions_denied('scan_series_events'); + return; } if ( defined $params->{action} ) { - assign_events( $config, $request ) if ( $params->{action} eq 'assign_events' ); + assign_events( $config, $request ) if ( $params->{action} eq 'assign_events' ); } show_events( $config, $request ); sub show_events { - my $config = shift; - my $request = shift; + my $config = shift; + my $request = shift; - my $params = $request->{params}->{checked}; - my $permissions = $request->{permissions}; - unless ( $permissions->{assign_series_events} == 1 ) { - uac::permissions_denied('assign_series_events'); - return; - } + my $params = $request->{params}->{checked}; + my $permissions = $request->{permissions}; + unless ( $permissions->{assign_series_events} == 1 ) { + uac::permissions_denied('assign_series_events'); + return; + } - my $projects = project::get( $config, { project_id => $params->{project_id} } ); - my $project = $projects->[0]; + my $projects = project::get( $config, { project_id => $params->{project_id} } ); + my $project = $projects->[0]; - #print STDERR Dumper($project); - return unless ( @$projects == 1 ); + #print STDERR Dumper($project); + return unless ( @$projects == 1 ); - my $studios = studios::get( $config, { project_id => $params->{project_id}, studio_id => $params->{studio_id} } ); - my $studio = $studios->[0]; + my $studios = studios::get( $config, { project_id => $params->{project_id}, studio_id => $params->{studio_id} } ); + my $studio = $studios->[0]; - #print STDERR Dumper($studio); - return unless ( @$studios == 1 ); + #print STDERR Dumper($studio); + return unless ( @$studios == 1 ); - my $project_name = $project->{name}; - my $studio_name = $studio->{location}; + my $project_name = $project->{name}; + my $studio_name = $studio->{location}; - #get series_names - my $dbh = db::connect($config); - my $query = q{ + #get series_names + my $dbh = db::connect($config); + my $query = q{ select project_id, studio_id, series_id, series_name, title from calcms_series s, calcms_project_series ps where s.id=ps.series_id order by series_name, title }; - my $results = db::get( $dbh, $query ); + my $results = db::get( $dbh, $query ); - # get projects by id - my $projects_by_id = {}; - $projects = project::get($config); - for my $project (@$projects) { - $projects_by_id->{ $project->{project_id} } = $project; - } + # get projects by id + my $projects_by_id = {}; + $projects = project::get($config); + for my $project (@$projects) { + $projects_by_id->{ $project->{project_id} } = $project; + } - # get studios by id - my $studios_by_id = {}; - $studios = studios::get($config); - for my $studio (@$studios) { - $studios_by_id->{ $studio->{id} } = $studio; - } + # get studios by id + my $studios_by_id = {}; + $studios = studios::get($config); + for my $studio (@$studios) { + $studios_by_id->{ $studio->{id} } = $studio; + } - #add project and studio name to series - for my $result (@$results) { - $result->{project_name} = $projects_by_id->{ $result->{project_id} }->{name}; - $result->{studio_name} = $studios_by_id->{ $result->{studio_id} }->{location}; - $result->{series_name} = 'Einzelsendung' if $result->{series_name} eq '_single_'; - } - $params->{series} = $results; + #add project and studio name to series + for my $result (@$results) { + $result->{project_name} = $projects_by_id->{ $result->{project_id} }->{name}; + $result->{studio_name} = $studios_by_id->{ $result->{studio_id} }->{location}; + $result->{series_name} = 'Einzelsendung' if $result->{series_name} eq '_single_'; + } + $params->{series} = $results; - # get events not assigned to series - my $conditions = []; - my $bind_values = []; - if ( $project_name ne '' ) { - push @$conditions, 'e.project=?'; - push @$bind_values, $project_name; - } - if ( $studio_name ne '' ) { - push @$conditions, 'e.location=?'; - push @$bind_values, $studio_name; - } - $conditions = ' and ' . join( ' and ', @$conditions ) if ( @$conditions > 0 ); - $query = qq{ + # get events not assigned to series + my $conditions = []; + my $bind_values = []; + if ( $project_name ne '' ) { + push @$conditions, 'e.project=?'; + push @$bind_values, $project_name; + } + if ( $studio_name ne '' ) { + push @$conditions, 'e.location=?'; + push @$bind_values, $studio_name; + } + $conditions = ' and ' . join( ' and ', @$conditions ) if scalar(@$conditions) > 0; + $query = qq{ select e.id, program, project, location, start, series_name, title, episode, rerun from calcms_events e left join calcms_series_events se on se.event_id =e.id where se.event_id is null @@ -163,252 +163,252 @@ sub show_events { order by series_name,title,start limit 1000 }; - print '
' . Dumper($query) . Dumper($bind_values) . '
'; - $results = db::get( $dbh, $query, $bind_values ); + print '
' . Dumper($query) . Dumper($bind_values) . '
'; + $results = db::get( $dbh, $query, $bind_values ); - # detect title and episode - for my $result (@$results) { - $result->{rerun} .= ''; - if ( $result->{title} =~ /\#(\d+)([a-z])?\s*$/ ) { - $result->{episode} = $1 unless defined $result->{episode}; - $result->{rerun} = $2 || '' unless ( $result->{rerun} =~ /\d/ ); - $result->{title} =~ s/\#\d+[a-z]?\s*$//; - $result->{title} =~ s/\s+$//; - } - my $a = time::datetime_to_array( $result->{start} ); + # detect title and episode + for my $result (@$results) { + $result->{rerun} .= ''; + if ( $result->{title} =~ /\#(\d+)([a-z])?\s*$/ ) { + $result->{episode} = $1 unless defined $result->{episode}; + $result->{rerun} = $2 || '' unless ( $result->{rerun} =~ /\d/ ); + $result->{title} =~ s/\#\d+[a-z]?\s*$//; + $result->{title} =~ s/\s+$//; + } + my $a = time::datetime_to_array( $result->{start} ); - #print STDERR "($a->[0],$a->[1],$a->[2])\n"; - $result->{weekday} = time::weekday( $a->[0], $a->[1], $a->[2] ); - $result->{weekday} = $time::names->{de}->{weekdays_abbr}->[ $result->{weekday} - 1 ]; - } + #print STDERR "($a->[0],$a->[1],$a->[2])\n"; + $result->{weekday} = time::weekday( $a->[0], $a->[1], $a->[2] ); + $result->{weekday} = $time::names->{de}->{weekdays_abbr}->[ $result->{weekday} - 1 ]; + } - #fill template - $params->{unassigned_events} = $results; - $params->{sum_events} = @$results; - $params->{project_name} = $project_name; - $params->{studio_name} = $studio_name; + #fill template + $params->{unassigned_events} = $results; + $params->{sum_events} = @$results; + $params->{project_name} = $project_name; + $params->{studio_name} = $studio_name; - template::process( 'print', $params->{template}, $params ); + template::process( 'print', $params->{template}, $params ); } sub assign_events { - my $config = shift; - my $request = shift; + my $config = shift; + my $request = shift; - my $params = $request->{params}->{checked}; - my $permissions = $request->{permissions}; - unless ( $permissions->{assign_series_events} == 1 ) { - uac::permissions_denied('assign_series_events'); - return; - } + my $params = $request->{params}->{checked}; + my $permissions = $request->{permissions}; + unless ( $permissions->{assign_series_events} == 1 ) { + uac::permissions_denied('assign_series_events'); + return; + } - my $entry = {}; - for my $attr ( 'project_id', 'studio_id', 'series_id', 'event_ids' ) { - if ( defined $params->{$attr} ) { - $entry->{$attr} = $params->{$attr}; - } else { - uac::print_error( $attr . ' not given!' ); - return; - } - } + my $entry = {}; + for my $attr ( 'project_id', 'studio_id', 'series_id', 'event_ids' ) { + if ( defined $params->{$attr} ) { + $entry->{$attr} = $params->{$attr}; + } else { + uac::print_error( $attr . ' not given!' ); + return; + } + } - $config->{access}->{write} = 1; - for my $event_id ( split( /[\,\s]+/, $params->{event_ids} ) ) { - next unless $event_id =~ /^\d+/; - $entry->{event_id} = $event_id; + $config->{access}->{write} = 1; + for my $event_id ( split( /[\,\s]+/, $params->{event_ids} ) ) { + next unless $event_id =~ /^\d+/; + $entry->{event_id} = $event_id; - #get and parse event - my $request2 = { - params => { - checked => events::check_params( - $config, - { - event_id => $entry->{event_id}, - template => 'no', - limit => 1, - archive => 'all', - } - ) - }, - config => $request->{config}, - permissions => $request->{permissions} - }; - $request2->{params}->{checked}->{published} = 'all'; - my $events = events::get( $config, $request2 ); - my $event = $events->[0]; - unless ( defined $event ) { - print STDERR + #get and parse event + my $request2 = { + params => { + checked => events::check_params( + $config, + { + event_id => $entry->{event_id}, + template => 'no', + limit => 1, + archive => 'all', + } + ) + }, + config => $request->{config}, + permissions => $request->{permissions} + }; + $request2->{params}->{checked}->{published} = 'all'; + my $events = events::get( $config, $request2 ); + my $event = $events->[0]; + unless ( defined $event ) { + print STDERR "event not found for project $entry->{project_id}, studio $entry->{studio_id}, series $entry->{series_id}, event $entry->{event_id}\n"; - next; - } - print STDERR "'" . $event->{event_id} . "' '" . $event->{series_name} . "' '" . $event->{title} . "' '" . $event->{episode} . "'\n"; + next; + } + print STDERR "'" . $event->{event_id} . "' '" . $event->{series_name} . "' '" . $event->{title} . "' '" . $event->{episode} . "'\n"; - #next; + #next; - #check if series is assigned to project/studio - my $series = series::get( - $config, - { - project_id => $entry->{project_id}, - studio_id => $entry->{studio_id}, - series_id => $entry->{series_id}, - } - ); - if ( @$series == 0 ) { + #check if series is assigned to project/studio + my $series = series::get( + $config, + { + project_id => $entry->{project_id}, + studio_id => $entry->{studio_id}, + series_id => $entry->{series_id}, + } + ); + if ( scalar(@$series) == 0 ) { - # assign series to project/studio - project::assign_series( - $config, - { - project_id => $entry->{project_id}, - studio_id => $entry->{studio_id}, - series_id => $entry->{series_id}, - } - ); - } else { - print STDERR + # assign series to project/studio + project::assign_series( + $config, + { + project_id => $entry->{project_id}, + studio_id => $entry->{studio_id}, + series_id => $entry->{series_id}, + } + ); + } else { + print STDERR "event $entry->{event_id} already asigned to project $entry->{project_id}, studio $entry->{studio_id}, series $entry->{series_id}\n"; - } + } - #get series - $series = series::get( - $config, - { - project_id => $entry->{project_id}, - studio_id => $entry->{studio_id}, - series_id => $entry->{series_id}, - } - ); - if ( @$series == 1 ) { - my $serie = $series->[0]; + #get series + $series = series::get( + $config, + { + project_id => $entry->{project_id}, + studio_id => $entry->{studio_id}, + series_id => $entry->{series_id}, + } + ); + if ( scalar(@$series) == 1 ) { + my $serie = $series->[0]; - #set event's series name to value from series - my $series_name = $serie->{series_name} || ''; - if ( $series_name ne '' ) { + #set event's series name to value from series + my $series_name = $serie->{series_name} || ''; + if ( $series_name ne '' ) { - # prepend series_name from event to title on adding to single_events series - my $title = $event->{title}; - if ( $serie->{has_single_events} eq '1' ) { - $title = $event->{series_name} . ' - ' . $title if $event->{series_name} ne ''; - } + # prepend series_name from event to title on adding to single_events series + my $title = $event->{title}; + if ( $serie->{has_single_events} eq '1' ) { + $title = $event->{series_name} . ' - ' . $title if $event->{series_name} ne ''; + } - # save event content - series_events::save_content( - $config, - { - studio_id => $entry->{studio_id}, - id => $entry->{event_id}, #TODO: id=> event_id - series_name => $series_name, - title => $title, - episode => $event->{episode}, - rerun => $event->{rerun}, - modified_by => $params->{presets}->{user}, - } - ); + # save event content + series_events::save_content( + $config, + { + studio_id => $entry->{studio_id}, + id => $entry->{event_id}, #TODO: id=> event_id + series_name => $series_name, + title => $title, + episode => $event->{episode}, + rerun => $event->{rerun}, + modified_by => $params->{presets}->{user}, + } + ); - # add to history - $event->{project_id} = $entry->{project_id}; - $event->{studio_id} = $entry->{studio_id}; - $event->{series_id} = $entry->{series_id}; - $event->{event_id} = $entry->{event_id}; - $event->{series_name} = $series_name; - $event->{title} = $title; - $event->{user} = $params->{presets}->{user}; - event_history::insert( $config, $event ); + # add to history + $event->{project_id} = $entry->{project_id}; + $event->{studio_id} = $entry->{studio_id}; + $event->{series_id} = $entry->{series_id}; + $event->{event_id} = $entry->{event_id}; + $event->{series_name} = $series_name; + $event->{title} = $title; + $event->{user} = $params->{presets}->{user}; + event_history::insert( $config, $event ); - # print STDERR "ok\n"; - } - } else { - print STDERR "no series title found for studio $entry->{studio_id} series $entry->{series_id}, event $entry->{event_id}\n"; - next; - } + # print STDERR "ok\n"; + } + } else { + print STDERR "no series title found for studio $entry->{studio_id} series $entry->{series_id}, event $entry->{event_id}\n"; + next; + } - #assign event - my $result = series::assign_event( - $config, - { - project_id => $entry->{project_id}, - studio_id => $entry->{studio_id}, - series_id => $entry->{series_id}, - event_id => $entry->{event_id}, - manual => 1 - } - ); - unless ( defined $result ) { - uac::print_error("error on assigning event to series"); - return undef; - } - } + #assign event + my $result = series::assign_event( + $config, + { + project_id => $entry->{project_id}, + studio_id => $entry->{studio_id}, + series_id => $entry->{series_id}, + event_id => $entry->{event_id}, + manual => 1 + } + ); + unless ( defined $result ) { + uac::print_error("error on assigning event to series"); + return undef; + } + } - $config->{access}->{write} = 0; - uac::print_info("event successfully assigned to series"); + $config->{access}->{write} = 0; + uac::print_info("event successfully assigned to series"); - #$params->{getBack}=1; + #$params->{getBack}=1; } sub check_params { - my $params = shift; + my $params = shift; - my $checked = {}; + my $checked = {}; - my $debug = $params->{debug} || ''; - if ( $debug =~ /([a-z\_\,]+)/ ) { - $debug = $1; - } - $checked->{debug} = $debug; + my $debug = $params->{debug} || ''; + if ( $debug =~ /([a-z\_\,]+)/ ) { + $debug = $1; + } + $checked->{debug} = $debug; - #actions and roles - $checked->{action} = ''; - if ( defined $params->{action} ) { - if ( $params->{action} =~ /^(assign_events)$/ ) { - $checked->{action} = $params->{action}; - } - } + #actions and roles + $checked->{action} = ''; + if ( defined $params->{action} ) { + if ( $params->{action} =~ /^(assign_events)$/ ) { + $checked->{action} = $params->{action}; + } + } - #numeric values - $checked->{exclude} = 0; - for my $param ( 'id', 'project_id', 'studio_id', 'series_id', 'event_id' ) { - if ( ( defined $params->{$param} ) && ( $params->{$param} =~ /^\d+$/ ) ) { - $checked->{$param} = $params->{$param}; - } - } + #numeric values + $checked->{exclude} = 0; + for my $param ( 'id', 'project_id', 'studio_id', 'series_id', 'event_id' ) { + if ( ( defined $params->{$param} ) && ( $params->{$param} =~ /^\d+$/ ) ) { + $checked->{$param} = $params->{$param}; + } + } - for my $param ('event_ids') { - if ( ( defined $params->{$param} ) && ( $params->{$param} =~ /^[\d,]+$/ ) ) { - $checked->{$param} = $params->{$param}; - } - } + for my $param ('event_ids') { + if ( ( defined $params->{$param} ) && ( $params->{$param} =~ /^[\d,]+$/ ) ) { + $checked->{$param} = $params->{$param}; + } + } - if ( defined $checked->{studio_id} ) { - $checked->{default_studio_id} = $checked->{studio_id}; - } else { - $checked->{studio_id} = -1; - } + if ( defined $checked->{studio_id} ) { + $checked->{default_studio_id} = $checked->{studio_id}; + } else { + $checked->{studio_id} = -1; + } - $checked->{template} = template::check( $params->{template}, 'assignments' ); + $checked->{template} = template::check( $params->{template}, 'assignments' ); - if ( ( defined $checked->{action} ) && ( $checked->{action} eq 'save_schedule' ) ) { + if ( ( defined $checked->{action} ) && ( $checked->{action} eq 'save_schedule' ) ) { - #set defaults - $checked->{create_events} = 0; - $checked->{publish_events} = 0; - } - for my $param ( 'frequency', 'duration', 'default_duration', 'create_events', 'publish_events', 'live', 'count_episodes' ) { - if ( ( defined $params->{$param} ) && ( $params->{$param} =~ /(\d+)/ ) ) { - $checked->{$param} = $1; - } - } + #set defaults + $checked->{create_events} = 0; + $checked->{publish_events} = 0; + } + for my $param ( 'frequency', 'duration', 'default_duration', 'create_events', 'publish_events', 'live', 'count_episodes' ) { + if ( ( defined $params->{$param} ) && ( $params->{$param} =~ /(\d+)/ ) ) { + $checked->{$param} = $1; + } + } - #scalars - for my $param ( 'search', 'from', 'till' ) { - if ( defined $params->{$param} ) { - $checked->{$param} = $params->{$param}; - $checked->{$param} =~ s/^\s+//g; - $checked->{$param} =~ s/\s+$//g; - } - } + #scalars + for my $param ( 'search', 'from', 'till' ) { + if ( defined $params->{$param} ) { + $checked->{$param} = $params->{$param}; + $checked->{$param} =~ s/^\s+//g; + $checked->{$param} =~ s/\s+$//g; + } + } - return $checked; + return $checked; } __DATA__ diff --git a/website/agenda/planung/audio_recordings.cgi b/website/agenda/planung/audio_recordings.cgi index ef2aca6..5bdddd6 100755 --- a/website/agenda/planung/audio_recordings.cgi +++ b/website/agenda/planung/audio_recordings.cgi @@ -6,7 +6,7 @@ use warnings; use strict; use Data::Dumper; -use CGI::Simple (); +use CGI::Simple (); use ModPerl::Util (); use Date::Calc(); use Time::Local(); @@ -141,8 +141,9 @@ sub uploadRecording { #print STDERR Dumper($fh)."
"; my $fileInfo = uploadFile( $config, $fh, $params->{event_id}, $user, $params->{upload} ); $params->{error} .= $fileInfo->{error} if defined $fileInfo->{error}; - $params->{path} = $fileInfo->{path}; - $params->{size} = $fileInfo->{size}; + $params->{path} = $fileInfo->{path}; + $params->{size} = $fileInfo->{size}; + #$params->{duration} = $fileInfo->{duration}; $params = updateDatabase( $config, $params, $user ) if $params->{error} eq ''; } else { @@ -292,18 +293,19 @@ sub showAudioRecordings { event_id => $params->{event_id}, } ); + #print Dumper($audioRecordings); for my $recording (@$audioRecordings) { $recording->{size} =~ s/(\d)(\d\d\d)$/$1\.$2/g; $recording->{size} =~ s/(\d)(\d\d\d\.\d\d\d)$/$1\.$2/g; $recording->{processed} = $recording->{processed} ? 'yes' : 'no'; - $recording->{mastered} = $recording->{mastered} ? 'yes' : 'no'; + $recording->{mastered} = $recording->{mastered} ? 'yes' : 'no'; $recording->{eventDuration} = getDuration( $recording->{eventDuration} ); $recording->{audioDuration} = getDuration( $recording->{audioDuration} ); - $recording->{rmsLeft} ||= '-'; + $recording->{rmsLeft} ||= '-'; $recording->{rmsRight} ||= '-'; } @@ -323,19 +325,19 @@ sub showAudioRecordings { } -sub getDuration{ - my $duration=shift; - my $hour = int($duration / 3600); - $duration -= $hour * 3600; +sub getDuration { + my $duration = shift; + my $hour = int( $duration / 3600 ); + $duration -= $hour * 3600; - my $minutes = int($duration / 60); - $duration -= $minutes * 60; + my $minutes = int( $duration / 60 ); + $duration -= $minutes * 60; - my $seconds = int($duration); - $duration -= $seconds; + my $seconds = int($duration); + $duration -= $seconds; - my $milli= int(100 * $duration); - return sprintf("%02d:%02d:%02d.%02d",$hour, $minutes, $seconds, $milli); + my $milli = int( 100 * $duration ); + return sprintf( "%02d:%02d:%02d.%02d", $hour, $minutes, $seconds, $milli ); } sub uploadFile { @@ -391,7 +393,7 @@ sub updateDatabase { my $params = shift; my $user = shift; - my $eventDuration = getEventDuration($config, $params->{event_id}); + my $eventDuration = getEventDuration( $config, $params->{event_id} ); my $entry = { project_id => $params->{project_id}, @@ -402,6 +404,7 @@ sub updateDatabase { created_by => $user, eventDuration => $eventDuration }; + #print STDERR "updateDatabase:" . Dumper($entry); #connect @@ -425,14 +428,14 @@ sub updateDatabase { $params->{id} = $entry->{id}; } else { print STDERR "insert\n"; - $entry->{created_by} = $user; - $entry->{processed} = 0; - $entry->{mastered} = 0; - $entry->{rmsLeft} = 0.0; - $entry->{rmsRight} = 0.0; - $entry->{audioDuration} = 0.0; - $entry->{modified_at} = time(); - $params->{id} = audio_recordings::insert( $config, $dbh, $entry ); + $entry->{created_by} = $user; + $entry->{processed} = 0; + $entry->{mastered} = 0; + $entry->{rmsLeft} = 0.0; + $entry->{rmsRight} = 0.0; + $entry->{audioDuration} = 0.0; + $entry->{modified_at} = time(); + $params->{id} = audio_recordings::insert( $config, $dbh, $entry ); } $config->{access}->{write} = 0; $params->{action_result} = 'done!'; @@ -504,15 +507,15 @@ sub checkFilename { } # return event duration in seconds -sub getEventDuration{ - my $config = shift; - my $eventId = shift; +sub getEventDuration { + my $config = shift; + my $eventId = shift; + + if ( $eventId < 1 ) { + print STDERR "invalid eventId $eventId\n"; + return 0; + } - if ($eventId<1){ - print STDERR "invalid eventId $eventId\n"; - return 0; - } - my $request = { params => { checked => events::check_params( @@ -524,16 +527,16 @@ sub getEventDuration{ } ) }, - config => $config + config => $config }; - $request->{params}->{checked}->{published}='all'; - my $events = events::get( $config, $request ); - if (scalar @$events == 0){ - print STDERR "getEventDuration: no event found with event_id=$eventId\n"; + $request->{params}->{checked}->{published} = 'all'; + my $events = events::get( $config, $request ); + if ( scalar @$events == 0 ) { + print STDERR "getEventDuration: no event found with event_id=$eventId\n"; } - my $event = $events->[0]; - my $duration = time::get_duration_seconds( $event->{start}, $event->{end}, $config->{date}->{time_zone} ); - return $duration; + my $event = $events->[0]; + my $duration = time::get_duration_seconds( $event->{start}, $event->{end}, $config->{date}->{time_zone} ); + return $duration; } sub check_params { diff --git a/website/agenda/planung/calendar.cgi b/website/agenda/planung/calendar.cgi index 267877f..131a2ab 100644 --- a/website/agenda/planung/calendar.cgi +++ b/website/agenda/planung/calendar.cgi @@ -37,15 +37,15 @@ my $r = shift; my $config = config::get('../config/config.cgi'); my $debug = $config->{system}->{debug}; my ( $user, $expires ) = auth::get_user( $cgi, $config ); -return if ( ( !defined $user ) || ( $user eq '' ) ); +return if ( !defined $user ) || ( $user eq '' ); my $user_presets = uac::get_user_presets( - $config, - { - user => $user, - project_id => $params->{project_id}, - studio_id => $params->{studio_id} - } + $config, + { + user => $user, + project_id => $params->{project_id}, + studio_id => $params->{studio_id} + } ); $params->{default_studio_id} = $user_presets->{studio_id}; $params->{project_id} = $user_presets->{project_id} @@ -59,24 +59,24 @@ my $scriptName = 'calendar.cgi'; #add "all" studio to select box unshift @{ $user_presets->{studios} }, { - id => -1, - name => '-all-' + id => -1, + name => '-all-' }; # select studios, TODO: do in JS if ( $params->{studio_id} eq '-1' ) { - for my $studio ( @{ $user_presets->{studios} } ) { - delete $studio->{selected}; - $studio->{selected} = 1 if $params->{studio_id} eq $studio->{id}; - } + for my $studio ( @{ $user_presets->{studios} } ) { + delete $studio->{selected}; + $studio->{selected} = 1 if $params->{studio_id} eq $studio->{id}; + } } my $request = { - url => $ENV{QUERY_STRING} || '', - params => { - original => $params, - checked => check_params( $params, $config ), - }, + url => $ENV{QUERY_STRING} || '', + params => { + original => $params, + checked => check_params( $params, $config ), + }, }; $request = uac::prepare_request( $request, $user_presets ); @@ -85,21 +85,21 @@ $params = $request->{params}->{checked}; #print STDERR Dumper($request); if ( - ( - ( defined $params->{action} ) && ( ( $params->{action} eq 'show' ) - || ( $params->{action} eq 'edit_event' ) ) - ) - || ( $params->{part} == 1 ) + ( + ( defined $params->{action} ) && ( ( $params->{action} eq 'show' ) + || ( $params->{action} eq 'edit_event' ) ) + ) + || ( $params->{part} == 1 ) ) { - print "Content-type:text/html; charset=UTF-8;\n\n"; + print "Content-type:text/html; charset=UTF-8;\n\n"; } else { - #process header - my $headerParams = uac::set_template_permissions( $request->{permissions}, $params ); - $headerParams->{loc} = localization::get( $config, { user => $user, file => 'menu' } ); - template::process( 'print', template::check('default.html'), $headerParams ); - print q{ + #process header + my $headerParams = uac::set_template_permissions( $request->{permissions}, $params ); + $headerParams->{loc} = localization::get( $config, { user => $user, file => 'menu' } ); + template::process( 'print', template::check('default.html'), $headerParams ); + print q{ @@ -107,43 +107,43 @@ if ( }; - if ( $params->{list} eq '1' ) { - print q{ + if ( $params->{list} eq '1' ) { + print q{ }; - } + } } if ( defined $user_presets->{error} ) { - print "

"; - uac::print_error( $user_presets->{error} ); - return; + print "

"; + uac::print_error( $user_presets->{error} ); + return; } $config->{access}->{write} = 0; unless ( defined $params->{project_id} ) { - uac::print_error("Please select a project"); - return; + uac::print_error("Please select a project"); + return; } if ( $params->{project_id} ne '-1' ) { - if ( project::check( $config, { project_id => $params->{project_id} } ) ne '1' ) { - uac::print_error("invalid project"); - return; - } + if ( project::check( $config, { project_id => $params->{project_id} } ) ne '1' ) { + uac::print_error("invalid project"); + return; + } } unless ( defined $params->{studio_id} ) { - uac::print_error("Please select a studio"); - return; + uac::print_error("Please select a studio"); + return; } if ( $params->{studio_id} ne '-1' ) { - if ( studios::check( $config, { studio_id => $params->{studio_id} } ) ne '1' ) { - uac::print_error("invalid studio"); - return; - } + if ( studios::check( $config, { studio_id => $params->{studio_id} } ) ne '1' ) { + uac::print_error("invalid studio"); + return; + } } my $start_of_day = $params->{day_start}; @@ -153,291 +153,295 @@ our $hour_height = 60; our $yzoom = 1.5; showCalendar( - $config, $request, - { - hour_height => $hour_height, - yzoom => $yzoom, - start_of_day => $start_of_day, - end_of_day => $end_of_day, - } + $config, $request, + { + hour_height => $hour_height, + yzoom => $yzoom, + start_of_day => $start_of_day, + end_of_day => $end_of_day, + } ); sub showCalendar { - my $config = shift; - my $request = shift; - my $cal_options = shift; + my $config = shift; + my $request = shift; + my $cal_options = shift; - my $hour_height = $cal_options->{hour_height}; - my $yzoom = $cal_options->{yzoom}; - my $start_of_day = $cal_options->{start_of_day}; - my $end_of_day = $cal_options->{end_of_day}; + my $hour_height = $cal_options->{hour_height}; + my $yzoom = $cal_options->{yzoom}; + my $start_of_day = $cal_options->{start_of_day}; + my $end_of_day = $cal_options->{end_of_day}; - my $params = $request->{params}->{checked}; - my $permissions = $request->{permissions}; - unless ( $permissions->{read_series} == 1 ) { - uac::permissions_denied('read_series'); - return; - } + my $params = $request->{params}->{checked}; + my $permissions = $request->{permissions}; + unless ( $permissions->{read_series} == 1 ) { + uac::permissions_denied('read_series'); + return; + } - #get range from user settings - my $user_settings = user_settings::get( $config, { user => $params->{presets}->{user} } ); - $params->{range} = $user_settings->{range} unless defined $params->{range}; - $params->{range} = 28 unless defined $params->{range}; + #get range from user settings + my $user_settings = user_settings::get( $config, { user => $params->{presets}->{user} } ); + $params->{range} = $user_settings->{range} unless defined $params->{range}; + $params->{range} = 28 unless defined $params->{range}; - #get colors from user settings - print user_settings::getColorCss( $config, { user => $params->{presets}->{user} } ) if $params->{part} == 0; + #get colors from user settings + print user_settings::getColorCss( $config, { user => $params->{presets}->{user} } ) if $params->{part} == 0; - $params->{loc} = localization::get( $config, { user => $params->{presets}->{user}, file => 'all,calendar' } ); - my $language = $user_settings->{language} || 'en'; - $params->{language} = $language; - print localization::getJavascript( $params->{loc} ) if $params->{part} == 0; + $params->{loc} = localization::get( $config, { user => $params->{presets}->{user}, file => 'all,calendar' } ); + my $language = $user_settings->{language} || 'en'; + $params->{language} = $language; + print localization::getJavascript( $params->{loc} ) if $params->{part} == 0; - my $calendar = getCalendar( $config, $params, $language ); - my $options = {}; - my $events = []; + my $calendar = getCalendar( $config, $params, $language ); + my $options = {}; + my $events = []; - if ( ( $params->{part} == 1 ) || ( $params->{list} == 1 ) ) { + if ( ( $params->{part} == 1 ) || ( $params->{list} == 1 ) ) { - #set date range - my $from = $calendar->{from_date}; - my $till = $calendar->{till_date}; + #set date range + my $from = $calendar->{from_date}; + my $till = $calendar->{till_date}; - my $project_id = $params->{project_id}; - my $studio_id = $params->{studio_id}; + my $project_id = $params->{project_id}; + my $studio_id = $params->{studio_id}; - #build event filter - $options = { - project_id => $project_id, - template => 'no', - limit => 600, - get => 'no_content', - from_date => $from, - till_date => $till, - date_range_include => 1, - archive => 'all', - }; + #build event filter + $options = { + project_id => $project_id, + template => 'no', + limit => 600, + get => 'no_content', + from_date => $from, + till_date => $till, + date_range_include => 1, + archive => 'all', + }; - # set options depending on switches - if ( $params->{studio_id} ne '-1' ) { - $options->{studio_id} = $studio_id; - my $location = $params->{presets}->{studio}->{location}; - $options->{location} = $location if $location =~ /\S/; - } + # set options depending on switches + if ( $params->{studio_id} ne '-1' ) { + $options->{studio_id} = $studio_id; + my $location = $params->{presets}->{studio}->{location}; + $options->{location} = $location if $location =~ /\S/; + } - if ( $params->{project_id} ne '-1' ) { - $options->{project_id} = $project_id; - my $project = $params->{presets}->{project}->{name}; - $options->{project} = $project if $project =~ /\S/; - } + if ( $params->{project_id} ne '-1' ) { + $options->{project_id} = $project_id; + my $project = $params->{presets}->{project}->{name}; + $options->{project} = $project if $project =~ /\S/; + } - if ( defined $params->{series_id} ) { - $options->{series_id} = $params->{series_id}; - delete $options->{from_date}; - delete $options->{till_date}; - delete $options->{date_range_include}; - } + if ( defined $params->{series_id} ) { + $options->{series_id} = $params->{series_id}; + delete $options->{from_date}; + delete $options->{till_date}; + delete $options->{date_range_include}; + } - if ( $params->{search} =~ /\S/ ) { - $options->{search} = $params->{search}; - delete $options->{from_date} if ( $params->{list} == 1 ); - delete $options->{till_date} if ( $params->{list} == 1 ); - delete $options->{date_range_include} if ( $params->{list} == 1 ); - } + if ( $params->{search} =~ /\S/ ) { + if ( $params->{list} == 1 ) { + $options->{search} = $params->{search}; + delete $options->{from_date}; + delete $options->{till_date}; + delete $options->{date_range_include}; + } + } - $options->{draft} = 0 unless $params->{list} == 1; + $options->{draft} = 0 unless $params->{list} == 1; - #get events sorted by date - $events = getSeriesEvents( $config, $request, $options, $params ); - unless ( $params->{list} == 1 ) { - for my $event (@$events) { - $event->{origStart} = $event->{start}; - } - $events = break_dates( $events, $start_of_day ); - } + #get events sorted by date + $events = getSeriesEvents( $config, $request, $options, $params ); + unless ( $params->{list} == 1 ) { + for my $event (@$events) { + $event->{origStart} = $event->{start}; + } + $events = break_dates( $events, $start_of_day ); + } - # recalc after break (for list only?) - for my $event (@$events) { + # recalc after break (for list only?) + for my $event (@$events) { - #if ($event->{splitCount}>0){ - delete $event->{day}; - delete $event->{start_date}; - delete $event->{end_date}; - $event = events::calc_dates( $config, $event ); + #if ($event->{splitCount}>0){ + delete $event->{day}; + delete $event->{start_date}; + delete $event->{end_date}; + $event = events::calc_dates( $config, $event ); - #} - } + #} + } - my $events_by_start = {}; - for my $event (@$events) { - $events_by_start->{ $event->{start} } = $event; - } + my $events_by_start = {}; + for my $event (@$events) { + $events_by_start->{ $event->{start} } = $event; + } - #build series filter - $options = { - project_id => $project_id, - studio_id => $studio_id, - from => $from, - till => $till, - date_range_include => 1, - exclude => 0 - }; + #build series filter + $options = { + project_id => $project_id, + studio_id => $studio_id, + from => $from, + till => $till, + date_range_include => 1, + exclude => 0 + }; - if ( defined $params->{series_id} ) { - $options->{series_id} = $params->{series_id}; - delete $options->{from}; - delete $options->{till}; - delete $options->{date_range_include}; - } + if ( defined $params->{series_id} ) { + $options->{series_id} = $params->{series_id}; + delete $options->{from}; + delete $options->{till}; + delete $options->{date_range_include}; + } - if ( $params->{search} =~ /\S/ ) { - $options->{search} = $params->{search}; - delete $options->{from} if ( $params->{list} == 1 ); - delete $options->{till} if ( $params->{list} == 1 ); - delete $options->{date_range_include} if ( $params->{list} == 1 ); - } + if ( $params->{search} =~ /\S/ ) { + $options->{search} = $params->{search}; + if ( $params->{list} == 1 ) { + delete $options->{from}; + delete $options->{till}; + delete $options->{date_range_include}; + } + } - #get all series dates - my $series_dates = series_dates::get_series( $config, $options ); - my $id = 0; - for my $date (@$series_dates) { - $date->{schedule} = 1; + #get all series dates + my $series_dates = series_dates::get_series( $config, $options ); + my $id = 0; + for my $date (@$series_dates) { + $date->{schedule} = 1; - #$date->{event_id}=-1; - $date->{event_id} = $id; - $date->{origStart} = $date->{start}; - delete $date->{day}; - delete $date->{start_date}; - delete $date->{end_date}; - $id++; - } - unless ( $params->{list} == 1 ) { - $series_dates = break_dates( $series_dates, $start_of_day ); - } + #$date->{event_id}=-1; + $date->{event_id} = $id; + $date->{origStart} = $date->{start}; + delete $date->{day}; + delete $date->{start_date}; + delete $date->{end_date}; + $id++; + } + unless ( $params->{list} == 1 ) { + $series_dates = break_dates( $series_dates, $start_of_day ); + } - #merge series and events - for my $date (@$series_dates) { - $date = events::calc_dates( $config, $date ); - push @$events, $date; - } + #merge series and events + for my $date (@$series_dates) { + $date = events::calc_dates( $config, $date ); + push @$events, $date; + } - #get timeslot_dates - my $studio_dates = studio_timeslot_dates::get( $config, $options ); + #get timeslot_dates + my $studio_dates = studio_timeslot_dates::get( $config, $options ); - #print STDERR Dumper($options); - $id = 0; - for my $date (@$studio_dates) { - $date->{grid} = 1; - $date->{series_id} = -1; + #print STDERR Dumper($options); + $id = 0; + for my $date (@$studio_dates) { + $date->{grid} = 1; + $date->{series_id} = -1; - #$date->{event_id}=-1; - $date->{event_id} = $id; - $date->{origStart} = $date->{start}; - delete $date->{day}; - delete $date->{start_date}; - delete $date->{end_date}; - $id++; - } - unless ( $params->{list} == 1 ) { - $studio_dates = break_dates( $studio_dates, $start_of_day ); - } + #$date->{event_id}=-1; + $date->{event_id} = $id; + $date->{origStart} = $date->{start}; + delete $date->{day}; + delete $date->{start_date}; + delete $date->{end_date}; + $id++; + } + unless ( $params->{list} == 1 ) { + $studio_dates = break_dates( $studio_dates, $start_of_day ); + } - for my $date (@$studio_dates) { - $date = events::calc_dates( $config, $date ); - push @$events, $date; - } + for my $date (@$studio_dates) { + $date = events::calc_dates( $config, $date ); + push @$events, $date; + } - #get work_dates - my $work_dates = work_dates::get( $config, $options ); - for my $date (@$work_dates) { - $date->{work} = 1; - $date->{series_id} = -1; - $date->{event_id} = -1; - $date->{origStart} = $date->{start}; - delete $date->{day}; - delete $date->{start_date}; - delete $date->{end_date}; - } - unless ( $params->{list} == 1 ) { - $work_dates = break_dates( $work_dates, $start_of_day ); - } + #get work_dates + my $work_dates = work_dates::get( $config, $options ); + for my $date (@$work_dates) { + $date->{work} = 1; + $date->{series_id} = -1; + $date->{event_id} = -1; + $date->{origStart} = $date->{start}; + delete $date->{day}; + delete $date->{start_date}; + delete $date->{end_date}; + } + unless ( $params->{list} == 1 ) { + $work_dates = break_dates( $work_dates, $start_of_day ); + } - for my $date (@$work_dates) { - $date = events::calc_dates( $config, $date ); - push @$events, $date; - } + for my $date (@$work_dates) { + $date = events::calc_dates( $config, $date ); + push @$events, $date; + } - #get playout - delete $options->{exclude}; - my $playout_dates = playout::get( $config, $options ); - $id = 0; - for my $date (@$playout_dates) { - my $format = undef; - if ( defined $date->{'format'} ) { - $format = - ( $date->{'format'} || '' ) . " " . ( $date->{'format_version'} || '' ) . " " . ( $date->{'format_profile'} || '' ); - $format =~ s/MPEG Audio Version 1 Layer 3/MP3/g; - $format .= ' ' . ( $date->{'format_settings'} || '' ) if defined $date->{'format_settings'}; - $format .= '
'; - } + #get playout + delete $options->{exclude}; + my $playout_dates = playout::get( $config, $options ); + $id = 0; + for my $date (@$playout_dates) { + my $format = undef; + if ( defined $date->{'format'} ) { + $format = + ( $date->{'format'} || '' ) . " " . ( $date->{'format_version'} || '' ) . " " . ( $date->{'format_profile'} || '' ); + $format =~ s/MPEG Audio Version 1 Layer 3/MP3/g; + $format .= ' ' . ( $date->{'format_settings'} || '' ) if defined $date->{'format_settings'}; + $format .= '
'; + } - #print STDERR Dumper($date); - $date->{play} = 1; - $date->{series_id} = -1; - $date->{event_id} = $id; - $date->{title} = ''; - $date->{title} .= 'errors: ' . $date->{errors} . '
' if defined $date->{errors}; - $date->{title} .= formatDuration( $date->{duration} ) . "s
" if defined $date->{duration}; - $date->{title} .= formatLoudness( "L:", $date->{rms_left} ) . ' dB, ' if defined $date->{rms_left}; - $date->{title} .= formatLoudness( "R:", $date->{rms_right} ) . ' dB
' if defined $date->{rms_right}; - $date->{title} .= formatBitrate( $date->{bitrate} ) . ' ' . $date->{bitrate_mode} . '
' if defined $date->{bitrate}; - $date->{title} .= 'replay gain ' . sprintf( "%.1f", $date->{replay_gain} ) . '
' if defined $date->{replay_gain}; - $date->{title} .= ( ( $date->{sampling_rate} || '0' ) / 1000 ) . ' kHz
' if defined $date->{sampling_rate}; - $date->{title} .= ( $date->{channels} || '' ) . ' channels
' if defined $date->{channels}; - $date->{title} .= int( ( $date->{'stream_size'} || '0' ) / ( 1024 * 1024 ) ) . 'MB
' if defined $date->{'stream_size'}; - $date->{title} .= $format if defined $format; - $date->{title} .= 'library: ' . ( $date->{writing_library} || '' ) . '
' if defined $date->{'writing_library'}; - $date->{title} .= 'path: ' . ( $date->{file} || '' ) . '
' if defined $date->{file}; - $date->{title} .= 'updated_at: ' . ( $date->{updated_at} || '' ) . '
' if defined $date->{updated_at}; - $date->{title} .= 'modified_at: ' . ( $date->{modified_at} || '' ) . '
' if defined $date->{modified_at}; + #print STDERR Dumper($date); + $date->{play} = 1; + $date->{series_id} = -1; + $date->{event_id} = $id; + $date->{title} = ''; + $date->{title} .= 'errors: ' . $date->{errors} . '
' if defined $date->{errors}; + $date->{title} .= formatDuration( $date->{duration} ) . "s
" if defined $date->{duration}; + $date->{title} .= formatLoudness( "L:", $date->{rms_left} ) . ' dB, ' if defined $date->{rms_left}; + $date->{title} .= formatLoudness( "R:", $date->{rms_right} ) . ' dB
' if defined $date->{rms_right}; + $date->{title} .= formatBitrate( $date->{bitrate} ) . ' ' . $date->{bitrate_mode} . '
' if defined $date->{bitrate}; + $date->{title} .= 'replay gain ' . sprintf( "%.1f", $date->{replay_gain} ) . '
' if defined $date->{replay_gain}; + $date->{title} .= ( ( $date->{sampling_rate} || '0' ) / 1000 ) . ' kHz
' if defined $date->{sampling_rate}; + $date->{title} .= ( $date->{channels} || '' ) . ' channels
' if defined $date->{channels}; + $date->{title} .= int( ( $date->{'stream_size'} || '0' ) / ( 1024 * 1024 ) ) . 'MB
' if defined $date->{'stream_size'}; + $date->{title} .= $format if defined $format; + $date->{title} .= 'library: ' . ( $date->{writing_library} || '' ) . '
' if defined $date->{'writing_library'}; + $date->{title} .= 'path: ' . ( $date->{file} || '' ) . '
' if defined $date->{file}; + $date->{title} .= 'updated_at: ' . ( $date->{updated_at} || '' ) . '
' if defined $date->{updated_at}; + $date->{title} .= 'modified_at: ' . ( $date->{modified_at} || '' ) . '
' if defined $date->{modified_at}; - #print STDERR Dumper($date) if $date->{file}=~/180503/; - #$date->{title}.= 'rms_image: ' .($date->{rms_image}||'').'
' if defined $date->{rms_image}; + #print STDERR Dumper($date) if $date->{file}=~/180503/; + #$date->{title}.= 'rms_image: ' .($date->{rms_image}||'').'
' if defined $date->{rms_image}; - $date->{rms_image} = URI::Escape::uri_unescape( $date->{rms_image} ) if defined $date->{rms_image}; + $date->{rms_image} = URI::Escape::uri_unescape( $date->{rms_image} ) if defined $date->{rms_image}; - $date->{origStart} = $date->{start}; + $date->{origStart} = $date->{start}; - # set end date seconds to 00 to handle error at break_dates/join_dates - $date->{end} =~ s/(\d\d\:\d\d)\:\d\d/$1\:00/; - delete $date->{day}; - delete $date->{start_date}; - delete $date->{end_date}; - $id++; - } + # set end date seconds to 00 to handle error at break_dates/join_dates + $date->{end} =~ s/(\d\d\:\d\d)\:\d\d/$1\:00/; + delete $date->{day}; + delete $date->{start_date}; + delete $date->{end_date}; + $id++; + } - unless ( $params->{list} == 1 ) { - $playout_dates = break_dates( $playout_dates, $start_of_day ); - } + unless ( $params->{list} == 1 ) { + $playout_dates = break_dates( $playout_dates, $start_of_day ); + } - for my $date (@$playout_dates) { - $date = events::calc_dates( $config, $date ); - if ( defined $events_by_start->{ $date->{start} } ) { - $events_by_start->{ $date->{start} }->{duration} = $date->{duration} || 0; - $events_by_start->{ $date->{start} }->{rms_left} = $date->{rms_left} || 0; - $events_by_start->{ $date->{start} }->{rms_right} = $date->{rms_right} || 0; - $events_by_start->{ $date->{start} }->{playout_modified_at} = $date->{modified_at}; - $events_by_start->{ $date->{start} }->{playout_updated_at} = $date->{updated_at}; - } - push @$events, $date; - } + for my $date (@$playout_dates) { + $date = events::calc_dates( $config, $date ); + if ( defined $events_by_start->{ $date->{start} } ) { + $events_by_start->{ $date->{start} }->{duration} = $date->{duration} || 0; + $events_by_start->{ $date->{start} }->{rms_left} = $date->{rms_left} || 0; + $events_by_start->{ $date->{start} }->{rms_right} = $date->{rms_right} || 0; + $events_by_start->{ $date->{start} }->{playout_modified_at} = $date->{modified_at}; + $events_by_start->{ $date->{start} }->{playout_updated_at} = $date->{updated_at}; + } + push @$events, $date; + } - } + } - #output - printToolbar( $config, $params, $calendar ) if $params->{part} == 0; + #output + printToolbar( $config, $params, $calendar ) if $params->{part} == 0; - #if($params->{part}==1){ - print qq{ + #if($params->{part}==1){ + print qq{ }; - #} + #} - #filter events by time - unless ( $params->{list} == 1 ) { - $events = filterEvents( $events, $options, $start_of_day ); - } + #filter events by time + unless ( $params->{list} == 1 ) { + $events = filterEvents( $events, $options, $start_of_day ); + } - #sort events by start - @$events = sort { $a->{start} cmp $b->{start} } @$events; + #sort events by start + @$events = sort { $a->{start} cmp $b->{start} } @$events; - #for my $date(@$events){debugDate($date);} + #for my $date(@$events){debugDate($date);} - #separate by day (e.g. to 6 pm) - my $events_by_day = {}; - for my $event (@$events) { - my $day = time::datetime_to_date( time::add_hours_to_datetime( $event->{start}, -$start_of_day ) ); - push @{ $events_by_day->{$day} }, $event; - } + #separate by day (e.g. to 6 pm) + my $events_by_day = {}; + for my $event (@$events) { + my $day = time::datetime_to_date( time::add_hours_to_datetime( $event->{start}, -$start_of_day ) ); + push @{ $events_by_day->{$day} }, $event; + } - #get min and max hour from all events - unless ( $params->{list} == 1 ) { - my $min_hour = 48; - my $max_hour = 0; + #get min and max hour from all events + unless ( $params->{list} == 1 ) { + my $min_hour = 48; + my $max_hour = 0; - for my $event (@$events) { - if ( $event->{start} =~ /(\d\d)\:\d\d\:\d\d$/ ) { - my $hour = $1; - $hour += 24 if $hour < $start_of_day; - $min_hour = $hour if ( ( $hour < $min_hour ) && ( $hour >= $start_of_day ) ); - } - if ( $event->{end} =~ /(\d\d)\:\d\d\:\d\d$/ ) { - my $hour = $1; - $hour += 24 if $hour <= $start_of_day; - $max_hour = $hour if ( ( $hour > $max_hour ) && ( $hour <= $end_of_day ) ); - } - } - $cal_options->{min_hour} = $min_hour; - $cal_options->{max_hour} = $max_hour; - } + for my $event (@$events) { + if ( $event->{start} =~ /(\d\d)\:\d\d\:\d\d$/ ) { + my $hour = $1; + $hour += 24 if $hour < $start_of_day; + $min_hour = $hour if ( $hour < $min_hour ) && ( $hour >= $start_of_day ); + } + if ( $event->{end} =~ /(\d\d)\:\d\d\:\d\d$/ ) { + my $hour = $1; + $hour += 24 if $hour <= $start_of_day; + $max_hour = $hour if ( $hour > $max_hour ) && ( $hour <= $end_of_day ); + } + } + $cal_options->{min_hour} = $min_hour; + $cal_options->{max_hour} = $max_hour; + } - #print STDERR $start_of_day." ".$cal_options->{min_hour}."\n"; + #print STDERR $start_of_day." ".$cal_options->{min_hour}."\n"; - # calculate positions and find schedule errors (depending on position) - for my $date ( sort ( keys %$events_by_day ) ) { - my $events = $events_by_day->{$date}; - calc_positions( $events, $cal_options ); - find_errors($events); - } + # calculate positions and find schedule errors (depending on position) + for my $date ( sort ( keys %$events_by_day ) ) { + my $events = $events_by_day->{$date}; + calc_positions( $events, $cal_options ); + find_errors($events); + } - for my $event (@$events) { - next unless defined $event->{uploaded_at}; + for my $event (@$events) { + next unless defined $event->{uploaded_at}; #print STDERR "uploadAt=$event->{uploaded_at}, playoutModified:$event->{playout_modified_at}, playoutUpdatedAt:$event->{playout_updated_at}\n"; - next if ( defined $event->{playout_updated_at} ) && ( $event->{uploaded_at} lt $event->{playout_updated_at} ); + next if ( defined $event->{playout_updated_at} ) && ( $event->{uploaded_at} lt $event->{playout_updated_at} ); - #print STDERR Dumper($event); - #$event->{upload} ='pending' ; - #$event->{title}.='
pending'; - } + #print STDERR Dumper($event); + #$event->{upload} ='pending' ; + #$event->{title}.='
pending'; + } - if ( $params->{list} == 1 ) { - showEventList( $config, $permissions, $params, $events_by_day ); - } else { - if ( $params->{part} == 0 ) { - print qq{
}; - } - if ( $params->{part} == 1 ) { - calcCalendarTable( $config, $permissions, $params, $calendar, $events_by_day, $cal_options ); - printTableHeader( $config, $permissions, $params, $cal_options ); - printTableBody( $config, $permissions, $params, $cal_options ); - } - if ( $params->{part} == 0 ) { - printSeries( $config, $permissions, $params, $cal_options ); - print qq{ + if ( $params->{list} == 1 ) { + showEventList( $config, $permissions, $params, $events_by_day ); + } else { + if ( $params->{part} == 0 ) { + print qq{
}; + } + if ( $params->{part} == 1 ) { + calcCalendarTable( $config, $permissions, $params, $calendar, $events_by_day, $cal_options ); + printTableHeader( $config, $permissions, $params, $cal_options ); + printTableBody( $config, $permissions, $params, $cal_options ); + } + if ( $params->{part} == 0 ) { + printSeries( $config, $permissions, $params, $cal_options ); + print qq{ }; - } + } - # time has to be set when events come in - printJavascript( $config, $permissions, $params, $cal_options ); - if ( $params->{part} == 0 ) { - print qq{ + # time has to be set when events come in + printJavascript( $config, $permissions, $params, $cal_options ); + if ( $params->{part} == 0 ) { + print qq{ }; - } - if ( $params->{part} == 1 ) { - } - } + } + } } sub formatLoudness { - my $label = shift; - my $value = shift; - return '' unless defined $value; - return '' if $value == 0; - return '' if $value eq ''; + my $label = shift; + my $value = shift; + return '' unless defined $value; + return '' if $value == 0; + return '' if $value eq ''; - #print STDERR "'$value'\n"; - $value = sprintf( "%.1f", $value ); - my $class = 'ok'; - $class = 'warn' if $value > -18.5; - $class = 'error' if $value > -16.0; - $class = 'warn' if $value < -24.0; - $class = 'error' if $value < -27.0; - return qq{$label} . $value . qq{}; + #print STDERR "'$value'\n"; + $value = sprintf( "%.1f", $value ); + my $class = 'ok'; + $class = 'warn' if $value > -18.5; + $class = 'error' if $value > -16.0; + $class = 'warn' if $value < -24.0; + $class = 'error' if $value < -27.0; + return qq{$label} . $value . qq{}; } sub formatDuration { - my $duration = shift; - return '' unless defined $duration; - return '' if $duration eq ''; - my $result = int( ( $duration + 3600 ) * 10 + 0.5 ) % 600; - my $class = "ok"; - $class = "warn" if $result > 1; - $class = "error" if $result > 10; - return sprintf( qq{%.01f}, $class, $duration ); + my $duration = shift; + return '' unless defined $duration; + return '' if $duration eq ''; + my $result = int( ( $duration + 3600 ) * 10 + 0.5 ) % 600; + my $class = "ok"; + $class = "warn" if $result > 1; + $class = "error" if $result > 10; + return sprintf( qq{%.01f}, $class, $duration ); } sub formatBitrate { - my $bitrate = shift; - return '' if $bitrate eq ''; - if ( $bitrate >= 200 ) { - return qq{$bitrate}; - } elsif ( $bitrate < 190 ) { - return qq{$bitrate}; - } else { - return qq{$bitrate}; - } + my $bitrate = shift; + return '' if $bitrate eq ''; + if ( $bitrate >= 200 ) { + return qq{$bitrate}; + } elsif ( $bitrate < 190 ) { + return qq{$bitrate}; + } else { + return qq{$bitrate}; + } } sub debugDate { - my $date = shift; - $date->{program} = '' unless defined $date->{program}; - $date->{series_name} = '' unless defined $date->{series_name}; - $date->{title} = '' unless defined $date->{title}; - $date->{splitCount} = 0 unless defined $date->{splitCount}; - my $dt = ( $date->{start} || '' ) . " " . ( $date->{end} | '' ); - my $da = ( $date->{start_date} || '' ) . " " . ( $date->{end_date} || '' ); - my $type = "schedule:" . ( $date->{schedule} || "" ) . " grid:" . ( $date->{grid} || "" ); + my $date = shift; + $date->{program} = '' unless defined $date->{program}; + $date->{series_name} = '' unless defined $date->{series_name}; + $date->{title} = '' unless defined $date->{title}; + $date->{splitCount} = 0 unless defined $date->{splitCount}; + my $dt = ( $date->{start} || '' ) . " " . ( $date->{end} | '' ); + my $da = ( $date->{start_date} || '' ) . " " . ( $date->{end_date} || '' ); + my $type = "schedule:" . ( $date->{schedule} || "" ) . " grid:" . ( $date->{grid} || "" ); - #print STDERR "$dt $da count:$date->{splitCount} $type $date->{program}-$date->{series_name}-$date->{title}\n"; + #print STDERR "$dt $da count:$date->{splitCount} $type $date->{program}-$date->{series_name}-$date->{title}\n"; } # break dates at start_of_day sub break_dates { - my $dates = shift; - my $start_of_day = shift; + my $dates = shift; + my $start_of_day = shift; - #return $dates if $start_of_day eq '0'; + #return $dates if $start_of_day eq '0'; - for my $date (@$dates) { - next unless defined $date; + for my $date (@$dates) { + next unless defined $date; - $date->{splitCount} = 0 unless defined $date->{splitCount}; + $date->{splitCount} = 0 unless defined $date->{splitCount}; - #debugDate($date); + #debugDate($date); - next if $date->{splitCount} > 6; - my $nextDayStart = breaks_day( $date->{start}, $date->{end}, $start_of_day ); - next if $nextDayStart eq '0'; + next if $date->{splitCount} > 6; + my $nextDayStart = breaks_day( $date->{start}, $date->{end}, $start_of_day ); + next if $nextDayStart eq '0'; - # add new entry - my $entry = {}; - for my $key ( keys %$date ) { - $entry->{$key} = $date->{$key}; - } - $entry->{start} = $nextDayStart; - $entry->{splitCount}++; - push @$dates, $entry; + # add new entry + my $entry = {}; + for my $key ( keys %$date ) { + $entry->{$key} = $date->{$key}; + } + $entry->{start} = $nextDayStart; + $entry->{splitCount}++; + push @$dates, $entry; # print STDERR "add $entry->{start} $entry->{end} count:$entry->{splitCount} $entry->{program}-$entry->{series_name}-$entry->{title}\n"; - #modify existing entry - my $start_date = time::datetime_to_date( $date->{start} ); - $date->{end} = $nextDayStart; - $date->{splitCount}++; + #modify existing entry + my $start_date = time::datetime_to_date( $date->{start} ); + $date->{end} = $nextDayStart; + $date->{splitCount}++; # print STDERR "set $date->{start} $date->{end} count:$date->{splitCount} $date->{program}-$date->{series_name}-$date->{title}\n"; - } + } - return join_dates( $dates, $start_of_day ); + return join_dates( $dates, $start_of_day ); } # check if event breaks the start of day (e.g. 06:00) sub breaks_day { - my $start = shift; - my $end = shift; - my $start_of_day = shift; + my $start = shift; + my $end = shift; + my $start_of_day = shift; - my $starts = time::datetime_to_array($start); - my $startDate = time::array_to_date($starts); - my $startTime = time::array_to_time($starts); - $start = $startDate . ' ' . $startTime; + my $starts = time::datetime_to_array($start); + my $startDate = time::array_to_date($starts); + my $startTime = time::array_to_time($starts); + $start = $startDate . ' ' . $startTime; - my $ends = time::datetime_to_array($end); - my $endDate = time::array_to_date($ends); - my $endTime = time::array_to_time($ends); - $end = $endDate . ' ' . $endTime; + my $ends = time::datetime_to_array($end); + my $endDate = time::array_to_date($ends); + my $endTime = time::array_to_time($ends); + $end = $endDate . ' ' . $endTime; - my $dayStartTime = time::array_to_time($start_of_day); - my $dayStart = $startDate . ' ' . $dayStartTime; + my $dayStartTime = time::array_to_time($start_of_day); + my $dayStart = $startDate . ' ' . $dayStartTime; - # start before 6:00 of same day - return $dayStart if ( $start lt $dayStart ) && ( $end gt $dayStart ); + # start before 6:00 of same day + return $dayStart if ( $start lt $dayStart ) && ( $end gt $dayStart ); - # start before 6:00 of next day - my $nextDayStart = time::add_days_to_datetime( $dayStart, 1 ); + # start before 6:00 of next day + my $nextDayStart = time::add_days_to_datetime( $dayStart, 1 ); - #$nextDayStart=~s/:00$//; - return $nextDayStart if ( $start lt $nextDayStart ) && ( $end gt $nextDayStart ); + #$nextDayStart=~s/:00$//; + return $nextDayStart if ( $start lt $nextDayStart ) && ( $end gt $nextDayStart ); - return 0; + return 0; } # merge events with same seriesId and eventId at 00:00 sub join_dates { - my $dates = shift; - my $start_of_day = shift; + my $dates = shift; + my $start_of_day = shift; - return $dates if $start_of_day == 0; - @$dates = sort { $a->{start} cmp $b->{start} } @$dates; + return $dates if $start_of_day == 0; + @$dates = sort { $a->{start} cmp $b->{start} } @$dates; - my $prev_date = undef; - for my $date (@$dates) { - next unless defined $date; - unless ( defined $prev_date ) { - $prev_date = $date; - next; - } - if ( ( $date->{event_id} == $prev_date->{event_id} ) - && ( $date->{series_id} == $prev_date->{series_id} ) - && ( $date->{start} eq $prev_date->{end} ) - && ( $date->{start} =~ /00\:00\:\d\d/ ) ) - { - $prev_date->{end} = $date->{end}; - $date = undef; - next; - } - $prev_date = $date; - } + my $prev_date = undef; + for my $date (@$dates) { + next unless defined $date; + unless ( defined $prev_date ) { + $prev_date = $date; + next; + } + if ( ( $date->{event_id} == $prev_date->{event_id} ) + && ( $date->{series_id} == $prev_date->{series_id} ) + && ( $date->{start} eq $prev_date->{end} ) + && ( $date->{start} =~ /00\:00\:\d\d/ ) ) + { + $prev_date->{end} = $date->{end}; + $date = undef; + next; + } + $prev_date = $date; + } - my $results = []; - for my $date (@$dates) { - next unless defined $date; - push @$results, $date; - } + my $results = []; + for my $date (@$dates) { + next unless defined $date; + push @$results, $date; + } - return $results; + return $results; } sub filterEvents { - my $events = shift; - my $options = shift; - my $start_of_day = shift; + my $events = shift; + my $options = shift; + my $start_of_day = shift; - return [] unless defined $options->{from}; - return [] unless defined $options->{till}; + return [] unless defined $options->{from}; + return [] unless defined $options->{till}; - my $dayStartTime = time::array_to_time($start_of_day); - my $startDatetime = $options->{from} . ' ' . $dayStartTime; - my $endDatetime = $options->{till} . ' ' . $dayStartTime; + my $dayStartTime = time::array_to_time($start_of_day); + my $startDatetime = $options->{from} . ' ' . $dayStartTime; + my $endDatetime = $options->{till} . ' ' . $dayStartTime; - my $results = []; - for my $date (@$events) { - next if ( ( $date->{start} ge $endDatetime ) || ( $date->{end} le $startDatetime ) ); - push @$results, $date; - } - return $results; + my $results = []; + for my $date (@$events) { + next if ( ( $date->{start} ge $endDatetime ) || ( $date->{end} le $startDatetime ) ); + push @$results, $date; + } + return $results; } sub showEventList { - my $config = shift; - my $permissions = shift; - my $params = shift; - my $events_by_day = shift; - my $language = $params->{language}; + my $config = shift; + my $permissions = shift; + my $params = shift; + my $events_by_day = shift; + my $language = $params->{language}; - print qq{ + my $out = ''; + $out = qq{
@@ -745,120 +748,123 @@ sub showEventList { } if $params->{part} == 0; - my $i = 1; + my $i = 1; - my $scheduled_events = {}; - for my $date ( reverse sort ( keys %$events_by_day ) ) { - for my $event ( reverse @{ $events_by_day->{$date} } ) { - next unless defined $event; - next if defined $event->{grid}; - next if defined $event->{work}; - next if defined $event->{play}; + my $scheduled_events = {}; + for my $date ( reverse sort ( keys %$events_by_day ) ) { + for my $event ( reverse @{ $events_by_day->{$date} } ) { + next unless defined $event; + next if defined $event->{grid}; + next if defined $event->{work}; + next if defined $event->{play}; - #schedules with matching date are marked to be hidden in find_errors - next if defined $event->{hide}; - $event->{project_id} = $params->{project_id}; - $event->{studio_id} = $params->{studio_id}; - $event->{series_id} = '-1' unless defined $event->{series_id}; - $event->{event_id} = '-1' unless defined $event->{event_id}; - my $id = 'event_' . $event->{project_id} . '_' . $event->{studio_id} . '_' . $event->{series_id} . '_' . $event->{event_id}; + #schedules with matching date are marked to be hidden in find_errors + next if defined $event->{hide}; + $event->{project_id} = $params->{project_id}; + $event->{studio_id} = $params->{studio_id}; + $event->{series_id} = '-1' unless defined $event->{series_id}; + $event->{event_id} = '-1' unless defined $event->{event_id}; + my $id = 'event_' . $event->{project_id} . '_' . $event->{studio_id} . '_' . $event->{series_id} . '_' . $event->{event_id}; - my $class = 'event'; - $class = $event->{class} if defined $event->{class}; - $class = 'schedule' if defined $event->{schedule}; - if ( $class =~ /(event|schedule)/ ) { - $class .= ' scheduled' if defined $event->{scheduled}; - $class .= ' error' if defined $event->{error}; - $class .= ' no_series' if ( ( $class eq 'event' ) && ( $event->{series_id} eq '-1' ) ); + my $class = 'event'; + $class = $event->{class} if defined $event->{class}; + $class = 'schedule' if defined $event->{schedule}; + if ( $class =~ /(event|schedule)/ ) { + $class .= ' scheduled' if defined $event->{scheduled}; + $class .= ' error' if defined $event->{error}; + $class .= ' no_series' if ( ( $class eq 'event' ) && ( $event->{series_id} eq '-1' ) ); - for my $filter ( 'rerun', 'archived', 'playout', 'published', 'live', 'disable_event_sync', 'draft' ) { - $class .= ' ' . $filter if ( ( defined $event->{$filter} ) && ( $event->{$filter} eq '1' ) ); - } - $class .= ' preproduced' unless ( ( defined $event->{'live'} ) && ( $event->{'live'} eq '1' ) ); - $class .= ' no_playout' unless ( ( defined $event->{'playout'} ) && ( $event->{'playout'} eq '1' ) ); - $class .= ' no_rerun' unless ( ( defined $event->{'rerun'} ) && ( $event->{'rerun'} eq '1' ) ); - } + for my $filter ( 'rerun', 'archived', 'playout', 'published', 'live', 'disable_event_sync', 'draft' ) { + $class .= ' ' . $filter if ( ( defined $event->{$filter} ) && ( $event->{$filter} eq '1' ) ); + } + $class .= ' preproduced' unless ( ( defined $event->{'live'} ) && ( $event->{'live'} eq '1' ) ); + $class .= ' no_playout' unless ( ( defined $event->{'playout'} ) && ( $event->{'playout'} eq '1' ) ); + $class .= ' no_rerun' unless ( ( defined $event->{'rerun'} ) && ( $event->{'rerun'} eq '1' ) ); + } - $event->{start} ||= ''; - $event->{weekday_short_name} ||= ''; - $event->{start_date_name} ||= ''; - $event->{start_time_name} ||= ''; - $event->{end_time} ||= ''; - $event->{series_name} ||= ''; - $event->{title} ||= ''; - $event->{user_title} ||= ''; - $event->{episode} ||= ''; - $event->{rerun} ||= ''; - $event->{draft} ||= ''; - $id ||= ''; - $class ||= ''; + $event->{start} ||= ''; + $event->{weekday_short_name} ||= ''; + $event->{start_date_name} ||= ''; + $event->{start_time_name} ||= ''; + $event->{end_time} ||= ''; + $event->{series_name} ||= ''; + $event->{title} ||= ''; + $event->{user_title} ||= ''; + $event->{episode} ||= ''; + $event->{rerun} ||= ''; + $event->{draft} ||= ''; + $id ||= ''; + $class ||= ''; - my $archived = $event->{archived} || '-'; - $archived = '-' if $archived eq '0'; - $archived = 'x' if $archived eq '1'; + my $archived = $event->{archived} || '-'; + $archived = '-' if $archived eq '0'; + $archived = 'x' if $archived eq '1'; - my $live = $event->{live} || '-'; - $live = '-' if $live eq '0'; - $live = 'x' if $live eq '1'; + my $live = $event->{live} || '-'; + $live = '-' if $live eq '0'; + $live = 'x' if $live eq '1'; - my $rerun = $event->{rerun} || '-'; + my $rerun = $event->{rerun} || '-'; - #$rerun='-' if $rerun eq '0'; - #$rerun='x' if $rerun eq '1'; - $rerun = " [" . markup::base26( $event->{recurrence_count} + 1 ) . "]" - if ( defined $event->{recurrence_count} ) && ( $event->{recurrence_count} ne '' ) && ( $event->{recurrence_count} > 0 ); + $rerun = " [" . markup::base26( $event->{recurrence_count} + 1 ) . "]" + if ( defined $event->{recurrence_count} ) && ( $event->{recurrence_count} ne '' ) && ( $event->{recurrence_count} > 0 ); - my $draft = $event->{draft} || '0'; - $draft = '-' if $draft eq '0'; - $draft = 'x' if $draft eq '1'; + my $draft = $event->{draft} || '0'; + $draft = '-' if $draft eq '0'; + $draft = 'x' if $draft eq '1'; - my $title = $event->{title}; - $title .= ': ' . $event->{user_title} if $event->{user_title} ne ''; + my $title = $event->{title}; + $title .= ': ' . $event->{user_title} if $event->{user_title} ne ''; - print qq!! - . qq!! - . qq!! - . qq!! - . qq!! - . qq!! - . qq!! - . qq!! - . qq!! - . qq!! - . qq!! - . qq!! - . qq!! . "\n"; - } - $i++; - } - print qq{ + $out .= + qq!! + . qq!! + . qq!! + . qq!! + . qq!! + . qq!! + . qq!! + . qq!! + . qq!! + . qq!! + . qq!! + . qq!! + . qq!! . "\n"; + } + $i++; + if ( $i % 100 == 0 ) { + print $out; + $out = ''; + } + } + $out .= qq{
! - . time::dayOfYear( $event->{start} ) - . q!$event->{weekday_short_name},$event->{start_date_name}$event->{start_time_name} - $event->{end_time}$event->{series_name}$title$event->{episode}$rerun$draft$live$archived
! + . time::dayOfYear( $event->{start} ) + . q!$event->{weekday_short_name},$event->{start_date_name}$event->{start_time_name} - $event->{end_time}$event->{series_name}$title$event->{episode}$rerun$draft$live$archived
} if $params->{part} == 0; - my $project_id = $params->{project_id}; - my $studio_id = $params->{studio_id}; + my $project_id = $params->{project_id}; + my $studio_id = $params->{studio_id}; - #add handler for events not assigned to series - if ( ( $params->{studio_id} ne '' ) && ( $params->{studio_id} ne '-1' ) ) { - my $series = series::get( - $config, - { - project_id => $project_id, - studio_id => $studio_id - } - ); - print q{}; - } + #add handler for events not assigned to series + if ( ( $params->{studio_id} ne '' ) && ( $params->{studio_id} ne '-1' ) ) { + my $series = series::get( + $config, + { + project_id => $project_id, + studio_id => $studio_id + } + ); + $out .= q{}; + } - print qq{ + $out .= qq{ @@ -979,226 +987,226 @@ sub printTableHeader { !; - $out .= q{ + $out .= q{ }; - print $out; + print $out; } sub printTableBody { - my $config = shift; - my $permissions = shift; - my $params = shift; - my $cal_options = shift; + my $config = shift; + my $permissions = shift; + my $params = shift; + my $cal_options = shift; - my $days = $cal_options->{days}; - my $events_by_day = $cal_options->{events_by_day}; - my $yoffset = $cal_options->{yoffset}; + my $days = $cal_options->{days}; + my $events_by_day = $cal_options->{events_by_day}; + my $yoffset = $cal_options->{yoffset}; - my $project_id = $params->{project_id}; - my $studio_id = $params->{studio_id}; + my $project_id = $params->{project_id}; + my $studio_id = $params->{studio_id}; - if ( scalar( @{$days} ) == 0 ) { - uac::print_info("no dates found at the selected time span"); - } + if ( scalar( @{$days} ) == 0 ) { + uac::print_info("no dates found at the selected time span"); + } - my $out = q{ + my $out = q{ }; - print $out; + print $out; } sub printSeries { - my $config = shift; - my $permissions = shift; - my $params = shift; - my $cal_options = shift; + my $config = shift; + my $permissions = shift; + my $params = shift; + my $cal_options = shift; - my $project_id = $params->{project_id}; - my $studio_id = $params->{studio_id}; + my $project_id = $params->{project_id}; + my $studio_id = $params->{studio_id}; - my $series = series::get( - $config, - { - project_id => $project_id, - studio_id => $studio_id - } - ); + my $series = series::get( + $config, + { + project_id => $project_id, + studio_id => $studio_id + } + ); - my $out = ''; + my $out = ''; - #add schedule entry for series - if ( ( defined $permissions->{update_schedule} ) && ( $permissions->{update_schedule} eq '1' ) && ( @$series > 0 ) ) { - $out .= q{}; - } + #add schedule entry for series + if ( ( defined $permissions->{update_schedule} ) && ( $permissions->{update_schedule} eq '1' ) && ( scalar(@$series) > 0 ) ) { + $out .= q{}; + } - if ( ( $params->{studio_id} ne '' ) && ( $params->{studio_id} ne '-1' ) ) { - $out .= q{}; - } + if ( ( $params->{studio_id} ne '' ) && ( $params->{studio_id} ne '-1' ) ) { + $out .= q{}; + } - $out .= q{ + $out .= q{ }; - print $out; + print $out; } sub printJavascript { - my $config = shift; - my $permissions = shift; - my $params = shift; - my $cal_options = shift; + my $config = shift; + my $permissions = shift; + my $params = shift; + my $cal_options = shift; - my $startOfDay = $cal_options->{min_hour} % 24; + my $startOfDay = $cal_options->{min_hour} % 24; - #print STDERR "js: ".$cal_options->{min_hour}." ".$startOfDay."\n"; - my $out = q{ + #print STDERR "js: ".$cal_options->{min_hour}." ".$startOfDay."\n"; + my $out = q{ }; - print $out; + print $out; } #TODO: Javascript sub addCalendarButton { - my $params = shift; - my $calendar = shift; + my $params = shift; + my $calendar = shift; - #add calendar button - my $content = qq{ + #add calendar button + my $content = qq{
@@ -1227,38 +1235,38 @@ sub addCalendarButton {
}; - return $content; + return $content; } sub addSeries { - my $series = shift; - my $params = shift; + my $series = shift; + my $params = shift; - return unless defined $series; - return unless @$series > 0; + return unless defined $series; + return unless scalar @$series > 0; - my $out = ''; - $out .= q{ + my $out = ''; + $out .= q{ @@ -1273,22 +1281,22 @@ sub addSeries {
} . $params->{loc}->{label_series} . q{
}; - return $out; + return $out; } # create form to add events to series (that are not assigned to series, yet) sub addEventsToSeries { - my $series = shift; - my $params = shift; + my $series = shift; + my $params = shift; - return unless defined $series; - return unless @$series > 0; - my $project_id = $params->{project_id}; - my $studio_id = $params->{studio_id}; + return unless defined $series; + return unless scalar @$series > 0; + my $project_id = $params->{project_id}; + my $studio_id = $params->{studio_id}; - my $out = ''; - $out .= qq{ + my $out = ''; + $out .= qq{
} . $params->{loc}->{label_assign_event_series} . qq{
@@ -1301,42 +1309,42 @@ sub addEventsToSeries { + . $params->{loc}->{button_assign_event_series} . q{
}; - return $out; + return $out; } # insert form to create series on not assigned events sub createSeries { - my $params = shift; + my $params = shift; - my $project_id = $params->{project_id}; - my $studio_id = $params->{studio_id}; - return qq{ + my $project_id = $params->{project_id}; + my $studio_id = $params->{studio_id}; + return qq{
} . $params->{loc}->{label_create_series} . qq{
@@ -1357,274 +1365,274 @@ sub createSeries { } sub print_event { - my $params = shift; - my $event = shift; - my $ypos = shift; - my $yoffset = shift; - my $yzoom = shift; + my $params = shift; + my $event = shift; + my $ypos = shift; + my $yoffset = shift; + my $yzoom = shift; - $event->{project_id} = '-1' unless defined $event->{project_id}; - $event->{studio_id} = '-1' unless defined $event->{studio_id}; - $event->{series_id} = '-1' unless defined $event->{series_id}; - $event->{event_id} = '-1' unless defined $event->{event_id}; + $event->{project_id} = '-1' unless defined $event->{project_id}; + $event->{studio_id} = '-1' unless defined $event->{studio_id}; + $event->{series_id} = '-1' unless defined $event->{series_id}; + $event->{event_id} = '-1' unless defined $event->{event_id}; - my $id = 'event_' . $event->{project_id} . '_' . $event->{studio_id} . '_' . $event->{series_id} . '_' . $event->{event_id}; - $id = 'grid_' . $event->{project_id} . '_' . $event->{studio_id} . '_' . $event->{series_id} if defined $event->{grid}; - $id = 'work_' . $event->{project_id} . '_' . $event->{studio_id} . '_' . $event->{schedule_id} if defined $event->{work}; - $id = 'play_' . $event->{project_id} . '_' . $event->{studio_id} if defined $event->{play}; + my $id = 'event_' . $event->{project_id} . '_' . $event->{studio_id} . '_' . $event->{series_id} . '_' . $event->{event_id}; + $id = 'grid_' . $event->{project_id} . '_' . $event->{studio_id} . '_' . $event->{series_id} if defined $event->{grid}; + $id = 'work_' . $event->{project_id} . '_' . $event->{studio_id} . '_' . $event->{schedule_id} if defined $event->{work}; + $id = 'play_' . $event->{project_id} . '_' . $event->{studio_id} if defined $event->{play}; - my $class = $event->{class} || ''; - my $showIcons = 0; - if ( $class =~ /(event|schedule)/ ) { - $class .= ' scheduled' if defined $event->{scheduled}; - $class .= ' no_series' if ( ( $class eq 'event' ) && ( $event->{series_id} eq '-1' ) ); - $class .= " error x$event->{error}" if defined $event->{error}; + my $class = $event->{class} || ''; + my $showIcons = 0; + if ( $class =~ /(event|schedule)/ ) { + $class .= ' scheduled' if defined $event->{scheduled}; + $class .= ' no_series' if ( ( $class eq 'event' ) && ( $event->{series_id} eq '-1' ) ); + $class .= " error x$event->{error}" if defined $event->{error}; - for my $filter ( 'rerun', 'archived', 'playout', 'published', 'live', 'disable_event_sync', 'draft' ) { - $class .= ' ' . $filter if ( ( defined $event->{$filter} ) && ( $event->{$filter} eq '1' ) ); - } - $class .= ' preproduced' unless ( ( defined $event->{'live'} ) && ( $event->{'live'} eq '1' ) ); - $class .= ' no_playout' unless ( ( defined $event->{'playout'} ) && ( $event->{'playout'} eq '1' ) ); - $class .= ' no_rerun' unless ( ( defined $event->{'rerun'} ) && ( $event->{'rerun'} eq '1' ) ); - $showIcons = 1; - } + for my $filter ( 'rerun', 'archived', 'playout', 'published', 'live', 'disable_event_sync', 'draft' ) { + $class .= ' ' . $filter if ( ( defined $event->{$filter} ) && ( $event->{$filter} eq '1' ) ); + } + $class .= ' preproduced' unless ( ( defined $event->{'live'} ) && ( $event->{'live'} eq '1' ) ); + $class .= ' no_playout' unless ( ( defined $event->{'playout'} ) && ( $event->{'playout'} eq '1' ) ); + $class .= ' no_rerun' unless ( ( defined $event->{'rerun'} ) && ( $event->{'rerun'} eq '1' ) ); + $showIcons = 1; + } - my $ystart = $event->{ystart} - $yoffset; - my $yend = $event->{yend} - $yoffset - 10; + my $ystart = $event->{ystart} - $yoffset; + my $yend = $event->{yend} - $yoffset - 10; - $ystart = int( $ystart * $yzoom ); - $yend = int( $yend * $yzoom ); - my $height = $yend - $ystart + 2; + $ystart = int( $ystart * $yzoom ); + $yend = int( $yend * $yzoom ); + my $height = $yend - $ystart + 2; - if ( $ypos > 0 ) { - $height = q{height:} . ($height) . 'px;'; - } else { - $height = ''; - } + if ( $ypos > 0 ) { + $height = q{height:} . ($height) . 'px;'; + } else { + $height = ''; + } - # my $date = $event->{origStart} || $event->{start} || ''; - my $content = $event->{content} || ''; + # my $date = $event->{origStart} || $event->{start} || ''; + my $content = $event->{content} || ''; - if ( $class =~ /schedule/ ) { - my $frequency = getFrequency($event); - $content .= "
($frequency)" if defined $frequency; - } + if ( $class =~ /schedule/ ) { + my $frequency = getFrequency($event); + $content .= "
($frequency)" if defined $frequency; + } - my $attr = ''; - if ( $class =~ /play/ ) { - $attr .= ' rms="' . $event->{rms_image} . '"' if defined $event->{rms_image}; - $attr .= ' start="' . $event->{start} . '"' if defined $event->{start}; - } + my $attr = ''; + if ( $class =~ /play/ ) { + $attr .= ' rms="' . $event->{rms_image} . '"' if defined $event->{rms_image}; + $attr .= ' start="' . $event->{start} . '"' if defined $event->{start}; + } - if ( defined $event->{upload} ) { - $content .= '
uploading '; - } + if ( defined $event->{upload} ) { + $content .= '
uploading '; + } - if ($showIcons) { - $content = '
' . $content . '
'; - } + if ($showIcons) { + $content = '
' . $content . '
'; + } - my $time = ''; - $time = qq{ time="$event->{time}"} if $class =~ m/time/; + my $time = ''; + $time = qq{ time="$event->{time}"} if $class =~ m/time/; - my $date = ''; - $date = qq{ date="$event->{date}"} if $class =~ m/date/; + my $date = ''; + $date = qq{ date="$event->{date}"} if $class =~ m/date/; - my $line = q{
$content
}; - $line .= "\n"; - return $line; + my $line = q{
$content
}; + $line .= "\n"; + return $line; } sub getFrequency { - my $event = shift; + my $event = shift; - my $period_type = $event->{period_type}; - return undef unless defined $period_type; - return undef if $period_type ne 'days'; + my $period_type = $event->{period_type}; + return undef unless defined $period_type; + return undef if $period_type ne 'days'; - my $frequency = $event->{frequency}; - return undef unless defined $frequency; - return undef unless $frequency > 0; + my $frequency = $event->{frequency}; + return undef unless defined $frequency; + return undef unless $frequency > 0; - if ( ( $frequency >= 7 ) && ( ( $frequency % 7 ) == 0 ) ) { - $frequency /= 7; - return '1 week' if $frequency == 1; - return $frequency .= ' weeks'; - } + if ( ( $frequency >= 7 ) && ( ( $frequency % 7 ) == 0 ) ) { + $frequency /= 7; + return '1 week' if $frequency == 1; + return $frequency .= ' weeks'; + } - return '1 day' if $frequency == 1; - return $frequency .= ' days'; + return '1 day' if $frequency == 1; + return $frequency .= ' days'; } sub calc_positions { - my $events = $_[0]; - my $cal_options = $_[1]; + my $events = $_[0]; + my $cal_options = $_[1]; - my $start_of_day = $cal_options->{start_of_day}; + my $start_of_day = $cal_options->{start_of_day}; - for my $event (@$events) { - my ( $start_hour, $start_min ) = getTime( $event->{start_time} ); - my ( $end_hour, $end_min ) = getTime( $event->{end_time} ); + for my $event (@$events) { + my ( $start_hour, $start_min ) = getTime( $event->{start_time} ); + my ( $end_hour, $end_min ) = getTime( $event->{end_time} ); - $start_hour += 24 if $start_hour < $start_of_day; - $end_hour += 24 if $end_hour < $start_of_day; - $event->{ystart} = $start_hour * 60 + $start_min; - $event->{yend} = $end_hour * 60 + $end_min; - } + $start_hour += 24 if $start_hour < $start_of_day; + $end_hour += 24 if $end_hour < $start_of_day; + $event->{ystart} = $start_hour * 60 + $start_min; + $event->{yend} = $end_hour * 60 + $end_min; + } } sub find_errors { - my $events = $_[0]; + my $events = $_[0]; - for my $event (@$events) { - next if defined $event->{grid}; - next if defined $event->{work}; - next if defined $event->{play}; - next if ( defined $event->{draft} ) && ( $event->{draft} == 1 ); - next unless defined $event->{ystart}; - next unless defined $event->{yend}; - $event->{check_errors} = 1; - } + for my $event (@$events) { + next if defined $event->{grid}; + next if defined $event->{work}; + next if defined $event->{play}; + next if ( defined $event->{draft} ) && ( $event->{draft} == 1 ); + next unless defined $event->{ystart}; + next unless defined $event->{yend}; + $event->{check_errors} = 1; + } - #check next events - for my $i ( 0 .. @$events - 1 ) { - my $event = $events->[$i]; - next unless defined $event->{check_errors}; + #check next events + for my $i ( 0 .. scalar(@$events) - 1 ) { + my $event = $events->[$i]; + next unless defined $event->{check_errors}; - #look for conflicts with next 5 events of day - my $min_index = $i + 1; - next if $min_index >= @$events; - my $max_index = $i + 8; - $max_index = @$events - 1 if $max_index >= @$events; - for my $j ( $min_index .. $max_index ) { - my $event2 = $events->[$j]; - next unless defined $event2->{check_errors}; + #look for conflicts with next 5 events of day + my $min_index = $i + 1; + next if $min_index >= scalar @$events; + my $max_index = $i + 8; + $max_index = scalar(@$events) - 1 if $max_index >= (@$events); + for my $j ( $min_index .. $max_index ) { + my $event2 = $events->[$j]; + next unless defined $event2->{check_errors}; - #mark events if same start,stop,series_id, one is schedule one is event - if ( ( defined $event->{series_id} ) - && ( defined $event2->{series_id} ) - && ( $event->{series_id} == $event2->{series_id} ) ) - { - if ( ( $event->{ystart} eq $event2->{ystart} ) - && ( $event->{yend} eq $event2->{yend} ) ) - { - if ( ( defined $event->{schedule} ) && ( !( defined $event2->{schedule} ) ) ) { - $event->{hide} = 1; - $event2->{scheduled} = 1; - next; - } - if ( ( !( defined $event->{schedule} ) ) && ( defined $event2->{schedule} ) ) { - $event->{scheduled} = 1; - $event2->{hide} = 1; - next; - } - } elsif ( ( $event->{ystart} >= $event2->{ystart} ) - && ( $event->{scheduled} == 1 ) - && ( $event2->{scheduled} == 1 ) ) - { - #subsequent schedules - $event->{error}++; - $event2->{error} = 1 unless defined $event2->{error}; - $event2->{error}++; - next; - } - } elsif ( $event->{ystart} >= $event2->{ystart} ) { + #mark events if same start,stop,series_id, one is schedule one is event + if ( ( defined $event->{series_id} ) + && ( defined $event2->{series_id} ) + && ( $event->{series_id} == $event2->{series_id} ) ) + { + if ( ( $event->{ystart} eq $event2->{ystart} ) + && ( $event->{yend} eq $event2->{yend} ) ) + { + if ( ( defined $event->{schedule} ) && ( !( defined $event2->{schedule} ) ) ) { + $event->{hide} = 1; + $event2->{scheduled} = 1; + next; + } + if ( ( !( defined $event->{schedule} ) ) && ( defined $event2->{schedule} ) ) { + $event->{scheduled} = 1; + $event2->{hide} = 1; + next; + } + } elsif ( ( $event->{ystart} >= $event2->{ystart} ) + && ( $event->{scheduled} == 1 ) + && ( $event2->{scheduled} == 1 ) ) + { + #subsequent schedules + $event->{error}++; + $event2->{error} = 1 unless defined $event2->{error}; + $event2->{error}++; + next; + } + } elsif ( $event->{ystart} >= $event2->{ystart} ) { - #errors on multiple schedules or events - $event->{error}++; - $event2->{error} = 1 unless defined $event2->{error}; - $event2->{error}++; - } - } - } + #errors on multiple schedules or events + $event->{error}++; + $event2->{error} = 1 unless defined $event2->{error}; + $event2->{error}++; + } + } + } - #remove error tags from correctly scheduled entries (subsequent entries with same series id) - for my $event (@$events) { - delete $event->{error} - if ( - ( defined $event->{error} ) - && ( ( ( defined $event->{scheduled} ) && ( $event->{scheduled} == 1 ) ) - || ( ( defined $event->{hide} ) && ( $event->{hide} == 1 ) ) ) - ); - } + #remove error tags from correctly scheduled entries (subsequent entries with same series id) + for my $event (@$events) { + delete $event->{error} + if ( + ( defined $event->{error} ) + && ( ( ( defined $event->{scheduled} ) && ( $event->{scheduled} == 1 ) ) + || ( ( defined $event->{hide} ) && ( $event->{hide} == 1 ) ) ) + ); + } } sub printToolbar { - my $config = shift; - my $params = shift; - my $calendar = shift; + my $config = shift; + my $params = shift; + my $calendar = shift; - #print Dumper($params); - my $today = time::time_to_date(); + #print Dumper($params); + my $today = time::time_to_date(); - #setToday (TODO:javascript) - my $toolbar = '
'; + #setToday (TODO:javascript) + my $toolbar = '
'; - #$toolbar.='
'.addCalendarButton($params, $calendar).'
'; - $toolbar .= addCalendarButton( $params, $calendar ); - $toolbar .= qq{}; + #$toolbar.='
'.addCalendarButton($params, $calendar).'
'; + $toolbar .= addCalendarButton( $params, $calendar ); + $toolbar .= qq{}; - #ranges - my $ranges = { - $params->{loc}->{label_month} => 'month', - $params->{loc}->{label_4_weeks} => '28', - $params->{loc}->{label_2_weeks} => '14', - $params->{loc}->{label_1_week} => '7', - $params->{loc}->{label_day} => '1', - }; - $toolbar .= qq{ + #ranges + my $ranges = { + $params->{loc}->{label_month} => 'month', + $params->{loc}->{label_4_weeks} => '28', + $params->{loc}->{label_2_weeks} => '14', + $params->{loc}->{label_1_week} => '7', + $params->{loc}->{label_day} => '1', + }; + $toolbar .= qq{ }; - # start of day - my $day_start = $params->{day_start} || ''; - $toolbar .= qq{ + # start of day + my $day_start = $params->{day_start} || ''; + $toolbar .= qq{ }; - #filter - my $filter = $params->{filter} || ''; - $toolbar .= qq{ + #filter + my $filter = $params->{filter} || ''; + $toolbar .= qq{ }; - #search - $toolbar .= qq{ + #search + $toolbar .= qq{ @@ -1635,250 +1643,250 @@ sub printToolbar { }; - # - $toolbar .= qq{ + # + $toolbar .= qq{ } if $params->{list} == 1; - $toolbar .= qq{ + $toolbar .= qq{
}; - print $toolbar; + print $toolbar; } sub getTime { - my $time = shift; - if ( $time =~ /^(\d\d)\:(\d\d)/ ) { - return ( $1, $2 ); - } - return ( -1, -1 ); + my $time = shift; + if ( $time =~ /^(\d\d)\:(\d\d)/ ) { + return ( $1, $2 ); + } + return ( -1, -1 ); } sub getCalendar { - my $config = shift; - my $params = shift; - my $language = shift; + my $config = shift; + my $params = shift; + my $language = shift; - my $from_date = getFromDate( $config, $params ); - my $till_date = getTillDate( $config, $params ); - my $range = $params->{range}; + my $from_date = getFromDate( $config, $params ); + my $till_date = getTillDate( $config, $params ); + my $range = $params->{range}; - my $previous = ''; - my $next = ''; - if ( $range eq 'month' ) { - $previous = time::get_datetime( $from_date, $config->{date}->{time_zone} )->subtract( months => 1 )->set_day(1)->date(); - $next = time::get_datetime( $from_date, $config->{date}->{time_zone} )->add( months => 1 )->set_day(1)->date(); - } else { - $previous = time::get_datetime( $from_date, $config->{date}->{time_zone} )->subtract( days => $range )->date(); - $next = time::get_datetime( $from_date, $config->{date}->{time_zone} )->add( days => $range )->date(); - } - my ( $year, $month, $day ) = split( /\-/, $from_date ); - $month = $time::names->{$language}->{months_abbr}->[ $month - 1 ] || ''; + my $previous = ''; + my $next = ''; + if ( $range eq 'month' ) { + $previous = time::get_datetime( $from_date, $config->{date}->{time_zone} )->subtract( months => 1 )->set_day(1)->date(); + $next = time::get_datetime( $from_date, $config->{date}->{time_zone} )->add( months => 1 )->set_day(1)->date(); + } else { + $previous = time::get_datetime( $from_date, $config->{date}->{time_zone} )->subtract( days => $range )->date(); + $next = time::get_datetime( $from_date, $config->{date}->{time_zone} )->add( days => $range )->date(); + } + my ( $year, $month, $day ) = split( /\-/, $from_date ); + $month = $time::names->{$language}->{months_abbr}->[ $month - 1 ] || ''; - return { - from_date => $from_date, - till_date => $till_date, - next_date => $next, - previous_date => $previous, - month => $month, - year => $year - }; + return { + from_date => $from_date, + till_date => $till_date, + next_date => $next, + previous_date => $previous, + month => $month, + year => $year + }; } sub getFromDate { - my $config = shift; - my $params = shift; + my $config = shift; + my $params = shift; - if ( $params->{from_date} ne '' ) { - return $params->{from_date}; - } - my $date = $params->{date}; - if ( $date eq '' ) { - $date = DateTime->now( time_zone => $config->{date}->{time_zone} )->date(); - } + if ( $params->{from_date} ne '' ) { + return $params->{from_date}; + } + my $date = $params->{date}; + if ( $date eq '' ) { + $date = DateTime->now( time_zone => $config->{date}->{time_zone} )->date(); + } - if ( $params->{range} eq '28' ) { + if ( $params->{range} eq '28' ) { - #get start of 4 week period - $date = time::get_datetime( $date, $config->{date}->{time_zone} )->truncate( to => 'week' )->ymd(); - } - if ( $params->{range} eq 'month' ) { + #get start of 4 week period + $date = time::get_datetime( $date, $config->{date}->{time_zone} )->truncate( to => 'week' )->ymd(); + } + if ( $params->{range} eq 'month' ) { - #get first day of month - return time::get_datetime( $date, $config->{date}->{time_zone} )->set_day(1)->date(); - } + #get first day of month + return time::get_datetime( $date, $config->{date}->{time_zone} )->set_day(1)->date(); + } - #get date - return time::get_datetime( $date, $config->{date}->{time_zone} )->date(); + #get date + return time::get_datetime( $date, $config->{date}->{time_zone} )->date(); } sub getTillDate { - my $config = shift; - my $params = shift; - if ( $params->{till_date} ne '' ) { - return $params->{till_date}; - } - my $date = $params->{date} || ''; - if ( $date eq '' ) { - $date = DateTime->now( time_zone => $config->{date}->{time_zone} )->date(); - } - if ( $params->{range} eq '28' ) { - $date = time::get_datetime( $date, $config->{date}->{time_zone} )->truncate( to => 'week' )->ymd(); - } - if ( $params->{range} eq 'month' ) { + my $config = shift; + my $params = shift; + if ( $params->{till_date} ne '' ) { + return $params->{till_date}; + } + my $date = $params->{date} || ''; + if ( $date eq '' ) { + $date = DateTime->now( time_zone => $config->{date}->{time_zone} )->date(); + } + if ( $params->{range} eq '28' ) { + $date = time::get_datetime( $date, $config->{date}->{time_zone} )->truncate( to => 'week' )->ymd(); + } + if ( $params->{range} eq 'month' ) { - #get last day of month - return time::get_datetime( $date, $config->{date}->{time_zone} )->set_day(1)->add( months => 1 )->subtract( days => 1 )->date(); - } + #get last day of month + return time::get_datetime( $date, $config->{date}->{time_zone} )->set_day(1)->add( months => 1 )->subtract( days => 1 )->date(); + } - #add range to date - return time::get_datetime( $date, $config->{date}->{time_zone} )->add( days => $params->{range} )->date(); + #add range to date + return time::get_datetime( $date, $config->{date}->{time_zone} )->add( days => $params->{range} )->date(); } sub getSeriesEvents { - my $config = shift; - my $request = shift; - my $options = shift; - my $params = shift; + my $config = shift; + my $request = shift; + my $options = shift; + my $params = shift; - #get events by series id - my $series_id = $request->{params}->{checked}->{series_id}; - if ( defined $series_id ) { - my $events = series::get_events( $request->{config}, $options ); - return $events; - } + #get events by series id + my $series_id = $request->{params}->{checked}->{series_id}; + if ( defined $series_id ) { + my $events = series::get_events( $request->{config}, $options ); + return $events; + } - #get events (directly from database to get the ones, not assigned, yet) - delete $options->{studio_id}; - delete $options->{project_id}; - $options->{recordings} = 1; + #get events (directly from database to get the ones, not assigned, yet) + delete $options->{studio_id}; + delete $options->{project_id}; + $options->{recordings} = 1; - my $request2 = { - params => { - checked => events::check_params( $config, $options ) - }, - config => $request->{config}, - permissions => $request->{permissions} - }; - $request2->{params}->{checked}->{published} = 'all'; - $request2->{params}->{checked}->{draft} = '1' if $params->{list} == 1; + my $request2 = { + params => { + checked => events::check_params( $config, $options ) + }, + config => $request->{config}, + permissions => $request->{permissions} + }; + $request2->{params}->{checked}->{published} = 'all'; + $request2->{params}->{checked}->{draft} = '1' if $params->{list} == 1; - #delete $request2->{params}->{checked}->{locations_to_exclude} - # if ( ( $params->{studio_id} == -1 ) && ( defined $request2->{params}->{checked}->{locations_to_exclude} ) ); + #delete $request2->{params}->{checked}->{locations_to_exclude} + # if ( ( $params->{studio_id} == -1 ) && ( defined $request2->{params}->{checked}->{locations_to_exclude} ) ); - my $events = events::get( $config, $request2 ); + my $events = events::get( $config, $request2 ); - #print STDERR Dumper($request2->{params}->{checked}); - #print STDERR Dumper($events); - series::add_series_ids_to_events( $request->{config}, $events ); + #print STDERR Dumper($request2->{params}->{checked}); + #print STDERR Dumper($events); + series::add_series_ids_to_events( $request->{config}, $events ); - my $studios = studios::get( - $request->{config}, - { - project_id => $options->{project_id} - } - ); - my $studio_id_by_location = {}; - for my $studio (@$studios) { - $studio_id_by_location->{ $studio->{location} } = $studio->{id}; - } + my $studios = studios::get( + $request->{config}, + { + project_id => $options->{project_id} + } + ); + my $studio_id_by_location = {}; + for my $studio (@$studios) { + $studio_id_by_location->{ $studio->{location} } = $studio->{id}; + } - for my $event (@$events) { - $event->{project_id} = $options->{project_id} unless defined $event->{project_id}; - $event->{studio_id} = $studio_id_by_location->{ $event->{location} } unless defined $event->{studio_id}; - } + for my $event (@$events) { + $event->{project_id} = $options->{project_id} unless defined $event->{project_id}; + $event->{studio_id} = $studio_id_by_location->{ $event->{location} } unless defined $event->{studio_id}; + } - return $events; + return $events; } sub check_params { - my $params = shift; - my $config = shift; + my $params = shift; + my $config = shift; - my $checked = {}; - my $template = ''; - $checked->{template} = template::check( $params->{template}, 'series' ); + my $checked = {}; + my $template = ''; + $checked->{template} = template::check( $params->{template}, 'series' ); - my $debug = $params->{debug} || ''; - if ( $debug =~ /([a-z\_\,]+)/ ) { - $debug = $1; - } - $checked->{debug} = $debug; + my $debug = $params->{debug} || ''; + if ( $debug =~ /([a-z\_\,]+)/ ) { + $debug = $1; + } + $checked->{debug} = $debug; - #numeric values - $checked->{part} = 0; - $checked->{list} = 0; - for my $param ( 'id', 'project_id', 'studio_id', 'default_studio_id', 'user_id', 'series_id', 'event_id', 'part', 'list', 'day_start' ) - { - if ( ( defined $params->{$param} ) && ( $params->{$param} =~ /^\d+$/ ) ) { - $checked->{$param} = $params->{$param}; - } - } + #numeric values + $checked->{part} = 0; + $checked->{list} = 0; + for my $param ( 'id', 'project_id', 'studio_id', 'default_studio_id', 'user_id', 'series_id', 'event_id', 'part', 'list', 'day_start' ) + { + if ( ( defined $params->{$param} ) && ( $params->{$param} =~ /^\d+$/ ) ) { + $checked->{$param} = $params->{$param}; + } + } - $checked->{day_start} = $config->{date}->{day_starting_hour} unless defined $checked->{day_start}; - $checked->{day_start} %= 24; + $checked->{day_start} = $config->{date}->{day_starting_hour} unless defined $checked->{day_start}; + $checked->{day_start} %= 24; - if ( defined $checked->{studio_id} ) { + if ( defined $checked->{studio_id} ) { - # a studio is selected, use the studio from parameter - $checked->{default_studio_id} = $checked->{studio_id}; - } elsif ( ( defined $params->{studio_id} ) && ( $params->{studio_id} eq '-1' ) ) { + # a studio is selected, use the studio from parameter + $checked->{default_studio_id} = $checked->{studio_id}; + } elsif ( ( defined $params->{studio_id} ) && ( $params->{studio_id} eq '-1' ) ) { - # all studios selected, use -1 - $checked->{studio_id} = -1; - } else { + # all studios selected, use -1 + $checked->{studio_id} = -1; + } else { - # no studio given, use default studio - $checked->{studio_id} = $checked->{default_studio_id}; - } + # no studio given, use default studio + $checked->{studio_id} = $checked->{default_studio_id}; + } - $checked->{open_end} = 1; - for my $param ('open_end') { - if ( ( defined $params->{$param} ) && ( $params->{$param} =~ /^\d+$/ ) ) { - $checked->{$param} = $params->{$param}; - } - } + $checked->{open_end} = 1; + for my $param ('open_end') { + if ( ( defined $params->{$param} ) && ( $params->{$param} =~ /^\d+$/ ) ) { + $checked->{$param} = $params->{$param}; + } + } - #scalars - $checked->{search} = ''; - $checked->{filter} = ''; + #scalars + $checked->{search} = ''; + $checked->{filter} = ''; - #$checked->{range}='month'; - for my $param ( 'search', 'filter', 'range' ) { - if ( defined $params->{$param} ) { - $checked->{$param} = $params->{$param}; - $checked->{$param} =~ s/^\s+//g; - $checked->{$param} =~ s/\s+$//g; - } - } + #$checked->{range}='month'; + for my $param ( 'search', 'filter', 'range' ) { + if ( defined $params->{$param} ) { + $checked->{$param} = $params->{$param}; + $checked->{$param} =~ s/^\s+//g; + $checked->{$param} =~ s/\s+$//g; + } + } - for my $param ('expires') { - $checked->{$param} = time::check_datetime( $params->{$param} ); - } + for my $param ('expires') { + $checked->{$param} = time::check_datetime( $params->{$param} ); + } - for my $param ( 'date', 'from_date', 'till_date' ) { - $checked->{$param} = time::check_date( $params->{$param} ); - } + for my $param ( 'date', 'from_date', 'till_date' ) { + $checked->{$param} = time::check_date( $params->{$param} ); + } - for my $param ( 'series_name', 'title', 'excerpt', 'content', 'program', 'category', 'image', 'user_content' ) { - if ( defined $params->{$param} ) { + for my $param ( 'series_name', 'title', 'excerpt', 'content', 'program', 'category', 'image', 'user_content' ) { + if ( defined $params->{$param} ) { - #$checked->{$param}=uri_unescape(); - $checked->{$param} = $params->{$param}; - $checked->{$param} =~ s/^\s+//g; - $checked->{$param} =~ s/\s+$//g; - } - } + #$checked->{$param}=uri_unescape(); + $checked->{$param} = $params->{$param}; + $checked->{$param} =~ s/^\s+//g; + $checked->{$param} =~ s/\s+$//g; + } + } - #actions and roles - if ( defined $params->{action} ) { - if ( $params->{action} =~ /^(add_user|remove_user|delete|save|details|show|edit_event|save_event)$/ ) { - $checked->{action} = $params->{action}; - } - } + #actions and roles + if ( defined $params->{action} ) { + if ( $params->{action} =~ /^(add_user|remove_user|delete|save|details|show|edit_event|save_event)$/ ) { + $checked->{action} = $params->{action}; + } + } - return $checked; + return $checked; } diff --git a/website/agenda/planung/comment.cgi b/website/agenda/planung/comment.cgi index b51465a..730e176 100755 --- a/website/agenda/planung/comment.cgi +++ b/website/agenda/planung/comment.cgi @@ -35,12 +35,12 @@ my ( $user, $expires ) = auth::get_user( $cgi, $config ); return if ( ( !defined $user ) || ( $user eq '' ) ); my $user_presets = uac::get_user_presets( - $config, - { - user => $user, - project_id => $params->{project_id}, - studio_id => $params->{studio_id} - } + $config, + { + user => $user, + project_id => $params->{project_id}, + studio_id => $params->{studio_id} + } ); $params->{default_studio_id} = $user_presets->{studio_id}; $params->{studio_id} = $params->{default_studio_id} @@ -49,11 +49,11 @@ $params->{project_id} = $user_presets->{project_id} if ( ( !( defined $params->{action} ) ) || ( $params->{action} eq '' ) || ( $params->{action} eq 'login' ) ); my $request = { - url => $ENV{QUERY_STRING} || '', - params => { - original => $params, - checked => check_params($params), - }, + url => $ENV{QUERY_STRING} || '', + params => { + original => $params, + checked => check_params($params), + }, }; #set user at params->presets->user @@ -64,261 +64,261 @@ $params = $request->{params}->{checked}; #print Dumper($params); #show header if ( ( params::isJson() ) || ( defined $params->{action} ) ) { - print "Content-Type:text/html; charset=utf-8;\n\n"; + print "Content-Type:text/html; charset=utf-8;\n\n"; } else { - my $headerParams = uac::set_template_permissions( $request->{permissions}, $params ); - $headerParams->{loc} = localization::get( $config, { user => $user, file => 'menu' } ); - template::process( 'print', template::check('default.html'), $headerParams ); - print q{ + my $headerParams = uac::set_template_permissions( $request->{permissions}, $params ); + $headerParams->{loc} = localization::get( $config, { user => $user, file => 'menu' } ); + template::process( 'print', template::check('default.html'), $headerParams ); + print q{ } unless (params::isJson); } return unless defined uac::check( $config, $params, $user_presets ); if ( defined $params->{action} ) { - if ( $params->{action} eq 'get_json' ) { - getJson( $config, $request ); - return; - } - if ( $params->{action} eq 'setLock' ) { - setLock( $config, $request ); - return; - } - if ( $params->{action} eq 'setRead' ) { - setRead( $config, $request ); - return; - } + if ( $params->{action} eq 'get_json' ) { + getJson( $config, $request ); + return; + } + if ( $params->{action} eq 'setLock' ) { + setLock( $config, $request ); + return; + } + if ( $params->{action} eq 'setRead' ) { + setRead( $config, $request ); + return; + } } $config->{access}->{write} = 0; showComments( $config, $request ); sub showComments { - my $config = shift; - my $request = shift; + my $config = shift; + my $request = shift; - my $params = $request->{params}->{checked}; - my $permissions = $request->{permissions}; - unless ( $permissions->{read_comment} == 1 ) { - uac::permissions_denied('read_comment'); - return; - } + my $params = $request->{params}->{checked}; + my $permissions = $request->{permissions}; + unless ( $permissions->{read_comment} == 1 ) { + uac::permissions_denied('read_comment'); + return; + } - for my $attr ( 'project_id', 'studio_id' ) { - unless ( defined $params->{$attr} ) { - uac::print_error( "missing " . $attr . " to show comment" ); - return; - } - } + for my $attr ( 'project_id', 'studio_id' ) { + unless ( defined $params->{$attr} ) { + uac::print_error( "missing " . $attr . " to show comment" ); + return; + } + } - $config->{access}->{write} = 0; - my $dbh = db::connect($config); + $config->{access}->{write} = 0; + my $dbh = db::connect($config); - my $comment = $params->{comment}; - my $template_parameters = {}; + my $comment = $params->{comment}; + my $template_parameters = {}; - #my $nodes={}; - #my $sorted_nodes=[]; + #my $nodes={}; + #my $sorted_nodes=[]; - my $results = []; - if ( $params->{search} ne '' ) { - $params->{comment}->{search} = $params->{search}; - $results = comments::get_by_event( $dbh, $config, $request ); - } elsif ( $comment->{event_id} ne '' ) { - $results = comments::get_by_event( $dbh, $config, $request ); - } else { - $results = comments::get_by_time( $dbh, $config, $comment ); - } + my $results = []; + if ( $params->{search} ne '' ) { + $params->{comment}->{search} = $params->{search}; + $results = comments::get_by_event( $dbh, $config, $request ); + } elsif ( $comment->{event_id} ne '' ) { + $results = comments::get_by_event( $dbh, $config, $request ); + } else { + $results = comments::get_by_time( $dbh, $config, $comment ); + } - my $events = []; - my $comment_count = 0; - if ( @$results > 0 ) { - my $comments = modify_comments( $request, $results ); + my $events = []; + my $comment_count = 0; + if ( scalar(@$results) > 0 ) { + my $comments = modify_comments( $request, $results ); - $comments = comments::sort( $config, $comments ); + $comments = comments::sort( $config, $comments ); - $events = comments::get_events( $dbh, $config, $request, $comments ); - my $language = $config::config->{date}->{language} || 'en'; - for my $event (@$events) { - $event->{start} = time::date_time_format( $event->{start}, $language ); - $comment_count += $event->{comment_count} if ( defined $event->{comment_count} ); - $event->{cache_base_url} = $config::config->{cache}->{base_url}; - } - } - for my $param (%$comment) { - $template_parameters->{$param} = $comment->{$param}; - } + $events = comments::get_events( $dbh, $config, $request, $comments ); + my $language = $config::config->{date}->{language} || 'en'; + for my $event (@$events) { + $event->{start} = time::date_time_format( $event->{start}, $language ); + $comment_count += $event->{comment_count} if ( defined $event->{comment_count} ); + $event->{cache_base_url} = $config::config->{cache}->{base_url}; + } + } + for my $param (%$comment) { + $template_parameters->{$param} = $comment->{$param}; + } - $template_parameters->{search} = markup::fix_utf8( $request->{params}->{original}->{search} ); - $template_parameters->{events} = $events; - $template_parameters->{debug} = $config->{system}->{debug}; - $template_parameters->{event_count} = @$events + 0; - $template_parameters->{comment_count} = $comment_count; - $template_parameters->{is_empty} = 1 if ( @$events == 0 ); - $template_parameters->{projects} = project::get_with_dates($config); - $template_parameters->{controllers} = $config->{controllers}; - $template_parameters->{allow} = $permissions; - $template_parameters->{loc} = localization::get( $config, { user => $params->{presets}->{user}, file => 'comment' } ); + $template_parameters->{search} = markup::fix_utf8( $request->{params}->{original}->{search} ); + $template_parameters->{events} = $events; + $template_parameters->{debug} = $config->{system}->{debug}; + $template_parameters->{event_count} = scalar(@$events); + $template_parameters->{comment_count} = $comment_count; + $template_parameters->{is_empty} = 1 if scalar @$events == 0; + $template_parameters->{projects} = project::get_with_dates($config); + $template_parameters->{controllers} = $config->{controllers}; + $template_parameters->{allow} = $permissions; + $template_parameters->{loc} = localization::get( $config, { user => $params->{presets}->{user}, file => 'comment' } ); - #fill and output template - template::process( 'print', $params->{template}, $template_parameters ); + #fill and output template + template::process( 'print', $params->{template}, $template_parameters ); } sub modify_comments { - my $request = shift; - my $results = shift; + my $request = shift; + my $results = shift; - my $language = $config::config->{date}->{language} || 'en'; - for my $result (@$results) { - $result->{start_date_name} = time::date_format( $result->{created_at}, $language ); - $result->{start_time_name} = time::time_format( $result->{created_at} ); - $result->{ $result->{lock_status} } = 1; - $result->{ $result->{news_status} } = 1; - } - return $results; + my $language = $config::config->{date}->{language} || 'en'; + for my $result (@$results) { + $result->{start_date_name} = time::date_format( $result->{created_at}, $language ); + $result->{start_time_name} = time::time_format( $result->{created_at} ); + $result->{ $result->{lock_status} } = 1; + $result->{ $result->{news_status} } = 1; + } + return $results; } sub setLock { - my $config = shift; - my $request = shift; + my $config = shift; + my $request = shift; - my $params = $request->{params}->{checked}; - my $permissions = $request->{permissions}; + my $params = $request->{params}->{checked}; + my $permissions = $request->{permissions}; - unless ( $permissions->{update_comment_status_lock} == 1 ) { - uac::permissions_denied('update_comment_status_lock'); - return; - } + unless ( $permissions->{update_comment_status_lock} == 1 ) { + uac::permissions_denied('update_comment_status_lock'); + return; + } - my $comment = $params->{comment}; - $comment->{id} = $comment->{comment_id}; - if ( $comment->{id} eq '' ) { - return; - } + my $comment = $params->{comment}; + $comment->{id} = $comment->{comment_id}; + if ( $comment->{id} eq '' ) { + return; + } - #todo change set_news_status to lock_status in comment module - $comment->{set_lock_status} = $comment->{lockStatus}; - $comment->{set_lock_status} = 'blocked' unless $comment->{set_lock_status} eq 'show'; + #todo change set_news_status to lock_status in comment module + $comment->{set_lock_status} = $comment->{lockStatus}; + $comment->{set_lock_status} = 'blocked' unless $comment->{set_lock_status} eq 'show'; - $config->{access}->{write} = 1; - my $dbh = db::connect($config); - print STDERR "setLock " . Dumper($comment); - comments::set_lock_status( $dbh, $config, $comment ); - print "done\n"; + $config->{access}->{write} = 1; + my $dbh = db::connect($config); + print STDERR "setLock " . Dumper($comment); + comments::set_lock_status( $dbh, $config, $comment ); + print "done\n"; } sub setRead { - my $config = shift; - my $request = shift; + my $config = shift; + my $request = shift; - my $params = $request->{params}->{checked}; - my $permissions = $request->{permissions}; + my $params = $request->{params}->{checked}; + my $permissions = $request->{permissions}; - unless ( $permissions->{update_comment_status_read} == 1 ) { - uac::permissions_denied('update_comment_status_read'); - return; - } + unless ( $permissions->{update_comment_status_read} == 1 ) { + uac::permissions_denied('update_comment_status_read'); + return; + } - $config->{access}->{write} = 1; - my $dbh = db::connect($config); + $config->{access}->{write} = 1; + my $dbh = db::connect($config); - my $comment = $params->{comment}; - $comment->{id} = $comment->{comment_id}; - if ( $comment->{id} eq '' ) { - return; - } + my $comment = $params->{comment}; + $comment->{id} = $comment->{comment_id}; + if ( $comment->{id} eq '' ) { + return; + } - #todo change set_news_status to read_status in comment module - $comment->{set_news_status} = $comment->{readStatus}; - $comment->{set_news_status} = 'received' unless $comment->{set_news_status} eq 'unread'; + #todo change set_news_status to read_status in comment module + $comment->{set_news_status} = $comment->{readStatus}; + $comment->{set_news_status} = 'received' unless $comment->{set_news_status} eq 'unread'; - print STDERR "setRead " . Dumper($comment); - comments::set_news_status( $dbh, $config, $comment ); - print "done\n"; + print STDERR "setRead " . Dumper($comment); + comments::set_news_status( $dbh, $config, $comment ); + print "done\n"; } sub check_params { - my $params = shift; + my $params = shift; - my $checked = {}; + my $checked = {}; - #actions + #actions - if ( defined $params->{action} ) { - if ( $params->{action} =~ /^(setLock|setRead|showComment)$/ ) { - $checked->{action} = $params->{action}; - } - } + if ( defined $params->{action} ) { + if ( $params->{action} =~ /^(setLock|setRead|showComment)$/ ) { + $checked->{action} = $params->{action}; + } + } - #template - my $template = ''; - if ( defined $checked->{action} ) { - $template = template::check( $params->{template}, 'edit_comment' ) if $checked->{action} eq 'showComment'; - } else { - $template = template::check( $params->{template}, 'comments' ); - } - $checked->{template} = $template; + #template + my $template = ''; + if ( defined $checked->{action} ) { + $template = template::check( $params->{template}, 'edit_comment' ) if $checked->{action} eq 'showComment'; + } else { + $template = template::check( $params->{template}, 'comments' ); + } + $checked->{template} = $template; - #numeric values - for my $param ( 'project_id', 'studio_id', 'default_studio_id' ) { - if ( ( defined $params->{$param} ) && ( $params->{$param} =~ /^\d+$/ ) ) { - $checked->{$param} = $params->{$param}; - } - } - if ( defined $checked->{studio_id} ) { - $checked->{default_studio_id} = $checked->{studio_id}; - } else { - $checked->{studio_id} = -1; - } + #numeric values + for my $param ( 'project_id', 'studio_id', 'default_studio_id' ) { + if ( ( defined $params->{$param} ) && ( $params->{$param} =~ /^\d+$/ ) ) { + $checked->{$param} = $params->{$param}; + } + } + if ( defined $checked->{studio_id} ) { + $checked->{default_studio_id} = $checked->{studio_id}; + } else { + $checked->{studio_id} = -1; + } - my $comment = {}; + my $comment = {}; - for my $key ('readStatus') { - my $value = $params->{$key}; - $comment->{$key} = $value if ( defined $value ) && ( $value =~ /^(received|unread)$/ ); - } + for my $key ('readStatus') { + my $value = $params->{$key}; + $comment->{$key} = $value if ( defined $value ) && ( $value =~ /^(received|unread)$/ ); + } - for my $key ('lockStatus') { - my $value = $params->{$key}; - $comment->{$key} = $value if ( defined $value ) && ( $value =~ /^(blocked|show)$/ ); - } + for my $key ('lockStatus') { + my $value = $params->{$key}; + $comment->{$key} = $value if ( defined $value ) && ( $value =~ /^(blocked|show)$/ ); + } - $comment->{event_start} = time::check_date( $params->{event_start} ) || ''; - $comment->{from} = time::check_date( $params->{from} ) || ''; - $comment->{till} = time::check_date( $params->{till} ) || ''; + $comment->{event_start} = time::check_date( $params->{event_start} ) || ''; + $comment->{from} = time::check_date( $params->{from} ) || ''; + $comment->{till} = time::check_date( $params->{till} ) || ''; - my $event_id = $params->{event_id} || ''; - if ( $event_id =~ /^(\d+)$/ ) { - $comment->{event_id} = $1; - } else { + my $event_id = $params->{event_id} || ''; + if ( $event_id =~ /^(\d+)$/ ) { + $comment->{event_id} = $1; + } else { - # error('invalid event_id'); - } - $comment->{event_id} = '' unless defined $comment->{event_id}; + # error('invalid event_id'); + } + $comment->{event_id} = '' unless defined $comment->{event_id}; - my $id = $params->{comment_id} || ''; - if ( $id =~ /^(\d+)$/ ) { - $comment->{comment_id} = $1; - } - $comment->{comment_id} = '' unless defined $comment->{comment_id}; + my $id = $params->{comment_id} || ''; + if ( $id =~ /^(\d+)$/ ) { + $comment->{comment_id} = $1; + } + $comment->{comment_id} = '' unless defined $comment->{comment_id}; - my $age = $params->{age} || ''; - if ( $age =~ /^(\d+)$/ ) { - $comment->{age} = $1; - } - $comment->{age} = '365' unless defined $comment->{age}; + my $age = $params->{age} || ''; + if ( $age =~ /^(\d+)$/ ) { + $comment->{age} = $1; + } + $comment->{age} = '365' unless defined $comment->{age}; - my $search = $params->{search} || ''; - if ( ( defined $search ) && ( $search ne '' ) ) { - $search = substr( $search, 0, 100 ); - $search =~ s/^\s+//gi; - $search =~ s/\s+$//gi; - $search =~ s/\-\-//gi; - $search =~ s/\;//gi; - $checked->{search} = $search if $search ne ''; - } - $checked->{search} = '' unless defined $checked->{search}; - $checked->{comment} = $comment; + my $search = $params->{search} || ''; + if ( ( defined $search ) && ( $search ne '' ) ) { + $search = substr( $search, 0, 100 ); + $search =~ s/^\s+//gi; + $search =~ s/\s+$//gi; + $search =~ s/\-\-//gi; + $search =~ s/\;//gi; + $checked->{search} = $search if $search ne ''; + } + $checked->{search} = '' unless defined $checked->{search}; + $checked->{comment} = $comment; - return $checked; + return $checked; } diff --git a/website/agenda/planung/event.cgi b/website/agenda/planung/event.cgi index 2e9b046..23fc537 100755 --- a/website/agenda/planung/event.cgi +++ b/website/agenda/planung/event.cgi @@ -373,7 +373,7 @@ sub getJson { } ); - if ( @$series == 1 ) { + if ( scalar @$series == 1 ) { my $serie = $series->[0]; $event->{has_single_events} = $serie->{has_single_events}; if ( $event->{has_single_events} eq '1' ) { diff --git a/website/agenda/planung/image.cgi b/website/agenda/planung/image.cgi index d1d6d5b..26d7031 100755 --- a/website/agenda/planung/image.cgi +++ b/website/agenda/planung/image.cgi @@ -71,7 +71,7 @@ return unless defined uac::check( $config, $params, $user_presets ); my $local_media_dir = $config->{locations}->{local_media_dir}; my $local_media_url = $config->{locations}->{local_media_url}; -log::error( $config, 'cannot locate media dir' . $local_media_dir ) unless -e $local_media_dir; +log::error( $config, 'cannot locate media dir ' . $local_media_dir ) unless -e $local_media_dir; uac::permissions_denied('reading from local media dir') unless -r $local_media_dir; uac::permissions_denied('writing to local media dir') unless -w $local_media_dir; @@ -179,7 +179,7 @@ sub show_image { ); #print STDERR Dumper($imagesByNames); - $selectedImage = $imagesByNames->[0] if scalar(@$imagesByNames) > 0; + $selectedImage = $imagesByNames->[0] if scalar @$imagesByNames > 0; } my $finalResults = []; diff --git a/website/agenda/planung/imageUpload.cgi b/website/agenda/planung/imageUpload.cgi index 5b73d05..b70870d 100755 --- a/website/agenda/planung/imageUpload.cgi +++ b/website/agenda/planung/imageUpload.cgi @@ -350,6 +350,7 @@ sub process_image { #$image->Normalize(); $image->Write( 'jpg:' . $image_path ); + #write thumb my $thumb = $image; $thumb->Trim2Square; diff --git a/website/agenda/planung/projects.cgi b/website/agenda/planung/projects.cgi index 14a60bb..d498c99 100755 --- a/website/agenda/planung/projects.cgi +++ b/website/agenda/planung/projects.cgi @@ -137,7 +137,7 @@ sub save_project { return; } my $projects = project::get( $config, { name => $entry->{name} } ); - if ( @$projects > 0 ) { + if ( scalar @$projects > 0 ) { uac::print_error("project with name '$entry->{name}' already exists"); return; } diff --git a/website/agenda/planung/roles.cgi b/website/agenda/planung/roles.cgi index 1b33f1e..89d8364 100755 --- a/website/agenda/planung/roles.cgi +++ b/website/agenda/planung/roles.cgi @@ -334,12 +334,6 @@ sub show_roles { #add permission rows $columns = sort_columns($columns); - # print '
';
-	#    for my $key (@$columns){
-	#         printf ("        %-40s => '',\n", "'".$key."'");
-	#    }
-	#    print '
'; - for my $key (@$columns) { next if ( $key eq 'level' diff --git a/website/agenda/planung/series.cgi b/website/agenda/planung/series.cgi index fbf14ad..07c4684 100755 --- a/website/agenda/planung/series.cgi +++ b/website/agenda/planung/series.cgi @@ -361,9 +361,6 @@ sub save_series { } ); - #print STDERR "done\n"; - #my @series_ids=@$series_ids; - if ( $params->{action} eq 'create' ) { unless ( $permissions->{create_series} == 1 ) { diff --git a/website/agenda/planung/show-playout.cgi b/website/agenda/planung/show-playout.cgi index 1724f23..2bfbaa8 100755 --- a/website/agenda/planung/show-playout.cgi +++ b/website/agenda/planung/show-playout.cgi @@ -18,7 +18,6 @@ use studios; use series; use template; use playout; - binmode STDOUT, ":utf8"; my $r = shift; diff --git a/website/agenda/planung/showImage.cgi b/website/agenda/planung/showImage.cgi old mode 100644 new mode 100755 index 3b92530..be7cb6b --- a/website/agenda/planung/showImage.cgi +++ b/website/agenda/planung/showImage.cgi @@ -72,6 +72,11 @@ sub showImage { } my $filename = images::getInternalPath( $config, $params ); + unless ( defined $filename ) { + uac::permissions_denied("could not find path"); + return; + } + unless ( -e $filename ) { uac::permissions_denied("read $filename"); return; diff --git a/website/agenda/planung/studios.cgi b/website/agenda/planung/studios.cgi index 2bbe513..12eaeb7 100755 --- a/website/agenda/planung/studios.cgi +++ b/website/agenda/planung/studios.cgi @@ -106,7 +106,7 @@ sub delete_studio { } ); - unless ( @$studio_assignments == 0 ) { + unless ( scalar @$studio_assignments == 0 ) { uac::print_info("Studio unassigned from project"); uac::print_warn("Studio is assigned to other projects, so it will not be deleted"); return undef; @@ -141,7 +141,7 @@ sub save_studio { studios::update( $config, $entry ); } else { my $studios = studios::get( $config, { name => $entry->{name} } ); - if ( @$studios > 0 ) { + if ( scalar @$studios > 0 ) { uac::print_error("studio with name '$entry->{name}' already exists"); return; } @@ -166,7 +166,7 @@ sub save_studio { has_single_events => 1 } ); - if ( @$single_series == 0 ) { + if ( scalar @$single_series == 0 ) { series::insert( $config, { diff --git a/website/agenda/planung/templates/default.html b/website/agenda/planung/templates/default.html index bf69da7..7ac0514 100644 --- a/website/agenda/planung/templates/default.html +++ b/website/agenda/planung/templates/default.html @@ -51,10 +51,6 @@
- -
-
-