| 1 |
<?php if (! defined('ABSPATH')) exit; // Exit if accessed directly |
| 2 |
class HC3_Ui_Element_Pager extends HC3_Ui_Abstract_Collection |
| 3 |
{ |
| 4 |
public function __construct( |
| 5 |
HC3_Ui $ui, |
| 6 |
$to, |
| 7 |
$totalCount, |
| 8 |
$perPage, |
| 9 |
$currentPage = 1 |
| 10 |
) |
| 11 |
{ |
| 12 |
$this->ui = $ui; |
| 13 |
// parent::__construct( $items ); |
| 14 |
|
| 15 |
$this->to = $to; |
| 16 |
|
| 17 |
$this->_totalCount = $totalCount; |
| 18 |
$this->_perPage = $perPage; |
| 19 |
$this->_currentPage = $currentPage; |
| 20 |
|
| 21 |
$parts = array(); |
| 22 |
$disable = array(); |
| 23 |
|
| 24 |
if( $this->currentPage() == 1 ){ |
| 25 |
$disable[] = 'first'; |
| 26 |
$disable[] = 'previous'; |
| 27 |
} |
| 28 |
if( $this->currentPage() == 2 ){ |
| 29 |
$disable[] = 'first'; |
| 30 |
} |
| 31 |
if( $this->currentPage() == $this->numberOfPages() ){ |
| 32 |
$disable[] = 'next'; |
| 33 |
$disable[] = 'last'; |
| 34 |
} |
| 35 |
if( $this->currentPage() == ($this->numberOfPages() - 1) ){ |
| 36 |
$disable[] = 'last'; |
| 37 |
} |
| 38 |
|
| 39 |
$parts_config = array( |
| 40 |
'first' => array( '<<', array('page' => 1) ), |
| 41 |
'previous' => array( '<', array('page' => ($this->currentPage() - 1)) ), |
| 42 |
'next' => array( '>', array('page' => ($this->currentPage() + 1)) ), |
| 43 |
'last' => array( '>>', array('page' => $this->numberOfPages()) ), |
| 44 |
); |
| 45 |
|
| 46 |
foreach( $parts_config as $k => $a ){ |
| 47 |
if( in_array($k, $disable) ){ |
| 48 |
$parts[$k] = $this->ui->makeBlockInline( $a[0] ) |
| 49 |
->tag('border') |
| 50 |
->tag('muted') |
| 51 |
; |
| 52 |
} |
| 53 |
else { |
| 54 |
$parts[$k] = $this->ui->makeAhref( array($this->to, $a[1]), $a[0] ) |
| 55 |
// ->tag('border') |
| 56 |
->tag('secondary') |
| 57 |
; |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
$parts['current'] = $this->ui->makeBlockInline( $this->currentPage() . ' / ' . $this->numberOfPages() ) |
| 62 |
->tag('border') |
| 63 |
->tag('secondary') |
| 64 |
; |
| 65 |
|
| 66 |
parent::__construct( $parts ); |
| 67 |
} |
| 68 |
|
| 69 |
public function render() |
| 70 |
{ |
| 71 |
$out = $this->ui->makeListInline() |
| 72 |
->gutter(1) |
| 73 |
; |
| 74 |
|
| 75 |
$parts = $this->getChildren(); |
| 76 |
$show_order = array('first', 'previous', 'current', 'next', 'last'); |
| 77 |
|
| 78 |
foreach( $show_order as $k ){ |
| 79 |
if( ! isset($parts[$k]) ){ |
| 80 |
continue; |
| 81 |
} |
| 82 |
$out->add( $parts[$k] ); |
| 83 |
} |
| 84 |
|
| 85 |
return $out; |
| 86 |
} |
| 87 |
|
| 88 |
public function numberOfPages() |
| 89 |
{ |
| 90 |
if( ($this->_perPage == 0) || ($this->_totalCount == 0) ){ |
| 91 |
$return = 1; |
| 92 |
} |
| 93 |
else { |
| 94 |
$return = ceil( $this->_totalCount / $this->_perPage ); |
| 95 |
} |
| 96 |
|
| 97 |
return $return; |
| 98 |
} |
| 99 |
|
| 100 |
public function currentPage() |
| 101 |
{ |
| 102 |
return $this->_currentPage; |
| 103 |
} |
| 104 |
} |
| 105 |
|