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