summaryrefslogtreecommitdiff
path: root/httemplate/edit/part_event.html
blob: 6a532223e2dd645e70969491f3a799526027d9f7 (plain)
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
<% include( 'elements/edit.html',
              'name'   => 'Billing event definition',
              'table'  => 'part_event',
              'fields' => [
                            'event',
                            { field   => 'eventtable',
                              type    => 'select',
                              options => [ FS::part_event->eventtables ],
                              labels  => $eventtable_labels,
                              onchange => 'eventtable_changed',
                            },
                            { field   => 'agentnum',
                              type    => 'select-agent',
                              disable_empty => $disable_empty_agent,
                            },
                            { field   => 'check_freq',
                              type    => 'select',
                              options => [ '1d', '1m' ],
                              labels  => $check_freq_labels,
                            },
                            { field   => 'disabled',
                              type    => 'checkbox',
                              value   => 'Y',
                            },
                            { type    => 'title',
                              value   => 'Event Conditions',
                            },
                            { field   => 'conditionname',
                              type    => 'selectlayers',
                              options => [ keys %all_conditions ],
                              labels  => \%condition_labels,
                              onchange => 'condition_changed(what);',
                              layer_fields => \%condition_fields,
                              layer_values_callback => $condition_layer_values,
                              html_between       => n_a('action'),
                              m2name_table       => 'part_event_condition',
                              m2name_namecol     => 'conditionname',
                              m2_label           => 'Condition',
                              m2_new_default     => \@implicit_condition_objs,
                              m2_error_callback  => $condition_error_callback,
                              m2_remove_warnings => \%condition_remove_warnings,
                              m2_new_js          => 'condition_repop',
                              m2_remove_js       => 'condition_add',
                            },
                            { type    => 'title',
                              value   => 'Event Action',
                            },
                            { field   => 'action',
                              type     => 'selectlayers',
                              options  => [ keys %all_actions ],
                              labels   => \%action_labels,
                              onchange => 'action_changed(what);',
                              layer_fields => \%action_fields,
                              layer_values_callback => $action_layer_values,
                              html_between => n_a('action'),
                            },

                          ],
              'labels' => {
                            'eventpart'  => 'Event',
                            'event'      => 'Event name',
                            'eventtable' => 'Type',
                            'agentnum'   => 'Agent',
                            'check_freq' => 'Check frequency',
                            'disabled'   => 'Disable event',

                            'conditionname' => 'Add&nbsp;new&nbsp;condition',
                            #'weight',
                            'action'     => 'Action',
                          },
              'viewall_dir' => 'browse',
              'new_callback' => sub { #start empty for new events only
                my( $cgi, $object, $fields_listref ) = @_; 
                unshift @{ $fields_listref->[1]{'options'} }, '';
              },
              'error_callback' => $error_callback,

              'agent_virt'       => 1,
              'agent_null_right' => 'Edit global billing events',
          )
