-
Notifications
You must be signed in to change notification settings - Fork 3
/
edit_schedule.cgi
282 lines (225 loc) · 9.43 KB
/
edit_schedule.cgi
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#!/usr/bin/perl
require './jru-lib.pl';
sub build_cronline{
my $schid = $_[0];
my $cron_line = '';
if($in{'cron_period'} eq 'custom'){
$cron_line .= $in{'cron_custom'};
$cron_line .= ' root ';
}
$cron_line .= build_cmd_line(\%in);
return $cron_line;
}
&ReadParse();
if($in{'cron_period'} eq 'now'){
$in{'schid'} = '0'; #for on-demain schedules, we use the special ID 0
my $cmd0 = build_cmd_line(\%in);
#run the on-demand schedule
&redirect("/jri_publisher/report_run.cgi?schid=0&back=schedule");
return 0;
}
if($in{'submit_flag'} == 2){
my %schedules = load_schedules();
my %sched = %{$schedules{$in{'schid'}}};
my $old_period = 'custom';
if($sched{'cron'} =~ /^@(.*)/){
$old_period = $1;
}
my $cronfile = get_jri_cronfile($old_period); #file with old cron entry
my $sch_env = get_jasper_home().'/schedules/'.$sched{'schid'}.'_env.sh';
#get line number from id
my $ln=0;
if($sched{'fln'} =~ /[a-z]+([0-9]+)/){
$ln = $1;
}else{
&error('Invalid schedule ID '.$sched{'fln'});
}
my $lref = &read_file_lines($cronfile);
if($in{'cron_period'} ne $old_period){ #if period is different, files are different
@{$lref}[$ln] = '#'.@{$lref}[$ln]; #just comment out the line
$in{'submit_flag'} = 1; #change mode to add
&unlink_file($sch_env);
}elsif($in{'but_delete'}){
@{$lref}[$ln] = '#'.@{$lref}[$ln]; #just comment out the line
$in{'submit_flag'} = 0;
$in{'schid'} = 0;
&unlink_file($sch_env);
}else{ #period is the same, so just update line
@{$lref}[$ln] = build_cronline();
$in{'submit_flag'} = 0;
$in{'schid'} = 0;
}
&flush_file_lines($cronfile);
}
if($in{'submit_flag'} == 1){
my $cron_filename = get_jri_cronfile($in{'cron_period'});
if($in{'cron_period'} eq 'custom'){
#if file doesn't exist, add a header
if(! -f $cron_filename){
open(my $fh, '>', $cron_filename) or die "open:$!";
print $fh 'SHELL=/bin/sh'."\n";
print $fh 'PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin'."\n";
print $fh '# m h dom mon dow user command'."\n";
close $fh;
}
}
open(my $fh, '>>', $cron_filename) or die "open:$!".$cron_filename;
$fh->autoflush;
print $fh build_cronline()."\n";
close $fh;
unflush_file_lines($cron_filename);
$in{'submit_flag'} = 0;
}
&ui_print_header(undef, $text{'jru_schedule'}, "", "schedules", 0, 0);
print <<EOF;
<script type="text/javascript">
function update_select(){
var Sel = document.getElementById('cron_period');
if(Sel.options[Sel.selectedIndex].value == 'custom'){
document.getElementById('cron_custom').disabled = false;
}else{
document.getElementById('cron_custom').disabled = true;
}
}
function update_templ(){
var Sel = document.getElementById('repEmailTmpl');
if(Sel.options[Sel.selectedIndex].value != 'none'){
document.getElementById('repEmailTmpl').disabled = false;
}else{
document.getElementById('repEmailTmpl').disabled = true;
}
}
function save_opt_params(){
var Key = document.getElementsByName('optKey')[0];
var Val = document.getElementsByName('optVal')[0];
var Sel = document.getElementsByName('optSelParams')[0];
var label = Key.value + '=' + Val.value;
var option = document.createElement("option");
option.text = label;
option.value = label;
option.selected = true;
Sel.appendChild(option);
Key.value = Val.value = '';
}
function clear_opt_params(){
var Key = document.getElementsByName('optKey')[0];
var Val = document.getElementsByName('optVal')[0];
var Sel = document.getElementsByName('optSelParams')[0];
var i;
for(i = Sel.options.length - 1 ; i >= 0 ; i--)
{
Sel.remove(i);
}
Key.value = Val.value = '';
}
function clear_disable_obj(name){
var Obj = document.getElementsByName(name)[0];
Obj.disabled = !Obj.disabled;
}
function update_nomail(){
var mailObjs = ['repEmail', 'repEmailSubj', 'repEmailBody', 'repEmailTmpl'];
mailObjs.forEach(clear_disable_obj);
}
function update_with_templ(){
var mailObjs = ['repEmailTmpl', 'repEmailBody'];
mailObjs.forEach(clear_disable_obj);
}
</script>
EOF
# Show tabs
@tabs = ( [ "add", $text{'schedule_tab_add'}, "edit_schedule.cgi?mode=add" ],
[ "view", $text{'schedule_tab_view'}, "edit_schedule.cgi?mode=view" ]);
my @rep_formats = ('pdf', 'html', 'html2', 'rtf', 'xls', 'jxl', 'csv', 'xlsx', 'pptx', 'docx');
@opt_repformat = ();
foreach my $fmt (sort @rep_formats) {
push(@opt_repformat, [ $fmt, $fmt]);
}
#re-read the file, after we have added/removed a user
%datasources = get_jru_datasources();
@opt_rep_id = map { [$_, $_] } get_all_rep_ids();
@opt_datasources = map { [$_, $_]} sort keys %datasources;
@opt_cron_period = ();
unshift(@cron_period, 'now');
foreach my $per (@cron_period) {
if($per eq 'now' || $per eq 'custom' || -d '/etc/cron.'.$per){
push(@opt_cron_period, [ $per, $per]);
}
}
@opt_email_tmpl = map { [$_, $_]} get_email_templates();
%schedules = load_schedules();
print &ui_tabs_start(\@tabs, "mode", $in{'mode'} || "view", 1);
# START add tab
print &ui_tabs_start_tab("mode", "add");
print "$text{'schedule_desc1'}<p>\n";
print &ui_form_start("edit_schedule.cgi", "post");
print &ui_table_start($text{'schedule_params'}, undef, 2);
my %sched;
if($in{'name'}){
$sched{'rep_id'} = $in{'name'};
}
if($in{'schid'}){
%sched = %{$schedules{$in{'schid'}}};
my $old_period = 'custom';
if($sched{'cron'} =~ /^@(.*)/){
$old_period = $1;
}
$sched{'cron'} = $old_period;
print &ui_hidden('schid', $in{'schid'});
print &ui_hidden('submit_flag', 2); #edit
}else{
print &ui_hidden('submit_flag', 1); #add
}
#add cron variables - hour, minutes ...
print &ui_table_row($text{'schedule_cron'}, &ui_select("cron_period", $sched{'cron'}, \@opt_cron_period, 1, 0, undef, undef, 'id="cron_period" onchange="update_select()"').
" ".
&ui_textbox("cron_custom", '*/30 * * * *', 20, $sched{'cron'} eq 'custom' ? 0 : 1).
" ".
'<a href="https://crontab.guru" target="_blank">Cron helper</a>'
);
print &ui_table_row($text{'schedule_repname'}, &ui_select("repname", $sched{'rep_id'}, \@opt_rep_id, 1, 0));
print &ui_table_row($text{'schedule_repformat'}, &ui_select("repformat", $sched{'rep_format'}, \@opt_repformat, 1, 0));
print &ui_table_row($text{'schedule_datasource'}, &ui_select("datasource", $sched{'rep_ds'}, \@opt_datasources, 1, 0));
print &ui_table_row($text{'schedule_filename'}, &ui_textbox("filename", $sched{'rep_file'}, 20));
print &ui_table_row($text{'schedule_email'}, &ui_textbox("repEmail", $sched{'rep_rcpt'}, 20, $sched{'noemail'}).
&ui_checkbox("noemail", 1, '<i>'.$text{'schedule_noemail'}."</i>", $sched{'noemail'}, 'onclick="update_nomail()"'));
print &ui_table_end();
print &ui_hidden_table_start($text{'schedule_params_optional'}, undef, 2, 'optional_args', 0);
print &ui_table_row($text{'schedule_email_subj'}, &ui_textbox("repEmailSubj", $sched{'rep_email_subj'}, 20, $sched{'noemail'}));
my $with_tmpl = $sched{'rep_email_tmpl'} ? 1 : 0;
print &ui_table_row($text{'schedule_email_body'}, &ui_textarea("repEmailBody", $sched{'rep_email_body'}, 2, 20, 'off', $sched{'noemail'} || $with_tmpl));
print &ui_table_row($text{'schedule_email_tmpl'}, &ui_select("repEmailTmpl", $sched{'rep_email_tmpl'}, \@opt_email_tmpl, 1, 0, undef, !$with_tmpl).
&ui_checkbox("with_tmpl", 1, '<i>'.$text{'schedule_with_tmpl'}."</i>", $with_tmpl, 'onclick="update_with_templ()"'));
print &ui_table_row($text{'schedule_opt_params'},
&ui_textbox("optKey", '', 10, $sched{'noemail'}, 20, 'id=optKey').'='.
&ui_textbox("optVal", '', 10, $sched{'noemail'}, 20, 'id=optVal').
&ui_button('Save', 'btnOptParamSave', $sched{'noemail'}, 'onclick="save_opt_params()"').
&ui_button('Clear','btnOptParamClear', $sched{'noemail'}, 'onclick="clear_opt_params()"').
'</br>'.
&ui_select('optSelParams', [split(/&/, $sched{'url_opt_params'})], [split(/&/, $sched{'url_opt_params'})], undef, 1, undef, $sched{'noemail'})
);
print &ui_hidden_table_end('optional_args');
if($in{'schid'}){ #if we have and id, its edit mode
print &ui_form_end([ [ "but_update", $text{'jru_update'} ], ["but_delete", $text{'jru_delete'}] ]);
}else{
print &ui_form_end([ [ "", $text{'jru_addok'} ] ]);
}
print &ui_tabs_end_tab();
#END add tab
# START view tab
print &ui_tabs_start_tab("mode", "view");
print "$text{'schedule_desc2'}<p>\n";
my @tds = ( "width=5%", "width=10%", "width=25%", "width=5%", "width=10%", "width=20%", "width=15%", "width=10%");
print &ui_columns_start(['SchID', 'Cron', 'Name', 'Format', 'Datasource', 'Output', 'Email', 'Optional Params'], 100, 0, \@tds);
foreach my $schid (sort keys %schedules){
my %sched = %{$schedules{$schid}};
my @cols = ($schid, $sched{'cron'}, $sched{'rep_id'}, $sched{'rep_format'},
$sched{'rep_ds'}, $sched{'rep_file'}, $sched{'rep_rcpt'}, $sched{'url_opt_params'});
$cols[0] = '<a href="/jri_publisher/edit_schedule.cgi?mode=add&schid='.&urlize($schid).'">'.$schid."</a>";
$cols[7] = join('</br>', split(/&/, $cols[7]));
print &ui_columns_row(\@cols, \@tds);
}
print &ui_columns_end();
print &ui_tabs_end_tab();
#END list tab
print &ui_tabs_end(1);
&ui_print_footer("", $text{'index_return'});