Bug 15451: Better error handling
[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
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 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 qw ( -utf8 );
27 use C4::Output;
28 use Koha::DateUtils;;
29
30 use vars qw($debug);
31
32 BEGIN {
33     $debug = $ENV{DEBUG} || 0;
34 }
35
36 my $input = new CGI;
37 my $base;
38
39 if ( C4::Context->config('supportdir') ) {
40      $base = C4::Context->config('supportdir');
41 }
42 else {
43      $base = "/usr/share/koha/bin";
44 }
45
46 my $CONFIG_NAME = $ENV{'KOHA_CONF'};
47
48 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
49     {
50         template_name   => "tools/scheduler.tt",
51         query           => $input,
52         type            => "intranet",
53         authnotrequired => 0,
54         flagsrequired   => { tools => 'schedule_tasks' },
55         debug           => 1,
56     }
57 );
58
59 my $mode = $input->param('mode');
60 my $id   = $input->param('id');
61
62 if ( $mode eq 'job_add' ) {
63
64     # Retrieving the date according to the dateformat syspref
65     my $c4date = output_pref({ dt => dt_from_string( scalar $input->param('startdate') ), dateformat => 'iso', dateonly => 1 });
66
67     # Formatting it for Schedule::At
68     my $startdate = join('', (split /-/, $c4date));
69
70     my $starttime = $input->param('starttime');
71     $starttime =~ s/\://g;
72     my $start  = $startdate . $starttime;
73     my $report = $input->param('report');
74     my $format = $input->param('format');
75     my $email  = $input->param('email');
76     my $command =
77         "export KOHA_CONF=\"$CONFIG_NAME\"; " .
78         "$base/cronjobs/runreport.pl $report --format=$format --to=$email";
79
80 #FIXME commit ea899bc55933cd74e4665d70b1c48cab82cd1257 added recurring parameter (it is not in template) and call to add_cron_job (undefined)
81 #    my $recurring = $input->param('recurring');
82 #    if ($recurring) {
83 #        my $frequency = $input->param('frequency');
84 #        add_cron_job( $start, $command );
85 #    }
86 #    else {
87 #        #here was the the unless ( add_at_job
88 #    }
89
90     unless ( add_at_job( $start, $command ) ) {
91         $template->param( job_add_failed => 1 );
92     }
93 }
94
95 if ( $mode eq 'job_change' ) {
96     my $jobid = $input->param('jobid');
97     if ( $input->param('delete') ) {
98         remove_at_job($jobid);
99     }
100 }
101
102 my $jobs = get_jobs();
103 my @jobloop;
104 foreach my $job ( values %$jobs ) {
105     push @jobloop, $job;
106 }
107
108 @jobloop = sort { $a->{TIME} cmp $b->{TIME} } @jobloop;
109
110 my $reports = get_saved_reports();
111 if ( defined $id ) {
112     foreach my $report (@$reports) {
113         $report->{'selected'} = 1 if $report->{'id'} eq $id;
114     }
115 }
116
117 $template->param( 'savedreports' => $reports );
118 $template->param( JOBS           => \@jobloop );
119 my $time = localtime(time);
120 $template->param( 'time' => $time );
121 $template->param(
122     debug                    => $debug,
123 );
124 output_html_with_http_headers $input, $cookie, $template->output;