2 <% include('/elements/header-popup.html', $title) %>
4 <% include('/elements/header.html', $title) %>
8 <FORM NAME="OneTrueForm" ACTION="email-customers.html" METHOD="POST">
9 <INPUT TYPE="hidden" NAME="table" VALUE="<% $table %>">
10 %# Mixing search params with from address, subject, etc. required special-case
11 %# handling of those, risked name conflicts, and caused massive problems with
12 %# multi-valued search params. We are no longer in search context, so we
13 %# pack the search into a Storable string for later use.
14 <INPUT TYPE="hidden" NAME="search" VALUE="<% encode_base64(nfreeze(\%search)) %>">
15 <INPUT TYPE="hidden" NAME="popup" VALUE="<% $popup %>">
16 <INPUT TYPE="hidden" NAME="url" VALUE="<% $url | h %>">
18 % if ( $cgi->param('action') eq 'send' ) {
20 <FONT SIZE="+2">Sending notice</FONT>
22 <% include('/elements/progress-init.html',
24 [ qw( search table from subject html_body text_body msgnum ) ],
25 'process/email-customers.html',
30 % } elsif ( $cgi->param('action') eq 'preview' ) {
32 <FONT SIZE="+2">Preview notice</FONT>
36 % if ( $cgi->param('action') ) {
38 <TABLE BGCOLOR="#cccccc" CELLSPACING=0>
39 <INPUT TYPE="hidden" NAME="msgnum" VALUE="<% $cgi->param('msgnum') %>">
41 % if ( $msg_template ) {
42 <% include('/elements/tr-fixed.html',
43 'label' => 'Template:',
44 'value' => $msg_template->msgname,
49 <% include('/elements/tr-fixed.html',
52 'value' => scalar( $from ),
56 <% include('/elements/tr-fixed.html',
58 'label' => 'Subject:',
59 'value' => scalar( $subject ),
63 <INPUT TYPE="hidden" NAME="html_body" VALUE="<% $html_body |h %>">
65 <TD ALIGN="right" VALIGN="top">Message (HTML display): </TD>
66 <TD CLASS="background" ALIGN="left"><% $html_body %></TD>
69 % my $text_body = HTML::FormatText->new(leftmargin=>0)->format(
70 % HTML::TreeBuilder->new_from_content(
74 <INPUT TYPE="hidden" NAME="text_body" VALUE="<% $text_body |h %>">
76 <TD ALIGN="right" VALIGN="top">Message (Text display): </TD>
77 <TD CLASS="background" STYLE="background-color:white" ALIGN="left"><PRE><% $text_body %></PRE></TD>
82 % if ( $cgi->param('action') eq 'preview' ) {
85 function areyousure(href) {
86 return confirm("Send this notice to <% ($num_cust > 1) ? "$num_cust customers" : '1 customer' %> ?");
91 <INPUT TYPE="hidden" NAME="action" VALUE="send">
92 <INPUT TYPE="submit" VALUE="Send notice" onClick="return areyousure()">
98 <SCRIPT TYPE="text/javascript">
99 function toggle(obj) {
100 document.getElementById('table_no_template').style.display = (obj.value == 0) ? '' : 'none';
105 <& /elements/select-msg_template.html,
106 onchange => 'toggle(this)',
109 <TABLE BGCOLOR="#cccccc" CELLSPACING=0 WIDTH="100%" id="table_no_template">
110 <& /elements/tr-td-label.html, 'label' => 'From:' &>
111 <TD><& /elements/input-text.html,
112 'field' => 'from_name',
113 'value' => $conf->config('invoice_from_name', $agent_virt_agentnum) ||
114 $conf->config('company_name', $agent_virt_agentnum), #?
117 <& /elements/input-text.html,
118 'field' => 'from_addr',
119 'type' => 'email', # HTML5, woot
120 'value' => $conf->config('invoice_from', $agent_virt_agentnum),
124 <% include('/elements/tr-input-text.html',
125 'field' => 'subject',
126 'label' => 'Subject:',
132 <TD ALIGN="right" VALIGN="top" STYLE="padding-top:3px">Message: </TD>
133 <TD><& /elements/htmlarea.html,
134 'field' => 'html_body',
144 <INPUT TYPE="hidden" NAME="action" VALUE="preview">
145 <INPUT TYPE="submit" VALUE="Preview notice">
151 % if ( $cgi->param('action') eq 'send' ) {
152 <SCRIPT TYPE="text/javascript">
157 <% include('/elements/footer.html') %>
162 unless $FS::CurrentUser::CurrentUser->access_right('Bulk send customer notices');
164 my $conf = FS::Conf->new;
166 my $table = $cgi->param('table') or die "'table' required";
167 my $agent_virt_agentnum = $cgi->param('agent_virt_agentnum') || '';
169 my $popup = $cgi->param('popup');
170 my $url = $cgi->param('url');
171 my $pdest = { 'message' => "Notice sent" };
172 $pdest->{'url'} = $cgi->param('url') if $url;
175 if ( $cgi->param('search') ) {
176 %search = %{ thaw(decode_base64($cgi->param('search'))) };
179 %search = $cgi->Vars;
180 delete $search{$_} for qw( action table from subject html_body text_body popup url );
181 # FS::$table->search is expected to know which parameters might be
182 # multi-valued, and to accept scalar values for them also. No good
183 # solution to this since CGI can't tell whether a parameter _might_
184 # have had multiple values, only whether it does.
185 @search{keys %search} = map { /\0/ ? [ split /\0/, $_ ] : $_ } values %search;
188 my $title = 'Send customer notices';
192 if ( $cgi->param('from') ) {
193 $from = $cgi->param('from');
194 } elsif ( $cgi->param('from_name') ) {
195 $from = ($cgi->param('from_name') . ' <' . $cgi->param('from_addr') . '>');
196 } elsif ( $cgi->param('from_addr') ) {
197 $from = $cgi->param('from_addr');
200 my $subject = $cgi->param('subject') || '';
201 my $html_body = $cgi->param('html_body') || '';
203 my $msg_template = '';
205 if ( $cgi->param('action') eq 'preview' ) {
206 my $sql_query = "FS::$table"->search(\%search);
207 my $count_query = delete($sql_query->{'count_query'});
208 my $count_sth = dbh->prepare($count_query)
209 or die "Error preparing $count_query: ". dbh->errstr;
211 or die "Error executing $count_query: ". $count_sth->errstr;
212 my $count_arrayref = $count_sth->fetchrow_arrayref;
213 $num_cust = $count_arrayref->[0];
215 if ( $cgi->param('msgnum') ) {
216 $msg_template = qsearchs('msg_template',
217 { msgnum => $cgi->param('msgnum') } )
218 or die "template not found: ".$cgi->param('msgnum');
219 $sql_query->{'extra_sql'} .= ' LIMIT 1';
220 $sql_query->{'select'} = "$table.*";
221 $sql_query->{'order_by'} = '';
222 my $object = qsearchs($sql_query);
223 my $cust = $object->cust_main;
224 my %message = $msg_template->prepare(
225 'cust_main' => $cust,
228 ($from, $subject, $html_body) = @message{'from', 'subject', 'html_body'};