Bug 20538: Fix copyright notice in Asset.pm
[koha.git] / opac / opac-ics.pl
blob1fbf351ea8cec4057254b409d9725fbeae692442
1 #!/usr/bin/perl
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
22 use strict;
23 use warnings;
25 use CGI qw ( -utf8 );
26 use Data::ICal;
27 use Data::ICal::Entry::Event;
28 use DateTime;
29 use DateTime::Format::ICal;
30 use DateTime::Event::ICal;
31 use URI;
33 use C4::Auth;
34 use C4::Koha;
35 use C4::Circulation;
36 use C4::Members;
37 use Koha::DateUtils;
39 my $query = new CGI;
40 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
42 template_name => "opac-ics.tt",
43 query => $query,
44 type => "opac",
45 authnotrequired => 0,
46 debug => 1,
50 # Create Calendar
51 my $calendar = Data::ICal->new();
53 my $patron = Koha::Patrons->find( $borrowernumber );
54 my $pending_checkouts = $patron->pending_checkouts;
56 while ( my $c = $pending_checkouts->next ) {
57 my $issue = $c->unblessed_all_relateds;
58 my $vevent = Data::ICal::Entry::Event->new();
59 my $timestamp = DateTime->now(); # Defaults to UTC
60 # Send some values to the template to generate summary and description
61 $issue->{overdue} = $c->is_overdue;
62 $template->param(
63 overdue => $issue->{'overdue'},
64 title => $issue->{'title'},
65 barcode => $issue->{'barcode'},
67 # Catch the result of the template and split on newline
68 my ($summary,$description) = split /\n/, $template->output;
69 my $datestart;
70 if ($issue->{'overdue'} && $issue->{'overdue'} == 1) {
71 # Not much use adding an event in the past for a book that is overdue
72 # so we set datestart = now
73 $datestart = $timestamp;
74 } else {
75 $datestart = dt_from_string($issue->{'date_due'});
76 $datestart->set_time_zone('UTC');
78 # Create a UID that includes the issue number and the domain
79 my $domain = '';
80 my $baseurl = C4::Context->preference('OPACBaseURL');
81 if ( $baseurl ne '' ) {
82 my $url = URI->new($baseurl);
83 $domain = $url->host;
84 } else {
85 warn "Make sure the systempreference OPACBaseURL is set!";
87 my $uid = 'issue-' . $issue->{'issue_id'} . '@' . $domain;
88 # Create the event
89 $vevent->add_properties(
90 summary => $summary,
91 description => $description,
92 dtstamp => DateTime::Format::ICal->format_datetime($timestamp),
93 dtstart => DateTime::Format::ICal->format_datetime($datestart),
94 uid => $uid,
96 # Add it to the calendar
97 $calendar->add_entry($vevent);
100 print $query->header(
101 -type => 'application/octet-stream',
102 -attachment => 'koha.ics'
105 print $calendar->as_string;