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