%>
<SCRIPT TYPE="text/javascript">

  window.onload = function () { eventtable_changed(document.getElementById('eventtable')) };
  var notonload = 0;

  function eventtable_changed(what) {

%   if ( $JS_DEBUG ) {
      alert('eventtable_changed called on ' + what );
%   }

    var eventtable = what.options[what.selectedIndex].value;
%   if ( $JS_DEBUG ) {
      alert ("eventtable: " + eventtable);
%   }
    var eventdesc  = what.options[what.selectedIndex].text;

    //remove the ** Select type **
    if ( what.options[0].value == '' && notonload++ > 0 ) {
      what.options[0] = null;
    }

    ////
    // XXX gray out conditions that can't apply (in addition to the warning)?
    ////

    ////
    // update condition selects
    ////

    for ( var cnum=0; document.getElementById('conditionname'+cnum); cnum++ ) {
      var cond_id = 'conditionname' + cnum;
      var cond_select = document.getElementById(cond_id);

%     if ( $JS_DEBUG ) {
        alert('updating ' + cond_id);
%     }

      // save off the current value
      var conditionname = cond_select.options[cond_select.selectedIndex].value;
      var cond_desc     = cond_select.options[cond_select.selectedIndex].text;

      var seen_condition = condition_repop(cond_select);

      var warning = document.getElementById(cond_id + '_warning');
%     if ( $JS_DEBUG ) {
        alert('turning off warning; setting style.display of '+ cond_id +
              '_warning (' + warning + ') to none');
%     }
      warning.style.display = 'none';

      if ( ! seen_condition && conditionname != '' ) {
        // add the current (not valid) condition back
        opt(cond_select, conditionname, cond_desc, true );
        if ( ! condition_is_implicit(conditionname) ) {
          cond_select.parentNode.parentNode.style.display = '';
          cond_select.disabled = '';
          // turn on a warning and gray out the condition row
%         if ( $JS_DEBUG ) {
            alert('turning on warning; setting style.display of '+ cond_id +
                  '_warning (' + warning + ') to ""');
%         }
          warning.innerHTML = 'Not applicable to ' + eventdesc + ' events';
          warning.style.display = '';
        } else {
          if ( ! condition_in_eventtable(conditionname) ) {
%           if ( $JS_DEBUG ) {
              alert(conditionname + " not in " + eventtable + "; disabling");
%           }
            cond_select.parentNode.parentNode.style.display = 'none';
            cond_select.disabled = 'disabled';
          } else {
%           if ( $JS_DEBUG ) {
              alert(conditionname + " implicit for " + eventtable + "; enabling");
%           }
            cond_select.parentNode.parentNode.style.display = '';
            cond_select.disabled = '';
          }
        }
      }

    }


    ////
    // update action select
    ////

    // save off the current value first!!
    var action = what.form.action.options[what.form.action.selectedIndex].value;
    var a_desc = what.form.action.options[what.form.action.selectedIndex].text;
    var seen_action = false;

    // blank the current action select
    for ( var i = what.form.action.length; i >= 0; i-- )
      what.form.action.options[i] = null;

    if ( action == '' ) {
      opt(what.form.action, action, a_desc, true );
    }

    // repopulate it
%   foreach my $eventtable ( FS::part_event->eventtables ) {
%     tie my %actions, 'Tie::IxHash', FS::part_event->actions($eventtable);
%     #use Data::Dumper; warn Dumper(%actions);

      if ( eventtable == '<% $eventtable %>' ) {

%       foreach my $action ( keys %actions ) {
%         ( my $description = $actions{$action}->{'description'} ) =~ s/'/\\'/g;

          var sel = false;
          if ( action == '<% $action %>' ) {
            seen_action = true;
            sel = true;
          }
          opt( what.form.action, '<% $action %>', '<% $description %>', sel );
%       }

      }

%   }

    // by default, turn off warnings and enable the submit button
    var warning = document.getElementById('action_warning');
    warning.style.display = 'none';
    var submit_button = document.getElementById('submit');
    submit_button.disabled = '';

    if ( ! seen_action && action != '' ) {
      // add the current (not valid) action back
      opt( what.form.action, action, a_desc, true );
      // turn on a warning and disable the submit button
      //warning.innerHTML = a_desc + ' event not available as a ' +
      warning.innerHTML = 'Not available as a ' + eventdesc + ' action';
      warning.style.display = '';
      submit_button.disabled = 'disabled';
    }

  }

  function opt(what,value,text,selected) {
    var optionName = new Option(text, value, false, selected);
    var length = what.length;
    what.options[length] = optionName;
  }

  function action_changed(what) {
    // remove '** Select new **'
    if ( what.options[0].value == '' ) {
       what.options[0] = null;
    }
    // remove the warning, remove the invalid action, enable the submit button
    var warning = document.getElementById('action_warning');
    if ( warning.style.display == '' ) {
      warning.style.display = 'none';
      what.options[what.length-1] = null;
      document.getElementById('submit').disabled = '';
    }
  }

  function condition_changed(what) {
    // remove '** Select new **'
    if ( what.options[0].value == '' ) {
       what.options[0] = null;
    }

    var previousValue = what.getAttribute('previousValue');
    var previousText = what.getAttribute('previousText');
    var value = what.options[what.selectedIndex].value;
    var text = what.options[what.selectedIndex].text;

%   foreach my $value ( keys %condition_remove_warnings ) {
      if ( previousValue == '<% $value %>' ) {
        if ( !confirm( <% $condition_remove_warnings{$value} |js_string %> ) ) {
          for ( var i=0; i < what.length; i++ ) {
            if ( what.options[i].value == previousValue ) {
              what.selectedIndex = i;
            }
          }
          return false;
        }
      }
%   }

    //alert(previous + ' changed to ' + value);

    var field_regex = /(\d+)$/;
    var match = field_regex.exec(what.name);
    if ( !match ) {
      alert(what.name + " didn't match?!");
      return;
    }

    //add the previous condition *back* to all the other selects...
    condition_add(previousValue, previousText, match[1]);

    what.setAttribute('previousValue', value);
    what.setAttribute('previousText', text);

    // remove the new condition from all other selects
    condition_remove(value, match[1]);

  }

  function condition_avail(check_cond, curnum) {
    for ( var cnum=0; document.getElementById('conditionname'+cnum); cnum++ ) {
      if ( cnum == curnum ) continue;

      var cond_id = 'conditionname' + cnum;
      var cond_select = document.getElementById(cond_id);

      //alert("checking " + cond_id + " (" + cond_select.disabled + ")");

      if ( cond_select.disabled ) continue;

      // the current value
      var conditionname = cond_select.options[cond_select.selectedIndex].value;

      if ( check_cond == conditionname ) return false;

    }

    return true;

  }

  function condition_remove(remove_cond, curnum) {

    if ( remove_cond.length == 0 ) return;

    for ( var cnum=0; document.getElementById('conditionname'+cnum); cnum++ ) {
      if ( cnum == curnum ) continue;

      var cond_id = 'conditionname' + cnum;
      var cond_select = document.getElementById(cond_id);

      //for ( var i = cond_select.length; i >= 0; i-- ) {
      for ( var i=0; i < cond_select.length; i++ ) {
        if ( cond_select.options[i].value == remove_cond ) {
          cond_select.options[i] = null;
        }
      }

    }

  }

  function condition_add(add_condname, add_conddesc, curnum) {

    if ( add_condname.length == 0 ) return;

    var in_eventtable = condition_in_eventtable(add_condname);

    if ( ! in_eventtable ) return;

    for ( var cnum=0; document.getElementById('conditionname'+cnum); cnum++ ) {
      if ( cnum == curnum ) continue;

      var cond_id = 'conditionname' + cnum;
      var cond_select = document.getElementById(cond_id);

      if ( cond_select.disabled ) continue;

      //alert("adding " + add_condname + " to " + cond_id);

      opt(cond_select, add_condname, add_conddesc, false );

      cond_select.parentNode.parentNode.style.display = '';

    }

  }

  function condition_in_eventtable(condname) {

    var eventtable_el = document.getElementById('eventtable');
    var eventtable = eventtable_el.options[eventtable_el.selectedIndex].value;

    var in_eventtable = false;

%   foreach my $eventtable ( FS::part_event->eventtables ) {
%     tie my %conditions, 'Tie::IxHash',
%       FS::part_event_condition->conditions($eventtable);

      if ( eventtable == '<% $eventtable %>' ) {

%       foreach my $conditionname ( keys %conditions ) {

          if ( condname == '<% $conditionname %>' ) {
            in_eventtable = true;
          }

%       }

      }

%   }

    return in_eventtable;

  }

  function condition_is_implicit(condname) {

    if ( true <% @implicit_conditions
                   ? ( ' && '. join(' && ', map { "condname != '$_'" }
                                                @implicit_conditions
                                   )
                     )
                   : ''
              %> ) {
      return false;
    } else {
      return true;
    }
  }

  function condition_repop(cond_select) {

    var eventtable_el = document.getElementById('eventtable');
    var eventtable = eventtable_el.options[eventtable_el.selectedIndex].value;

    // save off the current value
    var conditionname = cond_select.options[cond_select.selectedIndex].value;
    var cond_desc     = cond_select.options[cond_select.selectedIndex].text;
    var seen_condition = false;

    //skip deleted conditions
    if ( cond_select.disabled && conditionname != '' && ! condition_is_implicit(conditionname) ) {
      return false;
    }

    var field_regex = /(\d+)$/;
    var match = field_regex.exec(cond_select.name);
    if ( !match ) {
      alert(what.name + " didn't match?!");
      return;
    }
    var cnum = match[1];

    // blank the current condition select
    for ( var i = cond_select.length; i >= 0; i-- )
      cond_select.options[i] = null;

    if ( conditionname == '' ) {
      opt(cond_select, conditionname, cond_desc, true );
    }

    // repopulate it
%   foreach my $eventtable ( FS::part_event->eventtables ) {
%     tie my %conditions, 'Tie::IxHash',
%       FS::part_event_condition->conditions($eventtable);

      if ( eventtable == '<% $eventtable %>' ) {

%       foreach my $conditionname ( keys %conditions ) {
%         my $description = $conditions{$conditionname}->{'description'};
%         $description =~ s/'/\\'/g;

          var sel = false;
          if ( conditionname == '<% $conditionname %>' ) {
            seen_condition = true;
            sel = true;
          }

          if ( condition_avail("<% $conditionname %>", cnum) ) {
            opt(cond_select, '<% $conditionname %>', '<% $description %>', sel);
          }

%       }

      }
        
%   }

    if ( cond_select.length > 1 || cond_select.length == 1 && cond_select.options[0].value.length > 0 ) {
       
      cond_select.parentNode.parentNode.style.display = '';
      cond_select.disabled = '';

    } else {
      cond_select.parentNode.parentNode.style.display = 'none';
      cond_select.disabled = 'disabled';
    }

    return seen_condition;

  }

