Changeset 106 in bookmarks for trunk/lib/Bookmarks.pm


Ignore:
Timestamp:
10/19/15 21:16:31 (9 years ago)
Author:
peter
Message:

#6: Added basic pagination to the bookmarks HTML list view.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/Bookmarks.pm

    r92 r106  
    9999} 
    100100 
     101sub get_count { 
     102    my $self = shift; 
     103    my $search = shift; 
     104 
     105    my ($sql, @bind) = sql_interp( 
     106        'select count(*) from resources join bookmarks on resources.uri = bookmarks.uri', 
     107        $self->_sql_where_clause($search), 
     108    ); 
     109    my $sth = $self->dbh->prepare($sql); 
     110    $sth->execute(@bind); 
     111    my ($count) = $sth->fetchrow_array; 
     112    return $count; 
     113} 
     114 
    101115sub search { 
    102116    my $self = shift; 
    103117    my $params = shift || {}; 
    104118    my $search = Bookmarks::Search->new($params); 
     119 
     120    my ($limit, $offset); 
     121    if ($search->page) { 
     122        my $count = $self->get_count($search); 
     123        use Data::Pageset; 
     124        my $pages = Data::Pageset->new({ 
     125            total_entries    => $count, 
     126            current_page     => $search->page, 
     127            entries_per_page => 15, 
     128            mode             => 'slide', 
     129        }); 
     130        $search->pages($pages); 
     131        $limit = $pages->entries_per_page; 
     132        $offset = $pages->skipped; 
     133    } else { 
     134        $limit = $search->limit; 
     135        $offset = $search->offset; 
     136    } 
    105137 
    106138    # build the query 
     
    110142    push @sql, $self->_sql_where_clause($search); 
    111143    push @sql, 'order by ctime desc'; 
    112     push @sql, ('limit', \$search->limit) if $search->limit; 
     144    push @sql, ('limit', \$limit) if $limit; 
    113145    # an offset is only allowed if we have a limit clause 
    114     push @sql, ('offset', \$search->offset) if $search->limit && $search->offset; 
     146    push @sql, ('offset', \$offset) if $limit && $offset; 
    115147 
    116148    my ($sql, @bind) = sql_interp(@sql); 
Note: See TracChangeset for help on using the changeset viewer.