1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
|
<%doc>
=pod
=head1 NAME
fk_to_one_select
=head1 DESCRIPTION
Given a foreign key and an optional row, this component produces a select
form element for that relationship.
If a row is given, then its value will be used as the default value
for the form element.
=head1 PARAMETERS
=over 4
=item * fk (required)
An C<Alzabo::ForeignKey> object representing the relationship.
=item * row (optional)
An Alzabo row object.
=item * class (optional)
This defaults to C<<
$m->base_comp->attr_if_exists('fk_to_one_select_class_default') >>.
=item * foreign_rows (optional)
This allows you to pre-select the rows in the foreign table that will
be used for the select options. Otherwise, the component simple grabs
all the available rows from the foreign table.
=item * display_column (optional)
This is the column in the foreign table which should be used as the
visible value of each option. This defaults to the column involved in
the foreign key relationship.
=back
=cut
</%doc>
<select name="<% $col_from->name | h %>" class="<% $class %>">
% unless ( $fk->from_is_dependent ) {
<option value=""></option>
% }
% foreach my $item (@foreign_rows) {
<option value="<% $item->select( $col_to->name ) %>" <% defined $current && $item->select( $col_to->name ) == $current ? 'selected="selected"' : '' %>><% $item->select( $display_column->name ) | h %></option>
% }
</select>
<%args>
$fk
$row => undef
@foreign_rows => ()
$class => $m->base_comp->attr_if_exists('fk_to_one_select_class_default')
$display_column => undef
</%args>
<%init>
# I'm a wuss. Don't know how to handle this.
my @col_from = $fk->columns_from;
return if @col_from > 1;
my $col_from = shift @col_from;
my $col_to = $fk->columns_to;
@foreign_rows = $col_to->table->all_rows->all_rows unless @foreign_rows;
my $current;
$current = $row->select( $col_from->name ) if $row;
$display_column = $col_to unless defined $display_column;
</%init>
|