* @copyright 2013 Dane Gardow * @date 01 January 2013 * @version 1.0 * @license Free * ******************************************************************************************/ if ( ! class_exists( "WP_Statistics_Pagination" ) ): // Prevent multiple class definitions class WP_Statistics_Pagination { /******************************************************* * PROPERTIES / DATA MEMBERS *******************************************************/ // Edit these as you desire const DEFAULT_ENTRIES_DISPLAY = 10; // Default number of entries to display per page const PAGE_GETVAR_NAME = "page"; // Name of GET variable name for page values (i.e., example.php?page=1) private $_paginationID = "pagination"; // ID Name of pagination object "pagination" is default // used also for form name for select options // Do not edit these values; they are simply null initializations private $_totalEntries = null; // Total number of entries (usually supplied by MySQL query) private $_pagesPerSection = null; // Total number of pages displayed per section (supplied by admin) private $_entriesPerPage = null; // Total number of entries displayed per page (supplied by user) private $_currentPage = null; // Current page viewed by user private $_displayOptions = array(); // Array of options for viewing how many entries per page (supplied by user) private $_errors = array(); // Array of encountered error messages private $_styles = array(); // Array of CSS styles for pagination navigation display /******************************************************* * CONSTRUCTOR *******************************************************/ function __construct( $totalEntries, $pagesPerSection, $options = "", $paginationID = "", $stylePageOff = "", $stylePageOn = "", $styleErrors = "", $styleSelect = "" ) { $this->setPaginationID( $paginationID ); // Set ID name of pagination object $this->setTotalEntries( $totalEntries ); // Set total entries $this->setPagesPerSection( $pagesPerSection ); // Set pages per section $this->setDisplayOptions( $options ); // Set viewing display options $this->setEntriesPerPage(); // Set entries per page (input from POST or cookies) $this->setCurrentPage(); // Set current page (input from GET) // ! This function must follow after setEntriesPerPage() $this->setStyles( $stylePageOff, $stylePageOn, $styleSelect, $styleErrors ); // Set CSS styles for pagination navigation display } /******************************************************* * UTILITY FUNCTIONS *******************************************************/ public function deleteCookie() // deletes the cookie if it exists { $cookieVar = $this->_getPOSTVarName(); if ( isset( $_COOKIE[ $cookieVar ] ) ) // If cookie is set { $_COOKIE[ $cookieVar ] = ""; // Clear cookie setcookie( $cookieVar, "", time() - 3600, "/" ); // Delete cookie } } private function _getURL( $input = 1 ) // returns appropriate URL with all GET variables intact { // updates only the particular GET variable in question $getVars = $_GET; // Get all GET variables /* Uncomment this if you need to exclude any GET variables (due to HTACCESS issues, for example) from being * processed in the ensuing URL. Simply enter in the GET variable name in the unset(...) function below. unset($getVars["foo"], $getVars["bar"], ... ); // Remove any so they do not appear in URL */ $output = '?' . http_build_query( array_merge( $getVars, array( $this->_getIDGETVarName() => $input ) ) ); $output .= '#' . $this->getPaginationID(); // Add #xxx at the end of URL for auto-scrolling return $output; } private function _isError() // determines if an error exists and registers any errors { if ( $this->_errors ) // If error already exists, return { return true; } if ( ! $this->_totalEntries ) // If total entries not set { $this->_errors[] = "The value for total entries has not been specified."; } if ( ! $this->_displayOptions ) // If display options not set { $this->_errors[] = "The values for display options have not been specified."; } if ( ! $this->_entriesPerPage ) // If entries per page not set { $this->_errors[] = "The value for entries per page has not been specified."; } if ( ! $this->_currentPage ) // If current page not set { $this->_errors[] = "The value for current page has not been specified."; } return ( $this->_errors ) ? true : false; } private function _validEntry( $input ) // determines if input is valid { if ( is_array( $input ) ) // If array { foreach ( $input as $element ) { // Recursion: evaluate each array element if ( ! $this->_validEntry( $element ) ) // If invalid { return false; } } return true; // If function makes it to this point, it is valid } else // If not array { if ( ( preg_match( "/^\d+$/", $input ) && $input > 0 ) || strtolower( $input ) == "all" ) // If positive digit or "all" { return true; } else { return false; } } } private function _navBox( $text, $destinationPage, $end = 0 ) // returns span-encased link for pagination bar { switch ( $end ) { case 1: $title = "First page"; break; case 2: $title = "Previous page"; break; case 3: $title = "Next page"; break; case 4: $title = "Last page (" . $this->getTotalPages() . ")"; break; default: $title = ""; break; } $title = ( $end > 0 && $title != "" ) ? 'title="' . $title . '"' : ''; $style = $this->_styles["pageOff"]; // Determine Style $style = ( $this->_currentPage == $destinationPage && ! $end ) ? $this->_styles["pageOn"] : $this->_styles["pageOff"]; // Determine Link URL/Text $url = ""; if ( $this->_currentPage != $destinationPage // If current page is not same as destination page && $destinationPage <= $this->getTotalPages() // and destination page does not exceed last page && $destinationPage >= 1 ) // and destination page is not less than first page { $text = '' . $text . ''; // then make $text a link } if ( $style ) { $style = 'class="' . $style . '"'; } $onClick = ( $url ) ? "onclick=\"location.href='" . $url . "'\"" : ""; // Determine if span element is clickable return '' . $text . ''; } /******************************************************* * DISPLAY FUNCTIONS *******************************************************/ public function display() // displays the pagination bar { if ( $this->_isError() ) // If error encountered, do not display, but display errors { return $this->displayErrors(); } $firstPage = 1; $previousPage = $this->_currentPage - 1; $nextPage = $this->_currentPage + 1; $lastPage = $this->getTotalPages(); $totalpages = $this->getTotalPages(); $pagesPerSection = $this->getPagesPerSection(); $sectionStart = $this->_currentPage - floor( $pagesPerSection / 2 ); // Section start is current page # minus half the # of pages per section if ( $sectionStart <= 0 ) // Adjust section start to 1 (first page) if # pages between 1st page { $sectionStart = 1; } // and current page is less than half the # of pages per section $sectionEnd = $sectionStart + $pagesPerSection - 1; // Section end is # pages per section after section start, // minus 1 (otherwise # of pages per section will exceed given amount by 1) if ( $sectionEnd > $lastPage ) // Adjust section end to last page if section end exceeds last page { $sectionEnd = $lastPage; } $sectionStart = $sectionEnd - $pagesPerSection + 1; // Adjust section start to # of pages behind section end $output = $this->_navBox( "<<", $firstPage, 1 ); // First page $output .= $this->_navBox( "<", $previousPage, 2 ); // Previous page for ( $i = $sectionStart; $i <= $sectionEnd; ++ $i ) { $output .= $this->_navBox( $i, $i ); } // Pagination $output .= $this->_navBox( ">", $nextPage, 3 ); // Next Page $output .= $this->_navBox( ">>", $lastPage, 4 ); // Last Page return $output; } public function displayErrors() // displays the errors encountered { if ( ! $this->_errors ) { return "No errors were encountered."; } $words = ( count( $this->_errors ) > 1 ) ? "errors were" : "error was"; // Determine CSS styling for error reporting if ( $this->_styles["errors"] ) { $css = 'class="' . $this->_styles["errors"] . '"'; } else { $css = ''; } $output = '