Bug 12031: Task Scheduler not sending mail
[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 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 = C4::Dates->new($input->param('startdate'));
66
67     # Formatting it for Schedule::At
68     my $startdate = join('', (split /-/, $c4date->output("iso")));
69
70     my $starttime = $input->param('starttime');
71     my $recurring = $input->param('recurring');
72     $starttime =~ s/\://g;
73     my $start  = $startdate . $starttime;
74     my $report = $input->param('report');
75     my $format = $input->param('format');
76     my $email  = $input->param('email');
77     my $command =
78         "export KOHA_CONF=\"$CONFIG_NAME\"; " . $base
79       . "/cronjobs/runreport.pl $report --format=$format --to=$email";
80
81     if ($recurring) {
82         my $frequency = $input->param('frequency');
83         add_cron_job( $start, $command );
84     }
85     else {
86         unless ( add_at_job( $start, $command ) ) {
87             $template->param( job_add_failed => 1 );
88         }
89     }
90 }
91
92 if ( $mode eq 'job_change' ) {
93     my $jobid = $input->param('jobid');
94     if ( $input->param('delete') ) {
95         remove_at_job($jobid);
96     }
97 }
98
99 my $jobs = get_jobs();
100 my @jobloop;
101 foreach my $job ( values %$jobs ) {
102     push @jobloop, $job;
103 }
104
105 @jobloop = sort { $a->{TIME} cmp $b->{TIME} } @jobloop;
106
107 my $reports = get_saved_reports();
108 if ( defined $id ) {
109     foreach my $report (@$reports) {
110         $report->{'selected'} = 1 if $report->{'id'} eq $id;
111     }
112 }
113
114 $template->param( 'savedreports' => $reports );
115 $template->param( JOBS           => \@jobloop );
116 my $time = localtime(time);
117 $template->param( 'time' => $time );
118 $template->param(
119     debug                    => $debug,
120 );
121 output_html_with_http_headers $input, $cookie, $template->output;