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