9c9a248c8bdadfcc04f621db02a2729022f106d8
[freeside.git] / rt / share / html / Elements / ServiceFields
1 <%doc>
2 Accessible Freeside svc_x fields go in here.  RT::URI::freeside::Internal
3 pulls all fields from cust_svc and the svc_x tables into ServiceInfo().
4 RT::Tickets_Overlay resolves "Service.foo" as "cust_svc.foo", and 
5 "Service.svc_acct.bar" as "JOIN svc_acct USING (svcnum) ... svc_acct.bar".
6
7 See /Elements/CustomerFields for notes on this data structure.
8 </%doc>
9 <%once>
10
11 my @service_fields = ( # ordered
12   {
13     # svcnum
14     Name    => 'Service',
15     Label   => 'Service',
16     Display => sub {
17                 my $Ticket = shift;
18                 my @return = ();
19                 foreach my $s (ticket_svc_resolvers($Ticket)) {
20                     push @return, \'<A HREF="', $s->HREF, \'">',
21                                   $s->AsString,
22                                   \'</A>',
23                                   \'<BR>';
24                 }
25                 pop @return;
26                 @return;
27               },
28     OrderBy => 'Service.Number',
29   },
30   {
31     #Column name (format string)
32     Name    => 'ServiceType',
33     # Column heading/query builder name
34     Label   => 'Service Type',
35     # Column value (coderef, cust_svc/svc_x field, or ServiceInfo key)
36     Display => 'ServiceType',
37     # Query builder options
38     # RT-SQL field, defaults to Name
39     QueryName => 'Service.svcpart',
40     Op      => equals_notequals,
41     Value   => select_table('part_svc', 'svcpart', 'svc'),
42     # RT-SQL sort key (if any)
43     OrderBy => 'Service.svcpart',
44   },
45   {
46     Name    => 'ServiceKey', # loosely corresponds to smartsearch/label field
47     Label   => '',
48     # not displayable
49     QueryLabel  => {
50       Type      => 'select',
51       Options   => [
52         'Service.svc_acct.username'       => loc('Username'),
53         'Service.svc_phone.phonenum'      => loc('Phone Number'),
54         'Service.svc_broadband.ip_addr'   => loc('IP Address'),
55         'Service.svc_broadband.mac_addr'  => loc('MAC Address'),
56       ],
57     },
58     Op      => matches_notmatches,
59     Value   => { Type => 'text', Size => 20 },
60   },
61   {
62     Name    => 'Router',
63     Label   => 'Router',
64     QueryName => 'Service.svc_broadband.routernum',
65     # not displayable
66     Op      => equals_notequals,
67     Value   => select_table('router', 'routernum', 'routername'),
68     OrderBy => 'Service.svc_broadband.routernum',
69   },
70
71 );
72 #helper subs
73 #Op      
74 sub equals_notequals {
75   {
76       Type => 'component',
77       Path => '/Elements/SelectBoolean',
78       Arguments => { TrueVal=> '=', FalseVal=> '!=' },
79   }
80 }
81 sub matches_notmatches {
82     {
83         Type => 'component',
84         Path => '/Elements/SelectMatch',
85     },
86 }
87
88 #Value
89 sub select_table {
90   my ($table, $value_col, $name_col, $hashref) = @_;
91   $hashref ||= { disabled => '' }; # common case
92   return {
93     Type => 'select',
94     Options => [
95       '' => '-',
96       map { $_->$value_col, $_->$name_col }
97       qsearch($table, $hashref)
98     ],
99   }
100 }
101
102 sub ticket_svc_resolvers {
103     my $Ticket = shift;
104     my @Services = @{ $Ticket->Services->ItemsArrayRef };
105     return map $_->TargetURI->Resolver, @Services;
106 }
107
108 sub svc_info_attribute {
109     my $attribute = shift;
110     sub {
111         my $Ticket = shift;
112         my @return;
113         foreach my $s (ticket_svc_resolvers($Ticket)) {
114             push @return, $s->ServiceInfo->{$attribute}, '<BR>';
115         }
116         pop @return; #trailing <BR>
117         @return;
118     };
119 }
120
121 </%once>
122 <%init>
123 use Data::Dumper;
124 #warn Dumper(\@service_fields);
125
126 my $arg = shift;
127 if ( $arg eq 'Names' ) {
128   return map { $_->{Name} } 
129   grep { exists $_->{Display} }
130   @service_fields;
131 }
132 elsif ( $arg eq 'ColumnMap' ) {
133   return map {
134     my $f = $_;
135     $f->{Name} => {
136         title     => $f->{Label},
137         attribute => $f->{OrderBy} || '',
138         value     => ref($f->{Display}) eq 'CODE' ? 
139                       $f->{Display} : 
140                       svc_info_attribute($f->{Display})
141     }
142   } #map
143   grep { exists $_->{Display} }
144   @service_fields;
145 }
146 elsif ( $arg eq 'Criteria' ) {
147   return map {
148     my $f = $_;
149     # argument to Search/Elements/ConditionRow
150     {
151       Name  => ($f->{QueryName} || $f->{Name}),
152       Field => ($f->{QueryLabel} || $f->{Label}),
153       Op    => $f->{Op},
154       Value => $f->{Value},
155     }
156   } #map
157   grep { exists($_->{Value}) }
158   @service_fields;
159 }
160 else { die "unknown ServiceFields mode '$arg'\n"; }
161 </%init>