summaryrefslogtreecommitdiff
path: root/httemplate/elements/select-cgp_rule_condition.html
blob: bc96ab48774cfe6b1b21a354d1b74629d6ac4341 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
% unless ( $opt{'js_only'} ) {

  <INPUT TYPE="hidden" NAME="<%$name%>" ID="<%$id%>" VALUE="<% $curr_value %>">

  <% include( 'select.html',
                'field'      => $name.'_conditionname',
                'id'         => $id.'_conditionname',
                'options'    => \@conditions,
                'curr_value' => $conditionname,
                'labels'     => { '' => 'Select Condition' },
                'onchange'   => $name.'_changed',
            )
  %>

  <% include( 'select.html',
                'field'      => $name.'_op',
                'id'         => $id.'_op',
                'options'    => \@op,
                'curr_value' => scalar($cgi->param($name.'_op'))
                                  || $cgp_rule_condition->op,
                'disabled'   => $disabled,
                'style'      => $style,
            )
  %>

  <% include( 'input-text.html',
                'field'      => $name.'_params',
                'id'         => $id.'_params',
                'curr_value' => scalar($cgi->param($name.'_params'))
                                  || $cgp_rule_condition->params,
                'disabled'   => $disabled,
                'style'      => $style,
                'nodarken_disabled' => 1,
            )
  %>

% # could add more UI sugar for date/time ranges, string #lists, etc.

% }
% unless ( $opt{'html_only'} || $opt{'js_only'} ) {
    <SCRIPT TYPE="text/javascript">
% }
% unless ( $opt{'html_only'} ) {

      function opt(what,value,text) {
        var optionName = new Option(text, value, false, false);
        var length = what.length;
        what.options[length] = optionName;
      }

      function <% $name %>_changed(what) {

        <% $onchange %>

        var <% $name %>_value = what.options[what.selectedIndex].value;

        var op_Element = what.form.<% $name %>_op;
        var params_Element = what.form.<% $name %>_params;

        //cond2op in javascript... not as elegant cause my js << my perl

        // if bool, hide/disable _op and _params entirely
        if ( <%$name%>_value == '' || <%$name%>_value == 'Human Generated' ) {
          op_Element.disabled = true;
          op_Element.style.visibility = "hidden";
          params_Element.disabled = true;
          params_Element.style.visibility = "hidden";
          return true;
        }
        
        var OpArray = [ 'is', 'is not' ];

        // if lt_ge, add em
        if ( <%$name%>_value == 'Message Size' || <%$name%>_value == 'Time of Day' || <%$name%>_value == 'Current Date' ) {
          OpArray.push('less than');
          OpArray.push('greater than');
        }

        // unless no_in, add em
        if ( <%$name%>_value != 'Message Size' && <%$name%>_value != 'Current Date' && <%$name%>_value != 'Existing Mailbox' ) {
          OpArray.push('in');
          OpArray.push('not in');
        }

        // blank the current op list
        for ( var i = op_Element.length; i >= 0; i-- )
          op_Element.options[i] = null;

        // update the _op select with this new array
        for ( var s = 0; s < OpArray.length; s++ )
            opt(what.form.<% $name %>_op, OpArray[s], OpArray[s]);

        // show _op and _params (in case we were a bool before)
        op_Element.disabled = false;
        op_Element.style.visibility = "visible";
        params_Element.disabled = false;
        params_Element.style.visibility = "visible";

      }
% }
% unless ( $opt{'html_only'} || $opt{'js_only'} ) {
    </SCRIPT>
% }
<%once>

my @conditions = (
  '',

  #generic http://www.communigate.com/CommunigatePro/Rules.html#Conditions
  'Submit Address',
  'Time of Day',
  'Current Date',
  'Current Day',
  'Preference',
  'FreeBusy',
  'Existing Mailbox',

  #email http://www.communigate.com/CommunigatePro/QueueRules.html#Conditions
  'From',
  'Sender',
  'To',
  'Cc',
  'Reply-To',
  'Any To or Cc',
  'Each To or Cc',
  'Return-Path',
  "'From' Name",
  'Subject',
  'Message-ID',
  'Message Size',
  'Human Generated',
  'Header Field',
  'Any Recipient',
  'Each Recipient',
  'Source',
  'Security',
  'Any Route',
  'Each Route'
);

my %bool = ( map { $_=>1 } ( #hide the op and valud dropdowns entirely
  '',
  'Human Generated',
));

my %no_in = ( map { $_=>1 } ( #hide in/not in
  'Message Size',
  'Current Date',
  'Existing Mailbox',
));

my %lt_gt = ( map { $_=>1 } ( #add less than/greater than
  'Message Size',
  'Time of Day',
  'Current Date',
));

my $cond2op = sub {
  my $cond = shift;
  return () if $bool{$cond}; 
  my @op = ( 'is', 'is not' );
  push @op, 'less than', 'greater than'  if $lt_gt{$cond};
  push @op, 'in', 'not in' unless $no_in{$cond};
  @op;
};

</%once>
<%init>

my %opt = @_;

my $name = $opt{'element_name'} || $opt{'field'} || 'ruleconditionnum';
#my $id = $opt{'id'} || 'contactnum';
my $id = $opt{'id'} || $opt{'field'} || 'ruleconditionnum';

my $curr_value = $opt{'curr_value'} || $opt{'value'};

my $onchange = '';
if ( $opt{'onchange'} ) {
  $onchange = $opt{'onchange'};
  $onchange .= '(what)' unless $onchange =~ /\(\w*\);?$/;
}

my $cgp_rule_condition;
if ( $curr_value ) {
  $cgp_rule_condition = qsearchs('cgp_rule_condition',
                                   { 'ruleconditionnum' => $curr_value } );
} else {
  $cgp_rule_condition = new FS::cgp_rule_condition {};
}

my $conditionname = scalar($cgi->param($name.'_conditionname'))
                    || $cgp_rule_condition->conditionname;

my @op = &$cond2op($conditionname);

my $disabled = scalar(@op) ? '' : 1;
my $style = $disabled ? 'visibility:hidden' : '';

</%init>