<?php
// DbPagination.php
// Copyright P.Kassel, M.Seliger, G.Trietsch, all Rights Reserved
namespace Docuform\Database;use DI\Container;use Docuform\Lib\ApiDateTime;use Firebase\JWT\JWT;class DbPagination{public $container;public $pageSize;public $pageToken;public $nextPageToken;public $nextPageLink;public $timestampColumn;public $idColumn;public $alias;public $sortDirection;public function __construct(Container $container,string $timestampColumn,string $idColumn,string $alias='',string $sortDirection='ASC',$maxResults=PAGINATION_MAX_RESULTS){$this->container=$container;$this->timestampColumn=$timestampColumn;$this->idColumn=$idColumn;$this->alias=$alias.(empty($alias)?'':'.');$this->sortDirection=empty($sortDirection)?'ASC':strtoupper($sortDirection);if($maxResults<=0||$maxResults>PAGINATION_MAX_RESULTS){$maxResults=PAGINATION_MAX_RESULTS;}$request=$container->get('RestApi')->getRequest();$this->pageSize=0;if(isset($request->getQueryParams()['pageSize'])){$this->pageSize=(int)$request->getQueryParams()['pageSize'];}elseif($this->pageSize<=0)$this->pageSize=PAGINATION_DEFAULT_PAGESIZE;if($this->pageSize>$maxResults){$this->pageSize=$maxResults;}$this->pageToken='';if(!empty($request->getQueryParams()['pageToken'])){$this->pageToken=JWT::urlsafeB64Decode($request->getQueryParams()['pageToken']);}$this->nextPageToken='';}public function addToWhereClause(string & $whereClause,array & $params):string{if(!empty($this->pageToken)){$database=$this->container->get('database');$timestampColumn=$this->alias.$this->timestampColumn;$idColumn=$this->alias.$this->idColumn;$pageToken=explode('_',$this->pageToken);$sortDirection=(empty($this->sortDirection)||$this->sortDirection==='ASC')?'>':'<';if(count($pageToken)===2){if($pageToken[0]>0){$T=new ApiDateTime();$T->setTimestamp($pageToken[0]);$T=$database->DbDate($T);DbSQL::addCondition($whereClause,"($timestampColumn$sortDirection? OR ($timestampColumn=? AND $idColumn$sortDirection?))");$params[]=$T;$params[]=$T;$params[]=$pageToken[1];}else{DbSQL::addCondition($whereClause,"$idColumn$sortDirection?");$params[]=$pageToken[1];}}}return $whereClause;}public function addOrderBy(string & $orderBy=''):string{if(!empty($this->pageSize)){if(!empty($this->timestampColumn)){$orderBy.=(empty($orderBy)?'':',').$this->alias."$this->timestampColumn $this->sortDirection";}if(!empty($this->idColumn)){$orderBy.=(empty($orderBy)?'':',').$this->alias."$this->idColumn $this->sortDirection";}}else{$orderBy='PrinterIP';}return $orderBy;}public function getFetchLimit():int{return $this->pageSize<=0?0:$this->pageSize+1;}public function paginate(array &$data):void{if(is_array($data)&&!empty($this->pageSize)){$limit=$this->pageSize;if(count($data)>$limit){$lastPageObj=$data[$limit-1];$ColDef=$lastPageObj->findColumnMapping('name',$this->timestampColumn);if($ColDef!==false){$alias=$ColDef['alias'];if(is_array($alias)){$subArray=key($alias);$timestampColumn=$lastPageObj->$subArray[current($alias)];}else{$timestampColumn=$lastPageObj->$alias;}if(!empty($timestampColumn)){$T=new ApiDateTime();$T->fromIso($timestampColumn);$timestampColumn=$T->getTimestamp();}}if(empty($timestampColumn)){$timestampColumn=0;}$ColDef=$lastPageObj->findColumnMapping('name',$this->idColumn);if($ColDef!==false){$alias=$ColDef['alias'];if(is_array($alias)){$subArray=key($alias);$idColumn=$lastPageObj->$subArray[current($alias)];}else{$idColumn=$lastPageObj->$alias;}}if(empty($idColumn)){$idColumn=0;}$this->nextPageToken=JWT::urlsafeB64Encode($timestampColumn.'_'.$idColumn);$request=$this->container->get('RestApi')->getRequest();$query=parse_url($request->getUri(),PHP_URL_QUERY);parse_str(empty($query)?"":$query,$query_array);$query_array['pageToken']=$this->nextPageToken;$this->nextPageLink=current(explode('?',$request->getUri())).'?'.http_build_query($query_array);$data=array_slice($data,0,$limit);}}}}