RT#21463: Option to show disabled package definitions [does not work on IE]
[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', #or method if you pass an order_by
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     'presorted'      => 0, #set true to disable sorting the records on name_col
32
33     #instead of the primary key... only for special cases
34     'value_col'      => 'columnname',
35
36     #basic params controlling the resulting <SELECT>
37     'pre_options'    => [ 'value' => 'option' ], #before normal options
38     'post_options'   => [ 'value' => 'option' ], #after normal options
39     'empty_label'    => '', #better specify it though, the default might change
40     'multiple'       => 0, # bool
41     'all_selected'   => 0, # useful with multiple
42     'disable_empty'  => 0, # bool (implied by multiple)
43     'label_showkey'  => 0, # bool
44     'label_callback' => sub { my $record = shift; return "label"; },
45
46     #more params controlling HTML stuff about the <SELECT>
47     'element_name'   => '', #HTML element name, defaults to the name of
48                             # the primary key column
49     'field'          => '', #synonym for element_name
50     'element_etc'    => '', #additional attributes (i.e. "DISABLED") for the
51                             #<SELECT> element
52     'onchange'       => '', #javascript code
53
54     #params controlling the <OPTION>s
55     'extra_option_attributes' => [ 'field' ], #field or method in $table objects
56                                               #(are prefixed w/data- per HTML5)
57     'hidden_sub' => sub { my $rec = shift; $rec->disabled; }, #sets option hidden att if true
58                                                               #won't hide selected options
59                                                               #hidden att not supported IE < 11
60
61     #special return options
62     'js_only'      => 0, #set true to return only the JS portions (i.e. nothing)
63     'html_only'    => 0, #set true to return only the HTML portions (no-op, i.e. return everything)
64
65     #debugging
66     'debug'          => 0, #set true to enable
67
68   )
69
70 </%doc>
71 % unless ( $opt{'js_only'} ) {
72
73 <SELECT <% $opt{'multiple'} ? 'MULTIPLE' : '' %>
74         NAME = "<% $opt{'element_name'} || $opt{'field'} || $key %>"
75         ID   = "<% $opt{'id'} || $key %>"
76         <% $onchange %>
77         <% $size %>
78         <% $opt{'element_etc'} %>
79 >
80
81 % while ( @pre_options ) { 
82 %   my $pre_opt   = shift(@pre_options);
83 %   my $pre_label = shift(@pre_options);
84 %   my $selected = $opt{'all_selected'}
85 %                   || ( ref($value) && $value->{$pre_opt} )
86 %                   || ( $value eq $pre_opt );
87     <OPTION VALUE="<% $pre_opt %>"
88             <% $selected ? 'SELECTED' : '' %>
89     ><% $pre_label %>
90 % } 
91
92 % unless ( $opt{'multiple'} || $opt{'disable_empty'} ) {
93     <OPTION VALUE=""><% $opt{'empty_label'} || 'all' %>
94 % }
95
96 % foreach my $record ( 
97 % # we have the order_by parameter for this
98 % #sort {    $a->$name_col() cmp $b->$name_col()
99 % #                           || $a->$key()      <=> $b->$key()
100 % #                         }
101 %                          @records
102 %                    )
103 % {
104 %   my $recvalue = $record->$key();
105 %   my $selected;
106 %   if ( $opt{'all_selected'} ) {
107 %     $selected = 1;
108 %   } elsif ( $opt{'compare_sub'} && !ref($value) ) {
109 %     $selected = &{ $opt{'compare_sub'} }( $value, $recvalue );
110 %   } else {
111 %     $selected =    ( ref($value) && $value->{$recvalue} )
112 %                 || ( $value && $value eq $recvalue ); #not == because of value_col
113 %   }
114 %   $opt{'hidden_sub'} ||= sub { 0 };
115     <OPTION VALUE="<% $recvalue %>"
116             <% $selected ? ' SELECTED' : '' %>
117             <% (&{$opt{'hidden_sub'}}($record) && !$selected) ? ' hidden' : '' %>
118 %           foreach my $att ( @{ $opt{'extra_option_attributes'} } ) {
119               data-<% $att %>="<% $record->$att() |h %>"
120 %           }
121     ><% $opt{'label_showkey'} ? "$recvalue: " : '' %>
122      <% $opt{'label_callback'}
123           ? &{ $opt{'label_callback'} }( $record )
124           : $record->$name_col()
125         |h
126      %>
127 % } 
128
129 % while ( @post_options ) { 
130 %   my $post_opt   = shift(@post_options);
131 %   my $post_label = shift(@post_options);
132 %   my $selected =    ( ref($value) && $value->{$post_opt} )
133 %                  || ( $value eq $post_opt );
134     <OPTION VALUE="<% $post_opt %>"
135             <% $selected ? 'SELECTED' : '' %>
136     ><% $post_label %>
137 % } 
138
139 </SELECT>
140
141 %}
142 <%init>
143
144 my( %opt ) = @_;
145
146 warn "elements/select-table.html: \n". Dumper(\%opt)
147   if exists $opt{debug} && $opt{debug};
148
149 $opt{'extra_option_attributes'} ||= [];
150
151 my $onchange = '';
152 if ( $opt{'onchange'} ) {
153   $onchange = $opt{'onchange'};
154   $onchange .= '(this)' unless $onchange =~ /\(\w*\);?$/;
155   $onchange =~ s/\(what\);/\(this\);/g; #ugh, terrible hack.  all onchange
156                                         #callbacks should act the same
157   $onchange = 'onChange="'. $onchange. '"';
158 }
159
160 my $dbdef_table = dbdef->table($opt{'table'})
161   or die "can't find dbdef for ". $opt{'table'}. " table\n";
162
163 my $key = $opt{'value_col'} || $dbdef_table->primary_key;
164
165 my $name_col = $opt{'name_col'};
166
167 my $value = $opt{'curr_value'} || $opt{'value'};
168 $value = [ split(/\s*,\s*/, $value) ] if $opt{'multiple'} && $value =~ /,/;
169
170 #my $addl_from = $opt{'addl_from'} || '';
171 my $extra_sql = $opt{'extra_sql'} || '';
172 my $hashref   = $opt{'hashref'} || {};
173
174 if ( $opt{'agent_virt'} ) {
175   $extra_sql .=
176     ( $extra_sql =~ /WHERE/i || scalar(keys %$hashref ) ? ' AND ' : ' WHERE ' ).
177     $FS::CurrentUser::CurrentUser->agentnums_sql(
178                                     'null'       => $opt{'agent_null'},
179                                     'null_right' => $opt{'agent_null_right'},
180                                    );
181 }
182
183 my @records = ();
184 if ( $opt{'records'} ) {
185   @records = @{ $opt{'records'} };
186   @records = sort { $a->get($name_col) cmp $b->get($name_col) } @records
187     unless $opt{'presorted'};
188 } else {
189   @records = qsearch( {
190     'table'     => $opt{'table'},
191     'addl_from' => $opt{'addl_from'},
192     'hashref'   => $hashref,
193     'extra_sql' => $extra_sql,
194     'order_by'  => ( $opt{'order_by'} || "ORDER BY $name_col" ),
195   });
196 }
197
198 if ( ref( $value ) eq 'ARRAY' ) {
199   $value = { map { $_ => 1 } @$value };
200 }
201
202 unless (    !ref($value) && $value < 1 # !$value #ignore negatives too
203          or ! exists( $opt{hashref}->{disabled} ) #??
204          #or grep { $value == $_->$key() } @records
205        ) {
206   delete $opt{hashref}->{disabled};
207
208   foreach my $v ( ref($value) ? keys %$value : ($value) ) {
209     next if grep { $v == $_->$key() } @records;
210
211     $opt{hashref}->{$key} = $v;
212     my $record = qsearchs( {
213       'table'     => $opt{table},
214       'addl_from' => $opt{'addl_from'},
215       'hashref'   => $hashref,
216       'extra_sql' => $extra_sql,
217     });
218     push @records, $record if $record;
219
220   }
221 }
222
223 my @pre_options  = $opt{pre_options}  ? @{ $opt{pre_options} } : ();
224 my @post_options = $opt{post_options} ? @{ $opt{post_options} } : ();
225
226 my $size = $opt{'size'} ? 'SIZE=' . $opt{'size'} : '';
227
228 </%init>