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