fix A/R report
[freeside.git] / httemplate / elements / tr-input-locale-text.html
1 <%doc>
2 Usage:
3
4 In edit/foo.html:
5
6 <& /elements/tr-input-locale-text.html,
7   cgi     => $cgi, # needed to preserve values in error redirect
8   object  => $record,
9   field   => 'myfield',
10   label   => 'My Field',
11 &>
12
13 And in edit/process/foo.html:
14 <& elements/process.html,
15   ...
16   process_locale => 'myfield',
17 &>
18
19 'object' needs to be an FS::Record subclass instance for a table that has
20 a '_msgcat' localization table. For a table "foo" where "foo.myfield"
21 contains some customer-visible label (in the default locale),
22 "foo_msgcat.myfield" contains the translation of that label for a customer
23 locale. The foreign key in foo_msgcat must have the same name as the primary
24 key of foo.
25
26 Currently only a single field can be localized this way; including this
27 element more than once in the form will lead to conflicts. This is how
28 it should work; if at some point we need to localize several fields of the
29 same record, we should modify this element to show multiple inputs for each
30 locale.
31
32 </%doc>
33 <%init>
34
35 my %opt = @_;
36 my $object = delete $opt{object};
37 my $field = delete $opt{field};
38
39 # identify our locales
40 my $conf = FS::Conf->new;
41 my $default_locale = $conf->config('locale') || 'en_';
42 my @locales = grep { ! /^$default_locale/ } $conf->config('available-locales');
43
44 my $label = delete $opt{label};
45 my %labels = map { $_ => "$label&mdash;".FS::Locales->description($_) }
46               @locales;
47 @locales = sort { $labels{$a} cmp $labels{$b} } @locales;
48 my %curr_values;
49
50 # where are the msgcat records?
51 my $msgcat_table = $object->table . '_msgcat';
52 my $msgcat_pkey = dbdef->table($msgcat_table)->primary_key;
53 my %msgcat_pkeyvals;
54
55 # find existing msgcat records, if any, and record their message values
56 # and pkeys
57 my $pkey = $object->primary_key;
58 my $pkeyval = $object->get($pkey);
59 if ($pkeyval) { # of course if this is a new record there won't be any
60   my @linked = qsearch($msgcat_table, { $pkey => $pkeyval });
61   foreach (@linked) {
62     $curr_values{ $_->locale } = $_->get( $field );
63     $msgcat_pkeyvals{ $_->locale } = $_->get( $msgcat_pkey );
64   }
65 }
66
67 # sticky-on-error the locale inputs
68 if( my $cgi = $opt{cgi} ) {
69   my $i = 0;
70   # they're named 'foomsgnum0_locale' and 'foomsgnum0_myfield'
71   while ( my $locale = $cgi->param($msgcat_pkey . $i . '_locale') ) {
72     my $value = $cgi->param($msgcat_pkey . $i . '_' . $field);
73     $curr_values{ $locale } = $value;
74     $i++;
75   }
76 }
77
78 # compat with tr-input-text for styling
79 my $cell_style = $opt{'cell_style'} ? 'STYLE="'. $opt{'cell_style'}. '"' : '';
80
81 my $colspan = $opt{'colspan'} ? 'COLSPAN="'.$opt{'colspan'}.'"' : '';
82
83
84 </%init>
85 % # pass through %opt on all of these to retain formatting
86 % # one tr, td, and input for the default locale
87 <& tr-input-text.html,
88   %opt,
89   'label' => $label,
90   'field' => $field
91 &>
92 % # and one for each of the others 
93 % my $i = 0;
94 % foreach my $locale (@locales) {
95 %   my $basename = $msgcat_pkey . $i;
96 %   my $lfield = $basename . '_' . $field;
97 <& tr-td-label.html,
98   %opt,
99   'id' => $lfield, # uniqueness
100   'label' => $labels{$locale}
101 &>
102   <TD <% $colspan %><% $cell_style %> ID="<% $lfield %>_input0">
103     <& hidden.html,
104       'field' => $basename,
105       'curr_value' => $msgcat_pkeyvals{$locale},
106       # will be empty if this is a new record and/or new locale, that's fine
107     &>
108     <& hidden.html,
109       'field' => $basename . '_locale',
110       'curr_value' => $locale,
111     &>
112     <& input-text.html,
113       %opt,
114       'field' => $lfield,
115       'curr_value' => $curr_values{$locale},
116     &>
117   </TD>
118 </TR>
119 %   $i++;
120 % } # foreach $locale