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