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