default to a session cookie instead of setting an explicit timeout, weird timezone...
[freeside.git] / httemplate / edit / part_event.html
index c94da61..c8072e9 100644 (file)
@@ -1,4 +1,4 @@
-<% include( 'elements/edit.html',
+<& elements/edit.html,
               'name'   => 'Billing event definition',
               'table'  => 'part_event',
               'fields' => [
                               type    => 'checkbox',
                               value   => 'Y',
                             },
+                            { field   => '_initialize',
+                              type      => 'checkbox',
+                              onchange  => '_initialize_changed',
+                              value   => 'Y',
+                            }, 
                             { type    => 'title',
                               value   => 'Event Conditions',
                             },
                             { field   => 'conditionname',
-                              type    => 'selectlayers',
+                              type    => 'selectlayersx',
                               options => [ keys %all_conditions ],
                               labels  => \%condition_labels,
                               onchange => 'condition_changed(what);',
                               html_between       => n_a('action'),
                               m2name_table       => 'part_event_condition',
                               m2name_namecol     => 'conditionname',
-                              m2name_label       => 'Condition',
-                              m2name_new_default => \@implicit_condition_objs,
-                              m2name_error_callback  =>
-                                $condition_error_callback,
-                              m2name_remove_warnings =>
-                                \%condition_remove_warnings,
-                              m2name_new_js      => 'condition_repop',
-                              m2name_remove_js   => 'condition_add',
+                              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',
+                              type     => 'selectlayersx',
                               options  => [ keys %all_actions ],
                               labels   => \%action_labels,
                               onchange => 'action_changed(what);',
@@ -56,7 +59,6 @@
                               layer_values_callback => $action_layer_values,
                               html_between => n_a('action'),
                             },
-
                           ],
               'labels' => {
                             'eventpart'  => 'Event',
@@ -69,6 +71,7 @@
                             'conditionname' => 'Add&nbsp;new&nbsp;condition',
                             #'weight',
                             'action'     => 'Action',
+                            '_initialize' => 'Initialize event',
                           },
               'viewall_dir' => 'browse',
               'new_callback' => sub { #start empty for new events only
@@ -79,8 +82,7 @@
 
               'agent_virt'       => 1,
               'agent_null_right' => 'Edit global billing events',
-          )
-%>
+&>
 <SCRIPT TYPE="text/javascript">
 
   window.onload = function () { eventtable_changed(document.getElementById('eventtable')) };
@@ -93,6 +95,9 @@
 %   }
 
     var eventtable = what.options[what.selectedIndex].value;
+%   if ( $JS_DEBUG ) {
+      alert ("eventtable: " + eventtable);
+%   }
     var eventdesc  = what.options[what.selectedIndex].text;
 
     //remove the ** Select type **
     }
 
     ////
-    // XXX gray out conditions that can't apply?
+    // XXX gray out conditions that can't apply (in addition to the warning)?
     ////
 
     ////
 %     }
       warning.style.display = 'none';
 
-      if ( ! seen_condition && conditionname != '') {
+      if ( ! seen_condition && conditionname != '' ) {
         // add the current (not valid) condition back
         opt(cond_select, conditionname, cond_desc, true );
-        if ( true <% @implicit_conditions
-                       ? ( ' && '. join(' && ', map { "conditionname != '$_'" }
-                                                    @implicit_conditions
-                                       )
-                         )
-                       : ''
-                  %> ) {
+        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 none');
+                  '_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 = '';
+          }
         }
       }
 
 
     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;
 
       if ( eventtable == '<% $eventtable %>' ) {
 
 %       foreach my $conditionname ( keys %conditions ) {
-%         my $description = $conditions{$conditionname}->{'description'};
-%         $description =~ s/'/\\'/g;
 
-          if ( add_condname == '<% $conditionname %>' ) {
+          if ( condname == '<% $conditionname %>' ) {
             in_eventtable = true;
           }
 
 
 %   }
 
-    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);
+    return in_eventtable;
 
-      opt(cond_select, add_condname, add_conddesc, false );
+  }
 
-      cond_select.parentNode.parentNode.style.display = '';
+  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 cond_desc     = cond_select.options[cond_select.selectedIndex].text;
     var seen_condition = false;
 
-    if ( cond_select.disabled ) return false; //skip deleted conditions
+    //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 ( 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;
 
   }
 
+  function _initialize_changed(what) {
+    document.getElementById('disabled').disabled = what.checked;
+    if ( what.checked ) {
+%# because it's not an immediately obvious concept
+      alert('Initializing the event will treat it as "already run" on the '+
+            'current date for all existing customers.  This affects '+
+            'conditions that prevent running an event more than once.');
+    }
+  }
+
 </SCRIPT>
 <%once>
 
@@ -448,6 +499,9 @@ tie my %all_conditions, 'Tie::IxHash',
   '' => { 'description' => '*** Select new condition ***', },
   FS::part_event_condition->conditions();
 
+# *** Select new condition *** sorts to the beginning anyway
+(tied %all_conditions)->SortByValue;
+
 my %condition_labels = map { $_ => $all_conditions{$_}->{'description'} }
                            keys %all_conditions;
 
@@ -487,6 +541,8 @@ tie my %all_actions, 'Tie::IxHash',
   '' => { 'description' => '*** Select event action ***', },
   FS::part_event->actions();
 
+(tied %all_actions)->SortByValue;
+
 my %action_labels = map { $_ => $all_actions{$_}->{'description'} }
                         keys %all_actions;
 
@@ -600,7 +656,7 @@ my $condition_error_callback = sub {
 };
 
 my $condition_layer_values = sub {
-  #m2name_table option causes this to be
+  #m2_table option causes this to be
   # part_event_condition instead of part_event
   my ( $cgi, $part_event_condition, $switches ) = @_;
   scalar( #force hashref