fix fallout: allowing selectable choice for arbitrary things broke classic selectable...
[freeside.git] / httemplate / edit / elements / svc_Common.html
1 <% include( 'edit.html',
2
3                  'menubar' => [],
4
5                  'error_callback' => sub {
6                    my( $cgi, $svc_x, $fields, $opt ) = @_;
7                    #$svcnum = $svc_x->svcnum;
8                    $pkgnum  = $cgi->param('pkgnum');
9                    $svcpart = $cgi->param('svcpart');
10
11                    $part_svc = qsearchs( 'part_svc', { svcpart=>$svcpart });
12                    die "No part_svc entry!" unless $part_svc;
13
14                    label_fixup($part_svc, $opt);
15
16                    $svc_x->setfield('svcpart', $svcpart);
17
18                    if ( my $cb = $opt{'svc_error_callback'} ) {
19                      my $cust_pkg = $pkgnum
20                                       ? qsearchs('cust_pkg', {pkgnum=>$pkgnum})
21                                       : ''; #?
22                      &{ $cb }( $cgi,$svc_x, $part_svc,$cust_pkg, $fields,$opt);
23                    }
24
25                  },
26
27                  'edit_callback' => sub {
28                    my( $cgi, $svc_x, $fields, $opt ) = @_;
29                    #$svcnum = $svc_x->svcnum;
30                    my $cust_svc = $svc_x->cust_svc
31                      or die "Unknown (cust_svc) svcnum!";
32
33                    $pkgnum  = $cust_svc->pkgnum;
34                    $svcpart = $cust_svc->svcpart;
35   
36                    $part_svc = qsearchs ('part_svc', { svcpart=>$svcpart });
37                    die "No part_svc entry!" unless $part_svc;
38
39                    label_fixup($part_svc, $opt);
40                    
41                    if ( my $cb = $opt{'svc_edit_callback'} ) {
42                      my $cust_pkg = $pkgnum
43                                       ? qsearchs('cust_pkg', {pkgnum=>$pkgnum})
44                                       : ''; #?
45                      &{ $cb }( $cgi,$svc_x, $part_svc,$cust_pkg, $fields,$opt);
46                    }
47
48                    if ( $part_svc->has_router ) {
49                      my $router = qsearchs('router', {svcnum => $svc_x->svcnum});
50                      if ( $router ) {
51                        $svc_x->set("router_$_", $router->get($_))
52                          foreach ('routername', 'routernum');
53                        my ($block) = $router->addr_block; # one-to-one for now
54                        if ( $block ) {
55                          $svc_x->set('router_blocknum', $block->blocknum);
56                          # silly, but necessary...make the currently 
57                          # assigned block appear on the list
58                          my ($field) = grep {ref($_) and 
59                                              $_->{field} eq 'router_blocknum'}
60                                          @$fields;
61                          $field->{extra_sql} = 
62                            ' OR routernum = '.$router->routernum;
63                          $field->{curr_value} = $block->blocknum;
64                        }
65                      }
66                    }
67
68                  },
69
70                  'new_hashref_callback' => sub {
71                    #my( $cgi, $svc_x ) = @_;
72
73                    { pkgnum  => $pkgnum,
74                      svcpart => $svcpart,
75                    };
76
77                  },
78
79                  'new_callback' => sub {
80                    my( $cgi, $svc_x, $fields, $opt ) = @_;
81
82                    $part_svc = qsearchs( 'part_svc', { svcpart=>$svcpart });
83                    die "No part_svc entry!" unless $part_svc;
84
85                    label_fixup($part_svc, $opt);
86
87                    #$svcnum='';
88
89                    if ( my $cb = $opt{'svc_new_callback'} ) {
90                      my $cust_pkg = $pkgnum
91                                      ? qsearchs('cust_pkg', {pkgnum=>$pkgnum})
92                                      : ''; #?
93                      &{ $cb }( $cgi,$svc_x, $part_svc,$cust_pkg, $fields,$opt);
94                    }
95
96                    $svc_x->set_default_and_fixed;
97                  },
98
99                  'field_callback' => sub {
100                    my ($cgi, $object, $f) = @_;
101
102                    my $columndef = $part_svc->part_svc_column($f->{'field'});
103                    my $flag = $columndef->columnflag;
104
105                    if ( $flag eq 'F' ) { #fixed
106                      $f->{'type'} = length($columndef->columnvalue)
107                                       ? 'fixed'
108                                       : 'hidden';
109                      $f->{'value'} = $columndef->columnvalue;
110
111                    } elsif ( $flag eq 'A' ) { #auto assign from inventory
112                      $f->{'type'} = 'hidden';
113
114                    } elsif ( $flag eq 'M' ) { #manually assign from inventory
115                      $f->{'type'} = 'select-inventory_item';
116                      $f->{'empty_label'} = 'Select inventory item';
117                      $f->{'extra_sql'} = 'WHERE ( svcnum IS NULL ' .
118                         ($object->svcnum && ' OR svcnum = '.$object->svcnum) .
119                         ')';
120                      $f->{'classnum'} = $columndef->columnvalue;
121                      $f->{'disable_empty'} = $object->svcnum ? 1 : 0;
122
123                    } elsif ( $flag eq 'H' ) { #hardware
124                      $f->{'type'}        = 'select-hardware_type';
125                      $f->{'hashref'}     = {
126                                             'classnum'=>$columndef->columnvalue
127                                            };
128
129                    } elsif ( $flag eq 'S' #selectable choice
130                                && $f->{type} !~ /^select-svc(-domain|_pbx)$/ ) {
131                      $f->{type}    = 'select';
132                      $f->{options} = [ split( /\s*,\s*/,
133                                                 $columndef->columnvalue)
134                                      ];
135                    }
136
137                    if (    $f->{'type'} eq 'select-svc_pbx'
138                         || $f->{'type'} eq 'select-svc-domain' 
139                       )
140                    {
141                      $f->{'include_opt_callback'} =
142                        sub { ( 'pkgnum'  => $pkgnum,
143                                'svcpart' => $svcpart,
144                              );
145                            };
146                    }
147
148                    if ( $f->{'field'} eq 'custnum' && $pkgnum ) {
149                      my $cust_pkg = qsearchs('cust_pkg', {'pkgnum' => $pkgnum});
150                      $object->set('custnum', $cust_pkg->custnum);
151                    }
152
153                    if ( my $cb = $opt{'svc_field_callback'} ) {
154                      &{ $cb }( $cgi, $object, $f);
155                    }
156
157                  },
158
159                  'html_init' => sub {
160                    my $html;
161                    my $cust_main;
162                    if ( $pkgnum ) {
163                      my $cust_pkg = qsearchs('cust_pkg', {'pkgnum' => $pkgnum});
164                      $cust_main = $cust_pkg->cust_main if $cust_pkg;
165                      if ( $cust_main ) {
166                         $html = include( '/elements/small_custview.html',
167                                 $cust_main,
168                                 '',
169                                 1,
170                                 popurl(2). "view/cust_main.cgi"
171                               ). '<BR>';
172                      }
173                    }
174                    $html;
175                  },
176
177                  'html_table_bottom' => sub {
178                    my $svc_x = shift;
179                    my $html = '';
180                    foreach my $field ($svc_x->virtual_fields) {
181                      if ($part_svc->part_svc_column($field)->columnflag ne 'F'){
182                        # If the flag is X, it won't even show up
183                        # in $svc_acct->virtual_fields.
184                        $html .=
185                          $svc_x->pvf($field)->widget( 'HTML',
186                                                       'edit', 
187                                                       $svc_x->getfield($field)
188                                                     );
189                      }
190                    }
191                    $html;
192                  },
193
194                  'html_bottom' => sub {
195                    qq!<INPUT TYPE="hidden" NAME="pkgnum" VALUE="$pkgnum">!.
196                    qq!<INPUT TYPE="hidden" NAME="svcpart" VALUE="$svcpart">!;
197                  },
198
199                  %opt #pass through/override params
200              )
201 %>
202 <%once>
203
204 sub label_fixup {
205   my( $part_svc, $opt ) = @_;
206
207   $opt->{'name'} ||= $part_svc->svc;
208
209   my $svcdb = $part_svc->svcdb;
210   require "FS/$svcdb.pm";
211
212   if ( UNIVERSAL::can("FS::$svcdb", 'table_info') ) {
213     #$opt->{'name'} ||= "FS::$svcdb"->table_info->{'name'};
214
215     my $fields = "FS::$svcdb"->table_info->{'fields'};
216     $opt->{'fields'} ||= [ grep { $_ ne 'svcnum' } keys %$fields ];
217
218     $opt->{labels} ||= {
219                          map { $_ => ( ref($fields->{$_})
220                                          ? $fields->{$_}{'label'}
221                                          : $fields->{$_}
222                                      );
223                              }
224                          keys %$fields
225                        };
226   }
227
228   #false laziness w/view/svc_Common.html
229   #override default labels with service-definition labels if applicable
230   my $labels = $opt->{labels}; # with -> here
231   foreach my $field ( keys %{ $opt->{labels} } ) {
232     my $col = $part_svc->part_svc_column($field);
233     $labels->{$field} = $col->columnlabel if $col->columnlabel !~ /^\s*$/;
234   }
235
236   if ( $part_svc->has_router ) {
237     # these will be set up as pseudo-fields in the new_ and edit_ callbacks
238     push @{ $opt->{'fields'} }, (
239       { field => 'router_routernum',   type => 'hidden' },
240       { field => 'router_routername',  type => 'text', size => 32 },
241       # router address block selection
242       # (one-to-one for now)
243       { field => 'router_blocknum',
244         type  => 'select-table',
245         table       => 'addr_block',
246         hashref     => { 'routernum' => '0' },
247         agent_virt  => 1,
248         agent_null  => 1,
249         name_col    => 'cidr',
250         order_by    => 'ORDER BY ip_gateway, ip_netmask',
251         empty_label => '(none)',
252         disable_empty => 0,
253       },
254     );
255     $labels->{router_routername}  = 'Attached router name';
256     $labels->{router_blocknum}    = 'Attached address block';
257   }
258 }
259
260 </%once>
261 <%init>
262
263 my %opt = @_;
264
265 #my( $svcnum, $pkgnum, $svcpart, $part_svc );
266 my( $pkgnum, $svcpart, $part_svc );
267
268 #get & untaint pkgnum & svcpart
269 if ( ! $cgi->param('error')
270      && $cgi->param('pkgnum') && $cgi->param('svcpart')
271    )
272 {
273   $cgi->param('pkgnum') =~ /^(\d+)$/ or die 'unparsable pkgnum';
274   $pkgnum = $1;
275   $cgi->param('svcpart') =~ /^(\d+)$/ or die 'unparsable svcpart';
276   $svcpart = $1;
277   #$cgi->delete_all(); #so edit.html treats this correctly as new??
278 }
279
280 </%init>