default to a session cookie instead of setting an explicit timeout, weird timezone...
[freeside.git] / httemplate / search / elements / svc_Common.html
1 <& search.html, %opt &>
2 <%doc>
3 Currently does nothing but insert the classnames for fields chosen from an
4 inventory class.
5 </%doc>
6 <%init>
7 my %opt = @_;
8 my $query = $opt{query};
9 my $svcdb = $query->{'table'};
10
11 # to avoid looking up the inventory class of every service in the database,
12 # keep as much of the base query as possible.
13 my $item_query = { %$query };
14 $item_query->{'table'} = 'inventory_item';
15 $item_query->{'addl_from'} = 
16   " JOIN ( $svcdb ". $query->{'addl_from'} .
17   ") ON inventory_item.svcnum = $svcdb.svcnum ".
18   " JOIN inventory_class ON (inventory_item.classnum = inventory_class.classnum)";
19 # avoid conflict with inventory_item.agentnum
20 $item_query->{'extra_sql'} =~ s/ agentnum/ cust_main.agentnum/g;
21 $item_query->{'select'} = 'inventory_item.svcnum, '.
22                           'inventory_item.svc_field, '.
23                           'inventory_class.classname';
24 my @items = qsearch($item_query);
25 my %item_fields;
26 foreach my $i (@items) {
27   $item_fields{ $i->svc_field } ||= {};
28   $item_fields{ $i->svc_field }{ $i->svcnum } = $i->classname;
29 }
30
31 $opt{'sort_fields'} ||= [];
32 for ( my $i = 0; $i < @{ $opt{'fields'} }; $i++ ) {
33   my $f = $opt{'fields'}[$i];
34   next if ref($f); # it's not a plain table column
35   $opt{'sort_fields'}[$i] ||= $f;
36   my $classnames = $item_fields{$f}; # hashref of svcnum -> classname
37   next if !$classnames; # there are no inventory items in this column
38   $opt{'fields'}[$i] = sub {
39     my $svc = $_[0];
40     if ( exists($classnames->{$svc->svcnum}) ) {
41       return $svc->$f . '<BR><I>('. $classnames->{$svc->svcnum} . ')</I>';
42     } else {
43       return $svc->$f;
44     }
45   }; #sub
46 }
47
48 </%init>