combine ticket notification scrips, #15353
[freeside.git] / httemplate / elements / tr-select-reason.html
1 <%doc>
2
3 Example:
4
5   include( '/elements/tr-select-reason.html',
6
7     #required 
8     'field'         => 'reasonnum',
9     'reason_class'  => 'C', # currently 'C', 'R',  or 'S'
10                            # for cancel, credit, or suspend
11
12     #recommended
13     'cgi' => $cgi, #easiest way for things to be properly "sticky" on errors
14
15     #optional
16     'control_button' => 'element_name', #button to be enabled when a reason is
17                                         #selected
18     'id'             => 'element_id',
19
20     #deprecated ways to keep things "sticky" on errors
21     # (requires duplicate code in each using file to parse cgi params)
22     'curr_value'     => $curr_value,
23     'curr_value'     => {
24                           'typenum' => $typenum,
25                           'reason'  => $reason,
26                         },
27
28   )
29
30 </%doc>
31
32 <SCRIPT TYPE="text/javascript">
33   function sh_add<% $func_suffix %>()
34   {
35
36     if (document.getElementById('<% $id %>').selectedIndex == 0){
37       <% $controlledbutton ? $controlledbutton.'.disabled = true;' : ';' %>
38     }else{
39       <% $controlledbutton ? $controlledbutton.'.disabled = false;' : ';' %>
40     }
41
42 %if ($curuser->access_right($add_access_right)){
43
44     if (document.getElementById('<% $id %>').selectedIndex == 
45          (document.getElementById('<% $id %>').length - 1)) {
46       document.getElementById('new<% $id %>').disabled = false;
47       document.getElementById('new<% $id %>').style.display = 'inline';
48       document.getElementById('new<% $id %>Label').style.display = 'inline';
49       document.getElementById('new<% $id %>T').disabled = false;
50       document.getElementById('new<% $id %>T').style.display = 'inline';
51       document.getElementById('new<% $id %>TLabel').style.display = 'inline';
52     } else {
53       document.getElementById('new<% $id %>').disabled = true;
54       document.getElementById('new<% $id %>').style.display = 'none';
55       document.getElementById('new<% $id %>Label').style.display = 'none';
56       document.getElementById('new<% $id %>T').disabled = true;
57       document.getElementById('new<% $id %>T').style.display = 'none';
58       document.getElementById('new<% $id %>TLabel').style.display = 'none';
59     }
60
61 %}
62
63   }
64 </SCRIPT>
65
66 <TR>
67   <TD ALIGN="right"><% mt('Reason') |h %></TD>
68   <TD>
69     <SELECT id="<% $id %>" name="<% $name %>" onFocus="sh_add<% $func_suffix %>()" onChange="sh_add<% $func_suffix %>()">
70       <OPTION VALUE="" <% ($init_reason eq '') ? 'SELECTED' : '' %>><% mt('Select Reason...') |h %></OPTION>
71 %    foreach my $reason (@reasons) {
72       <OPTION VALUE="<% $reason->reasonnum %>" <% ($init_reason == $reason->reasonnum) ? 'SELECTED' : '' %>><% $reason->reasontype->type %> : <% $reason->reason %></OPTION>
73 %    }
74 %    if ($curuser->access_right($add_access_right)) {
75       <OPTION VALUE="-1" <% ($init_reason == -1) ? 'SELECTED' : '' %>><% mt('Add new reason') |h %></OPTION>
76 %    }
77 %
78     </SELECT>
79   </TD>
80 </TR>
81
82 %   my @types = qsearch( 'reason_type', { 'class' => $class } );
83 %   if (scalar(@types) < 1) {  # we should never reach this
84 <TR>
85   <TD ALIGN="right">
86     <P><% mt('No reason types. Please add some.') |h %></P>
87   </TD>
88 </TR>
89 %   }elsif (scalar(@types) == 1) {
90 <TR>
91   <TD ALIGN="right">
92     <P id="new<% $name %>TLabel" style="display:<% $display %>"><% mt('Reason Type') |h %></P>
93   </TD>
94   <TD>
95     <P id="new<% $name %>T" disabled="<% $disabled %>" style="display:<% $display %>"><% $types[0]->type %>
96     <INPUT type="hidden" name="new<% $name %>T" value="<% $types[0]->typenum %>">
97   </TD>
98 </TR>
99
100 %   }else{
101
102 <TR>
103   <TD ALIGN="right">
104     <P id="new<% $id %>TLabel" style="display:<% $display %>"><% mt('Reason Type') |h %></P>
105   </TD>
106   <TD>
107     <SELECT id="new<% $id %>T" name="new<% $name %>T" "<% $disabled %>" style="display:<% $display %>">
108 %     for my $type (@types) {
109         <OPTION VALUE="<% $type->typenum %>" <% ($init_type == $type->typenum) ? 'SELECTED' : '' %>><% $type->type %></OPTION>
110 %     }
111     </SELECT>
112   </TD>
113 </TR>
114 %   }
115
116 <TR>
117   <TD ALIGN="right">
118     <P id="new<% $id %>Label" style="display:<% $display %>"><% mt('New Reason') |h %></P>
119   </TD>
120   <TD><INPUT id="new<% $id %>" name="new<% $name %>" type="text" value="<% $init_newreason |h %>" "<% $disabled %>" style="display:<% $display %>"></TD>
121 </TR>
122
123 <%init>
124
125 my %opt = @_;
126
127 my $name = $opt{'field'};
128 my $class = $opt{'reason_class'};
129
130 my $init_reason;
131 if ( $opt{'cgi'} ) {
132   $init_reason = $opt{'cgi'}->param($name);
133 } else {
134   $init_reason = $opt{'curr_value'};
135 }
136
137 my $controlledbutton = $opt{'control_button'};
138
139 ( my $func_suffix = $name ) =~ s/\./_/g;
140
141 my $id = $opt{'id'} || $func_suffix;
142
143 my( $add_access_right, $access_right ); 
144 if ($class eq 'C') {
145   $access_right = 'Cancel customer';
146   $add_access_right = 'Add on-the-fly cancel reason';
147 } elsif ($class eq 'S') {
148   $access_right = 'Suspend customer package';
149   $add_access_right = 'Add on-the-fly suspend reason';
150 } elsif ($class eq 'R') {
151   $access_right = 'Post credit';
152   $add_access_right = 'Add on-the-fly credit reason';
153 } else {
154   die "illegal class: $class";
155 }
156
157 my( $display, $disabled ) = ( 'none', 'DISABLED' );
158 my( $init_type, $init_newreason ) = ( '', '' );
159 if ($init_reason == -1 || ref($init_reason) ) {
160
161   $display = 'inline';
162   $disabled = '';
163
164   if ( ref($init_reason) ) {
165     $init_type      = $init_reason->{'typenum'};
166     $init_newreason = $init_reason->{'reason'};
167     $init_reason = -1;
168   } elsif ( $opt{'cgi'} ) {
169     $init_type      = $opt{'cgi'}->param( "new${name}T" );
170     $init_newreason = $opt{'cgi'}->param( "new$name"    );
171   }
172
173 }
174
175 my $extra_sql =
176   "WHERE class = '$class' and (disabled = '' OR disabled is NULL)";
177
178 my @reasons = qsearch({
179   table     => 'reason', 
180   hashref   => {},
181   extra_sql => $extra_sql,
182   addl_from => 'LEFT JOIN reason_type '.
183                ' ON reason_type.typenum = reason.reason_type',
184   order_by  => 'ORDER BY reason_type.type ASC, reason.reason ASC',
185 });
186
187 my $curuser = $FS::CurrentUser::CurrentUser;
188
189 </%init>