4.x style
[freeside.git] / httemplate / edit / invoice_conf.html
1 <& elements/edit.html,
2     'body_etc'          => $body_etc,
3     'name_singular'     => 'invoice configuration',
4     'table'             => 'invoice_conf',
5     'viewall_dir'       => 'browse',
6     'fields'            => \@fields,
7     'labels'            => \%labels,
8     'new_callback'      => \&new_callback,
9     'edit_callback'     => \&edit_callback,
10     'error_callback'    => \&error_callback,
11     'html_init'         => \&html_init,
12     'html_table_bottom' => \&html_table_bottom,
13     'html_bottom'       => '</DIV>', # close tablebreak-tabs
14 &>
15 <%init>
16
17 my $curuser = $FS::CurrentUser::CurrentUser;
18
19 # ???
20 die "access denied"
21   unless $curuser->access_right([ 'Edit templates', 'Edit global templates' ]);
22
23 my $body_etc = '';
24 $body_etc = q!onload="document.getElementById('locale').onchange()"!
25   if $cgi->param('locale') eq 'new';
26
27 my $modenum = $cgi->param('modenum');
28 my $mode = $modenum ? qsearchs('invoice_mode', { modenum => $modenum }) : '';
29
30 my %textarea = (type => 'textarea', rows => 10, cols => 40);
31 my @fields = (
32       { field => 'modenum', type => 'hidden' },
33       { field => 'agentnum',
34         type  => 'select-agent',
35       },
36       { field => 'modename',  size=>60, },
37       { type  => 'tablebreak-tabs',
38         include_opt_callback => \&menubar_opt_callback,
39       },
40       { field => 'locale', type => 'hidden' },
41       { field => 'notice_name',   size=>60, },
42       { field => 'subject',       size=>60, },
43       { field => 'lpr',           size=>60, },
44
45       { type  => 'columnstart', aligned => 1 },
46       { type  => 'title', value => '<BR>' },
47       map ( { +{ type => 'justtitle', value => $_ } }
48         'Notes',
49         'Footer',
50         'Summary header',
51         'Return address',
52         'Watermark',
53         'Small footer',
54         'Enable coupon',
55       ),
56
57       { type  => 'columnnext' },
58       { type  => 'title', value => 'LaTeX' },
59       { field => 'latexnotes',          %textarea },
60       { field => 'latexfooter',         %textarea },
61       { field => 'latexsummary',        %textarea },
62       { field => 'latexreturnaddress',  %textarea },
63       { field => 'latexwatermark',      %textarea },
64       { field => 'latexsmallfooter',    %textarea },
65       { field => 'with_latexcoupon', type => 'checkbox', value => 'Y' },
66
67       { type  => 'columnnext' },
68       { type  => 'title', value => 'HTML' },
69       { field => 'htmlnotes',           %textarea }, #htmlarea?
70       { field => 'htmlfooter',          %textarea },
71       { field => 'htmlsummary',         %textarea },
72       { field => 'htmlreturnaddress',   %textarea },
73       { field => 'htmlwatermark',      %textarea },
74       # logo
75
76       { type  => 'columnend' },
77 );
78
79 my %labels = (
80   'confnum'             => 'Configuration',
81   'locale'              => 'Locale',
82   'agentnum'            => 'Agent',
83   'modename'            => 'Mode name',
84   'notice_name'         => 'Notice name',
85   'subject'             => 'Email Subject: header',
86   'lpr'                 => 'Alternate lpr command',
87
88   map { $_ => '' } (qw(
89     latexnotes
90     latexfooter
91     latexsummary
92     latexreturnaddress
93     latexwatermark
94     with_latexcoupon
95     latexsmallfooter
96     htmlnotes
97     htmlfooter
98     htmlsummary
99     htmlreturnaddress
100     htmlwatermark
101   ) ),
102
103 ); 
104
105 sub get_invoice_mode { # because we can't quite use agent_virt here
106   my $modenum = shift;
107   qsearchs({
108     'table'     => 'invoice_mode',
109     'hashref'   => { 'modenum' => $modenum },
110     'extra_sql' => ' AND '.
111       $FS::CurrentUser::CurrentUser->agentnums_sql(
112         'null_right' => 'Edit global templates',
113         'viewall_right' => 'Edit global templates' ),
114   });
115 };
116
117 sub error_callback {
118   my ($cgi, $object) = @_;
119   foreach (qw(modename agentnum)) {
120     $object->set($_, $cgi->param($_));
121   }
122   if ($object->confnum) {
123     return edit_callback(@_);
124   } else {
125     return new_callback(@_);
126   }
127 }
128
129 sub new_callback {
130   my ($cgi, $object, $fields_arrayref, $opt_hashref) = @_;
131   my $modenum;
132   if ( $cgi->param('locale') =~ /^(\w+)$/ ) {
133     $object->set('locale' => $1);
134   }
135
136   if ( $cgi->param('modenum') =~ /^(\d+)$/ ) {
137     $modenum = $1; # we're adding a locale to an existing mode
138     $object->set('modenum' => $modenum);
139     my $invoice_mode = get_invoice_mode($modenum)
140       or die "invoice mode $modenum not found";
141
142     $object->set('modename', $invoice_mode->modename);
143     $object->set('agentnum', $invoice_mode->agentnum);
144
145     # also, need to select a locale
146     # make a list of available locales
147     my %existing_locales = map { $_->locale }
148                           qsearch('invoice_conf', { modenum => $modenum });
149
150     my @locales = grep { !exists($existing_locales{$_}) } 
151                          FS::Conf->new->config('available-locales');
152     my %labels;
153     foreach (@locales) {
154       my %info = FS::Locales->locale_info($_);
155       $labels{$_} = $info{'label'};
156     }
157     unshift @locales, 'new';
158     $labels{'new'} = 'Select language';
159
160     # insert a field def
161     my $i = 0;
162     $i++ until ( $fields_arrayref->[$i]->{'field'} eq 'locale' );
163     my $locale_field = $fields_arrayref->[$i];
164
165     my $onchange_locale = "document.getElementById('submit').disabled = 
166     (this.options[this.selectedIndex].value == 'new');";
167
168     %$locale_field = (
169       field   => 'locale',
170       type    => 'select',
171       options => \@locales,
172       labels  => \%labels,
173       curr_value  => 'new',
174       onchange    => $onchange_locale,
175     );
176
177   } # otherwise it's a completely new mode, so the locale is default
178
179 }
180
181 sub edit_callback {
182   # massive false laziness with msg_template UI
183   my ($cgi, $object, $fields_arrayref, $opt_hashref) = @_;
184
185   # a little different here in that we treat the content object
186   # as "primary" (this is edit/invoice_conf.html, etc.)
187   # so all we need from the invoice_mode is its name
188   # (and agent identity)
189   my $modenum = $object->modenum;
190   my $invoice_mode = get_invoice_mode($modenum)
191     or die "invoice mode $modenum not found";
192   $object->set('modename', $invoice_mode->modename);
193   $object->set('agentnum', $invoice_mode->agentnum);
194 }
195
196 sub menubar_opt_callback {
197   my $object = shift;
198   my $modenum = $object->modenum or return; 
199   my (@tabs, @options, %labels);
200   my $display_new = 0;
201   my $selected = '';
202   foreach my $l ('', FS::Conf->new->config('available-locales')) {
203     my $invoice_conf =
204       qsearchs('invoice_conf', { modenum => $modenum, locale => $l });
205     if ( $invoice_conf ) {
206       my %info = FS::Locales->locale_info($l) if $l;
207       my $label = $info{'label'} || mt('Default');
208       push @tabs, $label, $invoice_conf->confnum;
209       $selected = $label if $object->locale eq $l;
210     }
211     else {
212       $display_new = 1; # there is at least one unused locale left
213     }
214   }
215   push @tabs, mt('New'), "modenum=$modenum;locale=new" if $display_new;
216   $selected = mt('New') if $object->locale eq 'new';
217   $selected ||= mt('Default');
218   (
219     'url_base' => $cgi->url() . '?',
220     'selected' => $selected,
221     'tabs'     => \@tabs
222   );
223 }
224
225 sub html_init {
226 q!
227 <STYLE>
228 .fstabcontainer th { vertical-align: middle; text-align: center }
229 </STYLE>
230 !
231 }
232
233 sub html_table_bottom {
234   my $object = shift;
235   my $locale = '';
236   my $modenum = '';
237
238   if ($object->locale =~ /^(\w+)$/) {
239     $locale = $1;
240   }
241   if ($object->modenum =~ /^(\d+)$/) {
242     $modenum = $1;
243   }
244   my $html;
245   my $show_delete = 1;
246   # don't allow the default locale to be removed unless it's the last one
247   # in the mode
248   $show_delete = 0 if (
249     $locale eq 'new' or
250     $modenum eq '' or
251     ($locale eq '' and
252      FS::invoice_conf->count("modenum = $modenum and locale is not null") > 0
253     )
254   );
255
256   if ( $show_delete ) {    
257     # set up a delete link
258     my $confnum = $object->confnum;
259     my $url = $p."misc/delete-invoice_conf.html?$confnum";
260     my $link = qq!<A HREF="javascript:areyousure('$url','Really delete this configuration?')">! .
261       'Delete this configuration' .
262       '</A>';
263     $html = qq!<TR><TD></TD>
264       <TD STYLE="font-style: italic; font-size: small">$link</TD></TR>
265       <SCRIPT TYPE="text/javascript">
266       function areyousure(url, message) {
267           if (confirm(message)) window.location.href = url;
268       }
269       </SCRIPT>
270       !;
271   }
272   $html;
273 }
274
275 </%init>