</SCRIPT>
<%once>

#misc (eventtable, check_freq)

my $eventtable_labels = FS::part_event->eventtable_labels;
$eventtable_labels->{''} = '** Select type **';

my $check_freq_labels = FS::part_event->check_freq_labels;

#conditions

tie my %all_conditions, 'Tie::IxHash', 
  '' => { 'description' => '*** Select new condition ***', },
  FS::part_event_condition->conditions();

my %condition_labels = map { $_ => $all_conditions{$_}->{'description'} }
                           keys %all_conditions;

#my %condition_fields = map { $_ => $all_conditions{$_}->{option_fields} } 
#                           keys %all_conditions;
my %condition_fields = map { my $c = $_;
                             tie my %opts, 'Tie::IxHash',
                               @{ $all_conditions{$c}->{'option_fields'} || []};
                             %opts = ( map { ( "$c.$_" => $opts{$_} ); }
                                           keys %opts
                                     );
                             ( $c => [ %opts ] );
                           } 
                           keys %all_conditions;

my @implicit_conditions = sort { $all_conditions{$a}->{'implicit_flag'} <=>
                                 $all_conditions{$b}->{'implicit_flag'}
                               }
                          grep { $all_conditions{$_}->{'implicit_flag'} }
                          keys %all_conditions;

