RT# 83401 Catch exception and display suitable error
[freeside.git] / httemplate / search / svc_domain.cgi
1 <& 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                                           emt('Pkg. Status'),
11                                           FS::UI::Web::cust_header(),
12                                         ],
13                  'fields'            => [ 'svcnum',
14                                           'svc',
15                                           'domain',
16                                           sub {
17                                             $cust_pkg_cache{$_[0]->svcnum} ||= $_[0]->cust_svc->cust_pkg;
18                                             return '' unless $cust_pkg_cache{$_[0]->svcnum};
19                                             $cust_pkg_cache{$_[0]->svcnum}->ucfirst_status
20                                           },
21                                           \&FS::UI::Web::cust_fields,
22                                         ],
23                  'links'             => [ $link,
24                                           $link,
25                                           $link,
26                                           '', # pkg status
27                                           ( map { $_ ne 'Cust. Status' ? $link_cust : '' }
28                                                 FS::UI::Web::cust_header()
29                                           ),
30                                         ],
31                  'align' => 'rllr'. FS::UI::Web::cust_aligns(),
32                  'color' => [ 
33                               '',
34                               '',
35                               '',
36                               sub {
37                                 $cust_pkg_cache{$_[0]->svcnum} ||= $_[0]->cust_svc->cust_pkg;
38                                 return '' unless $cust_pkg_cache{$_[0]->svcnum};
39                                 my $c = FS::cust_pkg::statuscolors;
40                                 $c->{$cust_pkg_cache{$_[0]->svcnum}->status };
41                               }, # pkg status
42                               FS::UI::Web::cust_colors(),
43                             ],
44                  'style' => [ 
45                               '',
46                               '',
47                               '',
48                               'b',
49                               FS::UI::Web::cust_styles(),
50                             ],
51               
52 &>
53 <%init>
54
55 die "access denied"
56   unless $FS::CurrentUser::CurrentUser->access_right('List services');
57
58 my %cust_pkg_cache;
59
60 my $conf = new FS::Conf;
61
62 my $orderby = 'ORDER BY svcnum';
63 my %svc_domain = ();
64 my @extra_sql = ();
65 if ( $cgi->param('magic') =~ /^(all|unlinked)$/ ) {
66
67   push @extra_sql, 'pkgnum IS NULL'
68     if $cgi->param('magic') eq 'unlinked';
69
70   if ( $cgi->param('sortby') =~ /^(\w+)$/ ) {
71     my $sortby = $1;
72     $orderby = "ORDER BY $sortby";
73   }
74
75 } elsif ( $cgi->param('svcpart') =~ /^(\d+)$/ ) {
76   push @extra_sql, "svcpart = $1";
77   if (defined($cgi->param('cancelled'))) {
78     if ($cgi->param('cancelled')) {
79       push @extra_sql, "cust_pkg.cancel IS NOT NULL";
80     } else {
81       push @extra_sql, "cust_pkg.cancel IS NULL";
82     }
83   }
84 } else {
85   $cgi->param('domain') =~ /^([\w\-\.]+)$/; 
86   $svc_domain{'domain'} = $1;
87 }
88
89 my $addl_from = ' LEFT JOIN cust_svc  USING ( svcnum  ) '.
90                 ' LEFT JOIN part_svc  USING ( svcpart ) '.
91                 ' LEFT JOIN cust_pkg  USING ( pkgnum  ) '.
92                 FS::UI::Web::join_cust_main('cust_pkg', 'cust_pkg');
93
94 #here is the agent virtualization
95 push @extra_sql, $FS::CurrentUser::CurrentUser->agentnums_sql( 
96                    'null_right' => 'View/link unlinked services'
97                  );
98
99 my $extra_sql = '';
100 if ( @extra_sql ) {
101   $extra_sql = ( keys(%svc_domain) ? ' AND ' : ' WHERE ' ).
102                join(' AND ', @extra_sql );
103 }
104
105 my $count_query = "SELECT COUNT(*) FROM svc_domain $addl_from ";
106 if ( keys %svc_domain ) {
107   $count_query .= ' WHERE '.
108                     join(' AND ', map "$_ = ". dbh->quote($svc_domain{$_}),
109                                       keys %svc_domain
110                         );
111 }
112 $count_query .= $extra_sql;
113
114 my $sql_query = {
115   'table'     => 'svc_domain',
116   'hashref'   => \%svc_domain,
117   'select'    => join(', ',
118                    'svc_domain.*',
119                    'part_svc.svc',
120                    'cust_main.custnum',
121                    FS::UI::Web::cust_sql_fields(),
122                  ),
123   'extra_sql' => $extra_sql,
124   'order_by'  => $orderby,
125   'addl_from' => $addl_from,
126 };
127
128 my $link = [ "${p}view/svc_domain.cgi?", 'svcnum' ];
129
130 #smaller false laziness w/svc_*.cgi here
131 my $link_cust = sub {
132   my $svc_x = shift;
133   $svc_x->custnum ? [ "${p}view/cust_main.cgi?", 'custnum' ] : '';
134 };
135
136 </%init>