<?php
/**
 * Register all actions and filters for the plugin
 *
 * @link       https://orbicular.media
 * @since      1.0.1
 *
 * @package    Cemetery_Sexton
 * @subpackage Cemetery_Sexton/public
 * 
 * @class            Custom Pagination adapted from GenericEasyPagination
 * @description      This class is meant to present the records of a database query split between multiple pages
 * @autor            Bryan Meeks and Olavo Alexandrino
 * @contact          bryan.meeks@ihstech.net and oalexandrino@yahoo.com.br
 * @date             Feb 2010
 * @ps               Class CustomPagination is tested using Class ADODB (http://php.weblogs.com/adodb#downloads)
 * @dataBaseTested   Microsoft SQL Server 2003 (using Class ADODB)
 */
Class Cemetery_Sexton_Pagination {
    
    var $getsVars;
    var $recordByPage;
    var $PagesFound;
    var $totalRecords;
    var $currentPage;
    var $msg_initialPage;
    var $msg_finalPage;
    var $msg_previousPage;
    var $msg_nextPage;
    var $msg_next10Results;
    var $msg_previous10Results;
    var $msg_page;
    var $msg_of;
    var $msg_to;
    var $msg_even;
    
    //Setting Language, Current Page and Records by Page
    function CustomPagination($pageno,$recordByPage) {
        $this->_configCurrentPage($pageno);
        $this->_setRecordByPage($recordByPage);
        $this->msg_initialPage   = "Initial Page";
        $this->msg_finalPage     = "Final Page";
        $this->msg_previousPage  = "Previous";
        $this->msg_nextPage      = "Next";
        $this->msg_next10Results = "Next 10 Pages";
        $this->msg_previous10Results = "Previous 10 Pages";
        $this->msg_page          = "Go to Page";
        $this->msg_of            = "";
        $this->msg_to            = "to";
        
    }
    
    //Setting current page
    function _configCurrentPage($pageno) {
        if (empty($pageno)) {
            $this->currentPage = 1;
        }else{
            $this->currentPage = $pageno;
        }
    }
    
    //Setting the records listed in each page
    function _setRecordByPage($recordByPage) {
        $this->recordByPage = $recordByPage;
    }
    
    //Setting the total number of records retrieved
    function setTotalRecords($totalRecords) {
        $this->totalRecords = $totalRecords;
        $this->PagesFound = ceil($this->totalRecords / $this->recordByPage);
    }
    
    //Defining the additional parameters that may be necessary on the search and will be passed between pages with the GET method
    function setGetVars($getsVars) {
        $this->getsVars = $getsVars;
    }
    
    
    //Outputs the total number of records
    function getTotalRecords() {
        return $this->totalRecords;
    }
    
    //Outputs the total number of pages
    function getPagesFound() {
        return $this->PagesFound;
    }
    
    //Outputs the records list based in current page
    function getListCurrentRecords() {
        $regFinal = $this->recordByPage * $this->currentPage;
        $regInicial = $regFinal - $this->recordByPage;
        
        if ($regInicial == 0) {
            $regInicial++;
        }
        if ($this->currentPage == $this->PagesFound) {
            $regFinal = $this->totalRecords;
        }
        if ($this->currentPage > 1){
            $regInicial++;
        }
        $result = $this->msg_of." $regInicial ".$this->msg_to." $regFinal";
        return $result;
    }
    
    // Renders table headers for paginaiton
    function renderTableHeader($name,$orderby,$currentorder,$sorttype,$pagename,$recpage,$pageparams) {
        $currentpage = $this->currentPage;
        $link = "<a href='?pagename=".$pagename."&pageno=".$currentpage."&orderby=".$orderby."&sorttype=".$sorttype."&recpage=".$recpage."&".$pageparams."' title='Sort by ".$name."'>".$name."</a>";
        if( $orderby == $currentorder & $sorttype == 'DESC'){
            $link .= '<i class="fas fa-sort-amount-down"></i>';
        }else if( $orderby == $currentorder & $sorttype == 'ASC'){
            $link .= '<i class="fas fa-sort-amount-up"></i>';
        }
        //$link = "<a href='?pagename=".$pagename."&pageno=".$currentpage."&orderby=".$orderby."&sorttype=".$sorttype."&recpage=".$recpage."&".$pageparams."' title='Sort by ".$name."'>".$name."</a>";
        echo $link;
    }
    
    //Render pagination footer navigation links
    function renderNavigation($orderby,$sorttype,$pagename,$recpage,$pageparams) {
        $result = "";
        $previous = $this->currentPage - 1;
        $next     = $this->currentPage + 1;
        
        if ($this->PagesFound>1){
            //First
            if ($this->currentPage!=1){
                $result .= "<a href='?pagename=".$pagename."&pageno=1&orderby=".$orderby."&sorttype=".$sorttype."&recpage=".$recpage."&".$pageparams."' title='".$this->msg_initialPage."'>First</a>";
            }
            //Previous
            if ($this->currentPage>1){
                $result .= "<a href='?pagename=".$pagename."&pageno=".$previous."&orderby=".$orderby."&sorttype=".$sorttype."&recpage=".$recpage."&".$pageparams."'>Previous</a> ";
            }
            
            //Numbers 1-10,11-20,etc...
            $totalRecordsControl = $this->totalRecords;
            if (($totalRecordsControl%$this->recordByPage!=0)) {
                while($totalRecordsControl%$this->recordByPage!=0) {
                    $totalRecordsControl++;
                }
            }
            $ultimo = substr($this->currentPage,-1);
            if ($ultimo == 0) {
                $begin = ($this->currentPage-9);
                $pageInicial = ($this->currentPage - $ultimo);
                $end = $this->currentPage;
            }else{
                $pageInicial = ($this->currentPage - $ultimo);
                $begin = ($this->currentPage-$ultimo)+1;
                $end = $pageInicial+10;
            }
            $num = $this->PagesFound;
            if ($end>$num) {
                $end = $num;
            }
            for ($a = $begin; $a <= $end ; $a++) {
                if ($a!=$this->currentPage) {
                    $result .= "<a href='?pagename=".$pagename."&pageno=".$a."&orderby=".$orderby."&sorttype=".$sorttype."&recpage=".$recpage."&".$pageparams."'>".$a."</a> ";
                }else{
                    $result .= "<strong> ".$a." </strong>";
                }
            }
            //Next
            if (($this->currentPage < $this->PagesFound) && ($this->PagesFound >= 1)) {
                $result .= "<a href='?pagename=".$pagename."&pageno=".$next."&orderby=".$orderby."&sorttype=".$sorttype."&recpage=".$recpage."&".$pageparams."'>Next</a> ";
            }
            //Last
            if (($this->PagesFound>1)&&($this->currentPage!=$this->PagesFound)) {
                $result .= "<a href='?pagename=".$pagename."&pageno=".$this->PagesFound."&orderby=".$orderby."&sorttype=".$sorttype."&recpage=".$recpage."&".$pageparams."' title='".$this->msg_finalPage."'>Last</a> ";
            }
            return $result;
        }
    }
    
    //Display Records x to xx of xxx
    function displayResultInfo() {
        $display = 'Results '.$this->getListCurrentRecords().' of '.$this->getTotalRecords();
        return $display;
    }
    
    //Display Page x of xx
    function displayPageInfo() {
        $display = 'Page '.$this->currentPage.' of '.$this->getPagesFound();
        return $display;
    }
    
    //Render Show 25 50 75 100 Results
    function renderResultsByPage($orderby,$sorttype,$pagename,$recpage,$pageparams) {
        $currentpage = $this->currentPage;
        $rstotal = $this->getTotalRecords();
        
        if ($rstotal >= $recpage) {
            $result = 'Show';
            $option = array('25','50','75','100');
            foreach ($option as $recoption) {
                if ($rstotal >= $recoption) {
                    if ($recpage == $recoption && $rstotal <> $recoption) {
                        $result .= '<strong> '.$recoption.' </strong>';
                    }else if($rstotal <> $recoption){
                        $result .= " ";
                        $result .= "<a href='?pagename=".$pagename."&pageno=1&orderby=".$orderby."&sorttype=".$sorttype."&recpage=".$recoption."&".$pageparams."'>".$recoption."</a>";
                        $result .= " ";
                    }
                }
            }
            if ($rstotal < 100 && $rstotal <> $recpage) {
                $result .= " <a href='?pagename=".$pagename."&pageno=1&orderby=".$orderby."&sorttype=".$sorttype."&recpage=".$rstotal."&".$pageparams."'>All</a> ";
            }
            if ($rstotal == $recpage) {
                $result .= " <strong>All</strong> ";
            }
            $result .= 'Results';
        }else {
            $result = 'All Results Shown';
        }
        return $result;
    }
}
?>