fu: Close-up of Fu, bringing a scoop of water to her mouth (Default)
fu ([personal profile] fu) wrote in [site community profile] changelog2010-12-16 05:58 pm

[dw-free] convert interests.bml to TT

[commit: http://hg.dwscoalition.org/dw-free/rev/11ed0d63e6fd]

http://bugs.dwscoalition.org/show_bug.cgi?id=3110

Create alternative method for non-BML context.

Patch by [personal profile] kareila.

Files modified:
  • cgi-bin/LJ/User.pm
  • cgi-bin/weblib.pl
--------------------------------------------------------------------------------
diff -r a6852c049c62 -r 11ed0d63e6fd cgi-bin/LJ/User.pm
--- a/cgi-bin/LJ/User.pm	Fri Dec 17 01:53:39 2010 +0800
+++ b/cgi-bin/LJ/User.pm	Fri Dec 17 01:58:35 2010 +0800
@@ -8259,7 +8259,7 @@ sub user_search_display {
     }
 
     if (defined $args{perpage}) {
-        my %items = BML::paging(\@display, $args{curpage}, $args{perpage});
+        my %items = LJ::paging( \@display, $args{curpage}, $args{perpage} );
 
         # Fancy paging bar
         my $opts;
diff -r a6852c049c62 -r 11ed0d63e6fd cgi-bin/weblib.pl
--- a/cgi-bin/weblib.pl	Fri Dec 17 01:53:39 2010 +0800
+++ b/cgi-bin/weblib.pl	Fri Dec 17 01:58:35 2010 +0800
@@ -424,6 +424,53 @@ sub paging_bar
         $navcrap = BML::fill_template("standout", { 'DATA' => $navcrap });
     }
     return $navcrap;
+}
+
+# drop-in replacement for BML::paging in non-BML context
+sub paging {
+    my ( $listref, $page, $pagesize ) = @_;
+    $page = 1 unless $page && $page == int $page;
+    return unless $pagesize;  # let's not divide by zero
+    my @items = @{ $listref };
+    my %self;
+
+    my $newurl = sub {
+        # replaces BML::page_newurl
+        my $page = $_[0];
+        my $r = DW::Request->get;
+        my $args = $r->get_args;
+        my ( $url ) = split /\?/, $r->uri;
+        my @pair = ();
+
+        foreach ( sort grep { $_ ne "page" } keys %$args ) {
+            push @pair, ( LJ::eurl( $_ ) . "=" . LJ::eurl( $args->{$_} ) );
+        }
+        push @pair, "page=$page";
+        return $url . "?" . join( "&", @pair );
+    };
+
+    $self{itemcount} = scalar @items;
+
+    $self{pages} = $self{itemcount} / $pagesize;
+    $self{pages} = int( $self{pages} ) + 1
+        if $self{pages} != int( $self{pages} );  # round up any fraction
+
+    $page = 1 if $page < 1;
+    $page = $self{pages} if $page > $self{pages};
+    $self{page} = $page;
+
+    $self{itemfirst} = $pagesize * ( $page - 1 ) + 1;
+    $self{itemlast} = $pagesize * $page;
+    $self{itemlast} = $self{itemcount} if $self{pages} == $page;
+
+    my @range = ( $self{itemfirst} - 1 ) .. ( $self{itemlast} - 1 );
+    $self{items} = [ @items[@range] ];
+
+    my ( $prev, $next ) = ( $newurl->( $page - 1 ), $newurl->( $page + 1 ) );
+    $self{backlink} = "<a href=\"$prev\">&lt;&lt;&lt;</a>" unless $page == 1;
+    $self{nextlink} = "<a href=\"$next\">&gt;&gt;&gt;</a>" unless $page == $self{pages};
+
+    return %self;
 }
 
 # <LJFUNC>
--------------------------------------------------------------------------------

Post a comment in response:

This account has disabled anonymous posting.
If you don't have an account you can create one now.
HTML doesn't work in the subject.
More info about formatting

If you are unable to use this captcha for any reason, please contact us by email at support@dreamwidth.org