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