a85cea193bfc9555379ebe48a4b3d356ffcab6aa
[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-table,
27                                                          #XXX tbd:
28                                                          # more?
29                                       },
30                                       ...
31                                     ],
32                         'layer2' => [ 'l2fieldname',
33                                       ...
34                                     ],
35
36     #current values for layer fields above
37     'layer_values' => { 'layer'  => { 'fieldname'  => 'current_value',
38                                       'fieldname2' => 'field2value',
39                                       ...
40                                     },
41                         'layer2' => { 'l2fieldname' => 'l2value',
42                                       ...
43                                     },
44                         ...
45                       },
46
47     #or manual control, instead of layer_fields and layer_values above
48     #called with args: my( $layer, $layer_fields, $layer_values, $layer_prefix )
49     'layer_callback' => 
50
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
59   )
60
61 </%doc>
62 % unless ( grep $opt{$_}, qw(html_only js_only select_only layers_only) ) {
63     <SCRIPT TYPE="text/javascript">
64 % }
65 % unless ( grep $opt{$_}, qw(html_only select_only layers_only) ) {
66
67 %     if ( $opt{layermap} ) {
68 %       my %map = %{ $opt{layermap} };
69         var layermap = { "":"",
70                          <% join(',', map { qq("$_":"$map{$_}") } keys %map ) %>
71                        };
72 %     }
73
74       function <% $key %>changed(what) {
75
76         <% $opt{'onchange'} %>
77
78         var <% $key %>layer = what.options[what.selectedIndex].value;
79
80 %       foreach my $layer ( @layers ) {
81 %
82 %         if ( $opt{layermap} ) {
83           if ( layermap[ <% $key %>layer ] == "<% $layer %>" ) {
84 %         } else {
85           if (<% $key %>layer == "<% $layer %>" ) {
86 %         }
87
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;
92 %           }
93
94 %           my $element = "document.getElementById('${key}d$layer').style";
95             <% $element %>.display = "";
96             <% $element %>.zIndex = 1;
97
98           }
99 %       }
100
101         //<% $opt{'onchange'} %>
102
103       }
104 % }
105 % unless ( grep $opt{$_}, qw(html_only js_only select_only layers_only) ) {
106     </SCRIPT>
107 % }
108 %
109 % unless ( grep $opt{$_}, qw(js_only layers_only) ) {
110
111     <SELECT NAME          = "<% $key %>"
112             ID            = "<% $key %>"
113             previousValue = "<% $selected %>"
114             previousText  = "<% $options{$selected} %>"
115             onChange="<% $key %>changed(this);"
116     >
117
118 %     foreach my $option ( keys %$options ) {
119
120         <OPTION VALUE="<% $option %>"
121                 <% $option eq $selected ? ' SELECTED' : '' %>
122         ><% $options->{$option} %></OPTION>
123
124 %     }
125
126     </SELECT>
127
128 % }
129 % unless ( grep $opt{$_}, qw(js_only select_only layers_only) ) {
130
131 <% ref($between) ? &{$between}($key) : $between %>
132
133 % }
134 %
135 % unless ( grep $opt{$_}, qw(js_only select_only) ) {
136
137 %   foreach my $layer ( @layers ) {
138 %     my $selected_layer;
139 %     if ( $opt{layermap} ) {
140 %       $selected_layer = $opt{layermap}->{$selected};
141 %     } else {
142 %       $selected_layer = $selected;
143 %     }
144
145       <DIV ID="<% $key %>d<% $layer %>"
146            STYLE="<% $selected_layer eq $layer
147                        ? 'display: ""  ; z-index: 1'
148                        : 'display: none; z-index: 0'
149                   %>"
150       >
151
152         <% &{$layer_callback}($layer, $layer_fields, $layer_values, $layer_prefix) %>
153
154       </DIV>
155
156 %   }
157
158 % }
159 <%once>
160
161 my $conf = new FS::Conf;
162 my $money_char = $conf->config('money_char') || '$';
163
164 </%once>
165 <%init>
166
167 my %opt = @_;
168
169 #use Data::Dumper;
170 #warn Dumper(%opt);
171
172 my $key = $opt{field}; # || 'generate_one' #?
173
174 tie my %options, 'Tie::IxHash',
175    map { $_ => $opt{'labels'}->{$_} }
176        @{ $opt{'options'} }; #just arrayref for now
177
178 my $between = exists($opt{html_between}) ? $opt{html_between} : '';
179 my $options = \%options;
180
181 my @layers = ();
182 if ( $opt{layermap} ) {
183   my %layers = map { $opt{layermap}->{$_} => 1 } keys %options;
184   @layers = keys %layers;
185 } else {
186   @layers = keys %options;
187 }
188
189 my $selected = exists($opt{curr_value}) ? $opt{curr_value} : '';
190
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};
195
196 my $layer_callback = $opt{layer_callback} || \&layer_callback;
197
198 sub layer_callback {
199   my( $layer, $layer_fields, $layer_values, $layer_prefix ) = @_;
200
201   return  '' unless $layer && exists $layer_fields->{$layer};
202   tie my %fields, 'Tie::IxHash', @{ $layer_fields->{$layer} };
203
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
206
207   return '' unless keys %fields;
208   my $html = "<TABLE>";
209
210   foreach my $field ( keys %fields ) {
211
212     my $lf = ref($fields{$field})
213                ? $fields{$field}
214                : { 'label'=>$fields{$field} };
215
216     my $value = $layer_values->{$layer}{$field};
217
218     my $type = $lf->{type} || 'text';
219
220     my $include = $type;
221     $include = "input-$include" if $include =~ /^(text|money)$/;
222     $include = "tr-$include" unless $include eq 'hidden';
223
224     $html .= include( "/elements/$include.html",
225                         %$lf,
226                         'field'      => "$layer_prefix$field",
227                         'id'         => "$layer_prefix$field", #separate?
228                         #don't want field0_label0...?
229                         'label_id'   => $layer_prefix.$field."_label",
230
231                         'value'      => ( $lf->{'value'} || $value ), #hmm.
232                         'curr_value' => $value,
233                     );
234
235   }
236   $html .= '</TABLE>';
237   return $html;
238 }
239
240 </%init>