Updated for 1.9.1
[freeside.git] / httemplate / search / svc_forward.cgi
1 <% include( 'elements/search.html',
2                  'title'             => "Mail forward Search Results",
3                  'name'              => 'mail forwards',
4                  'query'             => $sql_query,
5                  'count_query'       => $count_query,
6                  'redirect'          => $link,
7                  'header'            => [ '#',
8                                           'Service',
9                                           'Mail to',
10                                           'Forwards to',
11                                           FS::UI::Web::cust_header(),
12                                         ],
13                  'fields'            => [ 'svcnum',
14                                           'svc',
15                                           $format_src,
16                                           $format_dst,
17                                           \&FS::UI::Web::cust_fields,
18                                         ],
19                  'links'             => [ $link,
20                                           $link,
21                                           $link_src,
22                                           $link_dst,
23                                           ( map { $_ ne 'Cust. Status' ? $link_cust : '' }
24                                                 FS::UI::Web::cust_header()
25                                           ),
26                                         ],
27                  'align' => 'rlll'. FS::UI::Web::cust_aligns(),
28                  'color' => [ 
29                               '',
30                               '',
31                               '',
32                               '',
33                               FS::UI::Web::cust_colors(),
34                             ],
35                  'style' => [ 
36                               '',
37                               '',
38                               '',
39                               '',
40                               FS::UI::Web::cust_styles(),
41                             ],
42              )
43 %>
44 <%init>
45
46 die "access denied"
47   unless $FS::CurrentUser::CurrentUser->access_right('List services');
48
49 my $conf = new FS::Conf;
50
51 my $orderby = 'ORDER BY svcnum';
52 my @extra_sql = ();
53 if ( $cgi->param('magic') =~ /^(all|unlinked)$/ ) {
54
55   push @extra_sql, 'pkgnum IS NULL'
56     if $cgi->param('magic') eq 'unlinked';
57
58   if ( $cgi->param('sortby') =~ /^(\w+)$/ ) {
59     my $sortby = $1;
60     $orderby = "ORDER BY $sortby";
61   }
62
63 } elsif ( $cgi->param('svcpart') =~ /^(\d+)$/ ) {
64   push @extra_sql, "svcpart = $1";
65 }
66
67 my $addl_from = ' LEFT JOIN cust_svc  USING ( svcnum  ) '.
68                 ' LEFT JOIN part_svc  USING ( svcpart ) '.
69                 ' LEFT JOIN cust_pkg  USING ( pkgnum  ) '.
70                 ' LEFT JOIN cust_main USING ( custnum ) ';
71
72 #here is the agent virtualization
73 push @extra_sql, $FS::CurrentUser::CurrentUser->agentnums_sql( 
74                    'null_right' => 'View/link unlinked services'
75                  );
76
77 my $extra_sql = 
78   scalar(@extra_sql)
79     ? ' WHERE '. join(' AND ', @extra_sql )
80     : '';
81
82 my $count_query = "SELECT COUNT(*) FROM svc_forward $addl_from $extra_sql";
83 my $sql_query = {
84   'table'     => 'svc_forward',
85   'hashref'   => {},
86   'select'    => join(', ',
87                    'svc_forward.*',
88                    'part_svc.svc',
89                    'cust_main.custnum',
90                    FS::UI::Web::cust_sql_fields(),
91                  ),
92   'extra_sql' => "$extra_sql $orderby",
93   'addl_from' => $addl_from,
94 };
95
96 #        <TH>Service #<BR><FONT SIZE=-1>(click to view forward)</FONT></TH>
97 #        <TH>Mail to<BR><FONT SIZE=-1>(click to view account)</FONT></TH>
98 #        <TH>Forwards to<BR><FONT SIZE=-1>(click to view account)</FONT></TH>
99
100 my $link = [ "${p}view/svc_forward.cgi?", 'svcnum' ];
101
102 my $format_src = sub {
103   my $svc_forward = shift;
104   if ( $svc_forward->srcsvc_acct ) {
105     $svc_forward->srcsvc_acct->email;
106   } else {
107     my $src = $svc_forward->src;
108     $src = "<I>(anything)</I>$src" if $src =~ /^@/;
109     $src;
110   }
111 };
112
113 my $link_src = sub {
114   my $svc_forward = shift;
115   if ( $svc_forward->srcsvc_acct ) {
116     [ "${p}view/svc_acct.cgi?", 'srcsvc' ];
117   } else {
118     '';
119   }
120 };
121
122 my $format_dst = sub {
123   my $svc_forward = shift;
124   if ( $svc_forward->dstsvc_acct ) {
125     $svc_forward->dstsvc_acct->email;
126   } else {
127     $svc_forward->dst;
128   }
129 };
130
131 my $link_dst = sub {
132   my $svc_forward = shift;
133   if ( $svc_forward->dstsvc_acct ) {
134     [ "${p}view/svc_acct.cgi?", 'dstsvc' ];
135   } else {
136     '';
137   }
138 };
139
140 #smaller false laziness w/svc_*.cgi here
141 my $link_cust = sub {
142   my $svc_x = shift;
143   $svc_x->custnum ? [ "${p}view/cust_main.cgi?", 'custnum' ] : '';
144 };
145
146 </%init>