my @implicit_condition_objs = map {
                                    new FS::part_event_condition {
                                      'conditionname' => $_,
                                    };
                                  }
                                  @implicit_conditions;

my %condition_remove_warnings =
  map  { ( $_ => $all_conditions{$_}->{'remove_warning'} ); }
  grep { $all_conditions{$_}->{'remove_warning'} }
  keys %all_conditions;

#actions

tie my %all_actions, 'Tie::IxHash', 
  '' => { 'description' => '*** Select event action ***', },
  FS::part_event->actions();

my %action_labels = map { $_ => $all_actions{$_}->{'description'} }
                        keys %all_actions;

#my %action_fields = map { $_ => $all_actions{$_}->{option_fields} }
#                        keys %all_actions;
my %action_fields = map { my $action = $_;
                          tie my %opts, 'Tie::IxHash',
                            @{ $all_actions{$action}->{option_fields} || [] };
                          %opts = ( map { ( "$action.$_" => $opts{$_} ); }
                                        keys %opts
                                  );
                          ( $action => [ %opts ] );
                        }
                        keys %all_actions;

#subs

sub n_a {
  my $t = shift;

  return sub {
    my $field = shift;
    qq( <FONT ID="${field}_warning" STYLE="display:none" COLOR="#FF0000">).
      "Party Party Join us Join us".
      '</FONT>';
  };
}

