Created
August 18, 2010 15:03
-
-
Save phillipadsmith/535031 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%args> | |
$category => undef | |
@cat_ids => undef | |
$element_key => undef | |
$site_id => $story->get_site_id | |
$pages => 10 | |
$stories_per_page => 10 | |
$order => undef | |
$sort => undef | |
</%args> | |
<%perl> | |
my $offset = $burner->notes('offset') || 0; | |
my @params = ( | |
Order => $order || 'cover_date', | |
OrderDirection => $sort || 'DESC', | |
category_uri => "$category%" || '/%', | |
element_key_name => "$element_key", | |
site_id => $site_id, | |
unexpired => 1, | |
( $burner->get_mode == PUBLISH_MODE ? (published_version => 1) : () ), | |
); | |
if(length @cat_ids > 1 ) { | |
push(@params, (category_id => ANY( @cat_ids ) ) ); | |
} | |
my $page_count = $burner->notes('page_count') || do { | |
# How many stories do we have? | |
my $count = @{ ref($story)->list({ | |
@params, | |
Limit => (($pages - 1) * $stories_per_page) + 1, | |
}) | |
}; | |
# How many pages are there? | |
# Remember that the function 'ceil' requires the POSIX module | |
# Use int(x+0.99 instead) | |
int(0.99 + ($count / $stories_per_page)); | |
}; | |
$category = undef if $category =~ 'archive'; | |
my @stories = ref($story)->list({ | |
@params, | |
Limit => $stories_per_page +1, | |
Offset => $offset, | |
}); | |
if (@stories > $stories_per_page && $offset < ($pages - 1) * $stories_per_page) | |
{ | |
# We'll need more pages. | |
$burner->set_burn_again(1); | |
$burner->notes( offset => $offset + $stories_per_page ); | |
$burner->notes( page_count => $page_count ); | |
} else { | |
# We're done, clean out the notes. | |
$burner->clear_notes; | |
} | |
if(!@stories) { | |
die "WTF? No Stories. This shouldn't happen. " . @stories; | |
} | |
pop @stories; | |
return ($page_count, @stories ); | |
</%perl> | |
<%doc> | |
=pod | |
=head1 NAME | |
/util/cover_list_stories - Used in most cover.mc templates to assist with pagination | |
=head1 USAGE | |
my ( $page_count, @stories ) = $m->comp('/util/cover_list_stories.mc', | |
category => $cat_uri, | |
pages => $pages, | |
stories_per_page => $stories_per_page, | |
element_key => $element_key_name, | |
); | |
=head2 CHANGELOG & NOTES | |
=cut | |
</%doc> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment