1 <% include( 'elements/edit.html',
2 'name' => 'Billing event definition',
3 'table' => 'part_event',
6 { field => 'eventtable',
8 options => [ FS::part_event->eventtables ],
9 labels => $eventtable_labels,
10 onchange => 'eventtable_changed',
12 { field => 'agentnum',
13 type => 'select-agent',
14 disable_empty => $disable_empty_agent,
16 { field => 'check_freq',
18 options => [ '1d', '1m' ],
19 labels => $check_freq_labels,
21 { field => 'disabled',
26 value => 'Event Conditions',
28 { field => 'conditionname',
29 type => 'selectlayers',
30 options => [ keys %all_conditions ],
31 labels => \%condition_labels,
32 onchange => 'condition_changed(what);',
33 layer_fields => \%condition_fields,
34 layer_values_callback => $condition_layer_values,
35 html_between => n_a('action'),
36 m2name_table => 'part_event_condition',
37 m2name_namecol => 'conditionname',
38 m2name_label => 'Condition',
39 m2name_new_default => \@implicit_condition_objs,
40 m2name_error_callback =>
41 $condition_error_callback,
42 m2name_remove_warnings =>
43 \%condition_remove_warnings,
44 m2name_new_js => 'condition_repop',
45 m2name_remove_js => 'condition_add',
48 value => 'Event Action',
51 type => 'selectlayers',
52 options => [ keys %all_actions ],
53 labels => \%action_labels,
54 onchange => 'action_changed(what);',
55 layer_fields => \%action_fields,
56 layer_values_callback => $action_layer_values,
57 html_between => n_a('action'),
62 'eventpart' => 'Event',
63 'event' => 'Event name',
64 'eventtable' => 'Type',
65 'agentnum' => 'Agent',
66 'check_freq' => 'Check frequency',
67 'disabled' => 'Disable event',
69 'conditionname' => 'Add new condition',
73 'viewall_dir' => 'browse',
74 'new_callback' => sub { #start empty for new events only
75 my( $cgi, $object, $fields_listref ) = @_;
76 unshift @{ $fields_listref->[1]{'options'} }, '';
78 'error_callback' => $error_callback,
81 'agent_null_right' => 'Edit global billing events',
84 <SCRIPT TYPE="text/javascript">
86 window.onload = function () { eventtable_changed(document.getElementById('eventtable')) };
89 function eventtable_changed(what) {
92 alert('eventtable_changed called on ' + what );
95 var eventtable = what.options[what.selectedIndex].value;
96 var eventdesc = what.options[what.selectedIndex].text;
98 //remove the ** Select type **
99 if ( what.options[0].value == '' && notonload++ > 0 ) {
100 what.options[0] = null;
104 // XXX gray out conditions that can't apply?
108 // update condition selects
111 for ( var cnum=0; document.getElementById('conditionname'+cnum); cnum++ ) {
112 var cond_id = 'conditionname' + cnum;
113 var cond_select = document.getElementById(cond_id);
116 alert('updating ' + cond_id);
119 // save off the current value
120 var conditionname = cond_select.options[cond_select.selectedIndex].value;
121 var cond_desc = cond_select.options[cond_select.selectedIndex].text;
123 var seen_condition = condition_repop(cond_select);
125 var warning = document.getElementById(cond_id + '_warning');
127 alert('turning off warning; setting style.display of '+ cond_id +
128 '_warning (' + warning + ') to none');
130 warning.style.display = 'none';
132 if ( ! seen_condition && conditionname != '') {
133 // add the current (not valid) condition back
134 opt(cond_select, conditionname, cond_desc, true );
135 if ( true <% @implicit_conditions
136 ? ( ' && '. join(' && ', map { "conditionname != '$_'" }
142 // turn on a warning and gray out the condition row
144 alert('turning on warning; setting style.display of '+ cond_id +
145 '_warning (' + warning + ') to none');
147 warning.innerHTML = 'Not applicable to ' + eventdesc + ' events';
148 warning.style.display = '';
156 // update action select
159 // save off the current value first!!
160 var action = what.form.action.options[what.form.action.selectedIndex].value;
161 var a_desc = what.form.action.options[what.form.action.selectedIndex].text;
162 var seen_action = false;
164 // blank the current action select
165 for ( var i = what.form.action.length; i >= 0; i-- )
166 what.form.action.options[i] = null;
168 if ( action == '' ) {
169 opt(what.form.action, action, a_desc, true );
173 % foreach my $eventtable ( FS::part_event->eventtables ) {
174 % tie my %actions, 'Tie::IxHash', FS::part_event->actions($eventtable);
175 % #use Data::Dumper; warn Dumper(%actions);
177 if ( eventtable == '<% $eventtable %>' ) {
179 % foreach my $action ( keys %actions ) {
180 % ( my $description = $actions{$action}->{'description'} ) =~ s/'/\\'/g;
183 if ( action == '<% $action %>' ) {
187 opt( what.form.action, '<% $action %>', '<% $description %>', sel );
194 // by default, turn off warnings and enable the submit button
195 var warning = document.getElementById('action_warning');
196 warning.style.display = 'none';
197 var submit_button = document.getElementById('submit');
198 submit_button.disabled = '';
200 if ( ! seen_action && action != '' ) {
201 // add the current (not valid) action back
202 opt( what.form.action, action, a_desc, true );
203 // turn on a warning and disable the submit button
204 //warning.innerHTML = a_desc + ' event not available as a ' +
205 warning.innerHTML = 'Not available as a ' + eventdesc + ' action';
206 warning.style.display = '';
207 submit_button.disabled = 'disabled';
212 function opt(what,value,text,selected) {
213 var optionName = new Option(text, value, false, selected);
214 var length = what.length;
215 what.options[length] = optionName;
218 function action_changed(what) {
219 // remove '** Select new **'
220 if ( what.options[0].value == '' ) {
221 what.options[0] = null;
223 // remove the warning, remove the invalid action, enable the submit button
224 var warning = document.getElementById('action_warning');
225 if ( warning.style.display == '' ) {
226 warning.style.display = 'none';
227 what.options[what.length-1] = null;
228 document.getElementById('submit').disabled = '';
232 function condition_changed(what) {
233 // remove '** Select new **'
234 if ( what.options[0].value == '' ) {
235 what.options[0] = null;
238 var previousValue = what.getAttribute('previousValue');
239 var previousText = what.getAttribute('previousText');
240 var value = what.options[what.selectedIndex].value;
241 var text = what.options[what.selectedIndex].text;
243 % foreach my $value ( keys %condition_remove_warnings ) {
244 if ( previousValue == '<% $value %>' ) {
245 if ( !confirm( <% $condition_remove_warnings{$value} |js_string %> ) ) {
246 for ( var i=0; i < what.length; i++ ) {
247 if ( what.options[i].value == previousValue ) {
248 what.selectedIndex = i;
256 //alert(previous + ' changed to ' + value);
258 var field_regex = /(\d+)$/;
259 var match = field_regex.exec(what.name);
261 alert(what.name + " didn't match?!");
265 //add the previous condition *back* to all the other selects...
266 condition_add(previousValue, previousText, match[1]);
268 what.setAttribute('previousValue', value);
269 what.setAttribute('previousText', text);
271 // remove the new condition from all other selects
272 condition_remove(value, match[1]);
276 function condition_avail(check_cond, curnum) {
277 for ( var cnum=0; document.getElementById('conditionname'+cnum); cnum++ ) {
278 if ( cnum == curnum ) continue;
280 var cond_id = 'conditionname' + cnum;
281 var cond_select = document.getElementById(cond_id);
283 //alert("checking " + cond_id + " (" + cond_select.disabled + ")");
285 if ( cond_select.disabled ) continue;
288 var conditionname = cond_select.options[cond_select.selectedIndex].value;
290 if ( check_cond == conditionname ) return false;
298 function condition_remove(remove_cond, curnum) {
300 if ( remove_cond.length == 0 ) return;
302 for ( var cnum=0; document.getElementById('conditionname'+cnum); cnum++ ) {
303 if ( cnum == curnum ) continue;
305 var cond_id = 'conditionname' + cnum;
306 var cond_select = document.getElementById(cond_id);
308 //for ( var i = cond_select.length; i >= 0; i-- ) {
309 for ( var i=0; i < cond_select.length; i++ ) {
310 if ( cond_select.options[i].value == remove_cond ) {
311 cond_select.options[i] = null;
319 function condition_add(add_condname, add_conddesc, curnum) {
321 if ( add_condname.length == 0 ) return;
323 var eventtable_el = document.getElementById('eventtable');
324 var eventtable = eventtable_el.options[eventtable_el.selectedIndex].value;
326 var in_eventtable = false;
328 % foreach my $eventtable ( FS::part_event->eventtables ) {
329 % tie my %conditions, 'Tie::IxHash',
330 % FS::part_event_condition->conditions($eventtable);
332 if ( eventtable == '<% $eventtable %>' ) {
334 % foreach my $conditionname ( keys %conditions ) {
335 % my $description = $conditions{$conditionname}->{'description'};
336 % $description =~ s/'/\\'/g;
338 if ( add_condname == '<% $conditionname %>' ) {
339 in_eventtable = true;
348 if ( ! in_eventtable ) return;
350 for ( var cnum=0; document.getElementById('conditionname'+cnum); cnum++ ) {
351 if ( cnum == curnum ) continue;
353 var cond_id = 'conditionname' + cnum;
354 var cond_select = document.getElementById(cond_id);
356 if ( cond_select.disabled ) continue;
358 //alert("adding " + add_condname + " to " + cond_id);
360 opt(cond_select, add_condname, add_conddesc, false );
362 cond_select.parentNode.parentNode.style.display = '';
368 function condition_repop(cond_select) {
370 var eventtable_el = document.getElementById('eventtable');
371 var eventtable = eventtable_el.options[eventtable_el.selectedIndex].value;
373 // save off the current value
374 var conditionname = cond_select.options[cond_select.selectedIndex].value;
375 var cond_desc = cond_select.options[cond_select.selectedIndex].text;
376 var seen_condition = false;
378 if ( cond_select.disabled ) return false; //skip deleted conditions
380 var field_regex = /(\d+)$/;
381 var match = field_regex.exec(cond_select.name);
383 alert(what.name + " didn't match?!");
388 // blank the current condition select
389 for ( var i = cond_select.length; i >= 0; i-- )
390 cond_select.options[i] = null;
392 if ( conditionname == '' ) {
393 opt(cond_select, conditionname, cond_desc, true );
397 % foreach my $eventtable ( FS::part_event->eventtables ) {
398 % tie my %conditions, 'Tie::IxHash',
399 % FS::part_event_condition->conditions($eventtable);
401 if ( eventtable == '<% $eventtable %>' ) {
403 % foreach my $conditionname ( keys %conditions ) {
404 % my $description = $conditions{$conditionname}->{'description'};
405 % $description =~ s/'/\\'/g;
408 if ( conditionname == '<% $conditionname %>' ) {
409 seen_condition = true;
413 if ( condition_avail("<% $conditionname %>", cnum) ) {
414 opt(cond_select, '<% $conditionname %>', '<% $description %>', sel);
423 if ( cond_select.length > 1 || cond_select.length == 1 && cond_select.options[0].value.length > 0 ) {
425 cond_select.parentNode.parentNode.style.display = '';
428 cond_select.parentNode.parentNode.style.display = 'none';
431 return seen_condition;
438 #misc (eventtable, check_freq)
440 my $eventtable_labels = FS::part_event->eventtable_labels;
441 $eventtable_labels->{''} = '** Select type **';
443 my $check_freq_labels = FS::part_event->check_freq_labels;
447 tie my %all_conditions, 'Tie::IxHash',
448 '' => { 'description' => '*** Select new condition ***', },
449 FS::part_event_condition->conditions();
451 my %condition_labels = map { $_ => $all_conditions{$_}->{'description'} }
452 keys %all_conditions;
454 #my %condition_fields = map { $_ => $all_conditions{$_}->{option_fields} }
455 # keys %all_conditions;
456 my %condition_fields = map { my $c = $_;
457 tie my %opts, 'Tie::IxHash',
458 @{ $all_conditions{$c}->{'option_fields'} || []};
459 %opts = ( map { ( "$c.$_" => $opts{$_} ); }
464 keys %all_conditions;
466 my @implicit_conditions = sort { $all_conditions{$a}->{'implicit_flag'} <=>
467 $all_conditions{$b}->{'implicit_flag'}
469 grep { $all_conditions{$_}->{'implicit_flag'} }
470 keys %all_conditions;
472 my @implicit_condition_objs = map {
473 new FS::part_event_condition {
474 'conditionname' => $_,
477 @implicit_conditions;
479 my %condition_remove_warnings =
480 map { ( $_ => $all_conditions{$_}->{'remove_warning'} ); }
481 grep { $all_conditions{$_}->{'remove_warning'} }
482 keys %all_conditions;
486 tie my %all_actions, 'Tie::IxHash',
487 '' => { 'description' => '*** Select event action ***', },
488 FS::part_event->actions();
490 my %action_labels = map { $_ => $all_actions{$_}->{'description'} }
493 #my %action_fields = map { $_ => $all_actions{$_}->{option_fields} }
495 my %action_fields = map { my $action = $_;
496 tie my %opts, 'Tie::IxHash',
497 @{ $all_actions{$action}->{option_fields} || [] };
498 %opts = ( map { ( "$action.$_" => $opts{$_} ); }
501 ( $action => [ %opts ] );
512 qq( <FONT ID="${field}_warning" STYLE="display:none" COLOR="#FF0000">).
513 "Party Party Join us Join us".
518 my $action_layer_values = sub {
519 my( $cgi, $part_event ) = @_;
520 my $action = $cgi->param('action') || $part_event->action;
521 return {} unless $action;
522 scalar( #force hashref
524 #map { $_ => { $part_event->options } }
525 # keys %action_fields
526 map { my $action = $_;
527 my %fields = @{ $action_fields{$action} };
528 my %obj_opts = $part_event->options;
529 %obj_opts = map { ( "$action.$_" => $obj_opts{$_} ); }
532 map { #false laziness w/process/part_event.html
534 my $value = scalar($cgi->param($_)) || $obj_opts{$_};
536 if ( $option =~ /^(.*)\.reasonnum$/ && $value == -1 ) {
538 'typenum' => scalar( $cgi->param( "new${option}T" ) ),
539 'reason' => scalar( $cgi->param( "new${option}" ) ),
543 ( $option => $value );
547 ( $action => \%opts );
554 tie my %cgi_conditions, 'Tie::IxHash';
556 my $error_callback = sub {
557 my( $cgi, $object, $fields_listref ) = @_;
559 my @cond_params = grep /^conditionname\d+$/, $cgi->param;
561 %cgi_conditions = map {
563 my $conditionname = $cgi->param($param);
568 $cgi_key =~ /^$param\.$conditionname\.(.*)$/ or die 'wtf!';
570 #my $value = $cgi->param($_);
572 #my $info = $all_conditions->{$conditionname}
574 @{ $all_conditions{$conditionname}->{'option_fields'} || []};
575 my $info = $cond_opts{$key};
578 #false laziness w/process/part_event.html
579 if ( $info->{'type'} =~ /^(select|checkbox)-?multiple$/
580 or $info->{'type'} =~ /^select/ && $info->{'multiple'} ) {
581 $value = { map { $_ => 1 } $cgi->param($cgi_key) };
582 } elsif ( $info->{'type'} eq 'freq' ) {
583 $value = $cgi->param($cgi_key). $cgi->param($cgi_key.'_units');
585 $value = $cgi->param($cgi_key);
590 } grep /^$param\.$conditionname\./, $cgi->param
592 } grep $cgi->param($_), grep /^conditionname\d+$/, $cgi->param;
596 my $condition_error_callback = sub {
598 new FS::part_event_condition { 'conditionname' => $_, };
599 } keys %cgi_conditions;
602 my $condition_layer_values = sub {
603 #m2name_table option causes this to be
604 # part_event_condition instead of part_event
605 my ( $cgi, $part_event_condition, $switches ) = @_;
606 scalar( #force hashref
608 #map { $_ => { $part_event_condition->options } }
609 # keys %condition_fields
610 map { my $conditionname = $_;
611 my %opts = $switches->{'mode'} eq 'error'
612 ? %{ $cgi_conditions{$conditionname} || {} }
613 : $part_event_condition->options;
615 map { ( "$conditionname.$_" => $opts{$_} ); }
618 ( $conditionname => \%opts );
620 keys %condition_fields
629 my $curuser = $FS::CurrentUser::CurrentUser;
632 unless $curuser->access_right('Edit billing events')
633 || $curuser->access_right('Edit global billing events');
635 my $disable_empty_agent= ! $curuser->access_right('Edit global billing events');
637 %cgi_conditions = ();
638 my $use_cgi_conditions = 0;