3 Allows emailing one or more customers, based on a search for customers. Search can
4 be specified either through cust_main fields as cgi params, or through a base64 encoded
5 frozen hash in the 'search' cgi param. Form allows selecting an existing msg_template,
6 or creating a custom message, and shows a preview of the message before sending.
7 If linked to as a popup, include the cgi parameter 'popup' for proper header handling.
9 This may also be used as an element in other pages, enabling you to pass along
10 additional substitution parameters to a message template, with the following options:
12 form_action - the URL to submit the form to
14 sub_param_process - subroutine to override cgi param values (such as msgnum)
15 and parse/delete additional form fields from the cgi; should return a %sub_param
16 hash to be passed along for message substitution
18 alternate_form - an alternate form for template selection/message creation
20 title - the title of the page
25 <% include('/elements/header-popup.html', $title) %>
27 <% include('/elements/header.html', $title) %>
31 <FORM NAME="OneTrueForm" ACTION="<% $form_action %>" METHOD="POST">
32 <INPUT TYPE="hidden" NAME="table" VALUE="<% $table %>">
33 %# Mixing search params with from address, subject, etc. required special-case
34 %# handling of those, risked name conflicts, and caused massive problems with
35 %# multi-valued search params. We are no longer in search context, so we
36 %# pack the search into a Storable string for later use.
37 <INPUT TYPE="hidden" NAME="search" VALUE="<% encode_base64(nfreeze(\%search)) %>">
39 <INPUT TYPE="hidden" NAME="sub_param" VALUE="<% encode_base64(nfreeze(\%sub_param)) %>">
41 <INPUT TYPE="hidden" NAME="popup" VALUE="<% $popup %>">
42 <INPUT TYPE="hidden" NAME="url" VALUE="<% $url | h %>">
44 % if ( $cgi->param('action') eq 'send' ) {
46 <FONT SIZE="+2">Sending notice</FONT>
48 <% include('/elements/progress-init.html',
50 [ qw( search table from subject html_body text_body msgnum sub_param ) ],
51 'process/email-customers.html',
56 % } elsif ( $cgi->param('action') eq 'preview' ) {
58 <FONT SIZE="+2">Preview notice</FONT>
62 % if ( $cgi->param('action') ) {
64 <TABLE BGCOLOR="#cccccc" CELLSPACING=0>
65 <INPUT TYPE="hidden" NAME="msgnum" VALUE="<% $cgi->param('msgnum') %>">
67 % if ( $msg_template ) {
68 <% include('/elements/tr-fixed.html',
69 'label' => 'Template:',
70 'value' => $msg_template->msgname,
75 <% include('/elements/tr-fixed.html',
78 'value' => scalar( $from ),
82 <% include('/elements/tr-fixed.html',
84 'label' => 'Subject:',
85 'value' => scalar( $subject ),
89 <INPUT TYPE="hidden" NAME="html_body" VALUE="<% $html_body |h %>">
91 <TD ALIGN="right" VALIGN="top">Message (HTML display): </TD>
92 <TD CLASS="background" ALIGN="left"><% $html_body %></TD>
95 % my $text_body = HTML::FormatText->new(leftmargin=>0)->format(
96 % HTML::TreeBuilder->new_from_content(
100 <INPUT TYPE="hidden" NAME="text_body" VALUE="<% $text_body |h %>">
102 <TD ALIGN="right" VALIGN="top">Message (Text display): </TD>
103 <TD CLASS="background" STYLE="background-color:white" ALIGN="left"><PRE><% $text_body %></PRE></TD>
108 % if ( $cgi->param('action') eq 'preview' ) {
111 function areyousure(href) {
112 return confirm("Send this notice to <% ($num_cust > 1) ? "$num_cust customers" : '1 customer' %> ?");
117 <INPUT TYPE="hidden" NAME="action" VALUE="send">
118 <INPUT TYPE="submit" VALUE="Send notice" onClick="return areyousure()">
122 % } elsif ($alternate_form) {
124 <% $alternate_form %>
128 <SCRIPT TYPE="text/javascript">
129 function toggle(obj) {
130 document.getElementById('table_no_template').style.display = (obj.value == 0) ? '' : 'none';
135 <& /elements/select-msg_template.html,
136 onchange => 'toggle(this)',
139 <TABLE BGCOLOR="#cccccc" CELLSPACING=0 WIDTH="100%" id="table_no_template">
140 <& /elements/tr-td-label.html, 'label' => 'From:' &>
141 <TD><& /elements/input-text.html,
142 'field' => 'from_name',
143 'value' => $conf->config('invoice_from_name', $agent_virt_agentnum) ||
144 $conf->config('company_name', $agent_virt_agentnum), #?
147 <& /elements/input-text.html,
148 'field' => 'from_addr',
149 'type' => 'email', # HTML5, woot
150 'value' => $conf->config('invoice_from', $agent_virt_agentnum),
154 <% include('/elements/tr-input-text.html',
155 'field' => 'subject',
156 'label' => 'Subject:',
162 <TD ALIGN="right" VALIGN="top" STYLE="padding-top:3px">Message: </TD>
163 <TD><& /elements/htmlarea.html,
164 'field' => 'html_body',
174 <INPUT TYPE="hidden" NAME="action" VALUE="preview">
175 <INPUT TYPE="submit" VALUE="Preview notice">
177 % } #end not preview or alternate form
181 % if ( $cgi->param('action') eq 'send' ) {
182 <SCRIPT TYPE="text/javascript">
187 <% include('/elements/footer.html') %>
194 unless $FS::CurrentUser::CurrentUser->access_right('Bulk send customer notices');
196 my $conf = FS::Conf->new;
198 my $form_action = $opt{'form_action'} || 'email-customers.html';
199 my %sub_param = $opt{'sub_param_process'} ? &{$opt{'sub_param_process'}}($conf) : ();
200 my $alternate_form = $opt{'alternate_form'} ? &{$opt{'alternate_form'}}(%sub_param) : ();
201 my $title = $opt{'title'} || 'Send customer notices';
203 my $table = $cgi->param('table') or die "'table' required";
204 my $agent_virt_agentnum = $cgi->param('agent_virt_agentnum') || '';
206 my $popup = $cgi->param('popup');
207 my $url = $cgi->param('url');
208 my $pdest = { 'message' => "Notice sent" };
209 $pdest->{'url'} = $cgi->param('url') if $url;
212 if ( $cgi->param('search') ) {
213 %search = %{ thaw(decode_base64($cgi->param('search'))) };
216 %search = $cgi->Vars;
217 delete $search{$_} for qw( action table from subject html_body text_body popup url sub_param );
218 # FS::$table->search is expected to know which parameters might be
219 # multi-valued, and to accept scalar values for them also. No good
220 # solution to this since CGI can't tell whether a parameter _might_
221 # have had multiple values, only whether it does.
222 @search{keys %search} = map { /\0/ ? [ split /\0/, $_ ] : $_ } values %search;
227 if ( $cgi->param('from') ) {
228 $from = $cgi->param('from');
229 } elsif ( $cgi->param('from_name') ) {
230 $from = ($cgi->param('from_name') . ' <' . $cgi->param('from_addr') . '>');
231 } elsif ( $cgi->param('from_addr') ) {
232 $from = $cgi->param('from_addr');
235 my $subject = $cgi->param('subject') || '';
236 my $html_body = $cgi->param('html_body') || '';
238 my $msg_template = '';
240 if ( $cgi->param('action') eq 'preview' ) {
241 my $sql_query = "FS::$table"->search(\%search);
242 my $count_query = delete($sql_query->{'count_query'});
243 my $count_sth = dbh->prepare($count_query)
244 or die "Error preparing $count_query: ". dbh->errstr;
246 or die "Error executing $count_query: ". $count_sth->errstr;
247 my $count_arrayref = $count_sth->fetchrow_arrayref;
248 $num_cust = $count_arrayref->[0];
250 if ( $cgi->param('msgnum') ) {
251 $msg_template = qsearchs('msg_template',
252 { msgnum => $cgi->param('msgnum') } )
253 or die "template not found: ".$cgi->param('msgnum');
254 $sql_query->{'extra_sql'} .= ' LIMIT 1';
255 $sql_query->{'select'} = "$table.*";
256 $sql_query->{'order_by'} = '';
257 my $object = qsearchs($sql_query);
258 my $cust = $object->cust_main;
260 'cust_main' => $cust,
263 $msgopts{'sub_param'} = \%sub_param if %sub_param;
264 my %message = $msg_template->prepare(%msgopts);
265 ($from, $subject, $html_body) = @message{'from', 'subject', 'html_body'};