1e1ff79ee9289eff7b21d2c012477c8eab4bf5a1
[freeside.git] / httemplate / edit / elements / part_svc_column.html
1 <%doc>
2 To be called from part_svc.cgi.
3 <& elements/part_svc_column.html, 
4     'svc_acct',
5     # options...
6     'part_svc'  => $part_svc, # the existing part_svc to edit
7     'clone'     => 0,         # or a svcpart to clone from
8 &>
9
10 </%doc>
11 <%once>
12 # the semantics of this could be better
13
14 # all of these conditions are when NOT to allow that flag choice
15 # don't allow the 'inventory' flags (M, A) to be chosen for 
16 # fields that aren't free-text
17 my $inv_sub = sub { $_[0]->{disable_inventory} || $_[0]->{type} ne 'text' };
18
19 tie my %flag, 'Tie::IxHash',
20   ''  => { 'desc' => 'No default', 'condition' => sub { 0 } },
21   'D' => { 'desc' => 'Default', 
22            'condition' =>
23              sub { $_[0]->{disable_default } }
24          },
25   'F' => { 'desc' => 'Fixed (unchangeable)',
26            'condition' =>
27              sub { $_[0]->{disable_fixed} },
28          },
29   'S' => { 'desc' => 'Selectable Choice',
30            'condition' =>
31              sub { $_[0]->{disable_select} },
32          },
33   'M' => { 'desc' => 'Manual selection from inventory',
34            'condition' => $inv_sub,
35          },
36   'A' => { 'desc' => 'Automatically fill in from inventory',
37            'condition' => $inv_sub,
38          },
39   'H' => { 'desc' => 'Select from hardware class',
40            'condition' => sub { $_[0]->{type} ne 'select-hardware' },
41          },
42   'P' => { 'desc' => 'From package FCC 477 information',
43            'condition' => sub { $_[0]->{type} ne 'fcc_477_speed' }, # get values from package fcc 477 information
44          },
45   'X' => { 'desc' => 'Excluded',
46            'condition' => sub { 1 }, # obsolete
47          },
48 ;
49
50 # the semantics of this could be much better
51 sub flag_condition {
52   my $f = shift;
53   not &{ $flag{$f}->{'condition'} }(@_);
54 }
55
56 my %communigate_fields = (
57   'svc_acct'        => { map { $_=>1 }
58                             qw( file_quota file_maxnum file_maxsize
59                                 password_selfchange password_recover
60                               ),
61                             grep /^cgp_/, fields('svc_acct')
62   },
63   'svc_domain'      => { map { $_=>1 }
64                             qw( max_accounts trailer parent_svcnum ),
65                             grep /^(cgp|acct_def)_/, fields('svc_domain')
66   },
67 );
68 </%once>
69 <INPUT TYPE="hidden" NAME="svcdb" VALUE="<% $svcdb %>">
70 <BR><BR>
71 %# include export selection
72 <& export_svc.html,
73   part_svc => $part_svc,
74   svcdb => $svcdb
75 &>
76 For the selected table, you can give fields default or fixed (unchangeable)
77 values, or select an inventory class to manually or automatically fill in 
78 that field.
79 <& /elements/table-grid.html, cellpadding => 4 &>
80   <TR>
81     <TH BGCOLOR="#cccccc">Field</TH>
82     <TH BGCOLOR="#cccccc">Label</TH>
83     <TH BGCOLOR="#cccccc" COLSPAN=2>Modifier</TH>
84     <TH BGCOLOR="#cccccc"><% $manual_require ? 'Required?' : '' %></TH>
85   </TR>
86 % $part_svc->set('svcpart' => $opt{'clone'}) if $opt{'clone'}; # for now
87 % my $i = 0;
88 % foreach my $field (@fields) {
89 %   my $def = shift @defs;
90 %   my $part_svc_column = $part_svc->part_svc_column($field);
91 %   my $flag = $part_svc_column->columnflag;
92 %   my $formatter = $def->{'format'} || sub { shift };
93 %   my $value = &{$formatter}($part_svc_column->columnvalue);
94   <TR CLASS="row<%$i%>">
95     <TD ROWSPAN=2 CLASS="grid" ALIGN="right">
96       <% $def->{'label'} || $field %>
97     </TD>
98     <TD ROWSPAN=2 CLASS="grid">
99       <INPUT NAME="<% $svcdb %>__<% $field %>_label"
100              STYLE="text-align: right"
101              VALUE="<% $part_svc_column->columnlabel || $def->{'label'} |h %>">
102     </TD>
103
104     <TD ROWSPAN=1 CLASS="grid">
105 %   # flag selection
106 %   if ( $def->{'type'} eq 'disabled' ) {
107 %     $flag = '';
108       No default
109 %   } else {
110 %     my $name = $svcdb.'__'.$field.'_flag';
111       <SELECT NAME="<%$name%>"
112               ID="<%$name%>"
113               STYLE="width:100%"
114               onchange="flag_changed(this)">
115 %     foreach my $f (keys %flag) {
116 %       if ( flag_condition($f, $def, $svcdb, $field) ) {
117           <OPTION VALUE="<%$f%>"<% $flag eq $f ? ' SELECTED' : ''%>>
118             <% $flag{$f}->{desc} %>
119           </OPTION>
120 %       }
121 %     }
122       </SELECT>
123 %   } # if $def->{'type'} eq 'disabled'
124     </TD>
125     <TD CLASS="grid">
126 %   # value entry/selection
127 %   my $name = $svcdb.'__'.$field;
128 %   # These are all MANDATORY SELECT types.  Regardless of the flag value,
129 %   # there will never be a text input (either in svc_* or in part_svc) for
130 %   # these fields.
131 %   if ( $def->{'type'} eq 'checkbox' ) {
132       <& /elements/checkbox.html,
133           'field'       => $name,
134           'curr_value'  => $value,
135           'value'       => 'Y' &>
136 %
137 %   } elsif ( $def->{'type'} eq 'select' ) {
138 %
139 %     if ( $def->{'select_table'} ) {
140 %       # set the 'select_svc' flag to enable two-step selection of services
141 %       my $comp = '/elements/select-table.html';
142 %       $comp = '/elements/select-svc.html' if $def->{'select_svc'};
143       <& $comp,
144           'field'       => $name,
145           'id'          => $name.'_select',
146           'table'       => $def->{'select_table'},
147           'name_col'    => $def->{'select_label'},
148           'value_col'   => $def->{'select_key'},
149           'order_by'    => dbdef->table($def->{'select_table'})->primary_key,
150           'multiple'    => $def->{'multiple'},
151           'disable_empty' => $def->{'select_allow_empty'} ? undef : 1,
152           'empty_label' => $def->{'select_allow_empty'} ? ' ' : undef,
153           'curr_value'  => $value,
154           # these can be switched between multiple and singular,
155           # so put the complete curr_value in an attribute
156           'element_etc' => 'default="'.encode_entities($value).'"',
157       &>
158 %     } else {
159 %       my (@options, %labels);
160 %       if ( $def->{'select_list'} ) {
161 %         @options = @{ $def->{'select_list'} };
162 %         @labels{@options} = @options;
163 %       } elsif ( $def->{'select_hash'} ) {
164 %         if ( ref($def->{'select_hash'}) eq 'ARRAY' ) {
165 %           tie my %hash, 'Tie::IxHash', @{ $def->{'select_hash'} };
166 %           $def->{'select_hash'} = \%hash;
167 %         }
168 %         @options = keys( %{ $def->{'select_hash'} } );
169 %         %labels = %{ $def->{'select_hash'} };
170 %       }
171       <& /elements/select.html,
172           'field'       => $name,
173           'id'          => $name.'_select',
174           'options'     => \@options,
175           'labels'      => \%labels,
176           'multiple'    => $def->{'multiple'},
177           'curr_value'  => $value,
178       &>
179 %     }
180 %   } elsif ( $def->{'type'} =~ /^select-(.*?)(.html)?$/ && $1 ne 'hardware' ) {
181       <& "/elements/select-$1.html",
182           'field'       => $name,
183           'id'          => $name.'_select',
184           'multiple'    => $def->{'multiple'},
185           'curr_value'  => $value,
186       &>
187 %   } elsif ( $def->{'type'} eq 'communigate_pro-accessmodes' ) {
188       <& /elements/communigate_pro-accessmodes.html,
189           'element_name_prefix' => $name.'_',
190           'curr_value'  => $value,
191       &>
192 %   } elsif ( $def->{'type'} eq 'textarea' ) {
193 %   # special cases
194       <TEXTAREA NAME="<%$name%>"><% $value |h %></TEXTAREA>
195 %   } elsif ( $def->{'type'} eq 'disabled' ) {
196       <INPUT TYPE="hidden" NAME="<%$name%>" VALUE="">
197 %   } else {
198 %     # the normal case: a text input, and a _select which is an inventory
199 %     # or hardware class
200       <INPUT TYPE="text"
201              NAME="<%$name%>"
202              ID="<%$name%>" 
203              VALUE="<%$value%>">
204 %     my $mode = 'inventory';
205 %     my $multiple = 1;
206 %     if ( $def->{'type'} eq 'select-hardware' ) {
207 %       $mode = 'hardware';
208 %       $multiple = 0;
209 %     }
210 %
211 %     if ( $def->{'type'} eq 'fcc_477_speed' ) {
212 %       if ($field eq 'speed_up') {
213           <SPAN ID="<% $name %>_select">
214             upstream speed
215             <INPUT TYPE="hidden" ID="<% $name %>_select" NAME="<% $name %>_classnum" VALUE="up">
216           </SPAN>
217 %       } elsif ($field eq 'speed_down') {
218           <SPAN ID="<% $name %>_select">
219             downstream speed
220             <INPUT TYPE="hidden" ID="<% $name %>_select" NAME="<% $name %>_classnum" VALUE="down">
221           </SPAN>
222 %       }
223 %     } else {
224       <& /elements/select-table.html,
225           'field'       => $name.'_classnum',
226           'id'          => $name.'_select',
227           'table'       => $mode.'_class',
228           'name_col'    => 'classname',
229           'curr_value'  => $value,
230           'empty_label' => "Select $mode class",
231           'multiple'    => $multiple,
232       &>
233 %     }
234 %   }
235     </TD>
236     <TD>
237 %   if ($manual_require && 
238 %       (!$def->{'type'} || !(grep {$_ eq $def->{'type'}} ('checkbox','disabled')))
239 %   ) {
240       <INPUT ID="<% $name.'_required' %>" TYPE="checkbox" NAME="<% $svcdb %>__<% $field %>_required" VALUE="Y" 
241         <% ($part_svc_column->required || $def->{'required'}) ? 'CHECKED' : '' %> 
242         <% $def->{'required'} ? 'DISABLED' : '' %>
243        >
244 %   }
245     </TD>
246   </TR>
247   <TR CLASS="row<%$i%>">
248     <TD COLSPAN=3 CLASS="def_info">
249 %   if ( $def->{def_info} ) {
250       (<% $def->{def_info} %>)
251     </TD>
252   </TR>
253 %   }
254 % $i = 1-$i;
255 % } # foreach my $field
256 %
257 % # special case: svc_acct password edit ACL
258 % if ( $svcdb eq 'svc_acct' ) {
259 %   push @fields, 'restrict_edit_password';
260   <TR>
261     <TD COLSPAN=3 ALIGN="right">
262       <% emt('Require "Provision" access right to edit password') %>
263     </TD>
264     <TD COLSPAN=2>
265       <INPUT TYPE="checkbox" NAME="restrict_edit_password" VALUE="Y" \
266       <% $part_svc->restrict_edit_password ? 'CHECKED' : '' %>>
267     </TD>
268   </TR>
269 % }
270 % # special case: services with attached routers (false laziness...)
271 % if ( $svcdb eq 'svc_acct'
272 %      or $svcdb eq 'svc_broadband'
273 %      or $svcdb eq 'svc_dsl'
274 %      or $svcdb eq 'svc_circuit' ) {
275 %   push @fields, 'has_router';
276   <TR>
277     <TD COLSPAN=3 ALIGN="right">
278       <% emt('This service has an attached router') %>
279     </TD>
280     <TD COLSPAN=2>
281       <INPUT TYPE="checkbox" NAME="has_router" VALUE="Y" \
282       <% $part_svc->has_router ? 'CHECKED' : '' %>>
283     </TD>
284   </TR>
285 % }
286 </TABLE>
287 <& /elements/progress-init.html,
288   $svcdb, #form name
289   [ # form fields to send
290     'ALL'
291 #    qw(svc svcpart classnum selfservice_access disabled preserve exportnum),
292 #    @fields
293   ],
294   'process/part_svc.cgi',   # target
295   $p.'browse/part_svc.cgi', # redirect landing
296   $svcdb, #key
297 &>
298 % $svcpart = '' if $opt{clone};
299 <BR>
300 <INPUT NAME="submit"
301        TYPE="button"
302        VALUE="<% emt($svcpart ? 'Apply changes' : 'Add service') %>"
303        onclick="fixup_submit('<%$svcdb%>')"
304 >
305 <%init>
306 my $svcdb = shift;
307 my %opt = @_;
308 my $count = 0;
309 my $communigate = 0;
310 my $conf = FS::Conf->new;
311
312 my $part_svc = $opt{'part_svc'} || FS::part_svc->new;
313
314 # see if there are communigate exports configured
315 if ( exists $communigate_fields{$svcdb} ) {
316   $communigate = FS::part_export->count("exporttype like 'communigate%'");
317 }
318
319 my $svcpart = $opt{'clone'} || $part_svc->svcpart;
320
321 my @fields;
322 if ( defined( dbdef->table($svcdb) ) ) { # when is it ever not defined?
323   @fields = grep {
324     $_ ne 'svcnum'
325       and ( $communigate || ! $communigate_fields{$svcdb}->{$_} )
326       and ( !FS::part_svc->svc_table_fields($svcdb)->{$_}->{disable_part_svc_column}
327             || $part_svc->part_svc_column($_)->columnflag )
328   } fields($svcdb);
329 }
330 if ( $svcdb eq 'svc_acct'
331       or ( $svcdb eq 'svc_broadband' and $conf->exists('svc_broadband-radius') )
332    )
333 {
334   push @fields, 'usergroup';
335 }
336
337 my @defs = map { FS::part_svc->svc_table_fields($svcdb)->{$_} } @fields;
338 my $manual_require = FS::part_svc->svc_table_info($svcdb)->{'manual_require'};
339 </%init>