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