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