Ignore:
Timestamp:
Apr 18, 2012, 4:14:22 PM (4 years ago)
Author:
kake
Message:

Added a start at a form for indexing via locale+category.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk

    • Property svn:ignore
      •  

        old new  
        77cover_db
        88wiki.conf
         9META.yml
         10MYMETA.yml
         11META.json
         12MYMETA.json
  • trunk/lib/OpenGuides/CGI.pm

    r1328 r1355  
    55
    66use Carp qw( croak );
     7use CGI;
    78use CGI::Cookie;
    89
     
    436437}
    437438
     439=item B<make_index_form_dropdowns>
     440
     441    my @dropdowns = OpenGuides::CGI->make_index_form_dropdowns (
     442        guide    => $guide,
     443        selected => [
     444                      { type => "category", value => "pubs" },
     445                      { type => "locale", value => "holborn" },
     446                    ],
     447    );
     448    %tt_vars = ( %tt_vars, dropdowns => \@dropdowns );
     449
     450    # In the template
     451    [% FOREACH dropdown = dropdowns %]
     452      [% dropdown.type.ucfirst | html %]:
     453      [% dropdown.html %]
     454      <br />
     455    [% END %]
     456
     457Makes HTML dropdown selects suitable for passing to an indexing template.
     458
     459The C<selected> argument is optional; if supplied, it gives default values
     460for the dropdowns.  At least one category and one locale dropdown will be
     461returned; if no defaults are given for either then they'll default to
     462everything/everywhere.
     463
     464=cut
     465
     466sub make_index_form_dropdowns {
     467    my ( $class, %args ) = @_;
     468    my @selected = @{$args{selected} || [] };
     469    my $guide = $args{guide};
     470    my @dropdowns;
     471    my ( $got_cat, $got_loc );
     472    foreach my $criterion ( @selected ) {
     473        my $type = $criterion->{type} || "";
     474        my $value = $criterion->{value} || "";
     475        my $html;
     476        if ( $type eq "category" ) {
     477            $html = $class->_make_dropdown_html(
     478                        %$criterion, guide => $guide );
     479            $got_cat = 1;
     480        } elsif ( $type eq "locale" ) {
     481            $html = $class->_make_dropdown_html(
     482                        %$criterion, guide => $guide );
     483            $got_loc = 1;
     484        } else {
     485            warn "Unknown or missing criterion type: $type";
     486        }
     487        if ( $html ) {
     488            push @dropdowns, { type => $type, html => $html };
     489        }
     490    }
     491    if ( !$got_cat ) {
     492        push @dropdowns, { type => "category", html =>
     493            $class->_make_dropdown_html( type => "category", guide => $guide )
     494        };
     495    }
     496    if ( !$got_loc ) {
     497        push @dropdowns, { type => "locale", html =>
     498            $class->_make_dropdown_html( type => "locale", guide => $guide )
     499        };
     500    }
     501    # List the category dropdowns before the locale dropdowns, for consistency.
     502    @dropdowns = sort { $a->{type} cmp $b->{type} } @dropdowns;
     503    return @dropdowns;
     504}
     505
     506sub _make_dropdown_html {
     507    my ( $class, %args ) = @_;
     508    my ( $field_name, $any_label );
     509
     510    if ( $args{type} eq "locale" ) {
     511        $args{type} = "locales"; # hysterical raisins
     512        $any_label = " -- anywhere -- ";
     513        $field_name = "loc";
     514    } else {
     515        $any_label = " -- anything -- ";
     516        $field_name = "cat";
     517    }
     518
     519    my @options = $args{guide}->wiki->list_nodes_by_metadata(
     520        metadata_type => "category",
     521        metadata_value => $args{type},
     522        ignore_case => 1,
     523    );
     524    @options = map { s/^Category //; s/^Locale //; $_ } @options;
     525    my %labels = map { lc( $_ ) => $_ } @options;
     526    my @values = sort keys %labels;
     527    my $default = lc( $args{value} ) || "";
     528
     529    my $q = CGI->new( "" );
     530    return $q->popup_menu( -name => $field_name,
     531                           -values => [ "", @values ],
     532                           -labels => { "" => $any_label, %labels },
     533                           -default => $default );
     534}
     535
    438536=back
    439537
Note: See TracChangeset for help on using the changeset viewer.