RT#34078: Payment History Report / Statement
[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 pass along
10 additional substitution parameters to a message template, with the following options:
11
12 form_action - the URL to submit the form to
13
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
17
18 alternate_form - an alternate form for template selection/message creation
19
20 title - the title of the page
21
22 </%doc>
23
24 % if ($popup) {
25 <% include('/elements/header-popup.html', $title) %>
26 % } else {
27 <% include('/elements/header.html', $title) %>
28 % }
29
30
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)) %>">
38 % if (%sub_param) {
39 <INPUT TYPE="hidden" NAME="sub_param" VALUE="<% encode_base64(nfreeze(\%sub_param)) %>">
40 % }
41 <INPUT TYPE="hidden" NAME="popup" VALUE="<% $popup %>">
42 <INPUT TYPE="hidden" NAME="url" VALUE="<% $url | h %>">
43
44 % if ( $cgi->param('action') eq 'send' ) { 
45
46     <FONT SIZE="+2">Sending notice</FONT>
47
48     <% include('/elements/progress-init.html',
49                  'OneTrueForm',
50                  [ qw( search table from subject html_body text_body msgnum sub_param ) ],
51                  'process/email-customers.html',
52                  $pdest,
53               )
54     %>
55
56 % } elsif ( $cgi->param('action') eq 'preview' ) {
57
58     <FONT SIZE="+2">Preview notice</FONT>
59
60 % }
61
62 % if ( $cgi->param('action') ) {
63
64     <TABLE BGCOLOR="#cccccc" CELLSPACING=0>
65     <INPUT TYPE="hidden" NAME="msgnum" VALUE="<% $cgi->param('msgnum') %>">
66
67 %   if ( $msg_template ) {
68       <% include('/elements/tr-fixed.html',
69                    'label'      => 'Template:',
70                    'value'      => $msg_template->msgname,
71                 )
72       %>
73 % }
74
75       <% include('/elements/tr-fixed.html',
76                    'field'      => 'from',
77                    'label'      => 'From:',
78                    'value' => scalar( $from ),
79                 )
80       %>
81
82       <% include('/elements/tr-fixed.html',
83                    'field'      => 'subject',
84                    'label'      => 'Subject:',
85                    'value' => scalar( $subject ),
86                 )
87       %>
88
89       <INPUT TYPE="hidden" NAME="html_body" VALUE="<% $html_body |h %>">
90       <TR>
91         <TD ALIGN="right" VALIGN="top">Message (HTML display): </TD>
92         <TD CLASS="background" ALIGN="left"><% $html_body %></TD>
93       </TR>
94
95 %     my $text_body = HTML::FormatText->new(leftmargin=>0)->format(
96 %                       HTML::TreeBuilder->new_from_content(
97 %                         $html_body
98 %                       )
99 %                     );
100       <INPUT TYPE="hidden" NAME="text_body" VALUE="<% $text_body |h %>">
101       <TR>
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>
104       </TR>
105
106     </TABLE>
107
108 % if ( $cgi->param('action') eq 'preview' ) {
109
110       <SCRIPT>
111         function areyousure(href) {
112           return confirm("Send this notice to <% ($num_cust > 1) ? "$num_cust customers" : '1 customer' %> ?");
113         }
114       </SCRIPT>
115
116       <BR>
117       <INPUT TYPE="hidden" NAME="action" VALUE="send">
118       <INPUT TYPE="submit" VALUE="Send notice" onClick="return areyousure()">
119     
120 %   }
121
122 % } elsif ($alternate_form) {
123
124 <% $alternate_form %>
125
126 % } else {
127
128 <SCRIPT TYPE="text/javascript">
129 function toggle(obj) {
130   document.getElementById('table_no_template').style.display = (obj.value == 0) ? '' : 'none';
131 }
132
133 </SCRIPT>
134 Template: 
135     <& /elements/select-msg_template.html,
136          onchange => 'toggle(this)',
137     &>
138     <BR>
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), #?
145               'size'  => 20,
146           &>&nbsp;&lt;\
147           <& /elements/input-text.html,
148               'field' => 'from_addr',
149               'type'  => 'email', # HTML5, woot
150               'value' => $conf->config('invoice_from', $agent_virt_agentnum),
151               'size'  => 20,
152           &>&gt;</TD>
153  
154     <% include('/elements/tr-input-text.html',
155                  'field' => 'subject',
156                  'label' => 'Subject:',
157                  'size'  => 50,
158               )
159     %>
160
161     <TR>
162       <TD ALIGN="right" VALIGN="top" STYLE="padding-top:3px">Message: </TD>
163       <TD><& /elements/htmlarea.html, 
164                'field' => 'html_body',
165                'width' => 763,
166           &>
167       </TD>
168     </TR>
169
170   </TABLE>
171
172 %#Substitution vars:
173
174     <INPUT TYPE="hidden" NAME="action" VALUE="preview">
175     <INPUT TYPE="submit" VALUE="Preview notice">
176
177 % } #end not preview or alternate form
178
179 </FORM>
180
181 % if ( $cgi->param('action') eq 'send' ) {
182     <SCRIPT TYPE="text/javascript">
183       process();
184     </SCRIPT>
185 % }
186
187 <% include('/elements/footer.html') %>
188
189 <%init>
190
191 my %opt = @_;
192
193 die "access denied"
194   unless $FS::CurrentUser::CurrentUser->access_right('Bulk send customer notices');
195
196 my $conf = FS::Conf->new;
197
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';
202
203 my $table = $cgi->param('table') or die "'table' required";
204 my $agent_virt_agentnum = $cgi->param('agent_virt_agentnum') || '';
205
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;
210
211 my %search;
212 if ( $cgi->param('search') ) {
213   %search = %{ thaw(decode_base64($cgi->param('search'))) };
214 }
215 else {
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;
223
224
225 my $num_cust;
226 my $from = '';
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');
233 }
234
235 my $subject = $cgi->param('subject') || '';
236 my $html_body = $cgi->param('html_body') || '';
237
238 my $msg_template = '';
239
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;
245   $count_sth->execute
246     or die "Error executing $count_query: ". $count_sth->errstr;
247   my $count_arrayref = $count_sth->fetchrow_arrayref;
248   $num_cust = $count_arrayref->[0];
249
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;
259     my %msgopts = (
260       'cust_main' => $cust,
261       'object' => $object,
262     );
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'};
266   }
267 }
268
269 </%init>