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