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
27 use Data::ICal::Entry::Event;
29 use DateTime::Format::ICal;
30 use DateTime::Event::ICal;
40 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
42 template_name => "opac-ics.tt",
51 my $calendar = Data::ICal->new();
53 # get issued items ....
54 my $issues = GetPendingIssues($borrowernumber);
56 foreach my $issue ( @$issues ) {
57 my $vevent = Data::ICal::Entry::Event->new();
58 my $timestamp = DateTime->now(); # Defaults to UTC
59 # Send some values to the template to generate summary and description
61 overdue => $issue->{'overdue'},
62 title => $issue->{'title'},
63 barcode => $issue->{'barcode'},
65 # Catch the result of the template and split on newline
66 my ($summary,$description) = split /\n/, $template->output;
68 if ($issue->{'overdue'} && $issue->{'overdue'} == 1) {
69 # Not much use adding an event in the past for a book that is overdue
70 # so we set datestart = now
71 $datestart = $timestamp;
73 $datestart = dt_from_string($issue->{'date_due'});
74 $datestart->set_time_zone('UTC');
76 # Create a UID that includes the issue number and the domain
78 my $baseurl = C4::Context->preference('OPACBaseURL');
79 if ( $baseurl ne '' ) {
80 my $url = URI->new($baseurl);
83 warn "Make sure the systempreference OPACBaseURL is set!";
85 my $uid = 'issue-' . $issue->{'issue_id'} . '@' . $domain;
87 $vevent->add_properties(
89 description => $description,
90 dtstamp => DateTime::Format::ICal->format_datetime($timestamp),
91 dtstart => DateTime::Format::ICal->format_datetime($datestart),
94 # Add it to the calendar
95 $calendar->add_entry($vevent);
99 -type => 'application/octet-stream',
100 -attachment => 'koha.ics'
103 print $calendar->as_string;