<?php

/**
 * Class for handling data pagination (help with splitting specified large data set into chunks)
 */
final class VAPPV_TTW_UI_Data_Paginator {

	/**
	 * Lister button left and right offset
	 */
	public static $lister_lr_offset = 5;

	/**
	 * Pages max series, length
	 */
	public static $pages_sequence_max_length = 12;

	/**
	 * Paginate data based on entries total count, data offset and entities limit per page
	 *
	 * @param int $entities_total_count
	 * @param int $entities_offset
	 * @param int $entities_limit_per_page
	 *
	 * @return array
	 * @throws Exception
	 */
	public static function paginate(
		int $entities_total_count,
		int $entities_offset,
		int $entities_limit_per_page
	): array {

		//Calculate initial data
		$start_lister_page_nr = 0;
		$pages_total_count = ceil( $entities_total_count / $entities_limit_per_page );
		$current_page_nr = ceil( $entities_offset / $entities_limit_per_page );
		$postfix_data_offset  = ( $pages_total_count - 1 ) * $entities_limit_per_page;

		//Get "start" and "end" pagination pages
		$start_lister_page_nr = $current_page_nr - VAPPV_TTW_UI_Data_Paginator::$lister_lr_offset;
		$end_lister_page_nr = $start_lister_page_nr + VAPPV_TTW_UI_Data_Paginator::$pages_sequence_max_length;

		if ( $start_lister_page_nr + VAPPV_TTW_UI_Data_Paginator::$pages_sequence_max_length >= $pages_total_count ) {
			$start_lister_page_nr -= VAPPV_TTW_UI_Data_Paginator::$lister_lr_offset;
		}
		if ( $start_lister_page_nr < 0 ) {
			$start_lister_page_nr = 0;
		}
		if ( $end_lister_page_nr > $pages_total_count ) {
			$end_lister_page_nr = $pages_total_count;
		}

		//Create pagination pages array
		$paginated_pages = array();
		for ( $i = $start_lister_page_nr; $i < $end_lister_page_nr; $i++ ) {
			$next_page = array();
			$next_page['PageNr'] = $i + 1;
			$next_page['DataOffset'] = $i * $entities_limit_per_page;
			if ( (int)$i === (int)$current_page_nr ) {
				$next_page['IsActivePage'] = true;
			} else {
				$next_page['IsActivePage'] = false;
			}
			array_push( $paginated_pages, $next_page );
		}

		//Decide to write a postfix and prefix
		$DrawPaginationPostfix = false;
		if ( $pages_total_count <= VAPPV_TTW_UI_Data_Paginator::$pages_sequence_max_length ) {
			if ( $pages_total_count - $current_page_nr >= VAPPV_TTW_UI_Data_Paginator::$lister_lr_offset ) {
				if ( (int)$pages_total_count !== (int)$paginated_pages[ count( $paginated_pages ) - 1 ]['PageNr'] ) {
					$DrawPaginationPostfix = true;
				}
			}
		} else {
			$DrawPaginationPostfix = $start_lister_page_nr + VAPPV_TTW_UI_Data_Paginator::$pages_sequence_max_length < $pages_total_count;
		}

		$draw_pagination_prefix = $start_lister_page_nr != 0;

		//Pack results
		$result = array();

		$result['RequestedOffset']       = $entities_offset;
		$result['RequestedLimit']        = $entities_limit_per_page;
		$result['ItemsTotalCount']       = $entities_total_count;
		$result['PagesTotalCount']       = $pages_total_count;
		$result['CurrentPageNr']         = $current_page_nr;
		$result['DisplayingPages']       = $paginated_pages;
		$result['DrawPaginationPrefix']  = $draw_pagination_prefix;
		$result['DrawPaginationPostfix'] = $DrawPaginationPostfix;
		$result['PostfixDataOffset']     = $postfix_data_offset;

		return $result;
	}
}

/*
	© 2024 Copyright Vitalii A. Popov. All rights reserved.
*/

?>