summaryrefslogtreecommitdiff
path: root/rt/share/html/Elements/ServiceFields
blob: 9c9a248c8bdadfcc04f621db02a2729022f106d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<%doc>
Accessible Freeside svc_x fields go in here.  RT::URI::freeside::Internal
pulls all fields from cust_svc and the svc_x tables into ServiceInfo().
RT::Tickets_Overlay resolves "Service.foo" as "cust_svc.foo", and 
"Service.svc_acct.bar" as "JOIN svc_acct USING (svcnum) ... svc_acct.bar".

See /Elements/CustomerFields for notes on this data structure.
</%doc>
<%once>

my @service_fields = ( # ordered
  {
    # svcnum
    Name    => 'Service',
    Label   => 'Service',
    Display => sub {
                my $Ticket = shift;
                my @return = ();
                foreach my $s (ticket_svc_resolvers($Ticket)) {
                    push @return, \'<A HREF="', $s->HREF, \'">',
                                  $s->AsString,
                                  \'</A>',
                                  \'<BR>';
                }
                pop @return;
                @return;
              },
    OrderBy => 'Service.Number',
  },
  {
    #Column name (format string)
    Name    => 'ServiceType',
    # Column heading/query builder name
    Label   => 'Service Type',
    # Column value (coderef, cust_svc/svc_x field, or ServiceInfo key)
    Display => 'ServiceType',
    # Query builder options
    # RT-SQL field, defaults to Name
    QueryName => 'Service.svcpart',
    Op      => equals_notequals,
    Value   => select_table('part_svc', 'svcpart', 'svc'),
    # RT-SQL sort key (if any)
    OrderBy => 'Service.svcpart',
  },
  {
    Name    => 'ServiceKey', # loosely corresponds to smartsearch/label field
    Label   => '',
    # not displayable
    QueryLabel  => {
      Type      => 'select',
      Options   => [
        'Service.svc_acct.username'       => loc('Username'),
        'Service.svc_phone.phonenum'      => loc('Phone Number'),
        'Service.svc_broadband.ip_addr'   => loc('IP Address'),
        'Service.svc_broadband.mac_addr'  => loc('MAC Address'),
      ],
    },
    Op      => matches_notmatches,
    Value   => { Type => 'text', Size => 20 },
  },
  {
    Name    => 'Router',
    Label   => 'Router',
    QueryName => 'Service.svc_broadband.routernum',
    # not displayable
    Op      => equals_notequals,
    Value   => select_table('router', 'routernum', 'routername'),
    OrderBy => 'Service.svc_broadband.routernum',
  },

);
#helper subs
#Op      
sub equals_notequals {
  {
      Type => 'component',
      Path => '/Elements/SelectBoolean',
      Arguments => { TrueVal=> '=', FalseVal=> '!=' },
  }
}
sub matches_notmatches {
    {
        Type => 'component',
        Path => '/Elements/SelectMatch',
    },
}

#Value
sub select_table {
  my ($table, $value_col, $name_col, $hashref) = @_;
  $hashref ||= { disabled => '' }; # common case
  return {
    Type => 'select',
    Options => [
      '' => '-',
      map { $_->$value_col, $_->$name_col }
      qsearch($table, $hashref)
    ],
  }
}

sub ticket_svc_resolvers {
    my $Ticket = shift;
    my @Services = @{ $Ticket->Services->ItemsArrayRef };
    return map $_->TargetURI->Resolver, @Services;
}

sub svc_info_attribute {
    my $attribute = shift;
    sub {
        my $Ticket = shift;
        my @return;
        foreach my $s (ticket_svc_resolvers($Ticket)) {
            push @return, $s->ServiceInfo->{$attribute}, '<BR>';
        }
        pop @return; #trailing <BR>
        @return;
    };
}

</%once>
<%init>
use Data::Dumper;
#warn Dumper(\@service_fields);

my $arg = shift;
if ( $arg eq 'Names' ) {
  return map { $_->{Name} } 
  grep { exists $_->{Display} }
  @service_fields;
}
elsif ( $arg eq 'ColumnMap' ) {
  return map {
    my $f = $_;
    $f->{Name} => {
        title     => $f->{Label},
        attribute => $f->{OrderBy} || '',
        value     => ref($f->{Display}) eq 'CODE' ? 
                      $f->{Display} : 
                      svc_info_attribute($f->{Display})
    }
  } #map
  grep { exists $_->{Display} }
  @service_fields;
}
elsif ( $arg eq 'Criteria' ) {
  return map {
    my $f = $_;
    # argument to Search/Elements/ConditionRow
    {
      Name  => ($f->{QueryName} || $f->{Name}),
      Field => ($f->{QueryLabel} || $f->{Label}),
      Op    => $f->{Op},
      Value => $f->{Value},
    }
  } #map
  grep { exists($_->{Value}) }
  @service_fields;
}
else { die "unknown ServiceFields mode '$arg'\n"; }
</%init>