Merge branch 'master' of git.freeside.biz:/home/git/freeside
[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">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       <& /elements/select-table.html,
137           'field'       => $name,
138           'id'          => $name.'_select',
139           'table'       => $def->{'select_table'},
140           'name_col'    => $def->{'select_label'},
141           'value_col'   => $def->{'select_key'},
142           'order_by'    => dbdef->table($def->{'select_table'})->primary_key,
143           'multiple'    => $def->{'multiple'},
144           'disable_empty' => $def->{'select_allow_empty'} ? undef : 1,
145           'empty_label' => $def->{'select_allow_empty'} ? ' ' : undef,
146           'curr_value'  => $value,
147           # these can be switched between multiple and singular,
148           # so put the complete curr_value in an attribute
149           'element_etc' => 'default="'.encode_entities($value).'"',
150       &>
151 %     } else {
152 %       my (@options, %labels);
153 %       if ( $def->{'select_list'} ) {
154 %         @options = @{ $def->{'select_list'} };
155 %         @labels{@options} = @options;
156 %       } elsif ( $def->{'select_hash'} ) {
157 %         if ( ref($def->{'select_hash'}) eq 'ARRAY' ) {
158 %           tie my %hash, 'Tie::IxHash', @{ $def->{'select_hash'} };
159 %           $def->{'select_hash'} = \%hash;
160 %         }
161 %         @options = keys( %{ $def->{'select_hash'} } );
162 %         %labels = %{ $def->{'select_hash'} };
163 %       }
164       <& /elements/select.html,
165           'field'       => $name,
166           'id'          => $name.'_select',
167           'options'     => \@options,
168           'labels'      => \%labels,
169           'multiple'    => $def->{'multiple'},
170           'curr_value'  => $value,
171       &>
172 %     }
173 %   } elsif ( $def->{'type'} =~ /^select-(.*?)(.html)?$/ && $1 ne 'hardware' ) {
174       <& "/elements/select-$1.html",
175           'field'       => $name,
176           'id'          => $name.'_select',
177           'multiple'    => $def->{'multiple'},
178           'curr_value'  => $value,
179       &>
180 %   } elsif ( $def->{'type'} eq 'communigate_pro-accessmodes' ) {
181       <& /elements/communigate_pro-accessmodes.html,
182           'element_name_prefix' => $name.'_',
183           'curr_value'  => $value,
184       &>
185 %   } elsif ( $def->{'type'} eq 'textarea' ) {
186 %   # special cases
187       <TEXTAREA NAME="<%$name%>"><% $value |h %></TEXTAREA>
188 %   } elsif ( $def->{'type'} eq 'disabled' ) {
189       <INPUT TYPE="hidden" NAME="<%$name%>" VALUE="">
190 %   } else {
191 %     # the normal case: a text input, and a _select which is an inventory
192 %     # or hardware class
193       <INPUT TYPE="text"
194              NAME="<%$name%>"
195              ID="<%$name%>" 
196              VALUE="<%$value%>">
197 %     my $mode = 'inventory';
198 %     my $multiple = 1;
199 %     if ( $def->{'type'} eq 'select-hardware' ) {
200 %       $mode = 'hardware';
201 %       $multiple = 0;
202 %     }
203       <& /elements/select-table.html,
204           'field'       => $name.'_classnum',
205           'id'          => $name.'_select',
206           'table'       => $mode.'_class',
207           'name_col'    => 'classname',
208           'curr_value'  => $value,
209           'empty_label' => "Select $mode class",
210           'multiple'    => $multiple,
211       &>
212 %   }
213     </TD>
214     <TD>
215 %   if (!$def->{'type'} || !(grep {$_ eq $def->{'type'}} ('checkbox','disabled'))) {
216       <INPUT ID="<% $name.'_required' %>" TYPE="checkbox" NAME="<% $svcdb %>__<% $field %>_required" VALUE="Y" 
217         <% ($part_svc_column->required || $def->{'required'}) ? 'CHECKED' : '' %> 
218         <% $def->{'required'} ? 'DISABLED' : '' %>
219        >
220 %   }
221     </TD>
222   </TR>
223   <TR CLASS="row<%$i%>">
224     <TD COLSPAN=3 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 COLSPAN=2>
241       <INPUT TYPE="checkbox" NAME="restrict_edit_password" VALUE="Y" \
242       <% $part_svc->restrict_edit_password ? 'CHECKED' : '' %>>
243     </TD>
244   </TR>
245 % }
246 % # special case: services with attached routers (false laziness...)
247 % if ( $svcdb eq 'svc_acct'
248 %      or $svcdb eq 'svc_broadband'
249 %      or $svcdb eq 'svc_dsl'
250 %      or $svcdb eq 'svc_circuit' ) {
251 %   push @fields, 'has_router';
252   <TR>
253     <TD COLSPAN=3 ALIGN="right">
254       <% emt('This service has an attached router') %>
255     </TD>
256     <TD COLSPAN=2>
257       <INPUT TYPE="checkbox" NAME="has_router" VALUE="Y" \
258       <% $part_svc->has_router ? 'CHECKED' : '' %>>
259     </TD>
260   </TR>
261 % }
262 </TABLE>
263 <& /elements/progress-init.html,
264   $svcdb, #form name
265   [ # form fields to send
266     qw(svc svcpart classnum selfservice_access disabled preserve exportnum),
267     @fields
268   ],
269   'process/part_svc.cgi',   # target
270   $p.'browse/part_svc.cgi', # redirect landing
271   $svcdb, #key
272 &>
273 % $svcpart = '' if $opt{clone};
274 <BR>
275 <INPUT NAME="submit"
276        TYPE="button"
277        VALUE="<% emt($svcpart ? 'Apply changes' : 'Add service') %>"
278        onclick="fixup_submit('<%$svcdb%>')"
279 >
280 <%init>
281 my $svcdb = shift;
282 my %opt = @_;
283 my $count = 0;
284 my $communigate = 0;
285 my $conf = FS::Conf->new;
286
287 my $part_svc = $opt{'part_svc'} || FS::part_svc->new;
288
289 # see if there are communigate exports configured
290 if ( exists $communigate_fields{$svcdb} ) {
291   $communigate = FS::part_export->count("exporttype like 'communigate%'");
292 }
293
294 my $svcpart = $opt{'clone'} || $part_svc->svcpart;
295
296 my @fields;
297 if ( defined( dbdef->table($svcdb) ) ) { # when is it ever not defined?
298   @fields = grep {
299     $_ ne 'svcnum'
300       and ( $communigate || ! $communigate_fields{$svcdb}->{$_} )
301       and ( !FS::part_svc->svc_table_fields($svcdb)->{$_}->{disable_part_svc_column}
302             || $part_svc->part_svc_column($_)->columnflag )
303   } fields($svcdb);
304 }
305 if ( $svcdb eq 'svc_acct'
306       or ( $svcdb eq 'svc_broadband' and $conf->exists('svc_broadband-radius') )
307    )
308 {
309   push @fields, 'usergroup';
310 }
311
312 my @defs = map { FS::part_svc->svc_table_fields($svcdb)->{$_} } @fields;
313 </%init>