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