Merge remote-tracking branch 'origin/new/bug_3215'
[koha.git] / 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 under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 # This script builds an ICalendar file (rfc 2445) for use in programs such as Ical
21
22 use strict;
23 use warnings;
24
25 use CGI;
26 use Data::ICal;
27 use Data::ICal::Entry::Event;
28 use DateTime;
29 use DateTime::Format::ICal;
30 use Date::Calc qw (Parse_Date);
31
32 use C4::Auth;
33 use C4::Koha;
34 use C4::Circulation;
35 use C4::Members;
36 use C4::Dates;
37
38 my $query = new CGI;
39 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
40     {
41         template_name   => "opac-user.tmpl",
42         query           => $query,
43         type            => "opac",
44         authnotrequired => 0,
45         flagsrequired   => { borrow => 1 },
46         debug           => 1,
47     }
48 );
49
50 # get borrower information ....
51 my ( $borr ) =  GetMemberDetails( $borrowernumber );
52
53 # Create Calendar
54 my $calendar = Data::ICal->new();
55
56 # get issued items ....
57 my ($issues) = GetPendingIssues($borrowernumber);
58
59 foreach my $issue ( @$issues ) {
60     my $vevent = Data::ICal::Entry::Event->new();
61     my ($year,$month,$day)=Parse_Date($issue->{'date_due'});
62     ($year,$month,$day)=split /-|\/|\.|:/,$issue->{'date_due'} unless ($year && $month);
63 #    Decode_Date_EU2($string))
64     my $datestart = DateTime->new(
65         day    => $day,
66         month  => $month,
67         year   => $year,
68         hour   => 9,
69         minute => 0,
70         second => 0
71     );
72     my $dateend = DateTime->new(
73         day    => $day,
74         month  => $month,
75         year   => $year,
76         hour   => 10,
77         minute => 0,
78         second => 0
79     );
80     $vevent->add_properties(
81         summary => "$issue->{'title'} Due",
82         description =>
83 "Your copy of $issue->{'title'} barcode $issue->{'barcode'} is due back at the library today",
84         dtstart => DateTime::Format::ICal->format_datetime($datestart),
85         dtend   => DateTime::Format::ICal->format_datetime($dateend),
86     );
87     $calendar->add_entry($vevent);
88 }
89
90 print $query->header(
91     -type        => 'application/octet-stream',
92     -attachment => 'koha.ics'
93 );
94
95
96 print $calendar->as_string;