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