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