Bug 34893: Add checkpw change to REST API
[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 output_pref );;
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     # Retrieving the date according to the dateformat syspref
56     my $c4date = output_pref({ dt => dt_from_string( scalar $input->param('startdate') ), dateformat => 'iso', dateonly => 1 });
57
58     # Formatting it for Schedule::At
59     my $startdate = join('', (split /-/, $c4date));
60
61     my $starttime = $input->param('starttime');
62     $starttime =~ s/\://g;
63     my $start  = $startdate . $starttime;
64     my $report = $input->param('report');
65     if ($report) {
66         my $saved_report;
67         my $report_id = int($report);
68         if ($report_id) {
69             $saved_report = Koha::Reports->find($report_id);
70         }
71         if ( !$saved_report ) {
72             $report = undef;
73         }
74     }
75     my $format = $input->param('format');
76     if ($format) {
77         unless ( $format eq 'text' || $format eq 'csv' || $format eq 'html' ) {
78             $format = undef;
79         }
80     }
81     my $email = $input->param('email');
82     if ($email) {
83         my $is_valid = Koha::Email->is_valid($email);
84         if ( !$is_valid ) {
85             $email = undef;
86         }
87     }
88     if ( $report && $format && $email ) {
89
90         #NOTE: Escape any single quotes in email since we're wrapping it in single quotes in bash
91         $email =~ s/'/'"'"'/g;
92         my $command =
93               "export KOHA_CONF=\"$CONFIG_NAME\"; "
94             . "$base/cronjobs/runreport.pl $report --format=$format --to='$email'";
95
96         unless ( add_at_job( $start, $command ) ) {
97             $template->param( job_add_failed => 1 );
98         }
99     }
100     else {
101         $template->param( job_add_failed => 1 );
102     }
103 }
104
105 if ( $mode eq 'job_change' ) {
106     my $jobid = $input->param('jobid');
107     if ( $input->param('delete') ) {
108         remove_at_job($jobid);
109     }
110 }
111
112 my $jobs = get_jobs();
113 my @jobloop;
114 foreach my $job ( values %$jobs ) {
115     push @jobloop, $job;
116 }
117
118 @jobloop = sort { $a->{TIME} cmp $b->{TIME} } @jobloop;
119
120 my $reports = get_saved_reports();
121 if ( defined $id ) {
122     foreach my $report (@$reports) {
123         $report->{'selected'} = 1 if $report->{'id'} eq $id;
124     }
125 }
126
127 $template->param( 'savedreports' => $reports );
128 $template->param( JOBS           => \@jobloop );
129 my $time = localtime(time);
130 $template->param( 'time' => $time );
131 output_html_with_http_headers $input, $cookie, $template->output;