Lançado Adianti Framework 7.6!
Clique aqui para saber mais
menu

Adianti Solutions

API

Adianti, Framework, PHP, MVC, Active record, Front controller, IDE, RAD, Web, multiplataforma, geração de código, desenvolvimento rápido, relatórios, formulários, listagens, datagrids, gráficos, banco de dados, padrões de projeto, design patterns API do Adianti Framework.
API Docs
code
Selecione a classe

Source for file TPageNavigation.php

Documentation is available at TPageNavigation.php

  1. <?php
  2. namespace Adianti\Widget\Datagrid;
  3.  
  4. use Adianti\Core\AdiantiCoreTranslator;
  5. use Adianti\Widget\Base\TElement;
  6. use Adianti\Control\TAction;
  7. use Adianti\Widget\Container\TTable;
  8.  
  9. use Exception;
  10.  
  11. /**
  12.  * Page Navigation provides navigation for a datagrid
  13.  *
  14.  * @version    7.4
  15.  * @package    widget
  16.  * @subpackage datagrid
  17.  * @author     Pablo Dall'Oglio
  18.  * @copyright  Copyright (c) 2006 Adianti Solutions Ltd. (http://www.adianti.com.br)
  19.  * @license    http://www.adianti.com.br/framework-license
  20.  */
  21. {
  22.     private $limit;
  23.     private $count;
  24.     private $order;
  25.     private $page;
  26.     private $first_page;
  27.     private $action;
  28.     private $width;
  29.     private $direction;
  30.     private $hidden;
  31.     private $resume;
  32.     
  33.     /**
  34.      * Constructor method
  35.      */
  36.     public function __construct()
  37.     {
  38.         $this->hidden false;
  39.         $this->resume false;
  40.     }
  41.     
  42.     /**
  43.      * Hide
  44.      */
  45.     public function hide()
  46.     {
  47.         $this->hidden true;
  48.     }
  49.     
  50.     /**
  51.      * Enable counters
  52.      */
  53.     public function enableCounters()
  54.     {
  55.         $this->resume true;
  56.     }
  57.     
  58.     /**
  59.      * Get resume string
  60.      */
  61.     private function getResume()
  62.     {
  63.         if!$this->getCount() )
  64.         {
  65.             return AdiantiCoreTranslator::translate('No records found');
  66.         }
  67.         else
  68.         {
  69.             $max number_format( (min(( $this->getLimit($this->getPage() ) $this->getCount())) 0'''.');
  70.             $min number_format( (($this->getLimit(($this->getPage(1) ) 10'''.');
  71.             
  72.             return AdiantiCoreTranslator::translate('^1 to ^2 from ^3 records'$min$maxnumber_format($this->getCount()'''.'));
  73.         }
  74.     }
  75.     
  76.     /**
  77.      * Set the Amount of displayed records
  78.      * @param $limit An integer
  79.      */
  80.     public function setLimit($limit)
  81.     {
  82.         $this->limit  = (int) $limit;
  83.     }
  84.     
  85.     /**
  86.      * Returns the limit of records
  87.      */
  88.     public function getLimit()
  89.     {
  90.         return $this->limit;
  91.     }
  92.     
  93.     /**
  94.      * Define the PageNavigation's width
  95.      * @param $width PageNavigation's width
  96.      */
  97.     public function setWidth($width)
  98.     {
  99.         $this->width $width;
  100.     }
  101.     
  102.     /**
  103.      * Define the total count of records
  104.      * @param $count An integer (the total count of records)
  105.      */
  106.     public function setCount($count)
  107.     {
  108.         $this->count = (int) $count;
  109.     }
  110.     
  111.     /**
  112.      * Return the total count of records
  113.      */
  114.     public function getCount()
  115.     {
  116.         return $this->count;
  117.     }
  118.     
  119.     /**
  120.      * Define the current page
  121.      * @param $page An integer (the current page)
  122.      */
  123.     public function setPage($page)
  124.     {
  125.         $this->page = (int) $page;
  126.     }
  127.     
  128.     /**
  129.      * Returns the current page
  130.      */
  131.     public function getPage()
  132.     {
  133.         return $this->page;
  134.     }
  135.     
  136.     /**
  137.      * Define the first page
  138.      * @param $page An integer (the first page)
  139.      */
  140.     public function setFirstPage($first_page)
  141.     {
  142.         $this->first_page = (int) $first_page;
  143.     }
  144.     
  145.     /**
  146.      * Define the ordering
  147.      * @param $order A string containint the column name
  148.      */
  149.     public function setOrder($order)
  150.     {
  151.         $this->order $order;
  152.     }
  153.     
  154.     /**
  155.      * Define the ordering
  156.      * @param $direction asc, desc
  157.      */
  158.     public function setDirection($direction)
  159.     {
  160.         $this->direction $direction;
  161.     }
  162.     
  163.     /**
  164.      * Set the page navigation properties
  165.      * @param $properties array of properties
  166.      */
  167.     public function setProperties($properties)
  168.     {
  169.         $order      = isset($properties['order'])  addslashes($properties['order'])  '';
  170.         $page       = isset($properties['page'])   $properties['page']   1;
  171.         $direction  (isset($properties['direction']AND in_array($properties['direction']array('asc''desc')))  $properties['direction']   NULL;
  172.         $first_page = isset($properties['first_page']$properties['first_page']1;
  173.         
  174.         $this->setOrder($order);
  175.         $this->setPage($page);
  176.         $this->setDirection($direction);
  177.         $this->setFirstPage($first_page);
  178.     }
  179.     
  180.     /**
  181.      * Define the PageNavigation action
  182.      * @param $action TAction object (fired when the user navigates)
  183.      */
  184.     public function setAction($action)
  185.     {
  186.         $this->action $action;
  187.     }
  188.     
  189.     /**
  190.      * Show the PageNavigation widget
  191.      */
  192.     public function show()
  193.     {
  194.         if ($this->hidden)
  195.         {
  196.             return;
  197.         }
  198.         
  199.         if (!$this->action instanceof TAction)
  200.         {
  201.             throw new Exception(AdiantiCoreTranslator::translate('You must call ^1 before add this component'__CLASS__ . '::' 'setAction()'));
  202.         }
  203.         
  204.         if ($this->resume)
  205.         {
  206.             $total new TElement('div');
  207.             $total->{'class''tpagenavigation_resume';
  208.             $total->add($this->getResume());
  209.             $total->show();
  210.         }
  211.         
  212.         $first_page = isset($this->first_page$this->first_page 1;
  213.         $direction  'asc';
  214.         $page_size  = isset($this->limit$this->limit 10;
  215.         $max 10;
  216.         $registros $this->count;
  217.         
  218.         if (!$registros)
  219.         {
  220.             $registros 0;
  221.         }
  222.         
  223.         if ($page_size 0)
  224.         {
  225.             $pages = (int) ($registros $page_size$first_page +1;
  226.         }
  227.         else
  228.         {
  229.             $pages 1;
  230.         }
  231.         
  232.         $resto 0;
  233.         if ($page_size>0)
  234.         {
  235.             $resto $registros $page_size;
  236.         }
  237.         
  238.         $pages += $resto 0;
  239.         $last_page min($pages$max);
  240.         
  241.         $nav new TElement('nav');
  242.         $nav->{'class''tpagenavigation';
  243.         $nav->{'align''center';
  244.         
  245.         $ul new TElement('ul');
  246.         $ul->{'class''pagination';
  247.         $ul->{'style''display:inline-flex;';
  248.         $nav->add($ul);
  249.         
  250.         if ($first_page 1)
  251.         {
  252.             // first
  253.             $item new TElement('li');
  254.             $link new TElement('a');
  255.             $span new TElement('span');
  256.             $link->{'aria-label''Previous';
  257.             $ul->add($item);
  258.             $item->add($link);
  259.             $link->add($span);
  260.             $this->action->setParameter('offset'0);
  261.             $this->action->setParameter('limit',  $page_size);
  262.             $this->action->setParameter('direction'$this->direction);
  263.             $this->action->setParameter('page',   1);
  264.             $this->action->setParameter('first_page'1);
  265.             $this->action->setParameter('order'$this->order);
  266.  
  267.             $link->{'class'}     "page-link";
  268.             $link->{'href'}      $this->action->serialize();
  269.             $link->{'generator''adianti';
  270.             $span->add(TElement::tag('span'''['class'=>'fa fa-angle-double-left']));
  271.             
  272.             // previous
  273.             $item new TElement('li');
  274.             $link new TElement('a');
  275.             $span new TElement('span');
  276.             $link->{'aria-label''Previous';
  277.             $ul->add($item);
  278.             $item->add($link);
  279.             $link->add($span);
  280.             $this->action->setParameter('offset'($first_page $max -1$page_size);
  281.             $this->action->setParameter('limit',  $page_size);
  282.             $this->action->setParameter('direction'$this->direction);
  283.             $this->action->setParameter('page',   $first_page $max);
  284.             $this->action->setParameter('first_page'$first_page $max);
  285.             $this->action->setParameter('order'$this->order);
  286.  
  287.             $link->{'class'}     "page-link";
  288.             $link->{'href'}      $this->action->serialize();
  289.             $link->{'generator''adianti';
  290.             $span->add(TElement::tag('span'''['class'=>'fa fa-angle-left']))//$span->add('&laquo;');
  291.         }
  292.         
  293.         // active pages
  294.         for ($n $first_page$n <= $last_page $first_page -1$n++)
  295.         {
  296.             $offset ($n -1$page_size;
  297.             $item new TElement('li');
  298.             $link new TElement('a');
  299.             $span new TElement('span');
  300.             $ul->add($item);
  301.             $item->add($link);
  302.             $link->add($span);
  303.             $span->add($n);
  304.             
  305.             $this->action->setParameter('offset'$offset);
  306.             $this->action->setParameter('limit',  $page_size);
  307.             $this->action->setParameter('direction'$this->direction);
  308.             $this->action->setParameter('page',   $n);
  309.             $this->action->setParameter('first_page'$first_page);
  310.             $this->action->setParameter('order'$this->order);
  311.             
  312.             $link->{'href'}      $this->action->serialize();
  313.             $link->{'generator''adianti';
  314.             $link->{'class'}     'page-link';
  315.  
  316.             if($this->page == $n)
  317.             {
  318.                 $item->{'class''active page-item';
  319.             }
  320.         }
  321.         
  322.         // inactive pages/placeholders
  323.         for ($z=$n$z<=10$z++)
  324.         {
  325.             $item new TElement('li');
  326.             $link new TElement('a');
  327.             $span new TElement('span');
  328.             $item->{'class''off page-item';
  329.             $link->{'class''page-link';
  330.             $ul->add($item);
  331.             $item->add($link);
  332.             $link->add($span);
  333.             $span->add($z);
  334.         }
  335.         
  336.         if ($pages $max)
  337.         {
  338.             // next
  339.             $first_page $n;
  340.             $item new TElement('li');
  341.             $link new TElement('a');
  342.             $span new TElement('span');
  343.             $link->{'aria-label'"Next";
  344.             $ul->add($item);
  345.             $item->add($link);
  346.             $link->add($span);
  347.             $this->action->setParameter('offset',  ($n -1$page_size);
  348.             $this->action->setParameter('limit',   $page_size);
  349.             $this->action->setParameter('direction'$this->direction);
  350.             $this->action->setParameter('page',    $n);
  351.             $this->action->setParameter('first_page'$n);
  352.             $this->action->setParameter('order'$this->order);
  353.             $link->{'class'}     "page-link";
  354.             $link->{'href'}      $this->action->serialize();
  355.             $link->{'generator''adianti';
  356.             $span->add(TElement::tag('span'''['class'=>'fa fa-angle-right']))//$span->add('&raquo;');
  357.             
  358.             // last
  359.             $item new TElement('li');
  360.             $link new TElement('a');
  361.             $span new TElement('span');
  362.             $link->{'aria-label'"Next";
  363.             $ul->add($item);
  364.             $item->add($link);
  365.             $link->add($span);
  366.             $this->action->setParameter('offset',  ceil($registros $page_size)$page_size $page_size);
  367.             $this->action->setParameter('limit',   $page_size);
  368.             $this->action->setParameter('direction'$this->direction);
  369.             $this->action->setParameter('page',    ceil($registros $page_size));
  370.             $this->action->setParameter('first_page'(int) ($registros ($page_size *10)) *10 +1);
  371.             $this->action->setParameter('order'$this->order);
  372.             $link->{'class'}     "page-link";
  373.             $link->{'href'}      $this->action->serialize();
  374.             $link->{'generator''adianti';
  375.             $span->add(TElement::tag('span'''['class'=>'fa fa-angle-double-right']));
  376.         }
  377.         
  378.         $nav->show();
  379.     }
  380. }