Completed
Push — trunk ( 0e7a2e...6a6c5e )
by SuperNova.WS
07:28
created

PagingRenderer   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 117
ccs 0
cts 70
cp 0
rs 10
c 0
b 0
f 0
wmc 16

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A href() 0 7 3
A addPrevNext() 0 6 1
A addEllipsis() 0 6 3
A setDelta() 0 4 1
A addFirstLast() 0 6 1
B render() 0 25 3
A addNumbers() 0 12 3
1
<?php
2
/**
3
 * Created by Gorlum 25.11.2017 21:45
4
 */
5
6
namespace General\Helpers;
7
8
use DBAL\DbSqlPaging;
9
10
class PagingRenderer {
11
  const KEYWORD = 'sheet';
12
13
  /**
14
   * @var string $rootUrl
15
   */
16
  protected $rootUrl = '';
17
18
  /**
19
   * @var DbSqlPaging $pager
20
   */
21
  protected $pager;
22
23
  protected $delta = PAGING_SIZE_MAX_DELTA;
24
25
  protected $result = [];
26
27
  protected $current;
28
  protected $total;
29
30
  protected $from;
31
  protected $to;
32
33
  public function __construct(DbSqlPaging $pager, $rootUrl = '') {
34
    $this->pager = $pager;
35
    $this->rootUrl = URLHelper::addParam($rootUrl, self::KEYWORD);
36
  }
37
38
  /**
39
   * @param int $delta
40
   *
41
   * @return $this
42
   */
43
  public function setDelta($delta) {
44
    $this->delta = $delta;
45
46
    return $this;
47
  }
48
49
  protected function href($pageNum, $link, $style = '', $href = true, $active = true) {
50
    return
51
      [
52
        'HREF'     => $active ? $href : false,
53
        'STYLE'    => $active ? $style : 'inactive',
54
        'PAGE_NUM' => $pageNum,
55
        'TEXT'     => $link,
56
      ];
57
  }
58
59
  protected function addNumbers() {
60
    $this->from = max(
61
      1,
62
      $this->current - $this->delta - max(0, $this->current + $this->delta - $this->total)
63
    );
64
    $this->to = min(
65
      $this->total,
66
      $this->current + $this->delta - min(0, $this->current - $this->delta - 1)
67
    );
68
69
    for ($i = $this->from; $i <= $this->to; $i++) {
70
      array_push($this->result, $this->href($i, $i, $i == $this->current ? 'current' : 'before-after', $i != $this->current));
71
    }
72
  }
73
74
  protected function addEllipsis() {
75
    if ($this->from > 1) {
76
      array_unshift($this->result, $this->href(0, '...', 'ellipsis', false));
77
    }
78
    if ($this->to < $this->total) {
79
      array_push($this->result, $this->href(0, '...', 'ellipsis', false));
80
    }
81
  }
82
83
  protected function addPrevNext() {
84
    $prevActive = $this->current > 1;
85
    array_unshift($this->result, $this->href($this->current - 1, '&lt;&lt;', 'prev-next', $prevActive, $prevActive));
86
87
    $nextActive = $this->current < $this->total;
88
    array_push($this->result, $this->href($this->current + 1, '&gt;&gt;', 'prev-next', $nextActive, $nextActive));
89
  }
90
91
  protected function addFirstLast() {
92
    $firstActive = $this->current != 1;
93
    array_unshift($this->result, $this->href(1, '&#124;&lt;-', 'first-last', $firstActive, $firstActive));
94
95
    $lastActive = $this->current != $this->total;
96
    array_push($this->result, $this->href($this->total, '-&gt;&#124;', 'first-last', $lastActive, $lastActive));
97
  }
98
99
  /**
100
   * @return string
101
   */
102
  public function render() {
103
    $output = '';
104
105
    $this->result = [];
106
107
    $this->current = $this->pager->getCurrentPageNumber();
108
    $this->total = $this->pager->getTotalPages();
109
110
    if ($this->total > 1) {
111
      $this->addNumbers();
112
      $this->addEllipsis();
113
      $this->addPrevNext();
114
      $this->addFirstLast();
115
    }
116
117
    if(!empty($this->result)) {
118
      $template = gettemplate('_paging');
119
      $template->assign_recursive([
120
        'PAGING_ROOT' => $this->rootUrl,
121
        '.'           => ['paging' => $this->result]
122
      ]);
123
      $output = $template->assign_display('_paging');
124
    }
125
126
    return $output;
127
  }
128
129
}
130