1c756cdb9f40b95f7825b37f604d1f601e35932a
[freeside.git] / httemplate / misc / email-customers.html
1 <%doc>
2
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.
8
9 This may also be used as an element in other pages, enabling you to provide
10 an alternate initial form while using this for search freezing/thawing and 
11 preview/send actions, with the following options:
12
13 acl - the access right to use (defaults to 'Bulk send customer notices')
14
15 form_action - the URL to submit the form to
16
17 process_url - the URL for starting the JSRPC process
18
19 title - the title of the page
20
21 no_search_fields - arrayref of additional fields that are not search parameters
22
23 alternate_form - subroutine that returns alternate html for the initial form,
24 replaces msgnum/from/subject/html_body/action inputs and submit button,
25 not used if an action is specified
26
27 post_search_hook - sub hook for additional processing after search has been processed from cgi,
28 gets passed options 'conf' and 'search' (a reference to the unfrozen %search hash),
29 should be used to set msgnum or from/subject/html_body cgi params
30
31 </%doc>
32
33 % if ($popup) {
34 <% include('/elements/header-popup.html', $title) %>
35 % } else {
36 <% include('/elements/header.html', $title) %>
37 % }
38
39
40 <FORM NAME="OneTrueForm" ACTION="<% $form_action %>" METHOD="POST">
41 <INPUT TYPE="hidden" NAME="table" VALUE="<% $table %>">
42 %# Mixing search params with from address, subject, etc. required special-case
43 %# handling of those, risked name conflicts, and caused massive problems with 
44 %# multi-valued search params.  We are no longer in search context, so we 
45 %# pack the search into a Storable string for later use.
46 <INPUT TYPE="hidden" NAME="search" VALUE="<% encode_base64(nfreeze(\%search)) %>">
47 <INPUT TYPE="hidden" NAME="popup" VALUE="<% $popup %>">
48 <INPUT TYPE="hidden" NAME="url" VALUE="<% $url | h %>">
49 <INPUT TYPE="hidden" NAME="emailtovoice_contact" VALUE="<% scalar($cgi->param('emailtovoice_contact')) |h %>">
50 <INPUT TYPE="hidden" NAME="custnum" VALUE="<% scalar($cgi->param('custnum')) |h %>">
51
52 % if ( $cgi->param('action') eq 'send' ) { 
53
54     <INPUT TYPE="hidden" NAME="to_contact_classnum" VALUE="<% $cgi->param('to_contact_classnum') %>">
55     <FONT SIZE="+2">Sending notice</FONT>
56
57     <& /elements/progress-init.html,
58                  'OneTrueForm',
59                  [ qw( search table from subject html_body text_body
60                         msgnum to_contact_classnum emailtovoice_contact custnum ) ],
61                  $process_url,
62                  $pdest,
63     &>
64
65 % } elsif ( $cgi->param('action') eq 'preview' ) {
66     <INPUT TYPE="hidden" NAME="to_contact_classnum" VALUE="<% join(',', @contact_classnum) %>">
67     <FONT SIZE="+2">Preview notice</FONT>
68
69 % }
70
71 % if ( $cgi->param('action') ) {
72
73     <TABLE BGCOLOR="#cccccc" CELLSPACING=0>
74     <INPUT TYPE="hidden" NAME="msgnum" VALUE="<% scalar($cgi->param('msgnum')) %>">
75 %   if ( $msg_template ) {
76       <% include('/elements/tr-fixed.html',
77                    'label'      => 'Template:',
78                    'value'      => $msg_template->msgname,
79                 )
80       %>
81 % }
82
83       <% include('/elements/tr-fixed.html',
84                    'field'      => 'from',
85                    'label'      => 'From:',
86                    'value'      => $from,
87                 )
88       %>
89
90       <& /elements/tr-td-label.html, 'label' => 'To contacts:' &>
91         <td><% join('<BR>', @contact_classname) %></td>
92       </tr>
93
94       <% include('/elements/tr-fixed.html',
95                    'field'      => 'subject',
96                    'label'      => 'Subject:',
97                    'value'      => $subject,
98                 )
99       %>
100
101       <INPUT TYPE="hidden" NAME="html_body" VALUE="<% $html_body |h %>">
102       <TR>
103         <TH ALIGN="right" VALIGN="top">Message (HTML display): </TD>
104         <TD CLASS="background" ALIGN="left"><% $html_body %></TD>
105       </TR>
106
107 %     my $text_body = HTML::FormatText->new(leftmargin=>0)->format(
108 %                       HTML::TreeBuilder->new_from_content(
109 %                         $html_body
110 %                       )
111 %                     );
112       <INPUT TYPE="hidden" NAME="text_body" VALUE="<% $text_body |h %>">
113       <TR>
114         <TH ALIGN="right" VALIGN="top">Message (Text display): </TD>
115         <TD CLASS="background" STYLE="background-color:white" ALIGN="left">
116           <a href="javascript:void(0)" onclick="this.style.display='none'; document.getElementById('email-message-text').style.display=''">click to view</a>
117           <PRE id="email-message-text" style="display: none;"><% $text_body %></PRE>
118         </TD>
119       </TR>
120
121     </TABLE>
122
123 %   if ( $cgi->param('action') eq 'preview' ) {
124
125       <SCRIPT>
126         function areyousure(href) {
127           return confirm("Send this notice to <% ($num_cust > 1) ? "$num_cust customers" : '1 customer' %> ?");
128         }
129       </SCRIPT>
130
131       <BR>
132       <INPUT TYPE="hidden" NAME="action" VALUE="send">
133       <INPUT TYPE="submit" VALUE="Send notice" onClick="return areyousure()">
134     
135 %   }
136
137 % } elsif ($opt{'alternate_form'}) {
138
139 <% &{$opt{'alternate_form'}}() %>
140
141 % } else {
142
143 <SCRIPT TYPE="text/javascript">
144 function toggle(obj) {
145   document.getElementById('table_no_template').style.display = (obj.value == 0) ? '' : 'none';
146 }
147
148 </SCRIPT>
149 Template: 
150     <& /elements/select-msg_template.html,
151          onchange => 'toggle(this)',
152     &>
153     <BR>
154 % # select destination contact classes
155 <TABLE CELLSPACING=0 id="send_to_contacts_table">
156 <TR>
157  <TD>Send to contacts:</TD>
158  <TD>
159    <div id="contactclassesdiv">
160   <& /elements/checkboxes.html,
161     'style'               => 'display: inline; vertical-align: top',
162     'disable_links'       => 1,
163     'names_list'          => \@contact_checkboxes,
164     'element_name_prefix' => 'contact_class_',
165     'checked_callback'    => sub {
166       my($cgi, $name) = @_;
167       $name eq 'invoice' #others default to unchecked
168     },
169   &>
170    </div>
171 % if ($send_to_domain) {
172    <div>
173      <INPUT TYPE="checkbox" NAME="emailtovoice"  ID="emailtovoice" VALUE="ON" onclick="toggleDiv(this)">Email to voice
174    </div>
175    <div id="emailtovoicediv" style="display:none";>
176
177       <& /elements/select-cust_phone.html,
178                'cust_num'     => $cgi->param('custnum'),
179                'field_name'   => 'emailtovoice_contact',
180                'format'       => 'xxxxxxxxxx',
181                'phone_types'  => [ 'daytime', 'night', 'fax', 'mobile' ],
182       &>@<% $send_to_domain |h %>
183    </div>
184 % }
185  </TD>
186 </TR>
187 </TABLE>
188 <BR>
189 % # if sending a one-off message, show a form to edit it
190   <TABLE BGCOLOR="#cccccc" CELLSPACING=0 WIDTH="100%" id="table_no_template">
191     <& /elements/tr-td-label.html, 'label' => 'From:' &>
192       <TD><& /elements/input-text.html,
193               'field' => 'from_name',
194               'value' => $conf->config('invoice_from_name', $agent_virt_agentnum) ||
195                          $conf->config('company_name', $agent_virt_agentnum), #?
196               'size'  => 20,
197           &>&nbsp;&lt;\
198           <& /elements/input-text.html,
199               'field' => 'from_addr',
200               'type'  => 'email', # HTML5, woot
201               'value' => $conf->config('invoice_from', $agent_virt_agentnum),
202               'size'  => 20,
203           &>&gt;</TD>
204  
205     <& /elements/tr-input-text.html,
206                  'field' => 'subject',
207                  'label' => 'Subject:',
208                  'size'  => 50,
209     &>
210
211     <TR>
212       <TD ALIGN="right" VALIGN="top" STYLE="padding-top:3px">Message: </TD>
213       <TD><& /elements/htmlarea.html, 
214                'field' => 'html_body',
215                'width' => 763,
216           &>
217       </TD>
218     </TR>
219
220   </TABLE>
221
222 %#Substitution vars:
223
224     <INPUT TYPE="hidden" NAME="action" VALUE="preview">
225     <INPUT TYPE="hidden" NAME="custnum" VALUE="<% scalar($cgi->param('custnum')) |h %>">
226     <INPUT TYPE="submit" VALUE="Preview notice">
227
228 % } #end not action or alternate form
229
230 </FORM>
231
232 % if ( $cgi->param('action') eq 'send' ) {
233     <SCRIPT TYPE="text/javascript">
234       process();
235     </SCRIPT>
236 % }
237
238 <SCRIPT TYPE="text/javascript">
239 function toggleDiv(obj) {
240   var box_contactclasses = document.getElementById('contactclassesdiv');
241   var box_emailtovoice = document.getElementById('emailtovoicediv');
242
243   box_emailtovoice.style.display = (box_emailtovoice.style.display == 'none') ? 'block' : 'none';
244   document.getElementById('emailtovoice_contact').options[0].selected=true;
245
246   box_contactclasses.style.display = (box_contactclasses.style.display == 'none') ? 'block' : 'none';
247 }
248 </SCRIPT>
249
250 <& /elements/footer.html &>
251
252 <%init>
253
254 my %opt = @_;
255
256 $opt{'acl'} ||= 'Bulk send customer notices';
257
258 my $email_to;
259
260 die "access denied"
261   unless $FS::CurrentUser::CurrentUser->access_right($opt{'acl'});
262
263 my $conf = FS::Conf->new;
264 my @no_search_fields = qw( action table from subject html_body text_body popup url );
265
266 my $send_to_domain = $conf->config('email-to-voice_domain');
267
268 my $form_action = $opt{'form_action'} || 'email-customers.html';
269 my $process_url = $opt{'process_url'} || 'process/email-customers.html';
270 my $title = $opt{'title'} || 'Send customer notices';
271 push( @no_search_fields, @{$opt{'no_search_fields'}} ) if $opt{'no_search_fields'};
272
273 $m->comp('/elements/handle_uri_query');
274
275 my $table = $cgi->param('table') or die "'table' required";
276 my $agent_virt_agentnum = $cgi->param('agent_virt_agentnum') || '';
277
278 my $popup = $cgi->param('popup');
279 my $url   = $cgi->param('url');
280 my $pdest = { 'message' => "Notice sent" };
281 $pdest->{'url'} = $cgi->param('url') if $url;
282
283 my %search;
284 if ( $cgi->param('search') ) {
285   %search = %{ thaw(decode_base64( $cgi->param('search') )) };
286 }
287 else {
288   %search = $cgi->Vars;
289   delete $search{$_} for @no_search_fields;
290   # FS::$table->search is expected to know which parameters might be 
291   # multi-valued, and to accept scalar values for them also.  No good 
292   # solution to this since CGI can't tell whether a parameter _might_
293   # have had multiple values, only whether it does.
294   @search{keys %search} = map { /\0/ ? [ split /\0/, $_ ] : $_ } values %search;
295 }
296
297 &{$opt{'post_search_hook'}}(
298   'conf'   => $conf,
299   'search' => \%search,
300 ) if $opt{'post_search_hook'};
301
302 my $num_cust;
303 my $from = '';
304 if ( $cgi->param('from') ) {
305   $from = $cgi->param('from');
306 } elsif ( $cgi->param('from_name') ) {
307   $from = ($cgi->param('from_name') . ' <' . $cgi->param('from_addr') . '>');
308 } elsif ( $cgi->param('from_addr') ) {
309   $from = $cgi->param('from_addr');
310 }
311
312 my $subject = $cgi->param('subject') || '';
313 my $html_body = $cgi->param('html_body') || '';
314
315 my @contact_classnum;
316 my @contact_classname;
317
318 my $subject = $cgi->param('subject');
319 my $body = $cgi->param('body');
320
321 my $msg_template = '';
322
323 if ( $cgi->param('action') eq 'preview' ) {
324
325   my $sql_query = "FS::$table"->search(\%search);
326   my $count_query = delete($sql_query->{'count_query'});
327   my $count_sth = dbh->prepare($count_query)
328     or die "Error preparing $count_query: ". dbh->errstr;
329   $count_sth->execute
330     or die "Error executing $count_query: ". $count_sth->errstr;
331   my $count_arrayref = $count_sth->fetchrow_arrayref;
332   $num_cust = $count_arrayref->[0];
333
334   if ( $cgi->param('msgnum') ) {
335     $msg_template = qsearchs('msg_template', 
336                              { msgnum => scalar($cgi->param('msgnum')) } )
337         or die "template not found: ".$cgi->param('msgnum');
338     $sql_query->{'extra_sql'} .= ' LIMIT 1';
339     $sql_query->{'select'} = "$table.*";
340     $sql_query->{'order_by'} = '';
341     my $object = qsearchs($sql_query);
342     my $cust = $object->cust_main;
343     my %msgopts = (
344       'cust_main' => $cust,
345       'object' => $object,
346     );
347     my %message = $msg_template->prepare(%msgopts);
348     ($from, $subject, $html_body) = @message{'from', 'subject', 'html_body'};
349   }
350
351   # contact_class_X params in preview
352   foreach my $param ( $cgi->param ) {
353     if ($cgi->param('emailtovoice_contact')) {
354       $email_to = $cgi->param('emailtovoice_contact') . '@' . $send_to_domain;
355       push @contact_classnum, 'emailtovoice';
356       push @contact_classname, $email_to;
357     }
358     if ( $param =~ /^contact_class_(\w+)$/ ) {
359       push @contact_classnum, $1;
360       if ( $1 eq 'invoice' ) {
361         push @contact_classname, 'Invoice recipients';
362       } else {
363         my $contact_class = FS::contact_class->by_key($1);
364         push @contact_classname, encode_entities($contact_class->classname);
365       }
366     }
367   }
368
369 }
370
371 # and set up contact checkboxes for edit mode
372 my @contact_checkboxes = (
373   [ 'invoice' => { label => 'Invoice recipients' } ]
374 );
375
376 foreach my $class (qsearch('contact_class', { disabled => '' })) {
377   push @contact_checkboxes, [
378     $class->classnum,
379     { label => $class->classname }
380   ];
381 }
382
383 </%init>