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