UI for emailing customer statements on v4, RT#34078
[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 an
10 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 % if ($popup) {
33 <& /elements/header-popup.html, $title &>
34 % } else {
35 <& /elements/header.html, $title &>
36 % }
37
38
39 <FORM NAME="OneTrueForm" ACTION="<% $form_action %>" METHOD="GET">
40 <INPUT TYPE="hidden" NAME="table" VALUE="<% $table %>">
41 %# Mixing search params with from address, subject, etc. required special-case
42 %# handling of those, risked name conflicts, and caused massive problems with 
43 %# multi-valued search params.  We are no longer in search context, so we 
44 %# pack the search into a Storable string for later use.
45 <INPUT TYPE="hidden" NAME="search" VALUE="<% encode_base64(nfreeze(\%search)) %>">
46 <INPUT TYPE="hidden" NAME="popup" VALUE="<% $popup %>">
47 <INPUT TYPE="hidden" NAME="url" VALUE="<% $url | h %>">
48
49 % if ( $cgi->param('action') eq 'send' ) { 
50
51     <FONT SIZE="+2">Sending notice</FONT>
52
53     <% include('/elements/progress-init.html',
54                  'OneTrueForm',
55                  [ qw( search table from subject html_body text_body msgnum ) ],
56                  $process_url,
57                  $pdest,
58               )
59     %>
60
61 % } elsif ( $cgi->param('action') eq 'preview' ) {
62
63     <FONT SIZE="+2">Preview notice</FONT>
64
65 % }
66
67 % if ( $cgi->param('action') ) {
68
69     <TABLE CLASS="fsinnerbox">
70     <INPUT TYPE="hidden" NAME="msgnum" VALUE="<% $cgi->param('msgnum') %>">
71
72 %   if ( $msg_template ) {
73       <% include('/elements/tr-fixed.html',
74                    'label'      => 'Template:',
75                    'value'      => $msg_template->msgname,
76                 )
77       %>
78 % }
79
80       <% include('/elements/tr-fixed.html',
81                    'field'      => 'from',
82                    'label'      => 'From:',
83                    'value' => scalar( $from ),
84                 )
85       %>
86
87       <% include('/elements/tr-fixed.html',
88                    'field'      => 'subject',
89                    'label'      => 'Subject:',
90                    'value' => scalar( $subject ),
91                 )
92       %>
93
94       <INPUT TYPE="hidden" NAME="html_body" VALUE="<% $html_body |h %>">
95       <TR><TD COLSPAN=2>&nbsp;</TD></TR>
96       <TR>
97         <TH ALIGN="right" VALIGN="top">Message (HTML display): </TD>
98         <TD CLASS="background" ALIGN="left"><% $html_body %></TD>
99       </TR>
100
101 %     my $text_body = HTML::FormatText->new(leftmargin=>0)->format(
102 %                       HTML::TreeBuilder->new_from_content(
103 %                         $html_body
104 %                       )
105 %                     );
106       <INPUT TYPE="hidden" NAME="text_body" VALUE="<% $text_body |h %>">
107       <TR><TD COLSPAN=2>&nbsp;</TD></TR>
108       <TR>
109         <TH ALIGN="right" VALIGN="top">Message (Text display): </TD>
110         <TD CLASS="background" ALIGN="left">
111           <a href="javascript:void(0)" ID="email-message-text-view" style="color:#666666" onclick="showtext()">(view)</a>
112           <a href="javascript:void(0)" ID="email-message-text-hide" style="color:#666666; display: none;" onclick="hidetext()">(hide)</a>
113           <PRE id="email-message-text" style="display: none;"><% $text_body %></PRE>
114         </TD>
115       </TR>
116
117     </TABLE>
118
119 %   if ( $cgi->param('action') eq 'preview' ) {
120
121       <SCRIPT>
122
123         function showtext() {
124           $('#email-message-text-view').css('display','none');
125           $('#email-message-text-hide').css('display','');
126           $('#email-message-text').slideDown();
127         }
128
129         function hidetext() {
130           $('#email-message-text-view').css('display','');
131           $('#email-message-text-hide').css('display','none');
132           $('#email-message-text').slideUp();
133         }
134
135         function areyousure(href) {
136           return confirm("Send this notice to <% ($num_cust > 1) ? "$num_cust customers" : '1 customer' %> ?");
137         }
138       </SCRIPT>
139
140       <BR>
141       <INPUT TYPE="hidden" NAME="action" VALUE="send">
142       <INPUT TYPE="submit" VALUE="Send notice" onClick="return areyousure()">
143     
144 %   }
145
146 % } elsif ($opt{'alternate_form'}) {
147
148 <% &{$opt{'alternate_form'}}() %>
149
150 % } else {
151
152 <SCRIPT TYPE="text/javascript">
153 function toggle(obj) {
154   document.getElementById('table_no_template').style.display = (obj.value == 0) ? '' : 'none';
155 }
156
157 </SCRIPT>
158 Template: 
159     <& /elements/select-msg_template.html,
160          onchange => 'toggle(this)',
161     &>
162     <BR>
163   <TABLE BGCOLOR="#cccccc" CELLSPACING=0 WIDTH="100%" id="table_no_template">
164     <& /elements/tr-td-label.html, 'label' => 'From:' &>
165       <TD><& /elements/input-text.html,
166               'field' => 'from_name',
167               'value' => $conf->config('invoice_from_name', $agent_virt_agentnum) ||
168                          $conf->config('company_name', $agent_virt_agentnum), #?
169               'size'  => 20,
170           &>&nbsp;&lt;\
171           <& /elements/input-text.html,
172               'field' => 'from_addr',
173               'type'  => 'email', # HTML5, woot
174               'value' => $conf->config('invoice_from', $agent_virt_agentnum),
175               'size'  => 20,
176           &>&gt;</TD>
177  
178     <% include('/elements/tr-input-text.html',
179                  'field' => 'subject',
180                  'label' => 'Subject:',
181                  'size'  => 50,
182               )
183     %>
184
185     <TR>
186       <TD ALIGN="right" VALIGN="top" STYLE="padding-top:3px">Message: </TD>
187       <TD><& /elements/htmlarea.html, 
188                'field' => 'html_body',
189                'width' => 763,
190           &>
191       </TD>
192     </TR>
193
194   </TABLE>
195
196 %#Substitution vars:
197
198     <INPUT TYPE="hidden" NAME="action" VALUE="preview">
199     <INPUT TYPE="submit" VALUE="Preview notice">
200
201 % } #end not action or alternate form
202
203 </FORM>
204
205 % if ( $cgi->param('action') eq 'send' ) {
206     <SCRIPT TYPE="text/javascript">
207       process();
208     </SCRIPT>
209 % }
210
211 <% include('/elements/footer.html') %>
212
213 <%init>
214
215 my %opt = @_;
216
217 $opt{'acl'} ||= 'Bulk send customer notices';
218
219 die "access denied"
220   unless $FS::CurrentUser::CurrentUser->access_right($opt{'acl'});
221
222 my $conf = FS::Conf->new;
223 my @no_search_fields = qw( action table from subject html_body text_body popup url );
224
225 my $form_action = $opt{'form_action'} || 'email-customers.html';
226 my $process_url = $opt{'process_url'} || 'process/email-customers.html';
227 my $title = $opt{'title'} || 'Send customer notices';
228 push( @no_search_fields, @{$opt{'no_search_fields'}} ) if $opt{'no_search_fields'};
229
230 my $table = $cgi->param('table') or die "'table' required";
231 my $agent_virt_agentnum = $cgi->param('agent_virt_agentnum') || '';
232
233 my $popup = $cgi->param('popup');
234 my $url   = $cgi->param('url');
235 my $pdest = { 'message' => "Notice sent" };
236 $pdest->{'url'} = $cgi->param('url') if $url;
237
238 my %search;
239 if ( $cgi->param('search') ) {
240   %search = %{ thaw(decode_base64($cgi->param('search'))) };
241 }
242 else {
243   %search = $cgi->Vars;
244   delete $search{$_} for @no_search_fields;
245   # FS::$table->search is expected to know which parameters might be 
246   # multi-valued, and to accept scalar values for them also.  No good 
247   # solution to this since CGI can't tell whether a parameter _might_
248   # have had multiple values, only whether it does.
249   @search{keys %search} = map { /\0/ ? [ split /\0/, $_ ] : $_ } values %search;
250 }
251
252 &{$opt{'post_search_hook'}}(
253   'conf'   => $conf,
254   'search' => \%search,
255 ) if $opt{'post_search_hook'};
256
257 my $num_cust;
258 my $from = '';
259 if ( $cgi->param('from') ) {
260   $from = $cgi->param('from');
261 } elsif ( $cgi->param('from_name') ) {
262   $from = ($cgi->param('from_name') . ' <' . $cgi->param('from_addr') . '>');
263 } elsif ( $cgi->param('from_addr') ) {
264   $from = $cgi->param('from_addr');
265 }
266
267 my $subject = $cgi->param('subject') || '';
268 my $html_body = $cgi->param('html_body') || '';
269
270 my $msg_template = '';
271
272 if ( $cgi->param('action') eq 'preview' ) {
273
274   my $sql_query = "FS::$table"->search(\%search);
275   my $count_query = delete($sql_query->{'count_query'});
276   my $count_sth = dbh->prepare($count_query)
277     or die "Error preparing $count_query: ". dbh->errstr;
278   $count_sth->execute
279     or die "Error executing $count_query: ". $count_sth->errstr;
280   my $count_arrayref = $count_sth->fetchrow_arrayref;
281   $num_cust = $count_arrayref->[0];
282
283   if ( $cgi->param('msgnum') ) {
284     $msg_template = qsearchs('msg_template', 
285                              { msgnum => $cgi->param('msgnum') } )
286         or die "template not found: ".$cgi->param('msgnum');
287     $sql_query->{'extra_sql'} .= ' LIMIT 1';
288     $sql_query->{'select'} = "$table.*";
289     $sql_query->{'order_by'} = '';
290     my $object = qsearchs($sql_query);
291     my $cust = $object->cust_main;
292     my %msgopts = (
293       'cust_main' => $cust,
294       'object' => $object,
295     );
296     my %message = $msg_template->prepare(%msgopts);
297     ($from, $subject, $html_body) = @message{'from', 'subject', 'html_body'};
298   }
299 }
300
301 </%init>