improve speed in customer search, #13364
[freeside.git] / httemplate / search / cust_msg.html
1 <& 'elements/search.html',
2        'title' => $title,
3        'name'  => 'messages',
4        'query' => $query,
5        'count_query' => $count_query,
6        'header' => [ 
7                      'Date',
8                      'Template',
9                      'Destination',
10                      'Status',
11                      '', #error
12                   ],
13        'fields' => [ 
14                      sub { 
15                        my $date = $_[0]->_date;
16                        $date ? time2str('%Y-%m-%d %T',$_[0]->_date) : '' 
17                      },
18                      'msgname',
19                      'env_to',
20                      'status',
21                      sub { encode_entities($_[0]->error) },
22                   ],
23        'align' => 'rllcl',
24        'links' => [ ],
25        'link_onclicks' => [ 
26                     $sub_popup_link,
27                     $sub_popup_link,
28                     $sub_popup_link,
29                     '',
30                     '',
31                   ],
32        'color' => [ ('') x 3, 
33                      $statuscolor,
34                      $statuscolor,
35                   ],
36        'html_init' => $html_init,
37        'really_disable_download' => 1,
38 &>
39 <%init>
40 #hmm...
41 die "access denied"
42   unless $FS::CurrentUser::CurrentUser->access_right('View customers of all agents');
43
44 my $conf = new FS::Conf;
45
46 my $title = 'Outgoing Message Log';
47
48 my @where;
49 if ( $cgi->param('status') =~ /^(\w+)$/ ) {
50   push @where, "status = '$1'";
51 }
52 my ($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi, '');
53 push @where, "(_date >= $beginning AND _date <= $ending)";
54
55 my $order_by = '_date';
56 if ( $cgi->param('order_by') =~ /^(\w+)$/ ) {
57   $order_by = $1;
58 }
59
60 my $where = '';
61 $where = ' WHERE '.join(' AND ', @where) if @where;
62 my $query = {
63   'table'     => 'cust_msg',
64   'select'    => join(', ',
65                    'cust_msg.*',
66                    'msg_template.msgname',
67                  ),
68   'addl_from' => ' LEFT JOIN msg_template USING ( msgnum )',
69   'hashref'   => {},
70   'extra_sql' => $where,
71   'order_by'  => "ORDER BY $order_by",
72 };
73 my $count_query = 'SELECT COUNT(*) FROM cust_msg'.$where;
74
75 my $sub_popup_link = sub {
76   my $custmsgnum = $_[0]->custmsgnum;
77   include('/elements/popup_link_onclick.html',
78     'action'      => $p. 'view/cust_msg.html?' . $custmsgnum,
79     'actionlabel' => 'Message detail',
80     'width'       => 600,
81     'height'      => 500,
82   );
83 };
84
85 my %color = (
86   'prepared' => '0000FF',
87   'failed'   => 'FF0000',
88   'sent'     => '',
89 );
90 my $statuscolor = sub { $color{$_[0]->status} };
91
92 my $html_init = qq!<FORM ACTION="$p/search/cust_msg.html" METHOD="GET">
93 <TABLE cellspacing="10">!.
94 '<TR><TD>From '.
95 include('/elements/input-date-field.html',
96   { 'name'  => 'beginning', 'value' => $cgi->param('beginning') }
97 ).
98 '</TD><TD> To '.
99 include('/elements/input-date-field.html',
100   { 'name'  => 'ending', 'value' => ($cgi->param('ending') || ''), 
101     'noinit' => 1, }
102 ).
103 '</TD><TD> Status '.
104 include('/elements/select.html',
105   'field' => 'status',
106   'curr_value' => $cgi->param('status') || '',
107   'options' => [ '', 'failed', 'sent', 'prepared' ],
108   'labels' => { ''        => '(any)', 
109                 'failed'  => 'failed',
110                 'sent'    => 'sent',
111                 'prepared'=> 'prepared' },
112 ) .
113 '</TD>
114 <TD><INPUT type="submit" value="Search"></TD></TR>
115 </TABLE></FORM><BR>
116 <STYLE type="text/css">
117 a:link {text-decoration: none}
118 a:visited {text-decoration: none}
119 a:hover {text-decoration: underline}
120 </STYLE>';
121
122 </%init>