226 lines
8.8 KiB
PHP
226 lines
8.8 KiB
PHP
<?php
|
|
/**
|
|
* core/template/pagination.php
|
|
*
|
|
* This class is for managing template pagination.
|
|
*
|
|
* @version 3.0
|
|
* @author Joey Kimsey <Joey@thetempusproject.com>
|
|
* @link https://TheTempusProject.com/Core
|
|
* @license https://opensource.org/licenses/MIT [MIT LICENSE]
|
|
*/
|
|
namespace TheTempusProject\Bedrock\Classes;
|
|
|
|
use TheTempusProject\Houdini\Classes\Template;
|
|
use TheTempusProject\Hermes\Functions\Route as Routes;
|
|
use TheTempusProject\Bedrock\Functions\Input;
|
|
use TheTempusProject\Bedrock\Classes\Config;
|
|
use TheTempusProject\Bedrock\Functions\Check;
|
|
use TheTempusProject\Canary\Bin\Canary as Debug;
|
|
use TheTempusProject\Houdini\Classes\Components;
|
|
use TheTempusProject\Houdini\Classes\Views;
|
|
|
|
class Pagination extends Template {
|
|
//The settings that will not change
|
|
public static $paginationSettings = [];
|
|
|
|
//The instance for each generation
|
|
public static $instance = null;
|
|
|
|
public function __construct() {
|
|
if ( empty( self::$paginationSettings['limit'] ) ) {
|
|
$this->loadSettings();
|
|
}
|
|
// check for user settings
|
|
if ( empty( self::$paginationSettings['perPage'] ) ) {
|
|
self::$paginationSettings['perPage'] = DEFAULT_RESULTS_PER_PAGE;
|
|
if ( ( !empty( self::$paginationSettings['userPerPage'] ) ) && ( self::$paginationSettings['userPerPage'] <= self::$paginationSettings['maxPerPage'] ) ) {
|
|
self::$paginationSettings['perPage'] = self::$paginationSettings['userPerPage'];
|
|
}
|
|
}
|
|
// The query minimum and maximum based on current page and page limit
|
|
if ( self::$paginationSettings['currentPage'] == 1 ) {
|
|
self::$paginationSettings['min'] = 0;
|
|
} else {
|
|
self::$paginationSettings['min'] = ( ( self::$paginationSettings['currentPage'] - 1 ) * self::$paginationSettings['perPage'] );
|
|
}
|
|
// if ( self::$paginationSettings['currentPage'] == 1 ) {
|
|
self::$paginationSettings['max'] = self::$paginationSettings['perPage'];
|
|
// } else {
|
|
// self::$paginationSettings['max'] = ( self::$paginationSettings['currentPage'] * self::$paginationSettings['perPage'] );
|
|
// }
|
|
// The query limit based on our settings here
|
|
self::$paginationSettings['limit'] = [self::$paginationSettings['min'], self::$paginationSettings['max']];
|
|
}
|
|
|
|
private static function loadSettings() {
|
|
Debug::log( 'Loading Pagination Settings.' );
|
|
// hard cap built into system for displaying results
|
|
self::$paginationSettings['maxPerPage'] = MAX_RESULTS_PER_PAGE;
|
|
|
|
// hard cap built into system retrieving results
|
|
self::$paginationSettings['maxQuery'] = Config::getValue( 'database/dbMaxQuery' );
|
|
|
|
// Set max query to the lowest of the three settings since this will modify how many results are possible.
|
|
if ( self::$paginationSettings['maxQuery'] <= self::$paginationSettings['maxPerPage'] ) {
|
|
self::$paginationSettings['maxPerPage'] = self::$paginationSettings['maxQuery'];
|
|
}
|
|
|
|
// Check for results request to set/modify the perPage setting
|
|
if ( Input::exists( 'results' ) ) {
|
|
if ( Check::ID( Input::get( 'results' ) ) ) {
|
|
if ( Input::get( 'results' ) <= self::$paginationSettings['maxPerPage'] ) {
|
|
self::$paginationSettings['perPage'] = Input::get( 'results' );
|
|
}
|
|
}
|
|
}
|
|
if ( empty( self::$paginationSettings['perPage'] ) ) {
|
|
self::$paginationSettings['perPage'] = self::$paginationSettings['maxPerPage'];
|
|
}
|
|
|
|
// Check for pagination in get
|
|
if ( Input::exists( 'page' ) ) {
|
|
if ( Check::ID( Input::get( 'page' ) ) ) {
|
|
self::$paginationSettings['currentPage'] = (int) Input::get( 'page' );
|
|
} else {
|
|
self::$paginationSettings['currentPage'] = 1;
|
|
}
|
|
} else {
|
|
self::$paginationSettings['currentPage'] = 1;
|
|
}
|
|
|
|
if ( ( self::$paginationSettings['currentPage'] - 3 ) > 1 ) {
|
|
self::$paginationSettings['firstPage'] = ( self::$paginationSettings['currentPage'] - 2 );
|
|
} else {
|
|
self::$paginationSettings['firstPage'] = 1;
|
|
}
|
|
}
|
|
|
|
public static function generate() {
|
|
// account for empty values here instead of inside the script.
|
|
Debug::log( 'Creating new Pagination Instance.' );
|
|
self::$instance = new self();
|
|
return self::$instance;
|
|
}
|
|
|
|
public static function updatePaginationTotal( $count ) {
|
|
if ( empty( self::$paginationSettings ) ) {
|
|
self::generate();
|
|
}
|
|
if ( Check::id( $count ) ) {
|
|
Debug::log( 'Pagination: Updating results count' );
|
|
self::$paginationSettings['results'] = $count;
|
|
self::$paginationSettings['totalPages'] = ceil( ( self::$paginationSettings['results'] / self::$paginationSettings['perPage'] ) );
|
|
if ( ( self::$paginationSettings['currentPage'] + 3 ) < self::$paginationSettings['totalPages'] ) {
|
|
self::$paginationSettings['lastPage'] = self::$paginationSettings['currentPage'] + 3;
|
|
} else {
|
|
self::$paginationSettings['lastPage'] = self::$paginationSettings['totalPages'];
|
|
}
|
|
Debug::info( 'Pagination: results update completed.' );
|
|
} else {
|
|
Debug::info( 'Pagination: results update failed.' );
|
|
}
|
|
}
|
|
|
|
public static function paginate() {
|
|
$pageData = [];
|
|
if ( self::firstPage() != 1 ) {
|
|
$data[1]['ACTIVEPAGE'] = '';
|
|
$data[1]['PAGENUMBER'] = 1;
|
|
$data[1]['LABEL'] = 'First';
|
|
$pageData[1] = (object) $data[1];
|
|
}
|
|
for ( $x = self::firstPage(); $x < self::lastPage(); $x++ ) {
|
|
if ( $x == self::currentPage() ) {
|
|
$active = ' class="active"';
|
|
} else {
|
|
$active = '';
|
|
}
|
|
$data[$x]['ACTIVEPAGE'] = $active;
|
|
$data[$x]['PAGENUMBER'] = $x;
|
|
$data[$x]['LABEL'] = $x;
|
|
$pageData[$x] = (object) $data[$x];
|
|
}
|
|
if ( self::lastPage() <= self::totalPages() ) {
|
|
$x = self::totalPages();
|
|
if ( $x == self::currentPage() ) {
|
|
$active = ' class="active"';
|
|
} else {
|
|
$active = '';
|
|
}
|
|
$data[$x]['ACTIVEPAGE'] = $active;
|
|
$data[$x]['PAGENUMBER'] = $x;
|
|
$data[$x]['LABEL'] = 'Last';
|
|
$pageData[$x] = (object) $data[$x];
|
|
}
|
|
$pageData = (object) $pageData;
|
|
|
|
if ( self::totalPages() <= 1 ) {
|
|
Components::set( 'PAGINATION', 'no pagination' );
|
|
} else {
|
|
Components::set( 'PAGINATION', Views::simpleView( 'nav.pagination', $pageData ) );
|
|
}
|
|
}
|
|
|
|
public static function updatePrefs( $pageLimit ) {
|
|
if ( Check::id( $pageLimit ) ) {
|
|
Debug::log( 'Pagination: Updating user pref' );
|
|
self::$paginationSettings['userPerPage'] = $pageLimit;
|
|
} else {
|
|
Debug::info( 'Pagination: User pref update failed.' );
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Getters
|
|
*/
|
|
public static function getMin() {
|
|
if ( isset( self::$paginationSettings['min'] ) ) {
|
|
return self::$paginationSettings['min'];
|
|
} else {
|
|
Debug::info( 'Pagination: Min not found' );
|
|
return 0;
|
|
}
|
|
}
|
|
public static function getMax() {
|
|
if ( isset( self::$paginationSettings['max'] ) ) {
|
|
return self::$paginationSettings['max'];
|
|
} else {
|
|
Debug::info( 'Pagination: Max not found' );
|
|
return 0;
|
|
}
|
|
}
|
|
public static function perPage() {
|
|
if ( !empty( self::$paginationSettings['perPage'] ) ) {
|
|
return self::$paginationSettings['perPage'];
|
|
}
|
|
}
|
|
public static function firstPage() {
|
|
if ( !empty( self::$paginationSettings['firstPage'] ) ) {
|
|
return self::$paginationSettings['firstPage'];
|
|
} else {
|
|
Debug::info( 'Pagination: firstPage not found' );
|
|
}
|
|
}
|
|
public static function lastPage() {
|
|
if ( !empty( self::$paginationSettings['lastPage'] ) ) {
|
|
return self::$paginationSettings['lastPage'];
|
|
} else {
|
|
Debug::info( 'Pagination: lastPage not found' );
|
|
}
|
|
}
|
|
public static function totalPages() {
|
|
if ( !empty( self::$paginationSettings['totalPages'] ) ) {
|
|
return self::$paginationSettings['totalPages'];
|
|
} else {
|
|
Debug::info( 'Pagination: totalPages not found' );
|
|
}
|
|
}
|
|
public static function currentPage() {
|
|
if ( !empty( self::$paginationSettings['currentPage'] ) ) {
|
|
return self::$paginationSettings['currentPage'];
|
|
} else {
|
|
Debug::info( 'Pagination: currentPage not found' );
|
|
}
|
|
}
|
|
} |