c8116b4efc6b82b7151ecbc2529e12d772a3de5d
[srvgit] / opac / opac-ics.pl
1 #!/usr/bin/perl
2
3 # Copyright 2007 Liblime Ltd
4
5 # This file is part of Koha.
6 #
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.
11 #
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.
16 #
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>.
19
20 # This script builds an ICalendar file (rfc 2445) for use in programs such as Ical
21
22 use Modern::Perl;
23
24 use CGI qw ( -utf8 );
25 use Data::ICal;
26 use Data::ICal::Entry::Event;
27 use DateTime;
28 use DateTime::Format::ICal;
29 use DateTime::Event::ICal;
30 use URI;
31
32 use C4::Auth;
33 use C4::Koha;
34 use C4::Circulation;
35 use C4::Members;
36 use Koha::DateUtils;
37
38 my $query = CGI->new;
39 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
40     {
41         template_name   => "opac-ics.tt",
42         query           => $query,
43         type            => "opac",
44     }
45 );
46
47 # Create Calendar
48 my $calendar = Data::ICal->new();
49
50 my $patron = Koha::Patrons->find( $borrowernumber );
51 my $pending_checkouts = $patron->pending_checkouts;
52
53 while ( my $c = $pending_checkouts->next ) {
54     my $issue = $c->unblessed_all_relateds;
55     my $vevent = Data::ICal::Entry::Event->new();
56     my $timestamp = dt_from_string(undef,undef,"UTC"); #Get current time in UTC
57     # Send some values to the template to generate summary and description
58     $issue->{overdue} = $c->is_overdue;
59     $template->param(
60         overdue => $issue->{'overdue'},
61         title   => $issue->{'title'},
62         barcode => $issue->{'barcode'},
63     );
64     # Catch the result of the template and split on newline
65     my ($summary,$description) = split /\n/, $template->output;
66     my $datestart;
67     if ($issue->{'overdue'} && $issue->{'overdue'} == 1) {
68         # Not much use adding an event in the past for a book that is overdue
69         # so we set datestart = now
70         $datestart = $timestamp;
71     } else {
72         $datestart = dt_from_string($issue->{'date_due'});
73         $datestart->set_time_zone('UTC');
74     }
75     # Create a UID that includes the issue number and the domain
76     my $domain = '';
77     my $baseurl = C4::Context->preference('OPACBaseURL');
78     if ( $baseurl ne '' ) {
79         my $url = URI->new($baseurl);
80         $domain = $url->host;
81     } else {
82         warn "Make sure the systempreference OPACBaseURL is set!";
83     }
84     my $uid = 'issue-' . $issue->{'issue_id'} . '@' . $domain;
85     # Create the event
86     $vevent->add_properties(
87         summary     => $summary,
88         description => $description,
89         dtstamp     => DateTime::Format::ICal->format_datetime($timestamp),
90         dtstart     => DateTime::Format::ICal->format_datetime($datestart),
91         uid         => $uid,
92     );
93     # Add it to the calendar
94     $calendar->add_entry($vevent);
95 }
96
97 print $query->header(
98     -type       => 'application/octet-stream',
99     -attachment => 'koha.ics'
100 );
101
102 print $calendar->as_string;