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
9 'curr_value' => $selected_layer,
10 'options' => [ 'option1', 'option2' ],
11 'labels' => { 'option1' => 'Option 1 Label',
12 'option2' => 'Option 2 Label',
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:
23 # select, select-agent,
25 # select-part_referral,
33 'layer2' => [ 'l2fieldname',
38 #current values for layer fields above
39 'layer_values' => { 'layer' => { 'fieldname' => 'current_value',
40 'fieldname2' => 'field2value',
43 'layer2' => { 'l2fieldname' => 'l2value',
49 #or manual control, instead of layer_fields and layer_values above
50 #called with args: my( $layer, $layer_fields, $layer_values, $layer_prefix )
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
64 % unless ( grep $opt{$_}, qw(html_only js_only select_only layers_only) ) {
65 <SCRIPT TYPE="text/javascript">
67 % unless ( grep $opt{$_}, qw(html_only select_only layers_only) ) {
69 % if ( $opt{layermap} ) {
70 % my %map = %{ $opt{layermap} };
71 var layermap = { "":"",
72 <% join(',', map { qq("$_":"$map{$_}") } keys %map ) %>
76 function <% $key %>changed(what) {
78 <% $opt{'onchange'} %>
80 var <% $key %>layer = what.options[what.selectedIndex].value;
82 % foreach my $layer ( @layers ) {
84 % if ( $opt{layermap} ) {
85 if ( layermap[ <% $key %>layer ] == "<% $layer %>" ) {
87 if (<% $key %>layer == "<% $layer %>" ) {
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;
96 % my $element = "document.getElementById('${key}d$layer').style";
97 <% $element %>.display = "";
98 <% $element %>.zIndex = 1;
103 //<% $opt{'onchange'} %>
107 % unless ( grep $opt{$_}, qw(html_only js_only select_only layers_only) ) {
111 % unless ( grep $opt{$_}, qw(js_only layers_only) ) {
113 <SELECT NAME = "<% $key %>"
115 previousValue = "<% $selected %>"
116 previousText = "<% $options{$selected} %>"
117 onChange="<% $key %>changed(this);"
120 % foreach my $option ( keys %$options ) {
122 <OPTION VALUE="<% $option %>"
123 <% $option eq $selected ? ' SELECTED' : '' %>
124 ><% $options->{$option} |h %></OPTION>
131 % unless ( grep $opt{$_}, qw(js_only select_only layers_only) ) {
133 <% ref($between) ? &{$between}($key) : $between %>
137 % unless ( grep $opt{$_}, qw(js_only select_only) ) {
139 % foreach my $layer ( @layers ) {
140 % my $selected_layer;
141 % if ( $opt{layermap} ) {
142 % $selected_layer = $opt{layermap}->{$selected};
144 % $selected_layer = $selected;
147 <DIV ID="<% $key %>d<% $layer %>"
148 STYLE="<% $selected_layer eq $layer
149 ? 'display: block; z-index: 1'
150 : 'display: none; z-index: 0'
154 <% &{$layer_callback}($layer, $layer_fields, $layer_values, $layer_prefix) %>
163 my $conf = new FS::Conf;
164 my $money_char = $conf->config('money_char') || '$';
175 my $key = $opt{field}; # || 'generate_one' #?
177 tie my %options, 'Tie::IxHash',
178 map { $_ => $opt{'labels'}->{$_} }
179 @{ $opt{'options'} }; #just arrayref for now
181 my $between = exists($opt{html_between}) ? $opt{html_between} : '';
182 my $options = \%options;
185 if ( $opt{layermap} ) {
186 my %layers = map { $opt{layermap}->{$_} => 1 } keys %options;
187 @layers = keys %layers;
189 @layers = keys %options;
192 my $selected = exists($opt{curr_value}) ? $opt{curr_value} : '';
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};
199 my $layer_callback = $opt{layer_callback} || \&layer_callback;
202 my( $layer, $layer_fields, $layer_values, $layer_prefix ) = @_;
204 return '' unless $layer && exists $layer_fields->{$layer};
205 tie my %fields, 'Tie::IxHash', @{ $layer_fields->{$layer} };
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
210 return '' unless keys %fields;
211 my $html = "<TABLE>";
213 foreach my $field ( keys %fields ) {
215 my $lf = ref($fields{$field})
217 : { 'label'=>$fields{$field} };
219 my $value = $layer_values->{$layer}{$field};
221 my $type = $lf->{type} || 'text';
225 if ( $include eq 'date' ) {
226 # several important differences from other tr-*
227 $html .= include( '/elements/tr-input-date-field.html',
229 'name' => "$layer_prefix$field",
231 'label' => $lf->{label},
232 'format'=> $lf->{format},
233 'noinit'=> $date_noinit,
239 $include = "input-$include" if $include =~ /^(text|money|percentage)$/;
240 $include = "tr-$include" unless $include eq 'hidden';
241 $html .= include( "/elements/$include.html",
243 'field' => "$layer_prefix$field",
244 'id' => "$layer_prefix$field", #separate?
245 #don't want field0_label0...?
246 'label_id' => $layer_prefix.$field."_label",
248 'value' => ( $lf->{'value'} || $value ), #hmm.
249 'curr_value' => $value,