3 # Copyright 2007 Liblime Ltd
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 # This script builds an ICalendar file (rfc 2445) for use in programs such as Ical
26 use Data::ICal::Entry::Event;
28 use DateTime::Format::ICal;
29 use DateTime::Event::ICal;
32 use C4::Auth qw( get_template_and_user );
33 use Koha::DateUtils qw( dt_from_string );
36 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
38 template_name => "opac-ics.tt",
45 my $calendar = Data::ICal->new();
47 my $patron = Koha::Patrons->find( $borrowernumber );
48 my $pending_checkouts = $patron->pending_checkouts;
50 my $timestamp = dt_from_string(undef,undef,"UTC"); #Get current time in UTC
52 while ( my $c = $pending_checkouts->next ) {
53 my $issue = $c->unblessed_all_relateds;
54 my $vevent = Data::ICal::Entry::Event->new();
55 # Send some values to the template to generate summary and description
56 $issue->{overdue} = $c->is_overdue;
58 overdue => $issue->{'overdue'},
59 title => $issue->{'title'},
60 barcode => $issue->{'barcode'},
62 # Catch the result of the template and split on newline
63 my ($summary,$description) = split /\n/, $template->output;
64 my ( $datestart, $datestart_local );
65 if ($issue->{'overdue'} && $issue->{'overdue'} == 1) {
66 # Not much use adding an event in the past for a book that is overdue
67 # so we set datestart = now
68 $datestart = $timestamp->clone();
69 $datestart_local = $datestart->clone();
71 $datestart = dt_from_string($issue->{'date_due'});
72 $datestart_local = $datestart->clone();
73 $datestart->set_time_zone('UTC');
75 # Create a UID that includes the issue number and the domain
77 my $baseurl = C4::Context->preference('OPACBaseURL');
78 if ( $baseurl ne '' ) {
79 my $url = URI->new($baseurl);
82 warn "Make sure the systempreference OPACBaseURL is set!";
84 my $uid = 'issue-' . $issue->{'issue_id'} . '@' . $domain;
88 if ($issue->{'overdue'} && $issue->{'overdue'} == 1) {
89 # It's already overdue so make it due as an all day event today
90 $dtstart = [ $datestart->ymd(q{}), { VALUE => 'DATE' } ];
91 } elsif ( $datestart_local->hour eq '23' && $datestart_local->minute eq '59' ) {
92 # Checkouts due at 23:59 are "all day events"
93 $dtstart = [ $datestart->ymd(q{}), { VALUE => 'DATE' } ];
95 else { # Checkouts due any other time are instantaneous events at the date and time due
96 $dtstart = DateTime::Format::ICal->format_datetime($datestart);
99 $vevent->add_properties(
101 description => $description,
102 dtstamp => DateTime::Format::ICal->format_datetime($timestamp),
106 # Add it to the calendar
107 $calendar->add_entry($vevent);
110 print $query->header(
111 -type => 'application/octet-stream',
112 -attachment => 'koha.ics'
115 print $calendar->as_string;