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