[dw-free] Convert nav.bml to Template Toolkit
[commit: http://hg.dwscoalition.org/dw-free/rev/5663ae2d8b2b]
http://bugs.dwscoalition.org/show_bug.cgi?id=2255
Move menu nav page to TT.
Patch by
foxfirefey.
Files modified:
http://bugs.dwscoalition.org/show_bug.cgi?id=2255
Move menu nav page to TT.
Patch by
Files modified:
- cgi-bin/DW/Controller/Nav.pm
- cgi-bin/DW/Logic/MenuNav.pm
- htdocs/nav.bml
- htdocs/nav.bml.text
- views/nav.tt
- views/nav.tt.text
--------------------------------------------------------------------------------
diff -r e6d980963fc6 -r 5663ae2d8b2b cgi-bin/DW/Controller/Nav.pm
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/cgi-bin/DW/Controller/Nav.pm Fri Jan 08 23:39:21 2010 +0000
@@ -0,0 +1,72 @@
+#!/usr/bin/perl
+#
+# DW::Controller::Nav
+#
+# This controller is for navigation handlers.
+#
+# Authors:
+# foxfirefey <skittisheclipse@gmail.com>
+#
+# Copyright (c) 2009 by Dreamwidth Studios, LLC.
+#
+# This program is free software; you may redistribute it and/or modify it under
+# the same terms as Perl itself. For a copy of the license, please reference
+# 'perldoc perlartistic' or 'perldoc perlgpl'.
+#
+
+package DW::Controller::Nav;
+
+use strict;
+use warnings;
+use DW::Controller;
+use DW::Routing::Apache2;
+use DW::Template::Apache2;
+use DW::Logic::MenuNav;
+use JSON;
+
+# Defines the URL for routing. I could use register_string( '/nav' ... ) if I didn't want to capture arguments
+# This is an application page, not a user styled page, and the default format is HTML (ie, /nav gives /nav.html)
+DW::Routing::Apache2->register_regex( qr!^/nav(?:/([a-z]*))?$!, \&nav_handler, app => 1 );
+
+# handles menu nav pages
+sub nav_handler {
+ my $r = DW::Request->get;
+
+ # Check for a category like nav/read, then for a ?cat=read argument, else no category
+ my $cat = $_[0]->subpatterns->[0] || $r->get_args->{cat} || '';
+
+ # this function returns an array reference of menu hashes
+ my $menu_nav = DW::Logic::MenuNav->get_menu_display( $cat );
+ or return error_ml( '/nav.tt.error.invalidcat' );
+
+ # this data doesn't need HTML in the titles, like in the real menu
+ for my $menu ( @$menu_nav ) {
+ for my $item ( @{ $menu->{items} } ) {
+ $item->{text} = LJ::strip_html( $item->{text} );
+ }
+ }
+
+ # display according to the format
+ my $format = $_[0]->format;
+ if ( $format eq 'json' ) {
+ # this prints out the menu navigation as JSON and returns
+ $r->print( JSON::objToJson( $menu_nav ) );
+ return $r->OK;
+ } elsif ( $format eq 'html' ) {
+ # these variables will get passed to the template
+ my $vars = {
+ menu_nav => $menu_nav,
+ cat => $cat,
+ };
+
+ $vars->{cat_title} = $menu_nav->[0]->{title} if $cat;
+
+ # Now we tell it what template to render and pass in our variables
+ return DW::Template::Apache2->render_template( 'nav.tt', $vars );
+ } else {
+ # return 404 for an unknown format
+ return $r->NOT_FOUND;
+ }
+}
+
+1;
diff -r e6d980963fc6 -r 5663ae2d8b2b cgi-bin/DW/Logic/MenuNav.pm
--- a/cgi-bin/DW/Logic/MenuNav.pm Fri Jan 08 12:48:48 2010 -0600
+++ b/cgi-bin/DW/Logic/MenuNav.pm Fri Jan 08 23:39:21 2010 +0000
@@ -15,6 +15,7 @@ package DW::Logic::MenuNav;
package DW::Logic::MenuNav;
use strict;
+use LJ::Lang;
# name: get_menu_navigation
#
@@ -257,4 +258,69 @@ sub get_menu_navigation {
return \@nav;
}
+# name: get_menu_display
+#
+# des: Returns the menu navigation structure for the site, but processed for display.
+#
+# args: (optional)
+# $cat A string with a menu category name or array ref of multiple category names,
+# which will make this function only return menus in the wanted categories.
+# $u An LJ::User object for which the 'display' fields should be
+# calculated. Defaults to the remote user.
+#
+# returns: an arrayref of top-level menu items, each represented as a hashref
+# describing the menu as follows:
+# - name: the short (URL-friendly) name for this menu.
+# - title: the translated title for this menu
+# - items: an arrayref of menu items, containing hashrefs
+# giving the details for each one as follows:
+# - url: the URL that the link should lead to
+# - text: the translated text for the link
+# if there are no menus with items, returns undef
+sub get_menu_display {
+ my ( $class, $cat, $u ) = @_;
+
+ $u ||= LJ::get_remote();
+ my $menu_nav = DW::Logic::MenuNav->get_menu_navigation( $u );
+
+ foreach my $menu (@$menu_nav) {
+ # remove menu items not displayed
+ my @display = grep { $_->{display} } @{ $menu->{items} };
+
+ # will use this to filter out empty menus or unrequested menus
+ $menu->{display} = scalar( @display );
+
+ # if we have a cat, only display requested menu(s)
+ if ( $cat ) {
+ if ( ref( $cat ) eq 'ARRAY' ) {
+ $menu->{display} = 0 unless ( grep { $_ eq $menu->{name} } @$cat );
+ } else {
+ $menu->{display} = 0 unless $menu->{name} eq $cat;
+ }
+ }
+
+ # only translate and process menus that will be displayed
+ if ( $menu->{display} ) {
+ # translate all menu item labels that will be displayed
+ map { $_->{text} = LJ::Lang::ml( $_->{text}, $_->{text_opts} ) } @display;
+
+ # only include the text and url attributes
+ @display = map { { text => $_->{text}, url => $_->{url} } } @display;
+
+ # replace unprocessed menu items with processed ones
+ $menu->{items} = \@display;
+ }
+
+ # translate menu title -- keep the name for people's reference
+ $menu->{title} = LJ::Lang::ml( "menunav." . $menu->{name} );
+ }
+
+ # remove empty menus and only include title, name and item information
+ my @menus = map { { title => $_->{title}, name => $_->{name}, items => $_->{items} } }
+ grep { $_->{display} } @$menu_nav;
+
+ # Return undefined if we don't have any menus to return
+ return scalar( @menus ) ? \@menus : undef;
+}
+
1;
diff -r e6d980963fc6 -r 5663ae2d8b2b htdocs/nav.bml
--- a/htdocs/nav.bml Fri Jan 08 12:48:48 2010 -0600
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,53 +0,0 @@
-<?_c
-#
-# nav.bml
-#
-# Page that shows the sub-level navigation links given the top-level navigation header
-#
-# Authors:
-# Janine Smith <janine@netrophic.com>
-#
-# Copyright (c) 2009 by Dreamwidth Studios, LLC.
-#
-# This program is free software; you may redistribute it and/or modify it under
-# the same terms as Perl itself. For a copy of the license, please reference
-# 'perldoc perlartistic' or 'perldoc perlgpl'.
-#
-_c?><?page
-body<=
-<?_code
-{
- use strict;
- use vars qw(%GET %POST $title $headextra @errors @warnings);
-
- use DW::Logic::MenuNav;
-
- $title = $ML{'.title'};
-
- my $cat = $GET{cat};
- return $ML{'.error.nocat'} unless $cat;
-
- my $nav_links = DW::Logic::MenuNav->get_menu_navigation;
- my $ret;
-
- my @links_html;
- foreach my $cathash ( @{ $nav_links } ) {
- next if $cathash->{name} ne $cat;
- foreach my $item ( @{ $cathash->{items} } ) {
- push @links_html, "<a href='$item->{url}'>" . BML::ml( "$item->{text}", $item->{text_opts} ) . "</a>" if $item->{display};
- }
- }
- return $ML{'.error.invalidcat'} unless @links_html;
-
- $title = $ML{"menunav.$cat"};
- $ret .= join( "<br />", @links_html );
-
- return $ret;
-}
-_code?>
-<=body
-title=><?_code return $title; _code?>
-head<=
-<?_code return $headextra; _code?>
-<=head
-page?>
diff -r e6d980963fc6 -r 5663ae2d8b2b htdocs/nav.bml.text
--- a/htdocs/nav.bml.text Fri Jan 08 12:48:48 2010 -0600
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,7 +0,0 @@
-;; -*- coding: utf-8 -*-
-
-.error.invalidcat=The navigation category that you specified is invalid.
-
-.error.nocat=You did not specify a navigation category.
-
-.title=Navigation
diff -r e6d980963fc6 -r 5663ae2d8b2b views/nav.tt
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/views/nav.tt Fri Jan 08 23:39:21 2010 +0000
@@ -0,0 +1,22 @@
+[%# nav.tt
+
+Page that shows the sub-level navigation links given the top-level navigation header
+
+Authors:
+ foxfirefey <skittisheclipse@gmail.com>
+
+This program is free software; you may redistribute it and/or modify it under
+the same terms as Perl itself. For a copy of the license, please reference
+'perldoc perlartistic' or 'perldoc perlgpl'.
+%]
+
+[%- IF cat; sections.title = cat_title; ELSE; sections.title = '.title' | ml; END -%]
+
+[% FOREACH menu = menu_nav %]
+ [% IF NOT cat %]<h2 class="[% menu.name %]">[% menu.title | html %]</h2>[% END %]
+ <ul
+ [% FOREACH menu_item = menu.items %]
+ <li><a href="[% menu_item.url | url %]">[% menu_item.text | html %]</a></li>
+ [% END %]
+ </ul>
+[% END %]
diff -r e6d980963fc6 -r 5663ae2d8b2b views/nav.tt.text
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/views/nav.tt.text Fri Jan 08 23:39:21 2010 +0000
@@ -0,0 +1,7 @@
+;; -*- coding: utf-8 -*-
+
+.error.invalidcat=The navigation category that you specified is invalid.
+
+.error.nocat=You did not specify a navigation category.
+
+.title=Navigation
--------------------------------------------------------------------------------
