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