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