Merge branch 'master' of git.freeside.biz:/home/git/freeside
[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   ],
213   'contact' => [ # duplicate this for shipping
214     '$name'           => 'Company and contact name',
215     '$name_short'     => 'Company or contact name',
216     '$company'        => 'Company name',
217     '$contact'        => 'Contact name (last, first)',
218     '$contact_firstlast'=> 'Contact name (first last)',
219     '$first'          => 'First name',
220     '$last'           => 'Last name',
221     '$address1'       => 'Address line 1',
222     '$address2'       => 'Address line 2',
223     '$city'           => 'City',
224     '$county'         => 'County',
225     '$state'          => 'State',
226     '$zip'            => 'Zip',
227     '$country'        => 'Country',
228     '$daytime'        => 'Day phone',
229     '$night'          => 'Night phone',
230     '$mobile'         => 'Mobile phone',
231     '$fax'            => 'Fax',
232   ],
233   'service' => [
234     '$ship_address1'  => 'Address line 1',
235     '$ship_address2'  => 'Address line 2',
236     '$ship_city'      => 'City',
237     '$ship_county'    => 'County',
238     '$ship_state'     => 'State',
239     '$ship_zip'       => 'Zip',
240     '$ship_country'   => 'Country',
241   ],
242   'cust_bill' => [
243     '$invnum'         => 'Invoice#',
244   ],
245   'cust_pkg' => [
246     '$pkgnum'         => 'Package#',
247     '$pkg'            => 'Package description',
248     '$pkg_label'      => 'Description + comment',
249     '$status'         => 'Status',
250     '$statuscolor'    => 'Status color code',
251     '$start_ymd'      => 'Start date',
252     '$setup_ymd'      => 'Setup date',
253     '$last_bill_ymd'  => 'Last bill date',
254     '$next_bill_ymd'  => 'Next bill date',
255     '$susp_ymd'       => 'Suspended on date',
256     '$cancel_ymd'     => 'Canceled on date',
257     '$adjourn_ymd'    => 'Adjournment date',
258     '$expire_ymd'     => 'Expiration date',
259     '$labels_short'   => 'Service labels',
260     '$location_label' => 'Service location',
261   ],
262   'svc_acct'  => [
263     '$svcnum'         => 'Service#',
264     '$username'       => 'Login name',
265     '$password'       => 'Password',
266     '$domain'         => 'Domain name',
267   ],
268   'svc_domain' => [
269     '$svcnum'         => 'Service#',
270     '$domain'         => 'Domain name',
271     '$registrar'      => 'Registrar name',
272     '$catchall'       => 'Catchall email',
273   ],
274   'svc_phone' => [
275     '$svcnum'         => 'Service#',
276     '$phonenum'       => 'Phone number',
277     '$countrycode'    => 'Country code',
278     '$domain'         => 'Domain name'
279   ],
280   'svc_broadband' => [
281     '$svcnum'         => 'Service#',
282     '$ip_addr'        => 'IP address',
283     '$mac_addr'       => 'MAC address',
284     '$speed_up'       => 'Upstream speed',
285     '$speed_down'     => 'Downstream speed',
286   ],
287   'cust_pay'  => [
288     '$paynum'         => 'Payment#',
289     '$paid'           => 'Amount',
290     '$payby'          => 'Payment method',
291     '$date'           => 'Payment date',
292     '$payinfo'        => 'Card/account# (masked)',
293     '$error'          => 'Decline reason',
294   ],
295 );
296
297 tie my %sections, 'Tie::IxHash', (
298 'contact'   => 'Name and contact info (billing)',
299 'service'   => 'Service address',
300 'cust_main' => 'Customer status and payment info',
301 'cust_pkg'  => 'Package fields',
302 'cust_bill' => 'Invoice fields',
303 'cust_pay'  => 'Payment fields',
304 'svc_acct'  => 'Login service fields',
305 'svc_domain'=> 'Domain service fields',
306 'svc_phone' => 'Phone service fields',
307 'svc_broadband' => 'Broadband service fields',
308 );
309
310 my $widget = new HTML::Widgets::SelectLayers(
311   'options'   => \%sections,
312   'form_name' => 'dummy',
313   'html_between'=>'</FORM><FONT SIZE=-1>',
314   'selected_layer'=>(keys(%sections))[0],
315   'layer_callback' => sub {
316     my $section = shift;
317     my $html = include('/elements/table-grid.html');
318     my @hints = @{ $substitutions{$section} };
319     while(@hints) {
320       my $key = shift @hints;
321       $html .= qq!\n<TR><TD><A href="javascript:insertHtml('{$key}')">$key</A></TD>!;
322       $html .= "\n<TD>".shift(@hints).'</TD></TR>';
323     }
324     $html .= "\n</TABLE>";
325     return $html;
326   },
327 );
328
329 my $sidebar = '
330 <SCRIPT TYPE="text/javascript">
331 function insertHtml(what) {
332   var oEditor = CKEDITOR.instances["body"];
333   oEditor.insertHtml(what);
334 };
335
336 function areyousure(url, message) {
337   if (confirm(message))
338     window.location.href = url;
339 }
340 </SCRIPT>
341 <TD valign="top"><FORM name="dummy">
342 Substitutions: '
343 . $widget->html .
344 '<BR>Click links to insert.
345 <BR>Enclose substitutions and other Perl expressions in braces:
346 <BR>{ $name } = ExampleCo (Smith, John)
347 <BR>{ time2str("%D", time) } = '.time2str("%D", time).'
348 </FONT></TD>
349 ';
350
351 sub html_table_bottom {
352   my $object = shift;
353   $cgi->param('locale') =~ /^(\w+)$/;
354   my $locale = $1;
355   my $html;
356   if ( $locale and $locale ne 'new' ) {
357     # set up a delete link
358     my $msgnum = $object->msgnum;
359     my $url = $p."misc/delete-template_content.html?msgnum=$msgnum;locale=$1";
360     my $link = qq!<A HREF="javascript:areyousure('$url','Really delete this template?')">! .
361       'Delete this template' .
362       '</A>';
363     $html = qq!<TR><TD></TD>
364       <TD STYLE="font-style: italic; font-size: small">$link</TD></TR>!;
365   }
366   $html;
367 }
368
369 </%init>