email_search_result for cust_pkg and svc_broadband, RT#8736
[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
33 %   if ( $msg_template ) {
34       <% include('/elements/tr-fixed.html',
35                    'label'      => 'Template:',
36                    'value'      => $msg_template->msgname,
37                 )
38       %>
39 % }
40
41       <% include('/elements/tr-fixed.html',
42                    'field'      => 'from',
43                    'label'      => 'From:',
44                    'value' => scalar( $from ),
45                 )
46       %>
47
48       <% include('/elements/tr-fixed.html',
49                    'field'      => 'subject',
50                    'label'      => 'Subject:',
51                    'value' => scalar( $subject ),
52                 )
53       %>
54
55       <INPUT TYPE="hidden" NAME="html_body" VALUE="<% $html_body |h %>">
56       <TR>
57         <TD ALIGN="right" VALIGN="top">Message (HTML display): </TD>
58         <TD CLASS="background" ALIGN="left"><% $html_body %></TD>
59       </TR>
60
61 %     my $text_body = HTML::FormatText->new(leftmargin=>0)->format(
62 %                       HTML::TreeBuilder->new_from_content(
63 %                         $html_body
64 %                       )
65 %                     );
66       <INPUT TYPE="hidden" NAME="text_body" VALUE="<% $text_body |h %>">
67       <TR>
68         <TD ALIGN="right" VALIGN="top">Message (Text display): </TD>
69         <TD CLASS="background" STYLE="background-color:white" ALIGN="left"><PRE><% $text_body %></PRE></TD>
70       </TR>
71
72     </TABLE>
73
74 % if ( $cgi->param('action') eq 'preview' ) {
75
76       <SCRIPT>
77         function areyousure(href) {
78           return confirm("Send this notice to <% $num_cust %> customers?");
79         }
80       </SCRIPT>
81
82       <BR>
83       <INPUT TYPE="hidden" NAME="action" VALUE="send">
84       <INPUT TYPE="submit" VALUE="Send notice" onClick="return areyousure()">
85     
86 %   }
87
88 % } else {
89
90 <SCRIPT TYPE="text/javascript">
91 function toggle(obj) {
92   document.getElementById('table_no_template').style.display = (obj.value == 0) ? '' : 'none';
93 }
94
95 </SCRIPT>
96 Template: 
97     <% include('/elements/select-table.html',
98                   'label'         => 'Template:',
99                   'table'         => 'msg_template',
100                   'name_col'      => 'msgname',
101                   'empty_label'   => '(none)',
102                   'onchange'      => 'toggle(this)',
103               )
104     %><BR>
105   <TABLE BGCOLOR="#cccccc" CELLSPACING=0 WIDTH="100%" id="table_no_template">
106     <% include('/elements/tr-input-text.html',
107                  'field' => 'from',
108                  'label' => 'From:',
109               )
110     %>
111
112     <% include('/elements/tr-input-text.html',
113                  'field' => 'subject',
114                  'label' => 'Subject:',
115               )
116     %>
117
118     <TR>
119       <TD ALIGN="right" VALIGN="top">Message: </TD>
120       <TD><% include('/elements/htmlarea.html', 'field'=>'html_body') %></TD>
121     </TR>
122
123   </TABLE>
124
125 %#Substitution vars:
126
127     <INPUT TYPE="hidden" NAME="action" VALUE="preview">
128     <INPUT TYPE="submit" VALUE="Preview notice">
129
130 % }
131
132 </FORM>
133
134 % if ( $cgi->param('action') eq 'send' ) {
135     <SCRIPT TYPE="text/javascript">
136       process();
137     </SCRIPT>
138 % }
139
140 <% include('/elements/footer.html') %>
141
142 <%init>
143
144 die "access denied"
145   unless $FS::CurrentUser::CurrentUser->access_right('Bulk send customer notices');
146
147 my $table = $cgi->param('table') or die "'table' required";
148 my %search;
149 if ( $cgi->param('search') ) {
150   %search = %{ thaw(decode_base64($cgi->param('search'))) };
151 }
152 else {
153   %search = $cgi->Vars;
154   delete $search{$_} for qw( action table from subject html_body text_body );
155   # FS::$table->search is expected to know which parameters might be 
156   # multi-valued, and to accept scalar values for them also.  No good 
157   # solution to this since CGI can't tell whether a parameter _might_
158   # have had multiple values, only whether it does.
159   @search{keys %search} = map { /\0/ ? [ split /\0/, $_ ] : $_ } values %search;
160
161
162 my $title = 'Send bulk customer notices';
163
164 my $num_cust;
165 my $from = $cgi->param('from') || '';
166 my $subject = $cgi->param('subject') || '';
167 my $html_body = $cgi->param('html_body') || '';
168
169 my $msg_template = '';
170
171 if ( $cgi->param('action') eq 'preview' ) {
172   my $sql_query = "FS::$table"->search(\%search);
173   my $count_query = delete($sql_query->{'count_query'});
174   my $count_sth = dbh->prepare($count_query)
175     or die "Error preparing $count_query: ". dbh->errstr;
176   $count_sth->execute
177     or die "Error executing $count_query: ". $count_sth->errstr;
178   my $count_arrayref = $count_sth->fetchrow_arrayref;
179   $num_cust = $count_arrayref->[0];
180
181   if ( $cgi->param('msgnum') ) {
182     $msg_template = qsearchs('msg_template', 
183                              { msgnum => $cgi->param('msgnum') } )
184         or die "template not found: ".$cgi->param('msgnum');
185     $sql_query->{'extra_sql'} .= ' LIMIT 1';
186     $sql_query->{'order_by'} = '';
187     my $cust = qsearchs($sql_query)->cust_main;
188     my %message = $msg_template->prepare( 'cust_main' => $cust );
189     ($from, $subject, $html_body) = @message{'from', 'subject', 'html_body'};
190   }
191 }
192
193 </%init>