Optimize "Customer has a referring customer" condition, RT#74452
[freeside.git] / httemplate / elements / select-cgp_rule_condition.html
1 % unless ( $opt{'js_only'} ) {
2
3   <INPUT TYPE="hidden" NAME="<%$name%>" ID="<%$id%>" VALUE="<% $curr_value %>">
4
5   <% include( 'select.html',
6                 'field'      => $name.'_conditionname',
7                 'id'         => $id.'_conditionname',
8                 'options'    => \@conditions,
9                 'curr_value' => $conditionname,
10                 'labels'     => { '' => 'Select Condition' },
11                 'onchange'   => $name.'_changed',
12             )
13   %>
14
15   <% include( 'select.html',
16                 'field'      => $name.'_op',
17                 'id'         => $id.'_op',
18                 'options'    => \@op,
19                 'curr_value' => scalar($cgi->param($name.'_op'))
20                                   || $cgp_rule_condition->op,
21                 'disabled'   => $disabled,
22                 'style'      => $style,
23             )
24   %>
25
26   <% include( 'input-text.html',
27                 'field'      => $name.'_params',
28                 'id'         => $id.'_params',
29                 'curr_value' => scalar($cgi->param($name.'_params'))
30                                   || $cgp_rule_condition->params,
31                 'disabled'   => $disabled,
32                 'style'      => $style,
33                 'nodarken_disabled' => 1,
34             )
35   %>
36
37 % # could add more UI sugar for date/time ranges, string #lists, etc.
38
39 % }
40 % unless ( $opt{'html_only'} || $opt{'js_only'} ) {
41     <SCRIPT TYPE="text/javascript">
42 % }
43 % unless ( $opt{'html_only'} ) {
44
45       function opt(what,value,text) {
46         var optionName = new Option(text, value, false, false);
47         var length = what.length;
48         what.options[length] = optionName;
49       }
50
51       function <% $name %>_changed(what) {
52
53         <% $onchange %>
54
55         var <% $name %>_value = what.options[what.selectedIndex].value;
56
57         var op_Element = what.form.<% $name %>_op;
58         var params_Element = what.form.<% $name %>_params;
59
60         //cond2op in javascript... not as elegant cause my js << my perl
61
62         // if bool, hide/disable _op and _params entirely
63         if ( <%$name%>_value == '' || <%$name%>_value == 'Human Generated' ) {
64           op_Element.disabled = true;
65           op_Element.style.visibility = "hidden";
66           params_Element.disabled = true;
67           params_Element.style.visibility = "hidden";
68           return true;
69         }
70         
71         var OpArray = [ 'is', 'is not' ];
72
73         // if lt_ge, add em
74         if ( <%$name%>_value == 'Message Size' || <%$name%>_value == 'Time of Day' || <%$name%>_value == 'Current Date' ) {
75           OpArray.push('less than');
76           OpArray.push('greater than');
77         }
78
79         // unless no_in, add em
80         if ( <%$name%>_value != 'Message Size' && <%$name%>_value != 'Current Date' && <%$name%>_value != 'Existing Mailbox' ) {
81           OpArray.push('in');
82           OpArray.push('not in');
83         }
84
85         // blank the current op list
86         for ( var i = op_Element.length; i >= 0; i-- )
87           op_Element.options[i] = null;
88
89         // update the _op select with this new array
90         for ( var s = 0; s < OpArray.length; s++ )
91             opt(what.form.<% $name %>_op, OpArray[s], OpArray[s]);
92
93         // show _op and _params (in case we were a bool before)
94         op_Element.disabled = false;
95         op_Element.style.visibility = "visible";
96         params_Element.disabled = false;
97         params_Element.style.visibility = "visible";
98
99       }
100 % }
101 % unless ( $opt{'html_only'} || $opt{'js_only'} ) {
102     </SCRIPT>
103 % }
104 <%once>
105
106 my @conditions = (
107   '',
108
109   #generic http://www.communigate.com/CommunigatePro/Rules.html#Conditions
110   'Submit Address',
111   'Time of Day',
112   'Current Date',
113   'Current Day',
114   'Preference',
115   'FreeBusy',
116   'Existing Mailbox',
117
118   #email http://www.communigate.com/CommunigatePro/QueueRules.html#Conditions
119   'From',
120   'Sender',
121   'To',
122   'Cc',
123   'Reply-To',
124   'Any To or Cc',
125   'Each To or Cc',
126   'Return-Path',
127   "'From' Name",
128   'Subject',
129   'Message-ID',
130   'Message Size',
131   'Human Generated',
132   'Header Field',
133   'Any Recipient',
134   'Each Recipient',
135   'Source',
136   'Security',
137   'Any Route',
138   'Each Route'
139 );
140
141 my %bool = ( map { $_=>1 } ( #hide the op and valud dropdowns entirely
142   '',
143   'Human Generated',
144 ));
145
146 my %no_in = ( map { $_=>1 } ( #hide in/not in
147   'Message Size',
148   'Current Date',
149   'Existing Mailbox',
150 ));
151
152 my %lt_gt = ( map { $_=>1 } ( #add less than/greater than
153   'Message Size',
154   'Time of Day',
155   'Current Date',
156 ));
157
158 my $cond2op = sub {
159   my $cond = shift;
160   return () if $bool{$cond}; 
161   my @op = ( 'is', 'is not' );
162   push @op, 'less than', 'greater than'  if $lt_gt{$cond};
163   push @op, 'in', 'not in' unless $no_in{$cond};
164   @op;
165 };
166
167 </%once>
168 <%init>
169
170 my %opt = @_;
171
172 my $name = $opt{'element_name'} || $opt{'field'} || 'ruleconditionnum';
173 #my $id = $opt{'id'} || 'contactnum';
174 my $id = $opt{'id'} || $opt{'field'} || 'ruleconditionnum';
175
176 my $curr_value = $opt{'curr_value'} || $opt{'value'};
177
178 my $onchange = '';
179 if ( $opt{'onchange'} ) {
180   $onchange = $opt{'onchange'};
181   $onchange .= '(what)' unless $onchange =~ /\(\w*\);?$/;
182 }
183
184 my $cgp_rule_condition;
185 if ( $curr_value ) {
186   $cgp_rule_condition = qsearchs('cgp_rule_condition',
187                                    { 'ruleconditionnum' => $curr_value } );
188 } else {
189   $cgp_rule_condition = new FS::cgp_rule_condition {};
190 }
191
192 my $conditionname = scalar($cgi->param($name.'_conditionname'))
193                     || $cgp_rule_condition->conditionname;
194
195 my @op = &$cond2op($conditionname);
196
197 my $disabled = scalar(@op) ? '' : 1;
198 my $style = $disabled ? 'visibility:hidden' : '';
199
200 </%init>