my $action_layer_values = sub {
  my( $cgi, $part_event ) = @_;
  my $action = $cgi->param('action') || $part_event->action;
  return {} unless $action;
  scalar( #force hashref
    {
      #map { $_ => { $part_event->options } }
      #    keys %action_fields
      map { my $action = $_;
            my %fields = @{ $action_fields{$action} };
            my %obj_opts = $part_event->options;
            %obj_opts = map { ( "$action.$_" => $obj_opts{$_} ); }
                            keys %obj_opts;
            my %opts =
              map { #false laziness w/process/part_event.html
                    my $option = $_;
                    my $value = scalar($cgi->param($_)) || $obj_opts{$_};

                    if ( $option =~ /^(.*)\.reasonnum$/ && $value == -1 ) {
                      $value = {
                        'typenum' => scalar( $cgi->param( "new${option}T" ) ),
                        'reason'  => scalar( $cgi->param( "new${option}"  ) ),
                      };
                    }

                    ( $option => $value );

                  }
                  keys %fields;
            ( $action => \%opts );
          }
          keys %action_fields
    }
  );
};

tie my %cgi_conditions, 'Tie::IxHash';

my $error_callback = sub {
  my( $cgi, $object, $fields_listref ) = @_;

  my @cond_params = grep /^conditionname\d+$/, $cgi->param;

  %cgi_conditions = map {
    my $param = $_;
    my $conditionname = $cgi->param($param);
    $conditionname => {
      map { 

        my $cgi_key = $_;
        $cgi_key =~ /^$param\.$conditionname\.(.*)$/ or die 'wtf!';
        my $key = $1;
        #my $value = $cgi->param($_);

        #my $info = $all_conditions->{$conditionname}
        my %cond_opts =
          @{ $all_conditions{$conditionname}->{'option_fields'} || []};
        my $info = $cond_opts{$key};

        my $value;
        #false laziness w/process/part_event.html
        if (      $info->{'type'} =~ /^(select|checkbox)-?multiple$/
               or $info->{'type'} =~ /^select/ && $info->{'multiple'} ) {
          $value = { map { $_ => 1 } $cgi->param($cgi_key) };
        } elsif ( $info->{'type'} eq 'freq' ) {
          $value = $cgi->param($cgi_key). $cgi->param($cgi_key.'_units');
        } else {
          $value = $cgi->param($cgi_key);
        }

        $key => $value;

      } grep /^$param\.$conditionname\./, $cgi->param
    };
  } grep $cgi->param($_), grep /^conditionname\d+$/, $cgi->param;

};

my $condition_error_callback = sub {
  map {
    new FS::part_event_condition { 'conditionname' => $_, };
  } keys %cgi_conditions;
};

my $condition_layer_values = sub {
  #m2_table option causes this to be
  # part_event_condition instead of part_event
  my ( $cgi, $part_event_condition, $switches ) = @_;
  scalar( #force hashref
    {
      #map { $_ => { $part_event_condition->options } }
      #    keys %condition_fields
      map { my $conditionname = $_;
            my %opts = $switches->{'mode'} eq 'error'
                       ? %{ $cgi_conditions{$conditionname} || {} }
                       : $part_event_condition->options;
            %opts = (
              map { ( "$conditionname.$_" => $opts{$_} ); }
                  keys %opts
            );
            ( $conditionname => \%opts );
          }
          keys %condition_fields
    }
  );
};


</%once>
<%init>

my $curuser = $FS::CurrentUser::CurrentUser;

die "access denied"
  unless $curuser->access_right('Edit billing events')
      || $curuser->access_right('Edit global billing events');

my $disable_empty_agent= ! $curuser->access_right('Edit global billing events');

%cgi_conditions = ();
my $use_cgi_conditions = 0;

my $JS_DEBUG = 0;

</%init>