Bug 10240: DBRev 3.13.00.027
[koha.git] / tools / scheduler.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 use strict;
21 #use warnings; FIXME - Bug 2505
22 use C4::Context;
23 use C4::Scheduler;
24 use C4::Reports::Guided;
25 use C4::Auth;
26 use CGI;
27 use C4::Output;
28 use C4::Dates;
29
30 use vars qw($debug);
31
32 BEGIN {
33     $debug = $ENV{DEBUG} || 0;
34 }
35
36 my $input = new CGI;
37
38 my $base        = C4::Context->config('intranetdir');
39 my $CONFIG_NAME = $ENV{'KOHA_CONF'};
40
41 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
42     {
43         template_name   => "tools/scheduler.tmpl",
44         query           => $input,
45         type            => "intranet",
46         authnotrequired => 0,
47         flagsrequired   => { tools => 'schedule_tasks' },
48         debug           => 1,
49     }
50 );
51
52 my $mode = $input->param('mode');
53 my $id   = $input->param('id');
54
55 if ( $mode eq 'job_add' ) {
56
57     # Retrieving the date according to the dateformat syspref
58     my $c4date = C4::Dates->new($input->param('startdate'));
59
60     # Formatting it for Schedule::At
61     my $startdate = join('', (split /-/, $c4date->output("iso")));
62
63     my $starttime = $input->param('starttime');
64     my $recurring = $input->param('recurring');
65     $starttime =~ s/\://g;
66     my $start  = $startdate . $starttime;
67     my $report = $input->param('report');
68     my $format = $input->param('format');
69     my $email  = $input->param('email');
70     my $command =
71         "EXPORT KOHA_CONF=\"$CONFIG_NAME\"; " . $base
72       . "/tools/runreport.pl $report $format $email";
73
74     if ($recurring) {
75         my $frequency = $input->param('frequency');
76         add_cron_job( $start, $command );
77     }
78     else {
79         unless ( add_at_job( $start, $command ) ) {
80             $template->param( job_add_failed => 1 );
81         }
82     }
83 }
84
85 if ( $mode eq 'job_change' ) {
86     my $jobid = $input->param('jobid');
87     if ( $input->param('delete') ) {
88         remove_at_job($jobid);
89     }
90 }
91
92 my $jobs = get_jobs();
93 my @jobloop;
94 foreach my $job ( values %$jobs ) {
95     push @jobloop, $job;
96 }
97
98 @jobloop = sort { $a->{TIME} cmp $b->{TIME} } @jobloop;
99
100 my $reports = get_saved_reports();
101 if ( defined $id ) {
102     foreach my $report (@$reports) {
103         $report->{'selected'} = 1 if $report->{'id'} eq $id;
104     }
105 }
106
107 $template->param( 'savedreports' => $reports );
108 $template->param( JOBS           => \@jobloop );
109 my $time = localtime(time);
110 $template->param( 'time' => $time );
111 $template->param(
112     debug                    => $debug,
113 );
114 output_html_with_http_headers $input, $cookie, $template->output;