Files
racalmas/tools/sync_cms/lib/CalcmsEvents.pm
Milan 56881b92d0 rewrite Export to Google Calendar
In the past Google Calendar exports were done by first removing all
events of a day and then create new ones. This takes a lot of time for
export and runs into Google Calendar usage limits after some time.

By now content will be compared before removing/creating a single event
one. To be able to do so, all other sync sources and targets have been
removed, so its only possible to export from database to Google Calendar
by this change.

To trigger an export you need to create a trigger file.
run_jobs.pl runs periodically e.g. started by cron and checks if a
trigger file exists and start sync_cms.pl to export the selected events
to the Google Calendar. Trigger files and jobs are configured at
jobs.config. Each job has a source and target file containing the access
data for calcms and the calendar.

Configuration files have been cleaned up. Old Accounts and passwords have
been removed. They hopefully should have been not active for a long time ;-)
2018-09-22 23:09:03 +02:00

136 lines
3.0 KiB
Perl

package CalcmsEvents;
use strict;
use warnings;
use Common ('info','error');
use DateTime;
use Data::Dumper;
use creole_wiki;
use events;
use time;
#use config;
my $settings = {};
sub init($) {
$settings = shift || {};
}
sub set($$) {
my $key = shift;
my $value = shift;
$settings->{$key} = $value;
}
sub get($) {
my $key = shift;
return $settings->{$key};
}
# return a list of start_min, start_max request parameters.
sub splitRequest($$$) {
my $from = shift;
my $till = shift;
my $timeZone = shift;
return undef unless defined $from;
return undef unless defined $till;
return undef if $from eq '';
return undef if $till eq '';
my $dates = [];
my $start = time::get_datetime( $from, $timeZone );
my $end = time::get_datetime( $till, $timeZone );
#build a list of dates
my $date = $start;
my @dates = ();
while ( $date < $end ) {
push @dates, $date;
$date = $date->clone->add( days => 7 );
}
my $duration = $end - $date;
push @dates, $end->clone if $duration->delta_seconds <= 0;
#build a list of parameters from dates
$start = shift @dates;
for my $end (@dates) {
push @$dates,
{
from => $start,
till => $end
};
$start = $end;
}
return $dates;
}
#get a hash with per-day-lists days of a google calendar, given by its url defined at $calendar_name
sub getEvents($$) {
my $from = shift;
my $till = shift;
my $last_update = get('last_update');
info "getEvents from $from till $till";
my $request_parameters = {
from_date => $from,
till_date => $till,
project => get('project'),
archive => 'all',
template => 'no'
};
my $location = get('location') || '';
$request_parameters->{location} = $location if $location ne '';
my $config = $settings;
my %params = ();
my $request = {
url => $ENV{QUERY_STRING},
params => {
original => \%params,
checked => events::check_params( $config, $request_parameters, $settings ),
},
};
my $sourceEvents = events::get( $config, $request, $settings );
#return events by date
my $eventsByDate = {};
for my $source (@$sourceEvents) {
$source->{calcms_start} = $source->{start};
my $key = substr( $source->{start}, 0, 10 );
push @{ $eventsByDate->{$key} }, $source;
}
return $eventsByDate;
}
sub mapToSchema {
my $event = shift;
#override settings by source map filter
for my $key ( keys %{ get('mapping') } ) {
$event->{$key} = get('mapping')->{$key};
}
#resolve variables set in mapped values
for my $mkey ( keys %{ get('mapping') } ) {
for my $key ( keys %{$event} ) {
my $val = $event->{$key};
$val = $event->{$key} if ( $mkey eq $key );
$event->{$mkey} =~ s/<TMPL_VAR $key>/$val/g;
}
}
return $event;
}
#do not delete last line
1;