+<%init>
+
+die "access denied"
+ unless $FS::CurrentUser::CurrentUser->access_right('View customer services');
+
+my(%opt) = @_;
+
+my $conf = new FS::Conf;
+my $date_format = $conf->config('date_format') || '%m/%d/%Y';
+
+my $table = $opt{'table'};
+
+my $fields = $opt{'fields'}
+ #|| [ grep { $_ ne 'svcnum' } dbdef->table($table)->columns ];
+ || [ grep { $_ ne 'svcnum' } fields($table) ];
+
+my $svcnum;
+if ( $cgi->param('svcnum') ) {
+ $cgi->param('svcnum') =~ /^(\d+)$/ or die "unparseable svcnum";
+ $svcnum = $1;
+} else {
+ my($query) = $cgi->keywords;
+ $query =~ /^(\d+)$/ or die "no svcnum";
+ $svcnum = $1;
+}
+my $svc_x = qsearchs({
+ 'select' => $opt{'table'}.'.*',
+ 'table' => $opt{'table'},
+ 'addl_from' => ' LEFT JOIN cust_svc USING ( svcnum ) '.
+ ' LEFT JOIN cust_pkg USING ( pkgnum ) '.
+ ' LEFT JOIN cust_main USING ( custnum ) ',
+ 'hashref' => { 'svcnum' => $svcnum },
+ 'extra_sql' => ' AND '. $FS::CurrentUser::CurrentUser->agentnums_sql(
+ 'null_right' => 'View/link unlinked services'
+ ),
+}) or die "Unknown svcnum $svcnum in ". $opt{'table'}. " table\n";
+
+my $cust_svc = $svc_x->cust_svc;
+my ($label, $value, $svcdb, $part_svc );
+my $labels = $opt{labels} || {};
+
+if ( $cust_svc ) {
+ ($label, $value, $svcdb) = $cust_svc->label;
+
+ $part_svc = $cust_svc->part_svc;
+
+ #false laziness w/edit/svc_Common.html
+ #override default labels with service-definition labels if applicable
+ foreach my $field ( keys %$labels ) {
+ my $col = $part_svc->part_svc_column($field);
+ $labels->{$field} = $col->columnlabel if $col->columnlabel !~ /^\s*$/;
+ }
+} else {
+ $label = "Unlinked $table";
+ $value = $svc_x->label;
+ $svcdb = $table;
+ # just to satisfy callbacks
+ $part_svc = FS::part_svc->new({ svcpart => 0, svcdb => $table });
+}
+
+my $pkgnum = $cust_svc->pkgnum if $cust_svc;
+
+my($cust_pkg, $custnum);
+if ($pkgnum) {
+ $cust_pkg = $cust_svc->cust_pkg;
+ $custnum = $cust_pkg->custnum;
+} else {
+ $cust_pkg = '';
+ $custnum = '';
+}
+
+# attached routers
+if ( my $router = qsearchs('router', { svcnum => $svc_x->svcnum }) ) {
+ push @$fields,
+ 'router_routername',
+ 'router_block';
+
+ $labels->{'router_routername'} = 'Attached router';
+ $labels->{'router_block'} = 'Attached address block';
+ $svc_x->set('router_routername', $router->routername);
+ my $block = qsearchs('addr_block', { routernum => $router->routernum });
+ if ( $block ) {
+ $svc_x->set('router_block', $block->cidr);
+ } else {
+ $svc_x->set('router_block', '(none)');
+ }
+}
+
+my @inventory_items = $svc_x->inventory_item;
+
+my $format_field = sub {
+ my $f = shift;
+ my($field, $type, $value);
+ if ( ref($f) ) {
+ $field = $f->{'field'};
+ $type = $f->{'type'} || 'text';
+ } else {
+ $field = $f;
+ $type = 'text';
+ }
+
+ my $columndef = $part_svc->part_svc_column($field);
+ # skip fields that are fixed and empty
+ if ( $columndef->columnflag eq 'F'
+ and length($columndef->columnvalue) == 0 ) {
+ return;
+ }
+
+ # things that override the column value: value_callback, select
+ if ( ref($f) and $f->{'value_callback'} ) {
+
+ my $hack_strict_refs = \&{ $f->{'value_callback'} };
+ $value = &$hack_strict_refs($svc_x);
+
+ } elsif ( $type eq 'select-table' ) {
+ # imitates the /elements/select-table interface
+ $value = $svc_x->$field;
+
+ my $value_col = $f->{'value_col'} ||
+ dbdef->table($f->{'table'})->primary_key;
+ my $name_col = $f->{'name_col'} or die 'name_col required';
+ # we don't yet support multiple-valued fields here
+ my $obj = qsearchs($f->{'table'}, { $value_col => $value });
+ if ( $obj ) {
+ $value = $obj->$name_col; # can be any method of the object
+ } else {
+ # show the raw value, but mark it as an error
+ $value = '<SPAN CLASS="error">' . $f->{'table'} . ' ' .
+ encode_entities($value) . '</SPAN>';
+ }
+
+ } else {
+ $value = encode_entities($svc_x->$field);
+ }
+
+ # inventory-select field with multiple classes
+ # show the class name to disambiguate
+ if ( $columndef->columnflag =~ /^[MA]$/ && $columndef->columnvalue =~ /,/ )
+ {
+ my ($item) = grep { $_->svc_field eq $field } @inventory_items;
+ my $class = qsearchs('inventory_class', { classnum => $item->classnum });
+ $value .= ' <i>('. $class->classname . ')</i>' if $class;
+ }
+
+ # formatting tweaks
+ if ( $type eq 'date' and $value ) {
+ $value = time2str($date_format,$value)
+ } elsif ( $type eq 'datetime' and $value ) {
+ $value = time2str("$date_format %H:%M",$value)
+ } elsif ( $type eq 'checkbox' ) {
+ $value = $value eq 'Y' ? emt('Yes') : emt('No');
+ } elsif ( $type =~ /(input-)?mac_addr/ and $value =~ /\w/) {
+ my $vendor = Net::MAC::Vendor::lookup($value);
+ $value .= ' ('. $vendor->[0]. ')' if $vendor;
+ $value = $m->scomp('/elements/mac_addr.html', $value);
+ }
+
+ # 'link' option
+ my $href;
+ if ( ref($f) and exists $f->{'link'} ) {
+ my $link = $f->{'link'};
+ if ( ref($link) eq 'CODE' ) {
+ $link = &{$link}($svc_x);
+ }
+ if ( ref($link) eq 'ARRAY' ) {
+ my ($base, $method) = @$link;
+ $href = $base . $svc_x->$method();
+ } elsif ( !ref($link) ) {
+ $href = $link;
+ }
+
+ if ( $href ) {
+ $value = qq!<A HREF="$href">$value</A>!;
+ }
+ }
+
+ my $label = $opt{labels}->{$field} || $field;
+ return ($field, $label, $value);
+};
+
+&{ $opt{'svc_callback'} }( $cgi, $svc_x, $part_svc, $cust_pkg, $fields, \%opt )
+ if $opt{'svc_callback'};
+</%init>