combine ticket notification scrips, #15353
[freeside.git] / httemplate / elements / tr-select-reason.html
index 2f8f3a1..5a79d68 100755 (executable)
@@ -1,31 +1,61 @@
+<%doc>
+
+Example:
+
+  include( '/elements/tr-select-reason.html',
+
+    #required 
+    'field'         => 'reasonnum',
+    'reason_class'  => 'C', # currently 'C', 'R',  or 'S'
+                           # for cancel, credit, or suspend
+
+    #recommended
+    'cgi' => $cgi, #easiest way for things to be properly "sticky" on errors
+
+    #optional
+    'control_button' => 'element_name', #button to be enabled when a reason is
+                                        #selected
+    'id'             => 'element_id',
+
+    #deprecated ways to keep things "sticky" on errors
+    # (requires duplicate code in each using file to parse cgi params)
+    'curr_value'     => $curr_value,
+    'curr_value'     => {
+                          'typenum' => $typenum,
+                          'reason'  => $reason,
+                        },
+
+  )
+
+</%doc>
 
 <SCRIPT TYPE="text/javascript">
-  function sh_add<% $name %>()
+  function sh_add<% $func_suffix %>()
   {
 
-    if (document.getElementById('<% $name %>').selectedIndex == 0){
+    if (document.getElementById('<% $id %>').selectedIndex == 0){
       <% $controlledbutton ? $controlledbutton.'.disabled = true;' : ';' %>
     }else{
       <% $controlledbutton ? $controlledbutton.'.disabled = false;' : ';' %>
     }
 
-%if ($curuser->access_right($access_right)){
+%if ($curuser->access_right($add_access_right)){
 
-    if (document.getElementById('<% $name %>').selectedIndex == 
-         (document.getElementById('<% $name %>').length - 1)) {
-      document.getElementById('new<% $name %>').disabled = false;
-      document.getElementById('new<% $name %>').style.display = 'inline';
-      document.getElementById('new<% $name %>Label').style.display = 'inline';
-      document.getElementById('new<% $name %>T').disabled = false;
-      document.getElementById('new<% $name %>T').style.display = 'inline';
-      document.getElementById('new<% $name %>TLabel').style.display = 'inline';
-    }else{
-      document.getElementById('new<% $name %>').disabled = true;
-      document.getElementById('new<% $name %>').style.display = 'none';
-      document.getElementById('new<% $name %>Label').style.display = 'none';
-      document.getElementById('new<% $name %>T').disabled = true;
-      document.getElementById('new<% $name %>T').style.display = 'none';
-      document.getElementById('new<% $name %>TLabel').style.display = 'none';
+    if (document.getElementById('<% $id %>').selectedIndex == 
+         (document.getElementById('<% $id %>').length - 1)) {
+      document.getElementById('new<% $id %>').disabled = false;
+      document.getElementById('new<% $id %>').style.display = 'inline';
+      document.getElementById('new<% $id %>Label').style.display = 'inline';
+      document.getElementById('new<% $id %>T').disabled = false;
+      document.getElementById('new<% $id %>T').style.display = 'inline';
+      document.getElementById('new<% $id %>TLabel').style.display = 'inline';
+    } else {
+      document.getElementById('new<% $id %>').disabled = true;
+      document.getElementById('new<% $id %>').style.display = 'none';
+      document.getElementById('new<% $id %>Label').style.display = 'none';
+      document.getElementById('new<% $id %>T').disabled = true;
+      document.getElementById('new<% $id %>T').style.display = 'none';
+      document.getElementById('new<% $id %>TLabel').style.display = 'none';
     }
 
 %}
 </SCRIPT>
 
 <TR>
-  <TD ALIGN="right">Reason</TD>
+  <TD ALIGN="right"><% mt('Reason') |h %></TD>
   <TD>
-    <SELECT id="<% $name %>" name="<% $name %>" onFocus="sh_add<% $name %>()" onChange="sh_add<% $name %>()">
-%    my @reasons = qsearch( { table =>'reason', 
-%                             hashref => {},
-%                             extra_sql => $extra_sql,
-%                             addl_from => 'LEFT JOIN reason_type ON reason_type.typenum = reason.reason_type',
-%                           });
-      <OPTION VALUE="" <% ($init_reason eq "") ? 'SELECTED' : '' %>>Select Reason...</OPTION>
+    <SELECT id="<% $id %>" name="<% $name %>" onFocus="sh_add<% $func_suffix %>()" onChange="sh_add<% $func_suffix %>()">
+      <OPTION VALUE="" <% ($init_reason eq '') ? 'SELECTED' : '' %>><% mt('Select Reason...') |h %></OPTION>
 %    foreach my $reason (@reasons) {
-      <OPTION VALUE="<% $reason->reasonnum %>" <% ($init_reason == $reason->reasonnum) ? 'SELECTED' : '' %>><% $reason->reason %></OPTION>
+      <OPTION VALUE="<% $reason->reasonnum %>" <% ($init_reason == $reason->reasonnum) ? 'SELECTED' : '' %>><% $reason->reasontype->type %> : <% $reason->reason %></OPTION>
 %    }
-%    if ($curuser->access_right($access_right)) {
-      <OPTION VALUE="-1" <% ($init_reason == -1) ? 'SELECTED' : '' %>>Add new reason</OPTION>
+%    if ($curuser->access_right($add_access_right)) {
+      <OPTION VALUE="-1" <% ($init_reason == -1) ? 'SELECTED' : '' %>><% mt('Add new reason') |h %></OPTION>
 %    }
 %
     </SELECT>
   </TD>
 </TR>
 
+%   my @types = qsearch( 'reason_type', { 'class' => $class } );
+%   if (scalar(@types) < 1) {  # we should never reach this
 <TR>
   <TD ALIGN="right">
-    <P id="new<% $name %>TLabel" style="display:<% $display %>">Reason Type</P>
+    <P><% mt('No reason types. Please add some.') |h %></P>
+  </TD>
+</TR>
+%   }elsif (scalar(@types) == 1) {
+<TR>
+  <TD ALIGN="right">
+    <P id="new<% $name %>TLabel" style="display:<% $display %>"><% mt('Reason Type') |h %></P>
   </TD>
   <TD>
-    <SELECT id="new<% $name %>T" name="new<% $name %>T" disabled="<% $disabled %>" style="display:<% $display %>">
-%     for my $type (qsearch( 'reason_type', { 'class' => $class } )){
+    <P id="new<% $name %>T" disabled="<% $disabled %>" style="display:<% $display %>"><% $types[0]->type %>
+    <INPUT type="hidden" name="new<% $name %>T" value="<% $types[0]->typenum %>">
+  </TD>
+</TR>
+
+%   }else{
+
+<TR>
+  <TD ALIGN="right">
+    <P id="new<% $id %>TLabel" style="display:<% $display %>"><% mt('Reason Type') |h %></P>
+  </TD>
+  <TD>
+    <SELECT id="new<% $id %>T" name="new<% $name %>T" "<% $disabled %>" style="display:<% $display %>">
+%     for my $type (@types) {
         <OPTION VALUE="<% $type->typenum %>" <% ($init_type == $type->typenum) ? 'SELECTED' : '' %>><% $type->type %></OPTION>
 %     }
     </SELECT>
   </TD>
 </TR>
+%   }
 
 <TR>
   <TD ALIGN="right">
-    <P id="new<% $name %>Label" style="display:<% $display %>">New Reason</P>
+    <P id="new<% $id %>Label" style="display:<% $display %>"><% mt('New Reason') |h %></P>
   </TD>
-  <TD><INPUT id="new<% $name %>" name="new<% $name %>" type="text" value="<% $init_newreason %>" disabled="<% $disabled %>" style="display:<% $display %>"></TD>
+  <TD><INPUT id="new<% $id %>" name="new<% $name %>" type="text" value="<% $init_newreason |h %>" "<% $disabled %>" style="display:<% $display %>"></TD>
 </TR>
 
 <%init>
-my($name, $class, $init_reason, $init_type, $init_newreason, $controlledbutton) = @_;
-my($extra_sql, $curuser, $access_right, $display, $disabled); 
 
+my %opt = @_;
+
+my $name = $opt{'field'};
+my $class = $opt{'reason_class'};
+
+my $init_reason;
+if ( $opt{'cgi'} ) {
+  $init_reason = $opt{'cgi'}->param($name);
+} else {
+  $init_reason = $opt{'curr_value'};
+}
+
+my $controlledbutton = $opt{'control_button'};
+
+( my $func_suffix = $name ) =~ s/\./_/g;
+
+my $id = $opt{'id'} || $func_suffix;
+
+my( $add_access_right, $access_right ); 
 if ($class eq 'C') {
-  $access_right='Add on-the-fly cancel reason';
-}elsif ($class eq 'S') {
-  $access_right='Add on-the-fly suspend reason';
-}else{
-  print "illegal class: $class";
+  $access_right = 'Cancel customer';
+  $add_access_right = 'Add on-the-fly cancel reason';
+} elsif ($class eq 'S') {
+  $access_right = 'Suspend customer package';
+  $add_access_right = 'Add on-the-fly suspend reason';
+} elsif ($class eq 'R') {
+  $access_right = 'Post credit';
+  $add_access_right = 'Add on-the-fly credit reason';
+} else {
+  die "illegal class: $class";
 }
 
-if ($init_reason == -1){
+my( $display, $disabled ) = ( 'none', 'DISABLED' );
+my( $init_type, $init_newreason ) = ( '', '' );
+if ($init_reason == -1 || ref($init_reason) ) {
+
   $display = 'inline';
-  $disabled = 'false';
-}else{
-  $display = 'none';
-  $disabled = 'true';
+  $disabled = '';
+
+  if ( ref($init_reason) ) {
+    $init_type      = $init_reason->{'typenum'};
+    $init_newreason = $init_reason->{'reason'};
+    $init_reason = -1;
+  } elsif ( $opt{'cgi'} ) {
+    $init_type      = $opt{'cgi'}->param( "new${name}T" );
+    $init_newreason = $opt{'cgi'}->param( "new$name"    );
+  }
+
 }
 
-$extra_sql = "WHERE class = '$class' ORDER BY reason_type";
-$curuser = $FS::CurrentUser::CurrentUser;
+my $extra_sql =
+  "WHERE class = '$class' and (disabled = '' OR disabled is NULL)";
 
-</%init>
+my @reasons = qsearch({
+  table     => 'reason', 
+  hashref   => {},
+  extra_sql => $extra_sql,
+  addl_from => 'LEFT JOIN reason_type '.
+               ' ON reason_type.typenum = reason.reason_type',
+  order_by  => 'ORDER BY reason_type.type ASC, reason.reason ASC',
+});
 
+my $curuser = $FS::CurrentUser::CurrentUser;
+
+</%init>