<?php
/*
Child page listing and pagination
Author: Everyone
Pagination code: Sean Wragg (http://triggsolutions.com/php-snippets/simple-php-standalone-pagination/393)
Uses:
get_page_slug
getChildren
getChildrenMulti
subval_sort
find_url (if i18n plugin not installed)
find_i18n_url (if i18n plugin installed)
$parent
Usage example:
$subPages = new ChildList( get_page_slug( false ) );
$subPages->display( $_GET['p'] );
$subPages->paginate();
*/
class ChildList
{
private $slug = false;
private $children_slugs;
private $total = false;
private $page_nr = 0;
private $per_page = 10;
private $viewFile = false;
public function __construct( $slug = false, $sort_by = 'creDate', $order = 'desc' )
{
$this->slug = ($slug) ?: get_page_slug( false );
# Get an array of children slugs
# and a field against which they will be sorted
$this->children_slugs = getChildrenMulti( $this->slug, array( $sort_by ) );
# Remove children with children.
# This may change in the future
# and listing children with children may be added as a feature.
foreach( $this->children_slugs as $key => &$child )
{
if( self::hasChildren( $child['url'] ) )
unset( $this->children_slugs[ $key ] );
else
foreach( $child as $c_key => &$c_val )
{
if( $c_key == 'creDate' or $c_key == 'pubDate' )
$c_val = strtotime( $c_val );
}
}
unset( $child, $c_val );
# GetSimple native function:
# subval_sort( $a, $subkey, $order='asc', $natural = true )
$this->children_slugs =
subval_sort( $this->children_slugs, $sort_by, $order );
# Get total number of children
$this->total = count( $this->children_slugs );
return $this;
}
public function display( $page_nr = false, $per_page = false, $viewFile = false )
{
$this->page_nr = ( is_numeric( $page_nr ) && $page_nr !== false ) ? $page_nr : $this->page_nr;
$this->per_page = ( is_numeric( $per_page ) && $per_page !== false ) ? $per_page : $this->per_page;
if( $this->per_page > 0 )
{
# Check if given page is heigher than maximum possible
$max_page = ceil( $this->total / $this->per_page );
$this->page_nr = ( $this->page_nr > $max_page ) ? $max_page : $this->page_nr;
if( $this->total > $this->per_page )
{
# array_slice ( $array, $offset [, $length [, bool $preserve_keys = false ]] )
$this->children_slugs = array_slice(
$this->children_slugs,
$this->page_nr * $this->per_page,
$this->per_page
);
}
}
# Path may (and should) be changed to whatever is needed.
$path = get_theme_url();
# If no view file name was given,
# or no such file was found,
# try to find slug's default view file.
# If that one is also not present, settle with a default view file,
# that is always available.
if( file_exists( $path.'/'.$viewFile.'.inc.php' ) )
$this->viewFile = $path.'/'.$viewFile.'.inc.php';
elseif( file_exists( $path.'/'.$this->slug.'.inc.php' ) )
$this->viewFile = $path.'/'.$this->slug.'.inc.php';
else
$this->viewFile = $path.'/list.default.inc.php';
foreach( $this->children_slugs as $child )
{
$page_data = (array)getXML( GSDATAPAGESPATH . $child['url'] .'.xml' );
$this->_render( $page_data );
}
return $this;
}
public function paginate()
{
# get pagination url
# Get Simple's global $parent variable. It holds the slug of parent page.
global $parent;
if( function_exists( 'find_i18n_url' ) )
$url = find_i18n_url( $this->slug, $parent );
else
$url = find_url( $this->slug, $parent );
$url .= '?p=';
$pag_tag_open = '<div class="pagination">';
$pag_tag_close = '</div>';
$lnk_tag_open = ''; # normally <span>
$lnk_tag_close = ''; # normally </span>
$nr_pages = ceil( $this->total / $this->per_page );
$range_start = ( ($this->page_nr >= 5) ? ($this->page_nr - 3) : 1 );
$range_end = ( (($this->page_nr + 5) > $nr_pages ) ? $nr_pages : ($this->page_nr + 5) );
# Drawing number links before and after current page
if ( $this->page_nr >= 1 ) {
$r[] = $lnk_tag_open.'<a href="'. $url .'">|«</a>'.$lnk_tag_close;
$r[] = $lnk_tag_open.'<a href="'. $url . ( $this->page_nr - 1 ) .'">‹</a>'.$lnk_tag_close;
$r[] = ( ($range_start > 1) ? ' ... ' : '' );
}
if ( $range_end > 1 )
{
foreach( range( $range_start, $range_end ) as $key => $value )
{
if ( $value == ($this->page_nr + 1) ) $r[] = '<span>'. $value .'</span>';
else $r[] = $lnk_tag_open.'<a href="'. $url . ($value - 1) .'">'. $value .'</a>'.$lnk_tag_close;
}
}
if ( ( $this->page_nr + 1 ) < $nr_pages )
{
$r[] = ( ($range_end < $nr_pages) ? ' ... ' : '' );
$r[] = $lnk_tag_open.'<a href="'. $url . ( $this->page_nr + 1 ) .'">›</a>'.$lnk_tag_close;
$r[] = $lnk_tag_open.'<a href="'. $url . ( $nr_pages - 1 ) .'">»|</a>'.$lnk_tag_close;
}
# END Drawing number links before and after current page
echo ( (isset($r)) ? $pag_tag_open . implode("\r\n", $r) . $pag_tag_close : '');
}
# Checks if page has children
static public function hasChildren( $slug )
{
$children = getChildren( $slug );
if( !empty( $children ) )
return true;
else
return false;
}
# cut down viewer class
private function _render( $data = false )
{
extract( $GLOBALS );
if( !empty( $data ) && is_array( $data ) )
extract( $data );
ob_start();
if( $this->viewFile )
include $this->viewFile;
echo ob_get_clean();
}
}