Author: kake
Date: 2012-04-18 16:14:22 +0100 (Wed, 18 Apr 2012)
New Revision: 1355
Added:
trunk/t/96_show_index_form.t
trunk/templates/index_form.tt
Modified:
trunk/
trunk/Build.PL
trunk/MANIFEST
trunk/lib/OpenGuides.pm
trunk/lib/OpenGuides/CGI.pm
trunk/templates/map_index_leaflet.tt
trunk/templates/site_index.tt
Log:
Added a start at a form for indexing via locale+category.
Property changes on: trunk
___________________________________________________________________
Modified: svn:ignore
- .vimrc
*.swp
_build
Build
Makefile.PL
blib
cover_db
wiki.conf
+ .vimrc
*.swp
_build
Build
Makefile.PL
blib
cover_db
wiki.conf
META.yml
MYMETA.yml
META.json
MYMETA.json
Modified: trunk/Build.PL
===================================================================
--- trunk/Build.PL 2012-04-18 12:24:02 UTC (rev 1354)
+++ trunk/Build.PL 2012-04-18 15:14:22 UTC (rev 1355)
@@ -396,6 +396,7 @@
"footer.tt",
"header.tt",
"home_node.tt",
+ "index_form.tt",
"map_index.tt",
"map_index_leaflet.tt",
"missing_metadata.tt",
Modified: trunk/MANIFEST
===================================================================
--- trunk/MANIFEST 2012-04-18 12:24:02 UTC (rev 1354)
+++ trunk/MANIFEST 2012-04-18 15:14:22 UTC (rev 1355)
@@ -47,6 +47,7 @@
templates/header.tt
templates/json_index.tt
templates/home_node.tt
+templates/index_form.tt
templates/map_index.tt
templates/map_index_leaflet.tt
templates/missing_metadata.tt
@@ -168,6 +169,7 @@
t/93_redirect_without_spaces.t
t/94_leaflet.t
t/95_multiple_index.t
+t/96_show_index_form.t
t/templates/15_test.tt
wiki.cgi
META.json
Modified: trunk/lib/OpenGuides/CGI.pm
===================================================================
--- trunk/lib/OpenGuides/CGI.pm 2012-04-18 12:24:02 UTC (rev 1354)
+++ trunk/lib/OpenGuides/CGI.pm 2012-04-18 15:14:22 UTC (rev 1355)
@@ -4,6 +4,7 @@
$VERSION = '0.10';
use Carp qw( croak );
+use CGI;
use CGI::Cookie;
=head1 NAME
@@ -435,6 +436,103 @@
return $site_name . "_last_rc_visit";
}
+=item B<make_index_form_dropdowns>
+
+ my @dropdowns = OpenGuides::CGI->make_index_form_dropdowns (
+ guide => $guide,
+ selected => [
+ { type => "category", value => "pubs" },
+ { type => "locale", value => "holborn" },
+ ],
+ );
+ %tt_vars = ( %tt_vars, dropdowns => \@dropdowns );
+
+ # In the template
+ [% FOREACH dropdown = dropdowns %]
+ [% dropdown.type.ucfirst | html %]:
+ [% dropdown.html %]
+ <br />
+ [% END %]
+
+Makes HTML dropdown selects suitable for passing to an indexing template.
+
+The C<selected> argument is optional; if supplied, it gives default values
+for the dropdowns. At least one category and one locale dropdown will be
+returned; if no defaults are given for either then they'll default to
+everything/everywhere.
+
+=cut
+
+sub make_index_form_dropdowns {
+ my ( $class, %args ) = @_;
+ my @selected = @{$args{selected} || [] };
+ my $guide = $args{guide};
+ my @dropdowns;
+ my ( $got_cat, $got_loc );
+ foreach my $criterion ( @selected ) {
+ my $type = $criterion->{type} || "";
+ my $value = $criterion->{value} || "";
+ my $html;
+ if ( $type eq "category" ) {
+ $html = $class->_make_dropdown_html(
+ %$criterion, guide => $guide );
+ $got_cat = 1;
+ } elsif ( $type eq "locale" ) {
+ $html = $class->_make_dropdown_html(
+ %$criterion, guide => $guide );
+ $got_loc = 1;
+ } else {
+ warn "Unknown or missing criterion type: $type";
+ }
+ if ( $html ) {
+ push @dropdowns, { type => $type, html => $html };
+ }
+ }
+ if ( !$got_cat ) {
+ push @dropdowns, { type => "category", html =>
+ $class->_make_dropdown_html( type => "category", guide => $guide )
+ };
+ }
+ if ( !$got_loc ) {
+ push @dropdowns, { type => "locale", html =>
+ $class->_make_dropdown_html( type => "locale", guide => $guide )
+ };
+ }
+ # List the category dropdowns before the locale dropdowns, for consistency.
+ @dropdowns = sort { $a->{type} cmp $b->{type} } @dropdowns;
+ return @dropdowns;
+}
+
+sub _make_dropdown_html {
+ my ( $class, %args ) = @_;
+ my ( $field_name, $any_label );
+
+ if ( $args{type} eq "locale" ) {
+ $args{type} = "locales"; # hysterical raisins
+ $any_label = " -- anywhere -- ";
+ $field_name = "loc";
+ } else {
+ $any_label = " -- anything -- ";
+ $field_name = "cat";
+ }
+
+ my @options = $args{guide}->wiki->list_nodes_by_metadata(
+ metadata_type => "category",
+ metadata_value => $args{type},
+ ignore_case => 1,
+ );
+ @options = map { s/^Category //; s/^Locale //; $_ } @options;
+ my %labels = map { lc( $_ ) => $_ } @options;
+ my @values = sort keys %labels;
+ my $default = lc( $args{value} ) || "";
+
+ my $q = CGI->new( "" );
+ return $q->popup_menu( -name => $field_name,
+ -values => [ "", @values ],
+ -labels => { "" => $any_label, %labels },
+ -default => $default );
+}
+
=back
=head1 AUTHOR
Modified: trunk/lib/OpenGuides.pm
===================================================================
--- trunk/lib/OpenGuides.pm 2012-04-18 12:24:02 UTC (rev 1354)
+++ trunk/lib/OpenGuides.pm 2012-04-18 15:14:22 UTC (rev 1355)
@@ -983,6 +983,10 @@
$feed_base .= ";loc=" . lc( $criterion->{value} );
}
}
+ my @dropdowns = OpenGuides::CGI->make_index_form_dropdowns(
+ guide => $self,
+ selected => \@criteria );
+ $tt_vars{index_form_fields} = \@dropdowns;
$tt_vars{feed_base} = $feed_base;
}
Added: trunk/t/96_show_index_form.t
===================================================================
--- trunk/t/96_show_index_form.t (rev 0)
+++ trunk/t/96_show_index_form.t 2012-04-18 15:14:22 UTC (rev 1355)
@@ -0,0 +1,168 @@
+use strict;
+use Wiki::Toolkit::Setup::SQLite;
+use OpenGuides;
+use OpenGuides::CGI;
+use OpenGuides::Test;
+use Test::More;
+
+eval { require DBD::SQLite; };
+if ( $@ ) {
+ my ($error) = $@ =~ /^(.*?)\n/;
+ plan skip_all => "DBD::SQLite could not be used - no database to test with ($error)";
+}
+
+eval { require Test::HTML::Content; };
+if ( $@ ) {
+ plan skip_all => "Test::HTML::Content not available";
+}
+
+plan tests => 40;
+
+# Clear out the database from any previous runs.
+OpenGuides::Test::refresh_db();
+
+my $config = OpenGuides::Test->make_basic_config;
+my $guide = OpenGuides->new( config => $config );
+
+# Write some nodes with categories and things.
+OpenGuides::Test->write_data( guide => $guide, node => "A Node",
+ categories => "Apples\r\nBananas\r\nCherries",
+ locales => "Anerley\r\nBrockley\r\nChiswick",
+ return_output => 1 );
+
+# Test the form for altering the search - first with no criteria.
+my @dropdowns = eval {
+ OpenGuides::CGI->make_index_form_dropdowns( guide => $guide );
+};
+ok( !$@, "->make_index_form_dropdowns doesn't die when no criteria supplied" );
+my $html = join( " ", ( map { $_->{html} } @dropdowns ) );
+Test::HTML::Content::tag_ok( $html, "select", { name => "cat" },
+ "...and we have a 'cat' select" );
+like( $html, qr/apples.*bananas.*cherries/is,
+ "...and the categories seem to be in the right order" );
+Test::HTML::Content::tag_ok( $html, "select", { name => "loc" },
+ "...and we have a 'loc' select" );
+like( $html, qr/anerley.*brockley.*chiswick/is,
+ "...and the locales seem to be in the right order" );
+ok( $dropdowns[0]{type} eq "category" && $dropdowns[1]{type} eq "locale",
+ "...and category dropdown comes before locale dropdown" );
+my @cat_dropdowns = grep { $_->{type} eq "category" } @dropdowns;
+my @loc_dropdowns = grep { $_->{type} eq "locale" } @dropdowns;
+Test::HTML::Content::tag_ok( $cat_dropdowns[0]{html}, "option",
+ { value => "", selected => "selected" },
+ "...and the empty value is selected for category" );
+Test::HTML::Content::tag_ok( $loc_dropdowns[0]{html}, "option",
+ { value => "", selected => "selected" },
+ "...and the empty value is selected for locale" );
+
+# Now try it with one category, no locale.
+@dropdowns = eval {
+ OpenGuides::CGI->make_index_form_dropdowns(
+ guide => $guide,
+ selected => [ { type => "category", value => "bananas" } ],
+ );
+};
+ok( !$@, "->make_index_form_dropdowns doesn't die when category supplied" );
+$html = join( " ", ( map { $_->{html} } @dropdowns ) );
+Test::HTML::Content::tag_ok( $html, "select", { name => "cat" },
+ "...and we have a 'cat' select" );
+like( $html, qr/apples.*bananas.*cherries/is,
+ "...and the categories seem to be in the right order" );
+Test::HTML::Content::tag_ok( $html, "select", { name => "loc" },
+ "...and we have a 'loc' select" );
+like( $html, qr/anerley.*brockley.*chiswick/is,
+ "...and the locales seem to be in the right order" );
+ok( $dropdowns[0]{type} eq "category" && $dropdowns[1]{type} eq "locale",
+ "...and category dropdown comes before locale dropdown" );
+@cat_dropdowns = grep { $_->{type} eq "category" } @dropdowns;
+@loc_dropdowns = grep { $_->{type} eq "locale" } @dropdowns;
+Test::HTML::Content::tag_ok( $cat_dropdowns[0]{html}, "option",
+ { value => "bananas", selected => "selected" },
+ "...and the category is selected" );
+Test::HTML::Content::tag_ok( $cat_dropdowns[0]{html}, "option",
+ { value => "" },
+ "...and the empty value is present in the category dropdown" );
+Test::HTML::Content::no_tag( $cat_dropdowns[0]{html}, "option",
+ { value => "", selected => "selected" },
+ "...but not selected" );
+Test::HTML::Content::tag_ok( $loc_dropdowns[0]{html}, "option",
+ { value => "", selected => "selected" },
+ "...and the empty value is selected for locale" );
+
+# Now with one locale, no category.
+@dropdowns = eval {
+ OpenGuides::CGI->make_index_form_dropdowns(
+ guide => $guide,
+ selected => [ { type => "locale", value => "anerley" } ],
+ );
+};
+ok( !$@, "->make_index_form_dropdowns doesn't die when locale supplied" );
+$html = join( " ", ( map { $_->{html} } @dropdowns ) );
+Test::HTML::Content::tag_ok( $html, "select", { name => "cat" },
+ "...and we have a 'cat' select" );
+like( $html, qr/apples.*bananas.*cherries/is,
+ "...and the categories seem to be in the right order" );
+Test::HTML::Content::tag_ok( $html, "select", { name => "loc" },
+ "...and we have a 'loc' select" );
+like( $html, qr/anerley.*brockley.*chiswick/is,
+ "...and the locales seem to be in the right order" );
+ok( $dropdowns[0]{type} eq "category" && $dropdowns[1]{type} eq "locale",
+ "...and category dropdown comes before locale dropdown" );
+@cat_dropdowns = grep { $_->{type} eq "category" } @dropdowns;
+@loc_dropdowns = grep { $_->{type} eq "locale" } @dropdowns;
+Test::HTML::Content::tag_ok( $loc_dropdowns[0]{html}, "option",
+ { value => "anerley", selected => "selected" },
+ "...and the locale is selected" );
+Test::HTML::Content::tag_ok( $loc_dropdowns[0]{html}, "option",
+ { value => "" },
+ "...and the empty value is present in the locale dropdown" );
+Test::HTML::Content::no_tag( $loc_dropdowns[0]{html}, "option",
+ { value => "", selected => "selected" },
+ "...but not selected" );
+Test::HTML::Content::tag_ok( $cat_dropdowns[0]{html}, "option",
+ { value => "", selected => "selected" },
+ "...and the empty value is selected for category" );
+
+# Now test with a category and a locale.
+@dropdowns = eval {
+ OpenGuides::CGI->make_index_form_dropdowns(
+ guide => $guide,
+ selected => [
+ { type => "category", value => "cherries" },
+ { type => "locale", value => "chiswick" },
+ ],
+ );
+};
+ok( !$@,
+ "->make_index_form_dropdowns doesn't die when locale and categorysupplied" );
+$html = join( " ", ( map { $_->{html} } @dropdowns ) );
+Test::HTML::Content::tag_ok( $html, "select", { name => "cat" },
+ "...and we have a 'cat' select" );
+like( $html, qr/apples.*bananas.*cherries/is,
+ "...and the categories seem to be in the right order" );
+Test::HTML::Content::tag_ok( $html, "select", { name => "loc" },
+ "...and we have a 'loc' select" );
+like( $html, qr/anerley.*brockley.*chiswick/is,
+ "...and the locales seem to be in the right order" );
+ok( $dropdowns[0]{type} eq "category" && $dropdowns[1]{type} eq "locale",
+ "...and category dropdown comes before locale dropdown" );
+@cat_dropdowns = grep { $_->{type} eq "category" } @dropdowns;
+@loc_dropdowns = grep { $_->{type} eq "locale" } @dropdowns;
+Test::HTML::Content::tag_ok( $cat_dropdowns[0]{html}, "option",
+ { value => "cherries", selected => "selected" },
+ "...and the category is selected" );
+Test::HTML::Content::tag_ok( $cat_dropdowns[0]{html}, "option",
+ { value => "" },
+ "...and the empty value is present in the category dropdown" );
+Test::HTML::Content::no_tag( $cat_dropdowns[0]{html}, "option",
+ { value => "", selected => "selected" },
+ "...but not selected" );
+Test::HTML::Content::tag_ok( $loc_dropdowns[0]{html}, "option",
+ { value => "chiswick", selected => "selected" },
+ "...and the locale is selected" );
+Test::HTML::Content::tag_ok( $loc_dropdowns[0]{html}, "option",
+ { value => "" },
+ "...and the empty value is present in the locale dropdown" );
+Test::HTML::Content::no_tag( $loc_dropdowns[0]{html}, "option",
+ { value => "", selected => "selected" },
+ "...but not selected" );
Added: trunk/templates/index_form.tt
===================================================================
--- trunk/templates/index_form.tt (rev 0)
+++ trunk/templates/index_form.tt 2012-04-18 15:14:22 UTC (rev 1355)
@@ -0,0 +1,12 @@
+<form action="[% cgi_url %]" method="get">
+ <input type="hidden" name="action" value="index" />
+ [% IF centre_lat OR no_nodes_on_map %]
+ [%# this is how we know we're doing format=map! %]
+ <input type="hidden" name="format" value="map" />
+ [% END %]
+ [% FOREACH field = index_form_fields %]
+ [% field.type.ucfirst | html %]:
+ [% field.html %]
+ [% END %]
+ <input type="submit" value="Search" />
+</form>
Modified: trunk/templates/map_index_leaflet.tt
===================================================================
--- trunk/templates/map_index_leaflet.tt 2012-04-18 12:24:02 UTC (rev 1354)
+++ trunk/templates/map_index_leaflet.tt 2012-04-18 15:14:22 UTC (rev 1355)
@@ -20,6 +20,8 @@
(<a href="[% feed_base %]">view as a list</a>)
</h2>
+ [% INCLUDE index_form.tt %]
+
<div id="map_index_node_list">
<ul>
[% IF no_nodes_on_map %]
Modified: trunk/templates/site_index.tt
===================================================================
--- trunk/templates/site_index.tt 2012-04-18 12:24:02 UTC (rev 1354)
+++ trunk/templates/site_index.tt 2012-04-18 15:14:22 UTC (rev 1355)
@@ -23,6 +23,9 @@
[%- END -%]
(<a href="[% feed_base %];format=map">view on a map</a>)
</h2>
+
+[% INCLUDE index_form.tt %]
+
<ol>
[% FOREACH node = nodes %]
<li><a href="[% cgi_url %]?[% node.param %]">[% node.name %]</a>