new package editor
[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       //alert('start function define');
67       function <% $key %>changed(what) {
68
69         <% $opt{'onchange'} %>
70
71         var <% $key %>layer = what.options[what.selectedIndex].value;
72
73 %       foreach my $layer ( keys %$options ) {
74
75           if (<% $key %>layer == "<% $layer %>" ) {
76
77 %           foreach my $not ( grep { $_ ne $layer } keys %$options ) {
78 %             my $element = "document.getElementById('${key}d$not').style";
79               <% $element %>.display = "none";
80               <% $element %>.zIndex = 0;
81 %           }
82
83 %           my $element = "document.getElementById('${key}d$layer').style";
84             <% $element %>.display = "";
85             <% $element %>.zIndex = 1;
86
87           }
88 %       }
89
90         //<% $opt{'onchange'} %>
91
92       }
93       //alert('end function define');
94 % }
95 % unless ( grep $opt{$_}, qw(html_only js_only select_only layers_only) ) {
96     </SCRIPT>
97 % }
98 %
99 % unless ( grep $opt{$_}, qw(js_only layers_only) ) {
100
101     <SELECT NAME          = "<% $key %>"
102             ID            = "<% $key %>"
103             previousValue = "<% $selected %>"
104             previousText  = "<% $options{$selected} %>"
105             onChange="<% $key %>changed(this);"
106     >
107
108 %     foreach my $option ( keys %$options ) {
109
110         <OPTION VALUE="<% $option %>"
111                 <% $option eq $selected ? ' SELECTED' : '' %>
112         ><% $options->{$option} %></OPTION>
113
114 %     }
115
116     </SELECT>
117
118 % }
119 % unless ( grep $opt{$_}, qw(js_only select_only layers_only) ) {
120
121 <% ref($between) ? &{$between}($key) : $between %>
122
123 % }
124 %
125 % unless ( grep $opt{$_}, qw(js_only select_only) ) {
126
127 %   foreach my $layer ( keys %$options ) {
128
129       <DIV ID="<% $key %>d<% $layer %>"
130            STYLE="<% $layer eq $selected
131                        ? 'display: ""  ; z-index: 1'
132                        : 'display: none; z-index: 0'
133                   %>"
134       >
135
136         <% &{$layer_callback}($layer, $layer_fields, $layer_values, $layer_prefix) %>
137
138       </DIV>
139
140 %   }
141
142 % }
143 <%once>
144
145 my $conf = new FS::Conf;
146 my $money_char = $conf->config('money_char') || '$';
147
148 </%once>
149 <%init>
150
151 my %opt = @_;
152
153 #use Data::Dumper;
154 #warn Dumper(%opt);
155
156 my $key = $opt{field}; # || 'generate_one' #?
157
158 tie my %options, 'Tie::IxHash',
159    map { $_ => $opt{'labels'}->{$_} }
160        @{ $opt{'options'} }; #just arrayref for now
161
162 my $between = exists($opt{html_between}) ? $opt{html_between} : '';
163 my $options = \%options;
164
165 my $selected = exists($opt{curr_value}) ? $opt{curr_value} : '';
166
167 #XXX eek.  also eek $layer_fields in the layer_callback() call...
168 my $layer_fields = $opt{layer_fields};
169 my $layer_values = $opt{layer_values};
170 my $layer_prefix = $opt{layer_prefix};
171
172 my $layer_callback = $opt{layer_callback} || \&layer_callback;
173
174 sub layer_callback {
175   my( $layer, $layer_fields, $layer_values, $layer_prefix ) = @_;
176
177   return  '' unless $layer && exists $layer_fields->{$layer};
178   tie my %fields, 'Tie::IxHash', @{ $layer_fields->{$layer} };
179
180   #XXX this should become an element itself... (false laziness w/edit.html)
181   # but at least all the elements inside are the shared mason elements now
182
183   return '' unless keys %fields;
184   my $html = "<TABLE>";
185
186   foreach my $field ( keys %fields ) {
187
188     my $lf = ref($fields{$field})
189                ? $fields{$field}
190                : { 'label'=>$fields{$field} };
191
192     my $value = $layer_values->{$layer}{$field};
193
194     my $type = $lf->{type} || 'text';
195
196     my $include = $type;
197     $include = "input-$include" if $include =~ /^(text|money)$/;
198     $include = "tr-$include" unless $include eq 'hidden';
199
200     $html .= include( "/elements/$include.html",
201                         %$lf,
202                         'field'      => "$layer_prefix$field",
203                         'id'         => "$layer_prefix$field", #separate?
204                         #don't want field0_label0...?
205                         'label_id'   => $layer_prefix.$field."_label",
206
207                         'value'      => ( $lf->{'value'} || $value ), #hmm.
208                         'curr_value' => $value,
209                     );
210
211   }
212   $html .= '</TABLE>';
213   return $html;
214 }
215
216 </%init>