2.1.2
[freeside.git] / httemplate / search / svc_domain.cgi
1 <% include( 'elements/search.html',
2                  'title'             => "Domain Search Results",
3                  'name'              => 'domains',
4                  'query'             => $sql_query,
5                  'count_query'       => $count_query,
6                  'redirect'          => $link,
7                  'header'            => [ '#',
8                                           'Service',
9                                           'Domain',
10                                           FS::UI::Web::cust_header(),
11                                         ],
12                  'fields'            => [ 'svcnum',
13                                           'svc',
14                                           'domain',
15                                           \&FS::UI::Web::cust_fields,
16                                         ],
17                  'links'             => [ $link,
18                                           $link,
19                                           $link,
20                                           ( map { $_ ne 'Cust. Status' ? $link_cust : '' }
21                                                 FS::UI::Web::cust_header()
22                                           ),
23                                         ],
24                  'align' => 'rll'. FS::UI::Web::cust_aligns(),
25                  'color' => [ 
26                               '',
27                               '',
28                               '',
29                               FS::UI::Web::cust_colors(),
30                             ],
31                  'style' => [ 
32                               '',
33                               '',
34                               '',
35                               FS::UI::Web::cust_styles(),
36                             ],
37               )
38 %>
39 <%init>
40
41 die "access denied"
42   unless $FS::CurrentUser::CurrentUser->access_right('List services');
43
44 my $conf = new FS::Conf;
45
46 my $orderby = 'ORDER BY svcnum';
47 my %svc_domain = ();
48 my @extra_sql = ();
49 if ( $cgi->param('magic') =~ /^(all|unlinked)$/ ) {
50
51   push @extra_sql, 'pkgnum IS NULL'
52     if $cgi->param('magic') eq 'unlinked';
53
54   if ( $cgi->param('sortby') =~ /^(\w+)$/ ) {
55     my $sortby = $1;
56     $orderby = "ORDER BY $sortby";
57   }
58
59 } elsif ( $cgi->param('svcpart') =~ /^(\d+)$/ ) {
60   push @extra_sql, "svcpart = $1";
61 } else {
62   $cgi->param('domain') =~ /^([\w\-\.]+)$/; 
63   $svc_domain{'domain'} = $1;
64 }
65
66 my $addl_from = ' LEFT JOIN cust_svc  USING ( svcnum  ) '.
67                 ' LEFT JOIN part_svc  USING ( svcpart ) '.
68                 ' LEFT JOIN cust_pkg  USING ( pkgnum  ) '.
69                 ' LEFT JOIN cust_main USING ( custnum ) ';
70
71 #here is the agent virtualization
72 push @extra_sql, $FS::CurrentUser::CurrentUser->agentnums_sql( 
73                    'null_right' => 'View/link unlinked services'
74                  );
75
76 my $extra_sql = '';
77 if ( @extra_sql ) {
78   $extra_sql = ( keys(%svc_domain) ? ' AND ' : ' WHERE ' ).
79                join(' AND ', @extra_sql );
80 }
81
82 my $count_query = "SELECT COUNT(*) FROM svc_domain $addl_from ";
83 if ( keys %svc_domain ) {
84   $count_query .= ' WHERE '.
85                     join(' AND ', map "$_ = ". dbh->quote($svc_domain{$_}),
86                                       keys %svc_domain
87                         );
88 }
89 $count_query .= $extra_sql;
90
91 my $sql_query = {
92   'table'     => 'svc_domain',
93   'hashref'   => \%svc_domain,
94   'select'    => join(', ',
95                    'svc_domain.*',
96                    'part_svc.svc',
97                    'cust_main.custnum',
98                    FS::UI::Web::cust_sql_fields(),
99                  ),
100   'extra_sql' => $extra_sql,
101   'order_by'  => $orderby,
102   'addl_from' => $addl_from,
103 };
104
105 my $link = [ "${p}view/svc_domain.cgi?", 'svcnum' ];
106
107 #smaller false laziness w/svc_*.cgi here
108 my $link_cust = sub {
109   my $svc_x = shift;
110   $svc_x->custnum ? [ "${p}view/cust_main.cgi?", 'custnum' ] : '';
111 };
112
113 </%init>