Merge branch 'master' of /usr/local/git/koha_base.git/
[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 with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA  02111-1307 USA
19
20 # This script builds an ICalendar file (rfc 2445) for use in programs such as Ical
21
22 use strict;
23 use CGI;
24 use Data::ICal;
25 use Data::ICal::Entry::Event;
26 use Date::ICal;
27 use Date::Calc qw (Parse_Date);
28
29 use C4::Auth;
30 use C4::Koha;
31 use C4::Circulation::Circ2;
32 use C4::Date;
33
34 my $query = new CGI;
35 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
36     {
37         template_name   => "opac-user.tmpl",
38         query           => $query,
39         type            => "opac",
40         authnotrequired => 0,
41         flagsrequired   => { borrow => 1 },
42         debug           => 1,
43     }
44 );
45
46 # get borrower information ....
47 my ( $borr, $flags ) = getpatroninformation( undef, $borrowernumber );
48
49 # Create Calendar
50 my $calendar = Data::ICal->new();
51
52 # get issued items ....
53 my $issues = getissues($borr);
54
55 foreach my $key ( keys %$issues ) {
56     my $issue  = $issues->{$key};
57     my $vevent = Data::ICal::Entry::Event->new();
58     my ($year,$month,$day)=Parse_Date($issue->{'date_due'});
59     ($year,$month,$day)=split /-|\/|\.|:/,$issue->{'date_due'} unless ($year && $month);
60 #    Decode_Date_EU2($string))
61     my $datestart = Date::ICal->new( 
62         day => $day, 
63         month => $month, 
64         year => $year,
65         hour => 9,
66         min => 0,
67         sec => 0
68     )->ical;
69     my $dateend = Date::ICal->new( 
70         day => $day, 
71         month => $month, 
72         year => $year,
73         hour => 10,
74         min => 0,
75         sec => 0
76     )->ical;
77     $vevent->add_properties(
78         summary => "$issue->{'title'} Due",
79         description =>
80 "Your copy of $issue->{'title'} barcode $issue->{'barcode'} is due back at the library today",
81         dtstart => $datestart,
82         dtend => $dateend,
83     );
84     $calendar->add_entry($vevent);
85 }
86
87 print $query->header(
88     -type        => 'application/octet-stream',
89     -attachment => 'koha.ics'
90 );
91
92
93 print $calendar->as_string;