allow sending email to specific contact classes, #33316
[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="to_contact_classnum" VALUE="<% join(',', @contact_classnum) %>">
50
51 % if ( $cgi->param('action') eq 'send' ) { 
52
53     <FONT SIZE="+2">Sending notice</FONT>
54
55     <& /elements/progress-init.html,
56                  'OneTrueForm',
57                  [ qw( search table from subject html_body text_body
58                         msgnum to_contact_classnum ) ],
59                  $process_url,
60                  $pdest,
61     &>
62
63 % } elsif ( $cgi->param('action') eq 'preview' ) {
64
65     <FONT SIZE="+2">Preview notice</FONT>
66
67 % }
68
69 % if ( $cgi->param('action') ) {
70
71     <TABLE BGCOLOR="#cccccc" CELLSPACING=0>
72     <INPUT TYPE="hidden" NAME="msgnum" VALUE="<% scalar($cgi->param('msgnum')) %>">
73 %   if ( $msg_template ) {
74       <% include('/elements/tr-fixed.html',
75                    'label'      => 'Template:',
76                    'value'      => $msg_template->msgname,
77                 )
78       %>
79 % }
80
81       <% include('/elements/tr-fixed.html',
82                    'field'      => 'from',
83                    'label'      => 'From:',
84                    'value'      => $from,
85                 )
86       %>
87
88       <& /elements/tr-td-label.html, 'label' => 'To contacts:' &>
89         <td><% join('<BR>', @contact_classname) %></td>
90       </tr>
91
92       <% include('/elements/tr-fixed.html',
93                    'field'      => 'subject',
94                    'label'      => 'Subject:',
95                    'value'      => $subject,
96                 )
97       %>
98
99       <INPUT TYPE="hidden" NAME="html_body" VALUE="<% $html_body |h %>">
100       <TR>
101         <TH ALIGN="right" VALIGN="top">Message (HTML display): </TD>
102         <TD CLASS="background" ALIGN="left"><% $html_body %></TD>
103       </TR>
104
105 %     my $text_body = HTML::FormatText->new(leftmargin=>0)->format(
106 %                       HTML::TreeBuilder->new_from_content(
107 %                         $html_body
108 %                       )
109 %                     );
110       <INPUT TYPE="hidden" NAME="text_body" VALUE="<% $text_body |h %>">
111       <TR>
112         <TH ALIGN="right" VALIGN="top">Message (Text display): </TD>
113         <TD CLASS="background" STYLE="background-color:white" ALIGN="left">
114           <a href="javascript:void(0)" onclick="this.style.display='none'; document.getElementById('email-message-text').style.display=''">click to view</a>
115           <PRE id="email-message-text" style="display: none;"><% $text_body %></PRE>
116         </TD>
117       </TR>
118
119     </TABLE>
120
121 %   if ( $cgi->param('action') eq 'preview' ) {
122
123       <SCRIPT>
124         function areyousure(href) {
125           return confirm("Send this notice to <% ($num_cust > 1) ? "$num_cust customers" : '1 customer' %> ?");
126         }
127       </SCRIPT>
128
129       <BR>
130       <INPUT TYPE="hidden" NAME="action" VALUE="send">
131       <INPUT TYPE="submit" VALUE="Send notice" onClick="return areyousure()">
132     
133 %   }
134
135 % } elsif ($opt{'alternate_form'}) {
136
137 <% &{$opt{'alternate_form'}}() %>
138
139 % } else {
140
141 <SCRIPT TYPE="text/javascript">
142 function toggle(obj) {
143   document.getElementById('table_no_template').style.display = (obj.value == 0) ? '' : 'none';
144 }
145
146 </SCRIPT>
147 Template: 
148     <& /elements/select-msg_template.html,
149          onchange => 'toggle(this)',
150     &>
151     <BR>
152 % }
153 % # select destination contact classes
154 Send to contacts:
155   <& /elements/checkboxes.html,
156     'style'               => 'display: inline; vertical-align: top',
157     'disable_links'       => 1,
158     'names_list'          => \@contact_checkboxes,
159     'element_name_prefix' => 'contact_class_',
160     'checked_callback'    => sub {
161       my($cgi, $name) = @_;
162       $name eq 'invoice' #others default to unchecked
163     },
164   &>
165 <BR>
166 % # if sending a one-off message, show a form to edit it
167   <TABLE BGCOLOR="#cccccc" CELLSPACING=0 WIDTH="100%" id="table_no_template">
168     <& /elements/tr-td-label.html, 'label' => 'From:' &>
169       <TD><& /elements/input-text.html,
170               'field' => 'from_name',
171               'value' => $conf->config('invoice_from_name', $agent_virt_agentnum) ||
172                          $conf->config('company_name', $agent_virt_agentnum), #?
173               'size'  => 20,
174           &>&nbsp;&lt;\
175           <& /elements/input-text.html,
176               'field' => 'from_addr',
177               'type'  => 'email', # HTML5, woot
178               'value' => $conf->config('invoice_from', $agent_virt_agentnum),
179               'size'  => 20,
180           &>&gt;</TD>
181  
182     <& /elements/tr-input-text.html,
183                  'field' => 'subject',
184                  'label' => 'Subject:',
185                  'size'  => 50,
186     &>
187
188     <TR>
189       <TD ALIGN="right" VALIGN="top" STYLE="padding-top:3px">Message: </TD>
190       <TD><& /elements/htmlarea.html, 
191                'field' => 'html_body',
192                'width' => 763,
193           &>
194       </TD>
195     </TR>
196
197   </TABLE>
198
199 %#Substitution vars:
200
201     <INPUT TYPE="hidden" NAME="action" VALUE="preview">
202     <INPUT TYPE="submit" VALUE="Preview notice">
203
204 % } #end not action or alternate form
205
206 </FORM>
207
208 % if ( $cgi->param('action') eq 'send' ) {
209     <SCRIPT TYPE="text/javascript">
210       process();
211     </SCRIPT>
212 % }
213
214 <& /elements/footer.html &>
215
216 <%init>
217
218 my %opt = @_;
219
220 $opt{'acl'} ||= 'Bulk send customer notices';
221
222 die "access denied"
223   unless $FS::CurrentUser::CurrentUser->access_right($opt{'acl'});
224
225 my $conf = FS::Conf->new;
226 my @no_search_fields = qw( action table from subject html_body text_body popup url );
227
228 my $form_action = $opt{'form_action'} || 'email-customers.html';
229 my $process_url = $opt{'process_url'} || 'process/email-customers.html';
230 my $title = $opt{'title'} || 'Send customer notices';
231 push( @no_search_fields, @{$opt{'no_search_fields'}} ) if $opt{'no_search_fields'};
232
233 my $table = $cgi->param('table') or die "'table' required";
234 my $agent_virt_agentnum = $cgi->param('agent_virt_agentnum') || '';
235
236 my $popup = $cgi->param('popup');
237 my $url   = $cgi->param('url');
238 my $pdest = { 'message' => "Notice sent" };
239 $pdest->{'url'} = $cgi->param('url') if $url;
240
241 my %search;
242 if ( $cgi->param('search') ) {
243   %search = %{ thaw(decode_base64( $cgi->param('search') )) };
244 }
245 else {
246   %search = $cgi->Vars;
247   delete $search{$_} for @no_search_fields;
248   # FS::$table->search is expected to know which parameters might be 
249   # multi-valued, and to accept scalar values for them also.  No good 
250   # solution to this since CGI can't tell whether a parameter _might_
251   # have had multiple values, only whether it does.
252   @search{keys %search} = map { /\0/ ? [ split /\0/, $_ ] : $_ } values %search;
253 }
254
255 &{$opt{'post_search_hook'}}(
256   'conf'   => $conf,
257   'search' => \%search,
258 ) if $opt{'post_search_hook'};
259
260 my $num_cust;
261 my $from = '';
262 if ( $cgi->param('from') ) {
263   $from = $cgi->param('from');
264 } elsif ( $cgi->param('from_name') ) {
265   $from = ($cgi->param('from_name') . ' <' . $cgi->param('from_addr') . '>');
266 } elsif ( $cgi->param('from_addr') ) {
267   $from = $cgi->param('from_addr');
268 }
269
270 my $subject = $cgi->param('subject') || '';
271 my $html_body = $cgi->param('html_body') || '';
272
273 my @contact_classnum;
274 my @contact_classname;
275
276 my $subject = $cgi->param('subject');
277 my $body = $cgi->param('body');
278
279 my $msg_template = '';
280
281 if ( $cgi->param('action') eq 'preview' ) {
282
283   my $sql_query = "FS::$table"->search(\%search);
284   my $count_query = delete($sql_query->{'count_query'});
285   my $count_sth = dbh->prepare($count_query)
286     or die "Error preparing $count_query: ". dbh->errstr;
287   $count_sth->execute
288     or die "Error executing $count_query: ". $count_sth->errstr;
289   my $count_arrayref = $count_sth->fetchrow_arrayref;
290   $num_cust = $count_arrayref->[0];
291
292   if ( $cgi->param('msgnum') ) {
293     $msg_template = qsearchs('msg_template', 
294                              { msgnum => scalar($cgi->param('msgnum')) } )
295         or die "template not found: ".$cgi->param('msgnum');
296     $sql_query->{'extra_sql'} .= ' LIMIT 1';
297     $sql_query->{'select'} = "$table.*";
298     $sql_query->{'order_by'} = '';
299     my $object = qsearchs($sql_query);
300     my $cust = $object->cust_main;
301     my %msgopts = (
302       'cust_main' => $cust,
303       'object' => $object,
304     );
305     my %message = $msg_template->prepare(%msgopts);
306     ($from, $subject, $html_body) = @message{'from', 'subject', 'html_body'};
307   }
308
309   # contact_class_X params
310   foreach my $param ( $cgi->multi_param ) {
311     if ( $param =~ /^contact_class_(\w+)$/ ) {
312       push @contact_classnum, $1;
313       if ( $1 eq 'invoice' ) {
314         push @contact_classname, 'Invoice recipients';
315       } else {
316         my $contact_class = FS::contact_class->by_key($1);
317         push @contact_classname, encode_entities($contact_class->classname);
318       }
319     }
320   }
321 }
322
323 my @contact_checkboxes = (
324   [ 'invoice' => { label => 'Invoice recipients' } ]
325 );
326 foreach my $class (qsearch('contact_class', { disabled => '' })) {
327   push @contact_checkboxes, [
328     $class->classnum,
329     { label => $class->classname }
330   ];
331 }
332 </%init>