fbea9bd6d98cc9dcda76283799e7fc96cf3cf7ea
[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 <& /elements/table.html &>
68   <TR><TH COLSPAN=<% $columns %>>Exports</TH></TR>
69   <TR>
70 % # exports
71 % foreach my $part_export (@part_export) {
72     <TD>
73       <INPUT TYPE="checkbox" \
74              NAME="exportnum<% $part_export->exportnum %>" \
75              VALUE=1 \
76              <% $has_export_svc{$part_export->exportnum} ? 'CHECKED' : '' %>>
77       <% $part_export->label_html %>
78     </TD>
79 %   $count++;
80 %   if ( $count % $columns == 0 ) {
81   </TR>
82   <TR>
83 %   }
84 % }
85   </TR>
86 </TABLE><BR><BR>
87 For the selected table, you can give fields default or fixed (unchangeable)
88 values, or select an inventory class to manually or automatically fill in 
89 that field.
90 <& /elements/table-grid.html, cellpadding => 4 &>
91   <TR>
92     <TH BGCOLOR="#cccccc">Field</TH>
93     <TH BGCOLOR="#cccccc">Label</TH>
94     <TH BGCOLOR="#cccccc" COLSPAN=2>Modifier</TH>
95   </TR>
96 % $part_svc->set('svcpart' => $opt{'clone'}) if $opt{'clone'}; # for now
97 % my $i = 0;
98 % foreach my $field (@fields) {
99 %   my $def = shift @defs;
100 %   my $part_svc_column = $part_svc->part_svc_column($field);
101 %   my $flag = $part_svc_column->columnflag;
102 %   my $formatter = $def->{'format'} || sub { shift };
103 %   my $value = &{$formatter}($part_svc_column->columnvalue);
104   <TR CLASS="row<%$i%>">
105     <TD ROWSPAN=2 CLASS="grid" ALIGN="right">
106       <% $def->{'label'} || $field %>
107     </TD>
108     <TD ROWSPAN=2 CLASS="grid">
109       <INPUT NAME="<% $svcdb %>__<% $field %>_label"
110              STYLE="text-align: right"
111              VALUE="<% $part_svc_column->columnlabel || $def->{'label'} |h %>">
112     </TD>
113
114     <TD ROWSPAN=1 CLASS="grid">
115 %   # flag selection
116 %   if ( $def->{'type'} eq 'disabled' ) {
117 %     $flag = '';
118       No default
119 %   } else {
120 %     my $name = $svcdb.'__'.$field.'_flag';
121       <SELECT NAME="<%$name%>"
122               ID="<%$name%>"
123               STYLE="width:100%"
124               onchange="flag_changed(this)">
125 %     foreach my $f (keys %flag) {
126 %       if ( flag_condition($f, $def, $svcdb, $field) ) {
127           <OPTION VALUE="<%$f%>"<% $flag eq $f ? ' SELECTED' : ''%>>
128             <% $flag{$f}->{desc} %>
129           </OPTION>
130 %       }
131 %     }
132       </SELECT>
133 %   } # if $def->{'type'} eq 'disabled'
134     </TD>
135     <TD CLASS="grid">
136 %   # value entry/selection
137 %   my $name = $svcdb.'__'.$field;
138 %   # These are all MANDATORY SELECT types.  Regardless of the flag value,
139 %   # there will never be a text input (either in svc_* or in part_svc) for
140 %   # these fields.
141 %   if ( $def->{'type'} eq 'checkbox' ) {
142       <& /elements/checkbox.html,
143           'field'       => $name,
144           'curr_value'  => $value,
145           'value'       => 'Y' &>
146 %
147 %   } elsif ( $def->{'type'} eq 'select' ) {
148 %
149 %     if ( $def->{'select_table'} ) {
150       <& /elements/select-table.html,
151           'field'       => $name,
152           'id'          => $name.'_select',
153           'table'       => $def->{'select_table'},
154           'name_col'    => $def->{'select_label'},
155           'value_col'   => $def->{'select_key'},
156           'order_by'    => dbdef->table($def->{'select_table'})->primary_key,
157           'multiple'    => $def->{'multiple'},
158           'disable_empty' => 1,
159           'curr_value'  => $value,
160           # these can be switched between multiple and singular,
161           # so put the complete curr_value in an attribute
162           'element_etc' => 'default="'.encode_entities($value).'"',
163       &>
164 %     } else {
165 %       my (@options, %labels);
166 %       if ( $def->{'select_list'} ) {
167 %         @options = @{ $def->{'select_list'} };
168 %         @labels{@options} = @options;
169 %       } elsif ( $def->{'select_hash'} ) {
170 %         if ( ref($def->{'select_hash'}) eq 'ARRAY' ) {
171 %           tie my %hash, 'Tie::IxHash', @{ $def->{'select_hash'} };
172 %           $def->{'select_hash'} = \%hash;
173 %         }
174 %         @options = keys( %{ $def->{'select_hash'} } );
175 %         %labels = %{ $def->{'select_hash'} };
176 %       }
177       <& /elements/select.html,
178           'field'       => $name,
179           'id'          => $name.'_select',
180           'options'     => \@options,
181           'labels'      => \%labels,
182           'multiple'    => $def->{'multiple'},
183           'curr_value'  => $value,
184       &>
185 %     }
186 %   } elsif ( $def->{'type'} =~ /select-(.*?).html/ ) {
187       <& '/elements/'.$def->{'type'},
188           'field'       => $name,
189           'id'          => $name.'_select',
190           'multiple'    => $def->{'multiple'},
191           'curr_value'  => $value,
192       &>
193 %   } elsif ( $def->{'type'} eq 'communigate_pro-accessmodes' ) {
194       <& /elements/communigate_pro-accessmodes.html,
195           'element_name_prefix' => $name.'_',
196           'curr_value'  => $value,
197       &>
198 %   } elsif ( $def->{'type'} eq 'textarea' ) {
199 %   # special cases
200       <TEXTAREA NAME="<%$name%>"><% $value |h %></TEXTAREA>
201 %   } elsif ( $def->{'type'} eq 'disabled' ) {
202       <INPUT TYPE="hidden" NAME="<%$name%>" VALUE="">
203 %   } else {
204 %     # the normal case: a text input, and a _select which is an inventory
205 %     # or hardware class
206       <INPUT TYPE="text"
207              NAME="<%$name%>"
208              ID="<%$name%>" 
209              VALUE="<%$value%>">
210 %     # inventory class selection
211       <& /elements/select-table.html,
212           'field'       => $name.'_classnum',
213           'id'          => $name.'_select',
214           'table'       => 'inventory_class',
215           'name_col'    => 'classname',
216           'curr_value'  => $value,
217           'empty_label' => 'Select inventory class',
218           'multiple'    => 1,
219       &>
220 %   }
221     </TD>
222   </TR>
223   <TR CLASS="row<%$i%>">
224     <TD COLSPAN=2 CLASS="def_info">
225 %   if ( $def->{def_info} ) {
226       (<% $def->{def_info} %>)
227     </TD>
228   </TR>
229 %   }
230 % $i = 1-$i;
231 % } # foreach my $field
232 %
233 % # special case: svc_acct password edit ACL
234 % if ( $svcdb eq 'svc_acct' ) {
235 %   push @fields, 'restrict_edit_password';
236   <TR>
237     <TD COLSPAN=3 ALIGN="right">
238       <% emt('Require "Provision" access right to edit password') %>
239     </TD>
240     <TD>
241       <INPUT TYPE="checkbox" NAME="restrict_edit_password" VALUE="Y" \
242       <% $part_svc->restrict_edit_password ? 'CHECKED' : '' %>>
243     </TD>
244   </TR>
245 % }
246 </TABLE>
247 <& /elements/progress-init.html,
248   $svcdb, #form name
249   [ # form fields to send
250     qw(svc svcpart classnum selfservice_access disabled preserve exportnum),
251     @fields
252   ],
253   'process/part_svc.cgi',   # target
254   $p.'browse/part_svc.cgi', # redirect landing
255   $svcdb, #key
256 &>
257 % $svcpart = '' if $opt{clone};
258 <BR>
259 <INPUT NAME="submit"
260        TYPE="button"
261        VALUE="<% emt($svcpart ? 'Apply changes' : 'Add service') %>"
262        onclick="fixup_submit('<%$svcdb%>')"
263 >
264 <%init>
265 my $svcdb = shift;
266 my %opt = @_;
267 my $columns = 3;
268 my $count = 0;
269 my $communigate = 0;
270 my $conf = FS::Conf->new;
271
272 my $part_svc = $opt{'part_svc'} || FS::part_svc->new;
273
274 my @part_export;
275 my $export_info = FS::part_export::export_info($svcdb);
276 foreach (keys %{ $export_info }) {
277   push @part_export, qsearch('part_export', { exporttype => $_ });
278 }
279 $communigate = scalar(grep {$_->exporttype =~ /^communigate/} @part_export);
280
281 my $svcpart = $opt{'clone'} || $part_svc->svcpart;
282 my %has_export_svc;
283 if ( $svcpart ) {
284   foreach (qsearch('export_svc', { svcpart => $svcpart })) {
285     $has_export_svc{$_->exportnum} = 1;
286   }
287 }
288
289 my @fields;
290 if ( defined( dbdef->table($svcdb) ) ) { # when is it ever not defined?
291   @fields = grep {
292     $_ ne 'svcnum'
293       and ( $communigate || ! $communigate_fields{$svcdb}->{$_} )
294       and ( !FS::part_svc->svc_table_fields($svcdb)->{$_}->{disable_part_svc_column}
295             || $part_svc->part_svc_column($_)->columnflag )
296   } fields($svcdb);
297 }
298 if ( $svcdb eq 'svc_acct'
299       or ( $svcdb eq 'svc_broadband' and $conf->exists('svc_broadband-radius') )
300    )
301 {
302   push @fields, 'usergroup';
303 }
304
305 my @defs = map { FS::part_svc->svc_table_fields($svcdb)->{$_} } @fields;
306 </%init>