RT#36806: Add message template substitution to show last four digits of credit card...
[freeside.git] / httemplate / edit / msg_template.html
1 <& elements/edit.html,
2      'html_init'        => '<TABLE id="outerTable"><TR><TD>',
3      'body_etc'         => $body_etc,
4      'name_singular'    => 'template',
5      'table'            => 'msg_template',
6      'viewall_dir'      => 'browse',
7      'agent_virt'       => 1,
8      'agent_null'       => 1,
9      'agent_null_right' => [ 'View global templates', 'Edit global templates' ],
10
11      'fields'           => \@fields,
12      'labels'           => { 
13                              'msgnum'    => 'Template',
14                              'agentnum'  => 'Agent',
15                              'msgname'   => 'Template name',
16                              'from_addr' => 'From: ',
17                              'bcc_addr'  => 'Bcc: ',
18                              'locale'    => 'Locale',
19                              'subject'   => 'Subject: ',
20                              'body'      => 'Message body',
21                            },
22      'edit_callback'    => \&edit_callback,
23      'error_callback'   => \&edit_callback,
24      'html_bottom'      => '</DIV>',
25      'html_table_bottom'=> \&html_table_bottom,
26      'html_foot'        => ( $no_submit ? '' : "</TD>$sidebar</TR></TABLE>" ),
27      'no_submit'        => $no_submit,
28 &>
29 <%init>
30
31 my $curuser = $FS::CurrentUser::CurrentUser;
32
33 die "access denied"
34   unless $curuser->access_right([ 'View templates', 'View global templates',
35                                   'Edit templates', 'Edit global templates',
36                                ]);
37
38 my $body_etc = '';
39 $body_etc = q!onload="document.getElementById('locale').onchange()"!
40   if $cgi->param('locale') eq 'new';
41
42 my $msgnum = $cgi->param('msgnum');
43 my $msg_template = $msgnum ? qsearchs('msg_template', {msgnum=>$msgnum}) : '';
44
45 my $no_submit = 0;
46 my @fields = ();
47 if ( $curuser->access_right('Edit global templates') 
48      || (    $curuser->access_right('Edit templates')
49           && $msg_template
50           && $msg_template->agentnum
51           && $curuser->agentnums_href->{$msg_template->agentnum}
52         )
53    )
54 {
55   push @fields,
56       { field => 'agentnum',
57         type  => 'select-agent',
58       },
59       { field => 'msgname',   size=>60, },
60       { field => 'from_addr', size=>60, },
61       { field => 'bcc_addr',  size=>60, },
62       { type  => 'tablebreak-tabs',
63         include_opt_callback => \&menubar_opt_callback,
64       },
65       # template_content fields
66       { field => 'locale', type => 'hidden' },
67       { field => 'subject',   size=>60, },
68       { field => 'body',
69         type  => 'htmlarea',
70         width => 763,
71         config=> { extraPlugins => 'blockprotect' },
72       },
73   ;
74 } else { #readonly
75
76   $no_submit = 1;
77
78   push @fields,
79       { field => 'agentnum',
80         type  => 'select-agent',
81         fixed => 1,
82       },
83       { field => 'msgname',   type => 'fixed', },
84       { field => 'from_addr', type => 'fixed', },
85       { field => 'bcc_addr',  type => 'fixed', },
86       { type  => 'tablebreak-tabs',
87         include_opt_callback => \&menubar_opt_callback,
88       },
89       # template_content fields
90       { field => 'locale',  type => 'hidden' },
91       { field => 'subject', type => 'fixed', },
92       { field    => 'body',
93         type     => 'fixed',
94         noescape => 1,
95       },
96   ;
97
98 }
99
100 sub new_callback {
101   my ($cgi, $object, $fields_listref, $opt_hashref) = @_;
102   my $template_content = new FS::template_content { 'locale' => '' };
103   $object->{'Hash'} = { $object->hash, $template_content->hash };
104 }
105
106 sub edit_callback {
107   my ($cgi, $object, $fields_listref, $opt_hashref) = @_;
108   $cgi->param('locale') =~ /^(\w*)$/ or die 'bad locale '.$cgi->param('locale');
109   my $locale = $1;
110
111   # fetch the content object and merge its fields
112   my %args = (
113     'msgnum' => $object->msgnum,
114     'locale' => $locale
115   );
116   my $template_content = qsearchs('template_content', \%args) 
117                         || new FS::template_content( { %args });
118   $object->{'Hash'} = { $object->hash, $template_content->hash };
119
120   # set up the locale selector if this is a new content
121   if ( $locale eq 'new' ) {
122
123     # make a list of available locales
124     my $content_locales = $object->content_locales;
125     my @locales = grep { !exists($content_locales->{$_}) } 
126                          FS::Conf->new->config('available-locales');
127     my %labels;
128     foreach (@locales) {
129       my %info = FS::Locales->locale_info($_);
130       $labels{$_} = $info{'label'};
131     }
132     unshift @locales, 'new';
133     $labels{'new'} = 'Select language';
134
135     # insert a field def
136     my $i = 0;
137     $i++ until ( $fields_listref->[$i]->{'field'} eq 'locale' );
138     my $locale_field = $fields_listref->[$i];
139
140     my $onchange_locale = "document.getElementById('submit').disabled = 
141     (this.options[this.selectedIndex].value == 'new');";
142
143     %$locale_field = (
144       field   => 'locale',
145       type    => 'select',
146       options => \@locales,
147       labels  => \%labels,
148       curr_value  => 'new',
149       onchange    => $onchange_locale,
150     );
151   }
152 }
153
154 sub menubar_opt_callback {
155   my $object = shift;
156   # generate no tabs for new msg_templates.
157   my $msgnum = $object->msgnum or return; 
158   my (@tabs, @options, %labels);
159   push @tabs, mt('Default'), '';
160   my $display_new = 0;
161   my $selected = '';
162   foreach my $l (FS::Locales->locales) {
163     if ( exists $object->content_locales->{$l} ) {
164       my %info = FS::Locales->locale_info($l);
165       push @tabs, 
166            $info{'label'},
167            ';locale='.$l;
168       $selected = $info{'label'} if $object->locale eq $l;
169     }
170     else {
171       $display_new = 1; # there is at least one unused locale left
172     }
173   }
174   push @tabs, mt('New'), ';locale=new' if $display_new;
175   $selected = mt('New') if $object->locale eq 'new';
176   $selected ||= mt('Default');
177   (
178     'url_base' => $p.'edit/msg_template.html?msgnum='.$msgnum,
179     'selected' => $selected,
180     'tabs'     => \@tabs
181   );
182 }
183
184 my $onchange_locale = '';
185
186 # Create hints pane
187
188 my %substitutions = (
189   'cust_main' => [
190     '$display_custnum'=> 'Customer#',
191     '$agentnum'       => 'Agent#',
192     '$agent_name'     => 'Agent name',
193     '$payby'          => 'Payment method',
194     '$paymask'        => 'Card/account# (masked)',
195     '$payname'        => 'Name on card/bank name',
196     '$paytype'        => 'Account type',
197     '$payip'          => 'IP address used to submit payment info',
198     '$num_ncancelled_pkgs'  => '# of active packages',
199     '$num_cancelled_pkgs'   => '# of cancelled packages',
200     '$num_pkgs'       => '# of packages',
201     '$classname'      => 'Customer class',
202     '$categoryname'   => 'Customer category',
203     '$balance'        => 'Current balance',
204     '$credit_limit'   => 'Credit limit',
205     '$invoicing_list_emailonly' => 'Billing email address',
206     '$cust_status'    => 'Status',
207     '$ucfirst_cust_status'  => 'Status, capitalized',
208     '$cust_statuscolor'     => 'Status color code',
209     '$company_name'   => 'Our company name',
210     '$company_address'=> 'Our company address',
211     '$company_phonenum' => 'Our phone number',
212     '$selfservice_server_base_url' => 'Base URL of customer self-service',
213   ],
214   'contact' => [ # duplicate this for shipping
215     '$name'           => 'Company and contact name',
216     '$name_short'     => 'Company or contact name',
217     '$company'        => 'Company name',
218     '$contact'        => 'Contact name (last, first)',
219     '$contact_firstlast'=> 'Contact name (first last)',
220     '$first'          => 'First name',
221     '$last'           => 'Last name',
222     '$address1'       => 'Address line 1',
223     '$address2'       => 'Address line 2',
224     '$city'           => 'City',
225     '$county'         => 'County',
226     '$state'          => 'State',
227     '$zip'            => 'Zip',
228     '$country'        => 'Country',
229     '$daytime'        => 'Day phone',
230     '$night'          => 'Night phone',
231     '$mobile'         => 'Mobile phone',
232     '$fax'            => 'Fax',
233   ],
234   'service' => [
235     '$ship_address1'  => 'Address line 1',
236     '$ship_address2'  => 'Address line 2',
237     '$ship_city'      => 'City',
238     '$ship_county'    => 'County',
239     '$ship_state'     => 'State',
240     '$ship_zip'       => 'Zip',
241     '$ship_country'   => 'Country',
242   ],
243   'cust_bill' => [
244     '$invnum'         => 'Invoice#',
245     '$_date_pretty'   => 'Invoice date',
246     '$due_date'       => 'Invoice due date (timestamp)',
247     '$due_date2str'   => 'Invoice due date (human readable)',
248   ],
249   'cust_pkg' => [
250     '$pkgnum'         => 'Package#',
251     '$pkg'            => 'Package description',
252     '$pkg_label'      => 'Description + comment',
253     '$status'         => 'Status',
254     '$statuscolor'    => 'Status color code',
255     '$start_ymd'      => 'Start date',
256     '$setup_ymd'      => 'Setup date',
257     '$last_bill_ymd'  => 'Last bill date',
258     '$next_bill_ymd'  => 'Next bill date',
259     '$susp_ymd'       => 'Suspended on date',
260     '$cancel_ymd'     => 'Canceled on date',
261     '$adjourn_ymd'    => 'Adjournment date',
262     '$expire_ymd'     => 'Expiration date',
263     '$labels_short'   => 'Service labels',
264     '$location_label' => 'Service location',
265   ],
266   'svc_acct'  => [
267     '$svcnum'         => 'Service#',
268     '$username'       => 'Login name',
269     '$password'       => 'Password',
270     '$domain'         => 'Domain name',
271   ],
272   'svc_domain' => [
273     '$svcnum'         => 'Service#',
274     '$domain'         => 'Domain name',
275     '$registrar'      => 'Registrar name',
276     '$catchall'       => 'Catchall email',
277   ],
278   'svc_phone' => [
279     '$svcnum'         => 'Service#',
280     '$phonenum'       => 'Phone number',
281     '$countrycode'    => 'Country code',
282     '$domain'         => 'Domain name'
283   ],
284   'svc_broadband' => [
285     '$svcnum'         => 'Service#',
286     '$ip_addr'        => 'IP address',
287     '$mac_addr'       => 'MAC address',
288     '$speed_up'       => 'Upstream speed',
289     '$speed_down'     => 'Downstream speed',
290   ],
291   'cust_pay'  => [
292     '$paynum'         => 'Payment#',
293     '$paid'           => 'Amount',
294     '$payby'          => 'Payment method',
295     '$date'           => 'Payment date',
296     '$payinfo'        => 'Card/account# (masked)',
297     '$payinfo_end'    => 'Card/account last 4 digits',
298     '$error'          => 'Decline reason',
299   ],
300   'cust_refund'  => [
301     '$refundnum'      => 'Refund#',
302     '$refund'         => 'Refund Amount',
303     '$payby'          => 'Refund method',
304     '$date'           => 'Refund date',
305     '$payinfo'        => 'Card/account# (masked)',
306     '$payinfo_end'    => 'Card/account last 4 digits',
307   ],
308 );
309
310 tie my %sections, 'Tie::IxHash', (
311 'contact'   => 'Name and contact info (billing)',
312 'service'   => 'Service address',
313 'cust_main' => 'Customer status and payment info',
314 'cust_pkg'  => 'Package fields',
315 'cust_refund' => 'Refund fields',
316 'cust_bill' => 'Invoice fields',
317 'cust_pay'  => 'Payment fields',
318 'svc_acct'  => 'Login service fields',
319 'svc_domain'=> 'Domain service fields',
320 'svc_phone' => 'Phone service fields',
321 'svc_broadband' => 'Broadband service fields',
322 );
323
324 my $widget = new HTML::Widgets::SelectLayers(
325   'options'   => \%sections,
326   'form_name' => 'dummy',
327   'html_between'=>'</FORM><FONT SIZE=-1>',
328   'selected_layer'=>(keys(%sections))[0],
329   'layer_callback' => sub {
330     my $section = shift;
331     my $html = include('/elements/table-grid.html');
332     my @hints = @{ $substitutions{$section} };
333     while(@hints) {
334       my $key = shift @hints;
335       $html .= qq!\n<TR><TD><A href="javascript:insertHtml('{$key}')">$key</A></TD>!;
336       $html .= "\n<TD>".shift(@hints).'</TD></TR>';
337     }
338     $html .= "\n</TABLE>";
339     return $html;
340   },
341 );
342
343 my $sidebar = '
344 <SCRIPT TYPE="text/javascript">
345 function insertHtml(what) {
346   var oEditor = CKEDITOR.instances["body"];
347   oEditor.insertHtml(what);
348 };
349
350 function areyousure(url, message) {
351   if (confirm(message))
352     window.location.href = url;
353 }
354 </SCRIPT>
355 <TD valign="top"><FORM name="dummy">
356 Substitutions: '
357 . $widget->html .
358 '<BR>Click links to insert.
359 <BR>Enclose substitutions and other Perl expressions in braces:
360 <BR>{ $name } = ExampleCo (Smith, John)
361 <BR>{ time2str("%D", time) } = '.time2str("%D", time).'
362 </FONT></TD>
363 ';
364
365 sub html_table_bottom {
366   my $object = shift;
367   $cgi->param('locale') =~ /^(\w+)$/;
368   my $locale = $1;
369   my $html;
370   if ( $locale and $locale ne 'new' ) {
371     # set up a delete link
372     my $msgnum = $object->msgnum;
373     my $url = $p."misc/delete-template_content.html?msgnum=$msgnum;locale=$1";
374     my $link = qq!<A HREF="javascript:areyousure('$url','Really delete this template?')">! .
375       'Delete this template' .
376       '</A>';
377     $html = qq!<TR><TD></TD>
378       <TD STYLE="font-style: italic; font-size: small">$link</TD></TR>!;
379   }
380   $html;
381 }
382
383 </%init>