kohabug 1993 - task scheduler improvements
[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 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 use strict;
21 use C4::Context;
22 use C4::Scheduler;
23 use C4::Reports;
24 use C4::Auth;
25 use CGI;
26 use C4::Output;
27
28 my $input = new CGI;
29
30 my $base = C4::Context->config('intranetdir');
31 my $CONFIG_NAME = $ENV{'KOHA_CONF'};
32
33 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
34             {
35                 template_name   => "tools/scheduler.tmpl",
36                 query           => $input,
37                 type            => "intranet",
38                 authnotrequired => 0,
39                 flagsrequired   => { tools => 'schedule_tasks' },
40                 debug           => 1,
41             }
42         );
43
44 my $mode=$input->param('mode');
45 my $id = $input->param('id');
46
47 if ($mode eq 'job_add') {
48         my $startday   = $input->param('startday');
49         my $startmonth = $input->param('startmonth');
50         my $startyear  = $input->param('startyear');
51         my $starttime  = $input->param('starttime');
52         my $recurring = $input->param('recurring');     
53         $starttime  =~ s/\://g;
54         my $start = $startyear . $startmonth . $startday . $starttime;
55         my $report=$input->param('report');
56         my $format=$input->param('format');
57         my $email=$input->param('email');
58         my $command = "EXPORT KOHA_CONF=\"$CONFIG_NAME\"; ".$base."/tools/runreport.pl $report $format $email";
59         if ($recurring){
60             my $frequency = $input->param('frequency');
61             add_cron_job($start,$command);
62         }
63         else {
64             unless (add_at_job($start,$command)) {
65             $template->param(job_add_failed => 1);
66         }
67         }
68 }
69
70 if ($mode eq 'job_change'){
71         my $jobid = $input->param('jobid');
72         if ($input->param('delete')){
73                 remove_at_job($jobid);
74         }
75 }
76
77 my $jobs = get_jobs();
78 my @jobloop;
79  foreach my $job (values %$jobs) {
80      push @jobloop,$job;
81 }
82
83 @jobloop = sort {$a->{TIME} cmp $b->{TIME}} @jobloop;
84
85 my $reports = get_saved_reports();
86 if (defined $id) {
87     foreach my $report (@$reports) {
88         $report->{'selected'} = 1 if $report->{'id'} eq $id;
89     }
90 }
91
92 $template->param( 'savedreports' => $reports ); 
93 $template->param(JOBS => \@jobloop);
94 my $time = localtime(time);
95 $template->param('time' => $time);
96 output_html_with_http_headers $input, $cookie, $template->output;