no need for FS calendar buttons in RT 4.2
[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    => 'ServiceLocation',
47     Label   => 'Service Location',
48     Display => svc_location_attribute('location'),
49   },
50   {
51     Name    => 'ServiceKey', # loosely corresponds to smartsearch/label field
52     Label   => '',
53     # not displayable
54     QueryLabel  => {
55       Type      => 'select',
56       Options   => [
57         'Service.svc_acct.username'       => loc('Username'),
58         'Service.svc_phone.phonenum'      => loc('Phone Number'),
59         'Service.svc_broadband.ip_addr'   => loc('IP Address'),
60         'Service.svc_broadband.mac_addr'  => loc('MAC Address'),
61       ],
62     },
63     Op      => matches_notmatches,
64     Value   => { Type => 'text', Size => 20 },
65   },
66   {
67     Name    => 'Router',
68     Label   => 'Router',
69     QueryName => 'Service.svc_broadband.routernum',
70     # not displayable
71     Op      => equals_notequals,
72     Value   => select_table('router', 'routernum', 'routername'),
73     OrderBy => 'Service.svc_broadband.routernum',
74   },
75
76 );
77 #helper subs
78 #Op      
79 sub equals_notequals {
80   {
81       Type => 'component',
82       Path => '/Elements/SelectBoolean',
83       Arguments => { TrueVal=> '=', FalseVal=> '!=' },
84   }
85 }
86 sub matches_notmatches {
87     {
88         Type => 'component',
89         Path => '/Elements/SelectMatch',
90     },
91 }
92
93 #Value
94 sub select_table {
95   my ($table, $value_col, $name_col, $hashref) = @_;
96   $hashref ||= { disabled => '' }; # common case
97   return {
98     Type => 'select',
99     Options => [
100       '' => '-',
101       map { $_->$value_col, $_->$name_col }
102       qsearch($table, $hashref)
103     ],
104   }
105 }
106
107 sub ticket_svc_resolvers {
108     my $Ticket = shift;
109     my @Services = @{ $Ticket->Services->ItemsArrayRef };
110     return map $_->TargetURI->Resolver, @Services;
111 }
112
113 sub svc_info_attribute {
114     my $attribute = shift;
115     sub {
116         my $Ticket = shift;
117         my @return;
118         foreach my $s (ticket_svc_resolvers($Ticket)) {
119             push @return, $s->ServiceInfo->{$attribute}, '<BR>';
120         }
121         pop @return; #trailing <BR>
122         @return;
123     };
124 }
125
126 sub svc_location_attribute {
127     # Tricky: if the ticket is linked to a service, we want to return the
128     # service's location, but if it's not, we want to return the customer's
129     # default service location.
130     # If it's linked to Customer A and also to Service A, it should return
131     # Service A's location (and not Customer A's default service location).
132     # But if it's linked to Service A and also to Customer B, then what? We
133     # can't satisfy all the constraints here.
134     my $attribute = shift;
135     sub {
136         my @return;
137         my $Ticket = shift;
138         my @svc_resolvers = ticket_svc_resolvers($Ticket);
139         if (@svc_resolvers) {
140             foreach my $s (@svc_resolvers) {
141                 push @return, $s->ServiceInfo->{$attribute}, '<BR>';
142             }
143         } else {
144             my @cust_resolvers = map $_->TargetURI->Resolver,
145                                  @{ $Ticket->Customers->ItemsArrayRef };
146             foreach my $c (@cust_resolvers) {
147                 push @return, $c->CustomerInfo->{"ship_$attribute"}, '<BR>';
148             }
149         }
150         pop @return; #trailing <BR>
151         @return;
152     };
153 }
154
155 </%once>
156 <%init>
157
158 my $arg = shift;
159 if ( $arg eq 'Names' ) {
160   return map { $_->{Name} } 
161   grep { exists $_->{Display} }
162   @service_fields;
163 }
164 elsif ( $arg eq 'ColumnMap' ) {
165   return map {
166     my $f = $_;
167     $f->{Name} => {
168         title     => $f->{Label},
169         attribute => $f->{OrderBy} || '',
170         value     => ref($f->{Display}) eq 'CODE' ? 
171                       $f->{Display} : 
172                       svc_info_attribute($f->{Display})
173     }
174   } #map
175   grep { exists $_->{Display} }
176   @service_fields;
177 }
178 elsif ( $arg eq 'Criteria' ) {
179   return map {
180     my $f = $_;
181     # argument to Search/Elements/ConditionRow
182     {
183       Name  => ($f->{QueryName} || $f->{Name}),
184       Field => ($f->{QueryLabel} || $f->{Label}),
185       Op    => $f->{Op},
186       Value => $f->{Value},
187     }
188   } #map
189   grep { exists($_->{Value}) }
190   @service_fields;
191 }
192 else { die "unknown ServiceFields mode '$arg'\n"; }
193 </%init>