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