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