RT#34078: Payment History Report / Statement [Fixes for acl and text preview]
[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
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
50 % if ( $cgi->param('action') eq 'send' ) { 
51
52     <FONT SIZE="+2">Sending notice</FONT>
53
54     <% include('/elements/progress-init.html',
55                  'OneTrueForm',
56                  [ qw( search table from subject html_body text_body msgnum ) ],
57                  $process_url,
58                  $pdest,
59               )
60     %>
61
62 % } elsif ( $cgi->param('action') eq 'preview' ) {
63
64     <FONT SIZE="+2">Preview notice</FONT>
65
66 % }
67
68 % if ( $cgi->param('action') ) {
69
70     <TABLE BGCOLOR="#cccccc" CELLSPACING=0>
71     <INPUT TYPE="hidden" NAME="msgnum" VALUE="<% $cgi->param('msgnum') %>">
72
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' => scalar( $from ),
85                 )
86       %>
87
88       <% include('/elements/tr-fixed.html',
89                    'field'      => 'subject',
90                    'label'      => 'Subject:',
91                    'value' => scalar( $subject ),
92                 )
93       %>
94
95       <INPUT TYPE="hidden" NAME="html_body" VALUE="<% $html_body |h %>">
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>
108         <TH ALIGN="right" VALIGN="top">Message (Text display): </TD>
109         <TD CLASS="background" STYLE="background-color:white" ALIGN="left">
110           <a href="javascript:void(0)" onclick="this.style.display='none'; document.getElementById('email-message-text').style.display=''">click to view</a>
111           <PRE id="email-message-text" style="display: none;"><% $text_body %></PRE>
112         </TD>
113       </TR>
114
115     </TABLE>
116
117 %   if ( $cgi->param('action') eq 'preview' ) {
118
119       <SCRIPT>
120         function areyousure(href) {
121           return confirm("Send this notice to <% ($num_cust > 1) ? "$num_cust customers" : '1 customer' %> ?");
122         }
123       </SCRIPT>
124
125       <BR>
126       <INPUT TYPE="hidden" NAME="action" VALUE="send">
127       <INPUT TYPE="submit" VALUE="Send notice" onClick="return areyousure()">
128     
129 %   }
130
131 % } elsif ($opt{'alternate_form'}) {
132
133 <% &{$opt{'alternate_form'}}() %>
134
135 % } else {
136
137 <SCRIPT TYPE="text/javascript">
138 function toggle(obj) {
139   document.getElementById('table_no_template').style.display = (obj.value == 0) ? '' : 'none';
140 }
141
142 </SCRIPT>
143 Template: 
144     <& /elements/select-msg_template.html,
145          onchange => 'toggle(this)',
146     &>
147     <BR>
148   <TABLE BGCOLOR="#cccccc" CELLSPACING=0 WIDTH="100%" id="table_no_template">
149     <& /elements/tr-td-label.html, 'label' => 'From:' &>
150       <TD><& /elements/input-text.html,
151               'field' => 'from_name',
152               'value' => $conf->config('invoice_from_name', $agent_virt_agentnum) ||
153                          $conf->config('company_name', $agent_virt_agentnum), #?
154               'size'  => 20,
155           &>&nbsp;&lt;\
156           <& /elements/input-text.html,
157               'field' => 'from_addr',
158               'type'  => 'email', # HTML5, woot
159               'value' => $conf->config('invoice_from', $agent_virt_agentnum),
160               'size'  => 20,
161           &>&gt;</TD>
162  
163     <% include('/elements/tr-input-text.html',
164                  'field' => 'subject',
165                  'label' => 'Subject:',
166                  'size'  => 50,
167               )
168     %>
169
170     <TR>
171       <TD ALIGN="right" VALIGN="top" STYLE="padding-top:3px">Message: </TD>
172       <TD><& /elements/htmlarea.html, 
173                'field' => 'html_body',
174                'width' => 763,
175           &>
176       </TD>
177     </TR>
178
179   </TABLE>
180
181 %#Substitution vars:
182
183     <INPUT TYPE="hidden" NAME="action" VALUE="preview">
184     <INPUT TYPE="submit" VALUE="Preview notice">
185
186 % } #end not action or alternate form
187
188 </FORM>
189
190 % if ( $cgi->param('action') eq 'send' ) {
191     <SCRIPT TYPE="text/javascript">
192       process();
193     </SCRIPT>
194 % }
195
196 <% include('/elements/footer.html') %>
197
198 <%init>
199
200 my %opt = @_;
201
202 $opt{'acl'} ||= 'Bulk send customer notices';
203
204 die "access denied"
205   unless $FS::CurrentUser::CurrentUser->access_right($opt{'acl'});
206
207 my $conf = FS::Conf->new;
208 my @no_search_fields = qw( action table from subject html_body text_body popup url );
209
210 my $form_action = $opt{'form_action'} || 'email-customers.html';
211 my $process_url = $opt{'process_url'} || 'process/email-customers.html';
212 my $title = $opt{'title'} || 'Send customer notices';
213 push( @no_search_fields, @{$opt{'no_search_fields'}} ) if $opt{'no_search_fields'};
214
215 my $table = $cgi->param('table') or die "'table' required";
216 my $agent_virt_agentnum = $cgi->param('agent_virt_agentnum') || '';
217
218 my $popup = $cgi->param('popup');
219 my $url   = $cgi->param('url');
220 my $pdest = { 'message' => "Notice sent" };
221 $pdest->{'url'} = $cgi->param('url') if $url;
222
223 my %search;
224 if ( $cgi->param('search') ) {
225   %search = %{ thaw(decode_base64($cgi->param('search'))) };
226 }
227 else {
228   %search = $cgi->Vars;
229   delete $search{$_} for @no_search_fields;
230   # FS::$table->search is expected to know which parameters might be 
231   # multi-valued, and to accept scalar values for them also.  No good 
232   # solution to this since CGI can't tell whether a parameter _might_
233   # have had multiple values, only whether it does.
234   @search{keys %search} = map { /\0/ ? [ split /\0/, $_ ] : $_ } values %search;
235 }
236
237 &{$opt{'post_search_hook'}}(
238   'conf'   => $conf,
239   'search' => \%search,
240 ) if $opt{'post_search_hook'};
241
242 my $num_cust;
243 my $from = '';
244 if ( $cgi->param('from') ) {
245   $from = $cgi->param('from');
246 } elsif ( $cgi->param('from_name') ) {
247   $from = ($cgi->param('from_name') . ' <' . $cgi->param('from_addr') . '>');
248 } elsif ( $cgi->param('from_addr') ) {
249   $from = $cgi->param('from_addr');
250 }
251
252 my $subject = $cgi->param('subject') || '';
253 my $html_body = $cgi->param('html_body') || '';
254
255 my $msg_template = '';
256
257 if ( $cgi->param('action') eq 'preview' ) {
258
259   my $sql_query = "FS::$table"->search(\%search);
260   my $count_query = delete($sql_query->{'count_query'});
261   my $count_sth = dbh->prepare($count_query)
262     or die "Error preparing $count_query: ". dbh->errstr;
263   $count_sth->execute
264     or die "Error executing $count_query: ". $count_sth->errstr;
265   my $count_arrayref = $count_sth->fetchrow_arrayref;
266   $num_cust = $count_arrayref->[0];
267
268   if ( $cgi->param('msgnum') ) {
269     $msg_template = qsearchs('msg_template', 
270                              { msgnum => $cgi->param('msgnum') } )
271         or die "template not found: ".$cgi->param('msgnum');
272     $sql_query->{'extra_sql'} .= ' LIMIT 1';
273     $sql_query->{'select'} = "$table.*";
274     $sql_query->{'order_by'} = '';
275     my $object = qsearchs($sql_query);
276     my $cust = $object->cust_main;
277     my %msgopts = (
278       'cust_main' => $cust,
279       'object' => $object,
280     );
281     my %message = $msg_template->prepare(%msgopts);
282     ($from, $subject, $html_body) = @message{'from', 'subject', 'html_body'};
283   }
284 }
285
286 </%init>