4.x style
[freeside.git] / httemplate / elements / selectlayers.html
1 <%doc>
2
3 Example:
4
5   include( '/elements/selectlayers.html',
6     'field'        => $key, # SELECT element NAME (passed as form field)
7                             # also used as ID and a unique key for layers and
8                             # functions
9     'curr_value'   => $selected_layer,
10     'options'      => [ 'option1', 'option2' ],
11     'labels'       => { 'option1' => 'Option 1 Label',
12                         'option2' => 'Option 2 Label',
13                       },
14
15     #XXX put this handling it its own selectlayers-fields.html element?
16     'layer_prefix' => 'prefix_', #optional prefix for fieldnames
17     'layer_fields' => { 'layer'  => [ 'fieldname',
18                                       { label => 'fieldname2',
19                                         type  => 'text', #implemented:
20                                                          # text, money, fixed,
21                                                          # hidden, checkbox,
22                                                          # checkbox-multiple,
23                                                          # select, select-agent,
24                                                          # select-pkg_class,
25                                                          # select-part_referral,
26                                                          # select-taxclass,
27                                                          # select-table,
28                                                          #XXX tbd:
29                                                          # more?
30                                       },
31                                       ...
32                                     ],
33                         'layer2' => [ 'l2fieldname',
34                                       ...
35                                     ],
36                       },
37
38     #current values for layer fields above
39     'layer_values' => { 'layer'  => { 'fieldname'  => 'current_value',
40                                       'fieldname2' => 'field2value',
41                                       ...
42                                     },
43                         'layer2' => { 'l2fieldname' => 'l2value',
44                                       ...
45                                     },
46                         ...
47                       },
48
49     #or manual control, instead of layer_fields and layer_values above
50     #called with args: my( $layer, $layer_fields, $layer_values, $layer_prefix )
51     'layer_callback' => 
52
53     'html_between  => '', #optional HTML displayed between the SELECT and the
54                           #layers, scalar or coderef ('field' passed as a param)
55     'onchange'     => '', #javascript code run when the SELECT changes
56                           # ("what" is the element)
57     'js_only'      => 0, #set true to return only the JS portions
58     'html_only'    => 0, #set true to return only the HTML portions
59     'select_only'  => 0, #set true to return only the <SELECT> HTML
60     'layers_only'  => 0, #set true to return only the layers <DIV> HTML
61   )
62
63 </%doc>
64 % unless ( grep $opt{$_}, qw(html_only js_only select_only layers_only) ) {
65     <SCRIPT TYPE="text/javascript">
66 % }
67 % unless ( grep $opt{$_}, qw(html_only select_only layers_only) ) {
68
69 %     if ( $opt{layermap} ) {
70 %       my %map = %{ $opt{layermap} };
71         var layermap = { "":"",
72                          <% join(',', map { qq("$_":"$map{$_}") } keys %map ) %>
73                        };
74 %     }
75
76       function <% $key %>changed(what) {
77
78         <% $opt{'onchange'} %>
79
80         var <% $key %>layer = what.options[what.selectedIndex].value;
81
82 %       foreach my $layer ( @layers ) {
83 %
84 %         if ( $opt{layermap} ) {
85           if ( layermap[ <% $key %>layer ] == "<% $layer %>" ) {
86 %         } else {
87           if (<% $key %>layer == "<% $layer %>" ) {
88 %         }
89
90 %           foreach my $not ( grep { $_ ne $layer } @layers ) {
91 %             my $element = "document.getElementById('${key}d$not').style";
92               <% $element %>.display = "none";
93               <% $element %>.zIndex = 0;
94 %           }
95
96 %           my $element = "document.getElementById('${key}d$layer').style";
97             <% $element %>.display = "";
98             <% $element %>.zIndex = 1;
99
100           }
101 %       }
102
103         //<% $opt{'onchange'} %>
104
105       }
106 % }
107 % unless ( grep $opt{$_}, qw(html_only js_only select_only layers_only) ) {
108     </SCRIPT>
109 % }
110 %
111 % unless ( grep $opt{$_}, qw(js_only layers_only) ) {
112
113     <SELECT NAME          = "<% $key %>"
114             ID            = "<% $key %>"
115             previousValue = "<% $selected %>"
116             previousText  = "<% $options{$selected} %>"
117             onChange="<% $key %>changed(this);"
118     >
119
120 %     foreach my $option ( keys %$options ) {
121
122         <OPTION VALUE="<% $option %>"
123                 <% $option eq $selected ? ' SELECTED' : '' %>
124         ><% $options->{$option} |h %></OPTION>
125
126 %     }
127
128     </SELECT>
129
130 % }
131 % unless ( grep $opt{$_}, qw(js_only select_only layers_only) ) {
132
133 <% ref($between) ? &{$between}($key) : $between %>
134
135 % }
136 %
137 % unless ( grep $opt{$_}, qw(js_only select_only) ) {
138
139 %   foreach my $layer ( @layers ) {
140 %     my $selected_layer;
141 %     if ( $opt{layermap} ) {
142 %       $selected_layer = $opt{layermap}->{$selected};
143 %     } else {
144 %       $selected_layer = $selected;
145 %     }
146
147       <DIV ID="<% $key %>d<% $layer %>"
148            STYLE="<% $selected_layer eq $layer
149                        ? 'display: block; z-index: 1'
150                        : 'display: none; z-index: 0'
151                   %>"
152       >
153
154         <% &{$layer_callback}($layer, $layer_fields, $layer_values, $layer_prefix) %>
155
156       </DIV>
157
158 %   }
159
160 % }
161 <%once>
162
163 my $conf = new FS::Conf;
164 my $money_char = $conf->config('money_char') || '$';
165 my $date_noinit = 0;
166
167 </%once>
168 <%init>
169
170 my %opt = @_;
171
172 #use Data::Dumper;
173 #warn Dumper(%opt);
174
175 my $key = $opt{field}; # || 'generate_one' #?
176
177 tie my %options, 'Tie::IxHash',
178    map { $_ => $opt{'labels'}->{$_} }
179        @{ $opt{'options'} }; #just arrayref for now
180
181 my $between = exists($opt{html_between}) ? $opt{html_between} : '';
182 my $options = \%options;
183
184 my @layers = ();
185 if ( $opt{layermap} ) {
186   my %layers = map { $opt{layermap}->{$_} => 1 } keys %options;
187   @layers = keys %layers;
188 } else {
189   @layers = keys %options;
190 }
191
192 my $selected = exists($opt{curr_value}) ? $opt{curr_value} : '';
193
194 #XXX eek.  also eek $layer_fields in the layer_callback() call...
195 my $layer_fields = $opt{layer_fields};
196 my $layer_values = $opt{layer_values};
197 my $layer_prefix = $opt{layer_prefix};
198
199 my $layer_callback = $opt{layer_callback} || \&layer_callback;
200
201 sub layer_callback {
202   my( $layer, $layer_fields, $layer_values, $layer_prefix ) = @_;
203
204   return  '' unless $layer && exists $layer_fields->{$layer};
205   tie my %fields, 'Tie::IxHash', @{ $layer_fields->{$layer} };
206
207   #XXX this should become an element itself... (false laziness w/edit.html)
208   # but at least all the elements inside are the shared mason elements now
209
210   return '' unless keys %fields;
211   my $html = "<TABLE>";
212
213   foreach my $field ( keys %fields ) {
214
215     my $lf = ref($fields{$field})
216                ? $fields{$field}
217                : { 'label'=>$fields{$field} };
218
219     my $value = $layer_values->{$layer}{$field};
220
221     my $type = $lf->{type} || 'text';
222
223     my $include = $type;
224
225     if ( $include eq 'date' ) {
226       # several important differences from other tr-*
227       $html .= include( '/elements/tr-input-date-field.html',
228         {
229           'name'  => "$layer_prefix$field",
230           'value' => $value,
231           'label' => $lf->{label},
232           'format'=> $lf->{format},
233           'noinit'=> $date_noinit,
234         }
235       );
236       $date_noinit = 1;
237     }
238     else {
239       $include = "input-$include" if $include =~ /^(text|money|percentage)$/;
240       $include = "tr-$include" unless $include eq 'hidden';
241       $html .= include( "/elements/$include.html",
242                         %$lf,
243                         'field'      => "$layer_prefix$field",
244                         'id'         => "$layer_prefix$field", #separate?
245                         #don't want field0_label0...?
246                         'label_id'   => $layer_prefix.$field."_label",
247
248                         'value'      => ( $lf->{'value'} || $value ), #hmm.
249                         'curr_value' => $value,
250                     );
251     }
252   } #foreach $field
253   $html .= '</TABLE>';
254   return $html;
255 }
256
257 </%init>