summaryrefslogtreecommitdiff
path: root/httemplate/elements/tr-select-table.html
blob: 2e62d871749f2254f08ca15f570d4a67b297eb2d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<%doc>

Example:

  <& /elements/tr-select-table,

       'add_inline' => 0, #enable "on the fly" adding
 
       #options passed to tr-td-label.html and select-$table.html (or
       #select-table.html if that does not exist)
  &>

</%doc>
% unless ( $opt{'js_only'} ) {

    <% include('tr-td-label.html', @_ ) %>

      <TD <% $style %>>
% }

% my $element = '/elements/select-'.$opt{table}.'.html';
% if ( $m->interp->comp_exists($element) ) {
        <& $element, %opt &>
% } else {
        <& /elements/select-table.html, %opt &>
% }

% unless ( $opt{'js_only'} ) {
      </TD>
    </TR>
% }

% if ( $opt{'add_inline'} ) {

%   foreach my $add_field ( @{ $opt{'add_fields'} } ) {
%     my $label = scalar(@{ $opt{'add_fields'} }) > 1
%                   ? $add_field : $opt{'label'};

      <& /elements/tr-input-text.html,
           'label'   => "New $label",
           'field'   => $field."_$add_field",
           'id'      => $field."_$add_field",
           'colspan' => $opt{'colspan'},
      &>

%   }


%   unless ( $opt{'html_only'} ) {

      <SCRIPT TYPE="text/javascript">

%       my $ge = 'document.getElementById';

        function <% $field %>_changed(what) {

          var value = what.options[what.selectedIndex].value;

          var display = 'none';
          var visibility = 'hidden';
          if ( value == '-1' && ! what.disabled ) {
            display = '';
            visibility = '';
          }

%         foreach my $add_field ( @{ $opt{'add_fields'} } ) {
%           my $ge_f = "$ge('${field}_$add_field";
            <% $ge_f %>_label0').style.display    = display;
            <% $ge_f %>_label0').style.visibility = visibility;
            <% $ge_f %>_input0').style.display    = display;
            <% $ge_f %>_input0').style.visibility = visibility;
%         }

        }

        <% $field %>_changed(<% $ge %>('<% $field %>'));

      </SCRIPT>

%   }

% }

<%init>

my( %opt ) = @_;

my $style = $opt{'cell_style'} ? 'STYLE="'. $opt{'cell_style'}. '"' : '';

my $curuser = $FS::CurrentUser::CurrentUser;

my $field = '';
if ( $opt{'add_inline'} && $curuser->access_right($opt{'add_right'}) ) {

  push @{ $opt{'post_options'} }, '-1' => 'Add new '. lc($opt{'label'});

  $field = $opt{'field'} || dbdef->table($opt{'table'})->primary_key;

  my $onchange = ( $opt{'onchange'} ? delete($opt{'onchange'}).';' : '' ).
                 $field.'_changed(this);';
  $opt{'onchange'} = $onchange;

}

</%init>