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