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