summaryrefslogtreecommitdiff
path: root/httemplate/elements/select-table.html
blob: d24c9abe4602b64bd576ce95002906733d997710 (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<%doc>

Example:

  include( '/elements/select-table.html',

    #required
    'table'          => 'table_name',
    'name_col'       => 'name_column',
   
    #strongly recommended (you want your forms to be "sticky" on errors, right?)
    'curr_value'     => 'current_value',
    #'value' => #deprecated form of 'curr_value',
   
    #opt
    'empty_label'    => '', #better specify it though, the default might change
    'hashref'        => {},
    'extra_sql'      => '',
    'records'        => \@records, #instead of hashref
    'pre_options'    => [ 'value' => 'option' ], #before normal options
    'element_name'   => '', #HTML element name, defaults to the name of
                            # the primary key column
    'field'          => '', #synonym for element_name
    'element_etc'    => '', #additional attributes (i.e. "DISABLED") for the
                            #<SELECT> element
    'onchange'       => '', #javascript code
    'multiple'       => 0, # bool
    'disable_empty'  => 0, # bool (implied by multiple)
    'debug'          => 0, #set true to enable
    'label_callback' => sub { my $record = shift; return "label"; },
    'js_only'      => 0, #set true to return only the JS portions (i.e. nothing)
    'html_only'    => 0, #set true to return only the HTML portions (no-op, i.e. return everything)
  )

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

<SELECT <% $opt{'multiple'} ? 'MULTIPLE' : '' %>
        NAME = "<% $opt{'element_name'} || $opt{'field'} || $key %>"
        ID   = "<% $opt{'id'} || $key %>"
        <% $onchange %>
        <% $opt{'element_etc'} %>
>

% while ( @pre_options ) { 
    <OPTION VALUE="<% shift(@pre_options) %>"><% shift(@pre_options) %>

% } 

% unless ( $opt{'multiple'} || $opt{'disable_empty'} ) {
    <OPTION VALUE=""><% $opt{'empty_label'} || 'all' %>
% }

% foreach my $record ( sort { $a->$name_col() cmp $b->$name_col() } @records ) {
%   my $recvalue = $record->$key();
    <OPTION VALUE="<% $recvalue %>"
            <% ref($value) && $value->{$recvalue} || $value == $recvalue
               ? ' SELECTED' : ''
            %>
    ><% $opt{'label_callback'}
          ? &{ $opt{'label_callback'} }( $record )
          : $record->$name_col()
     %>
% } 

</SELECT>

%}
<%init>

my( %opt ) = @_;

warn "elements/select-table.html: \n". Dumper(%opt)
  if exists $opt{debug} && $opt{debug};

my $onchange = '';
if ( $opt{'onchange'} ) {
  $onchange = $opt{'onchange'};
  $onchange .= '(this)' unless $onchange =~ /\(\w*\);?$/;
  $onchange =~ s/\(what\);/\(this\);/g; #ugh, terrible hack.  all onchange
                                        #callbacks should act the same
  $onchange = 'onChange="'. $onchange. '"';
}

my $dbdef_table = dbdef->table($opt{'table'})
  or die "can't find dbdef for ". $opt{'table'}. " table\n";

my $key = $dbdef_table->primary_key; #? $opt{'primary_key'} ||

my $name_col = $opt{'name_col'};

my $value = $opt{'curr_value'} || $opt{'value'};
$value = [ split(/\s*,\s*/, $value) ] if $opt{'multiple'} && $value =~ /,/;

my $extra_sql = $opt{'extra_sql'} || '';
my $hashref =   $opt{'hashref'} || {};

if ( $opt{'agent_virt'} ) {
  $extra_sql .=
    ( $extra_sql =~ /WHERE/i || scalar(keys %$hashref ) ? ' AND ' : ' WHERE ' ).
    $FS::CurrentUser::CurrentUser->agentnums_sql(
                                    'null_right' => $opt{'agent_null_right'}
                                   );
}

my @records = ();
if ( $opt{'records'} ) {
  @records = @{ $opt{'records'} };
} else {
  @records = qsearch( {
    'table'     => $opt{'table'},
    'hashref'   => $hashref,
    'extra_sql' => $extra_sql,
    'order_by'  => ( $opt{'order_by'} || "ORDER BY $name_col" ),
  });
}

unless (    ! $value
         or ref($value)
         or ! exists( $opt{hashref}->{disabled} ) #??
         or grep { $value == $_->$key() } @records
       ) {
  delete $opt{hashref}->{disabled};
  $opt{hashref}->{$key} = $value;
  my $record = qsearchs( {
    'table'     => $opt{table},
    'hashref'   => $hashref,
    'extra_sql' => ( $opt{extra_sql} || '' ),
  });
  push @records, $record if $record;
}

if ( ref( $value ) eq 'ARRAY' ) {
  $value = { map { $_ => 1 } @$value };
}

my @pre_options = $opt{pre_options} ? @{ $opt{pre_options} } : ();

</%init>