package definition browse/search, filter by package class, RT#5255
[freeside.git] / httemplate / elements / select-table.html
1 <%doc>
2
3 Example:
4
5   include( '/elements/select-table.html',
6
7     ##
8     # required
9     ##
10     'table'          => 'table_name',
11     'name_col'       => 'name_column',
12    
13     #strongly recommended (you want your forms to be "sticky" on errors, right?)
14     'curr_value'     => 'current_value',
15     #'value' => #deprecated form of 'curr_value',
16    
17     ##
18     # optional
19     ##
20
21     #search params
22     'hashref'          => {},
23     'addl_from'        => '',
24     'extra_sql'        => '',
25     'agent_virt'       => 0, #set true and make sure the result is JOINed to
26                              #something with agentnum (usually cust_main)
27     'agent_null'       => 0, #set true to always show un-agented entries
28     'agent_null_right' => '', #right to see un-agented entries
29     #or
30     'records'        => \@records, #instead of search params
31
32     #basic params controlling the resulting <SELECT>
33     'pre_options'    => [ 'value' => 'option' ], #before normal options
34     'empty_label'    => '', #better specify it though, the default might change
35     'multiple'       => 0, # bool
36     'disable_empty'  => 0, # bool (implied by multiple)
37     'label_callback' => sub { my $record = shift; return "label"; },
38
39     #more params controlling HTML stuff about the <SELECT>
40     'element_name'   => '', #HTML element name, defaults to the name of
41                             # the primary key column
42     'field'          => '', #synonym for element_name
43     'element_etc'    => '', #additional attributes (i.e. "DISABLED") for the
44                             #<SELECT> element
45     'onchange'       => '', #javascript code
46
47     #special return options
48     'js_only'      => 0, #set true to return only the JS portions (i.e. nothing)
49     'html_only'    => 0, #set true to return only the HTML portions (no-op, i.e. return everything)
50
51     #debugging
52     'debug'          => 0, #set true to enable
53
54   )
55
56 </%doc>
57 % unless ( $opt{'js_only'} ) {
58
59 <SELECT <% $opt{'multiple'} ? 'MULTIPLE' : '' %>
60         NAME = "<% $opt{'element_name'} || $opt{'field'} || $key %>"
61         ID   = "<% $opt{'id'} || $key %>"
62         <% $onchange %>
63         <% $opt{'element_etc'} %>
64 >
65
66 % while ( @pre_options ) { 
67 %   my $pre_opt   = shift(@pre_options);
68 %   my $pre_label = shift(@pre_options);
69 %   my $selected =    ( ref($value) && $value->{$pre_opt} )
70 %                  || ( $value eq $pre_opt );
71     <OPTION VALUE="<% $pre_opt %>"
72             <% $selected ? 'SELECTED' : '' %>
73     ><% $pre_label %>
74 % } 
75
76 % unless ( $opt{'multiple'} || $opt{'disable_empty'} ) {
77     <OPTION VALUE=""><% $opt{'empty_label'} || 'all' %>
78 % }
79
80 % foreach my $record ( sort { $a->$name_col() cmp $b->$name_col() } @records ) {
81 %   my $recvalue = $record->$key();
82     <OPTION VALUE="<% $recvalue %>"
83             <% ref($value) && $value->{$recvalue} || $value == $recvalue
84                ? ' SELECTED' : ''
85             %>
86     ><% $opt{'label_callback'}
87           ? &{ $opt{'label_callback'} }( $record )
88           : $record->$name_col()
89      %>
90 % } 
91
92 </SELECT>
93
94 %}
95 <%init>
96
97 my( %opt ) = @_;
98
99 warn "elements/select-table.html: \n". Dumper(%opt)
100   if exists $opt{debug} && $opt{debug};
101
102 my $onchange = '';
103 if ( $opt{'onchange'} ) {
104   $onchange = $opt{'onchange'};
105   $onchange .= '(this)' unless $onchange =~ /\(\w*\);?$/;
106   $onchange =~ s/\(what\);/\(this\);/g; #ugh, terrible hack.  all onchange
107                                         #callbacks should act the same
108   $onchange = 'onChange="'. $onchange. '"';
109 }
110
111 my $dbdef_table = dbdef->table($opt{'table'})
112   or die "can't find dbdef for ". $opt{'table'}. " table\n";
113
114 my $key = $dbdef_table->primary_key; #? $opt{'primary_key'} ||
115
116 my $name_col = $opt{'name_col'};
117
118 my $value = $opt{'curr_value'} || $opt{'value'};
119 $value = [ split(/\s*,\s*/, $value) ] if $opt{'multiple'} && $value =~ /,/;
120
121 #my $addl_from = $opt{'addl_from'} || '';
122 my $extra_sql = $opt{'extra_sql'} || '';
123 my $hashref   = $opt{'hashref'} || {};
124
125 if ( $opt{'agent_virt'} ) {
126   $extra_sql .=
127     ( $extra_sql =~ /WHERE/i || scalar(keys %$hashref ) ? ' AND ' : ' WHERE ' ).
128     $FS::CurrentUser::CurrentUser->agentnums_sql(
129                                     'null'       => $opt{'agent_null'},
130                                     'null_right' => $opt{'agent_null_right'},
131                                    );
132 }
133
134 my @records = ();
135 if ( $opt{'records'} ) {
136   @records = @{ $opt{'records'} };
137 } else {
138   @records = qsearch( {
139     'table'     => $opt{'table'},
140     'addl_from' => $opt{'addl_from'},
141     'hashref'   => $hashref,
142     'extra_sql' => $extra_sql,
143     'order_by'  => ( $opt{'order_by'} || "ORDER BY $name_col" ),
144   });
145 }
146
147 unless (    $value < 1 # !$value #ignore negatives too
148          or ref($value)
149          or ! exists( $opt{hashref}->{disabled} ) #??
150          or grep { $value == $_->$key() } @records
151        ) {
152   delete $opt{hashref}->{disabled};
153   $opt{hashref}->{$key} = $value;
154   my $record = qsearchs( {
155     'table'     => $opt{table},
156     'addl_from' => $opt{'addl_from'},
157     'hashref'   => $hashref,
158     'extra_sql' => $extra_sql,
159   });
160   push @records, $record if $record;
161 }
162
163 if ( ref( $value ) eq 'ARRAY' ) {
164   $value = { map { $_ => 1 } @$value };
165 }
166
167 my @pre_options = $opt{pre_options} ? @{ $opt{pre_options} } : ();
168
169 </%init>