fix parsing of multiple To: addresses, #73241
[freeside.git] / httemplate / search / cust_msg.html
1 <& elements/search.html,
2        'title'         => $title,
3        'name_singular' => 'message',
4        'query'         => $query,
5        'count_query'   => $count_query,
6        'header'        => [ 
7                             'Date',
8                             'Type',
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                             sub {
19                               ucfirst($_[0]->msgtype) || $_[0]->msgname
20                             },
21                             sub {
22                               join('<BR>',
23                                 map { encode_entities($_->format) }
24                                 Email::Address->parse($_[0]->env_to)
25                               )
26                             },
27                             'status',
28                             sub { encode_entities($_[0]->error) },
29                          ],
30        'sort_fields'   => [ '_date',
31                             'msgtype',
32                             'env_to',
33                             'status',
34                             'error',
35                           ],
36        'align'         => 'rllcl',
37        'links'         => [],
38        'link_onclicks' => [ 
39                             $sub_popup_link,
40                             $sub_popup_link,
41                             $sub_popup_link,
42                             '',
43                             '',
44                           ],
45        'color'         => [ '',
46                             $typecolor,
47                             '',
48                             $statuscolor,
49                             $statuscolor,
50                           ],
51        'html_init'     => $html_init,
52        'really_disable_download' => 1,
53        'order_by'      => '_date DESC',
54 &>
55 <%init>
56 #hmm...
57 die "access denied"
58   unless $FS::CurrentUser::CurrentUser->access_right('View email logs');
59
60 my $conf = new FS::Conf;
61
62 my $title = 'Outgoing Message Log';
63
64 #here is the agent virtualization
65 my $agentnums_sql =
66   $FS::CurrentUser::CurrentUser->agentnums_sql( 'table' => 'cust_main' );
67
68 my @where = ( $agentnums_sql );
69
70 if ( $cgi->param('status') =~ /^(\w+)$/ ) {
71   push @where, "status = '$1'";
72 }
73 if ( $cgi->param('msgtype') =~ /^(\w+)$/ ) {
74   push @where, "msgtype = '$1'";
75 }
76 if ( $cgi->param('custnum') =~ /^(\d+)$/ ) {
77   push @where, "custnum = $1";
78 }
79 my ($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi, '');
80 push @where, "(_date >= $beginning AND _date <= $ending)";
81
82 my $order_by = '_date';
83 if ( $cgi->param('order_by') =~ /^(\w+)$/ ) {
84   $order_by = $1;
85 }
86
87 my $where = ' WHERE '. join(' AND ', @where);
88
89 my $join_cust_main = ' LEFT JOIN cust_main USING ( custnum ) ';
90
91 my $query = {
92   'table'     => 'cust_msg',
93   'select'    => join(', ',
94                    'cust_msg.*',
95                    'msg_template.msgname',
96                  ),
97   'addl_from' => ' LEFT JOIN msg_template USING ( msgnum ) '. $join_cust_main,
98   'hashref'   => {},
99   'extra_sql' => $where,
100   'order_by'  => "ORDER BY $order_by",
101 };
102 my $count_query = "SELECT COUNT(*) FROM cust_msg $join_cust_main $where";
103
104 my $sub_popup_link = sub {
105   my $custmsgnum = $_[0]->custmsgnum;
106   include('/elements/popup_link_onclick.html',
107     'action'      => $p. 'view/cust_msg.html?' . $custmsgnum,
108     'actionlabel' => 'Message detail',
109     'width'       => 680,
110     'height'      => 550,
111   );
112 };
113
114 my %color = (
115   'failed'   => 'FF0000',
116   'sent'     => '',
117
118   'invoice'  => '00CC00',
119   'receipt'  => '0000CC',
120   'admin'    => 'CC0000',
121   ''         => '000000',
122 );
123 my $statuscolor = sub { $color{$_[0]->status} };
124 my $typecolor = sub { $color{$_[0]->msgtype} };
125
126 my $html_init = qq!<FORM ACTION="$p/search/cust_msg.html" METHOD="GET">
127 <TABLE cellspacing="10">!.
128 '<TR><TD>From '.
129 include('/elements/input-date-field.html',
130   { 'name'  => 'beginning', 'value' => scalar($cgi->param('beginning')) }
131 ).
132 '</TD><TD> To '.
133 include('/elements/input-date-field.html',
134   { 'name'  => 'ending', 'value' => (scalar($cgi->param('ending')) || ''), 
135     'noinit' => 1, }
136 ).
137 '</TD><TD> Status '.
138 include('/elements/select.html',
139   'field' => 'status',
140   'curr_value' => scalar($cgi->param('status')) || '',
141   'options' => [ '', 'failed', 'sent', ],
142   'labels' => { ''        => '(any)', 
143                 'failed'  => 'failed',
144                 'sent'    => 'sent', },
145 ) .
146 '</TD><TD> Type '.
147 include('/elements/select.html',
148   'field' => 'msgtype',
149   'curr_value' => scalar($cgi->param('msgtype')) || '',
150   'options' => [ '', 'invoice', 'receipt', 'admin', 'report' ],
151   'labels'  => { ''         => '(any)',
152                  'invoice'  => 'Invoices',
153                  'receipt'  => 'Receipts',
154                  'admin'    => 'Admin notices',
155                  'report'   => 'Reports',
156                },
157 ) .
158 '</TD>
159 <TD><INPUT type="submit" value="Search"></TD></TR>
160 </TABLE></FORM><BR>
161 <STYLE type="text/css">
162 a:link {text-decoration: none}
163 a:visited {text-decoration: none}
164 a:hover {text-decoration: underline}
165 </STYLE>';
166
167 </%init>