<?php



/**

 * Page.class [HELPER]

 * Realiza a gestão e a paginação de resultados do sistema!

 * 

 * @copyright (c) year, Welton Thierree TagPage

 */

class Pager {



    /** DEFINE O PAGER */

    private $Page;

    Private $Limit;

    private $Offset;



    /** REALIZA A LEITURA */

    private $Tabela;

    private $Termos;

    private $Places;

    private $Select;

    private $Parses;



    /** DEFINE O PAGINATOR */

    private $Rows;

    private $Link;

    private $MaxLinks;

    private $First;

    private $Last;



    /** RENDERIZA O PAGINATOR */

    private $Paginator;



    function __construct($Link, $First = null, $Last = null, $MaxLinks = null) {

        $this->Link = (string) $Link;

        $this->First = ( (string) $First ? $First : '<');

        $this->Last = ( (string) $Last ? $Last : '>');

        $this->MaxLinks = ( (int) $MaxLinks ? $MaxLinks : 5);

    }



    public function ExePager($Page, $Limit) {

        $this->Page = ( (int) $Page ? $Page : 1);

        $this->Limit = (int) $Limit;

        $this->Offset = ($this->Page * $this->Limit) - $this->Limit;

    }



    public function ReturnPage() {

        if ($this->Page > 1):

            $nPage = $this->Page - 1;

            header("Location: {$this->Link}{$nPage}");

        endif;

    }



    function getPage() {

        return $this->Page;

    }



    function getLimit() {

        return $this->Limit;

    }



    function getOffset() {

        return $this->Offset;

    }



    public function ExePaginator($Tabela, $Termos = null, $ParseString = null) {

        $this->Tabela = (string) $Tabela;

        $this->Termos = (string) $Termos;

        $this->Places = (string) $ParseString;

        $this->getSintax();

    }

    

    public function FullPaginator($Query, $ParseString = null){

        $this->Select = (string) $Query;

        $this->Parses = (string) $ParseString;

        

        $this->getFullSintax();

    }



    public function getPaginator() {

        return $this->Paginator;

    }



    //PRIVATE



    private function getSintax() {

        $read = new Read;

        $read->ExeRead($this->Tabela, $this->Termos, $this->Places);

        $this->Rows = $read->getRowCount();



        if ($this->Rows > $this->Limit):

            $Paginas = ceil($this->Rows / $this->Limit);

            $MaxLinks = $this->MaxLinks;



            $this->Paginator = "<ul class=\"pagination pull-right\">";

            $this->Paginator .= "<li class=\"page-first\"><a title=\"{$this->First}\" href=\"{$this->Link}1\">{$this->First}</a></li>";



            for ($iPag = $this->Page - $MaxLinks; $iPag <= $this->Page - 1; $iPag ++):

                if ($iPag >= 1):

                    $this->Paginator .= "<li class=\"page-number\"><a title=\"{$iPag}\" href=\"{$this->Link}{$iPag}\">{$iPag}</a></li>";

                endif;

            endfor;

            

            $this->Paginator .= "<li class=\"page-number active\"><span class=\"active\">{$this->Page}</span></li>";

            

            for ($dPag = $this->Page + 1; $dPag <= $this->Page + $MaxLinks; $dPag ++):

                if ($dPag <= $Paginas):

                    $this->Paginator .= "<li class=\"page-number\"><a title=\"{$dPag}\" href=\"{$this->Link}{$dPag}\">{$dPag}</a></li>";

                endif;

            endfor;

            

            $this->Paginator .= "<li class=\"page-last\"><a title=\"{$this->Last}\" href=\"{$this->Link}{$Paginas}\">{$this->Last}</a></li>";

            $this->Paginator .= "</ul>";

        endif;

    }

    

    private function getFullSintax() {

        $read = new Read;

        $read->FullRead($this->Select, $this->Parses);

        $this->Rows = $read->getRowCount();



        if ($this->Rows > $this->Limit):

            $Paginas = ceil($this->Rows / $this->Limit);

            $MaxLinks = $this->MaxLinks;



            $this->Paginator = "<ul class=\"pagination pull-right\">";

            $this->Paginator .= "<li class=\"page-first\"><a class=\"transition\" title=\"{$this->First}\" href=\"{$this->Link}1\">{$this->First}</a></li>";



            for ($iPag = $this->Page - $MaxLinks; $iPag <= $this->Page - 1; $iPag ++):

                if ($iPag >= 1):

                    $this->Paginator .= "<li class=\"page-number\"><a class=\"transition\" title=\"{$iPag}\" href=\"{$this->Link}{$iPag}\">{$iPag}</a></li>";

                endif;

            endfor;

            

            $this->Paginator .= "<li class=\"page-number active\"><span class=\"active\">{$this->Page}</span></li>";

            

            for ($dPag = $this->Page + 1; $dPag <= $this->Page + $MaxLinks; $dPag ++):

                if ($dPag <= $Paginas):

                    $this->Paginator .= "<li class=\"page-number\"><a class=\"transition\" title=\"{$dPag}\" href=\"{$this->Link}{$dPag}\">{$dPag}</a></li>";

                endif;

            endfor;

            

            $this->Paginator .= "<li class=\"page-last\"><a class=\"transition\" title=\"{$this->Last}\" href=\"{$this->Link}{$Paginas}\">{$this->Last}</a></li>";

            $this->Paginator .= "</ul>";

        endif;

    }

    

}

