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,
32 'layer2' => [ 'l2fieldname',
36 #current values for layer fields above
37 'layer_values' => { 'layer' => { 'fieldname' => 'current_value',
38 'fieldname2' => 'field2value',
41 'layer2' => { 'l2fieldname' => 'l2value',
47 #or manual control, instead of layer_fields and layer_values above
48 #called with args: my( $layer, $layer_fields, $layer_values, $layer_prefix )
51 'html_between => '', #optional HTML displayed between the SELECT and the
52 #layers, scalar or coderef ('field' passed as a param)
53 'onchange' => '', #javascript code run when the SELECT changes
54 # ("what" is the element)
55 'js_only' => 0, #set true to return only the JS portions
56 'html_only' => 0, #set true to return only the HTML portions
57 'select_only' => 0, #set true to return only the <SELECT> HTML
58 'layers_only' => 0, #set true to return only the layers <DIV> HTML
62 % unless ( grep $opt{$_}, qw(html_only js_only select_only layers_only) ) {
63 <SCRIPT TYPE="text/javascript">
65 % unless ( grep $opt{$_}, qw(html_only select_only layers_only) ) {
67 % if ( $opt{layermap} ) {
68 % my %map = %{ $opt{layermap} };
69 var layermap = { "":"",
70 <% join(',', map { qq("$_":"$map{$_}") } keys %map ) %>
74 function <% $key %>changed(what) {
76 <% $opt{'onchange'} %>
78 var <% $key %>layer = what.options[what.selectedIndex].value;
80 % foreach my $layer ( @layers ) {
82 % if ( $opt{layermap} ) {
83 if ( layermap[ <% $key %>layer ] == "<% $layer %>" ) {
85 if (<% $key %>layer == "<% $layer %>" ) {
88 % foreach my $not ( grep { $_ ne $layer } @layers ) {
89 % my $element = "document.getElementById('${key}d$not').style";
90 <% $element %>.display = "none";
91 <% $element %>.zIndex = 0;
94 % my $element = "document.getElementById('${key}d$layer').style";
95 <% $element %>.display = "";
96 <% $element %>.zIndex = 1;
101 //<% $opt{'onchange'} %>
105 % unless ( grep $opt{$_}, qw(html_only js_only select_only layers_only) ) {
109 % unless ( grep $opt{$_}, qw(js_only layers_only) ) {
111 <SELECT NAME = "<% $key %>"
113 previousValue = "<% $selected %>"
114 previousText = "<% $options{$selected} %>"
115 onChange="<% $key %>changed(this);"
118 % foreach my $option ( keys %$options ) {
120 <OPTION VALUE="<% $option %>"
121 <% $option eq $selected ? ' SELECTED' : '' %>
122 ><% $options->{$option} %></OPTION>
129 % unless ( grep $opt{$_}, qw(js_only select_only layers_only) ) {
131 <% ref($between) ? &{$between}($key) : $between %>
135 % unless ( grep $opt{$_}, qw(js_only select_only) ) {
137 % foreach my $layer ( @layers ) {
138 % my $selected_layer;
139 % if ( $opt{layermap} ) {
140 % $selected_layer = $opt{layermap}->{$selected};
142 % $selected_layer = $selected;
145 <DIV ID="<% $key %>d<% $layer %>"
146 STYLE="<% $selected_layer eq $layer
147 ? 'display: "" ; z-index: 1'
148 : 'display: none; z-index: 0'
152 <% &{$layer_callback}($layer, $layer_fields, $layer_values, $layer_prefix) %>
161 my $conf = new FS::Conf;
162 my $money_char = $conf->config('money_char') || '$';
172 my $key = $opt{field}; # || 'generate_one' #?
174 tie my %options, 'Tie::IxHash',
175 map { $_ => $opt{'labels'}->{$_} }
176 @{ $opt{'options'} }; #just arrayref for now
178 my $between = exists($opt{html_between}) ? $opt{html_between} : '';
179 my $options = \%options;
182 if ( $opt{layermap} ) {
183 my %layers = map { $opt{layermap}->{$_} => 1 } keys %options;
184 @layers = keys %layers;
186 @layers = keys %options;
189 my $selected = exists($opt{curr_value}) ? $opt{curr_value} : '';
191 #XXX eek. also eek $layer_fields in the layer_callback() call...
192 my $layer_fields = $opt{layer_fields};
193 my $layer_values = $opt{layer_values};
194 my $layer_prefix = $opt{layer_prefix};
196 my $layer_callback = $opt{layer_callback} || \&layer_callback;
199 my( $layer, $layer_fields, $layer_values, $layer_prefix ) = @_;
201 return '' unless $layer && exists $layer_fields->{$layer};
202 tie my %fields, 'Tie::IxHash', @{ $layer_fields->{$layer} };
204 #XXX this should become an element itself... (false laziness w/edit.html)
205 # but at least all the elements inside are the shared mason elements now
207 return '' unless keys %fields;
208 my $html = "<TABLE>";
210 foreach my $field ( keys %fields ) {
212 my $lf = ref($fields{$field})
214 : { 'label'=>$fields{$field} };
216 my $value = $layer_values->{$layer}{$field};
218 my $type = $lf->{type} || 'text';
221 $include = "input-$include" if $include =~ /^(text|money)$/;
222 $include = "tr-$include" unless $include eq 'hidden';
224 $html .= include( "/elements/$include.html",
226 'field' => "$layer_prefix$field",
227 'id' => "$layer_prefix$field", #separate?
228 #don't want field0_label0...?
229 'label_id' => $layer_prefix.$field."_label",
231 'value' => ( $lf->{'value'} || $value ), #hmm.
232 'curr_value' => $value,