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