summaryrefslogtreecommitdiffstats
path: root/library/Pbs/Pagination.php
blob: 504d70cc47643e6b2ac4e95458cda5b8d5e3b7df (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?php
/*
 * Copyright (c) 2011 - OpenSLX GmbH, RZ Uni Freiburg
 * This program is free software distributed under the GPL version 2.
 * See http://gpl.openslx.org/
 *
 * If you have any feedback please consult http://feedback.openslx.org/ and
 * send your suggestions, praise, or complaints to feedback@openslx.org
 *
 * General information about OpenSLX can be found at http://openslx.org/
 */

class Pbs_Pagination {

  private $element;
  private $perpage;
  private $requestpage;
  private $maxNumber;
  private $numpages;
  private $pageUrl;

  public function pagination($url = null, $selected = null, $max = null) {
    if($selected == null)
      { $selected = $this->requestpage; }
    if($max == null)
      { $max = $this->numpages; }
    if($url == null)
      { $url = $this->pageUrl; }

#print_a($url,$selected,$max);

    $str = "<div class='pbs_pagination'>";
    if ( 1 <= $selected) {
      $str . = "<a href='$url/page/0' >&lt;&lt;</a>";
      $str . = "<a href='$url/page/".(($selected) - 1)."' rel='next'>&lt;</a>";
    } else {
      $str . = "<span class='disabled'>&lt;&lt;</span>";
      $str . = "<span class='disabled'>&lt;</span>";
    }

#
    $rightleft = 2;
    for($i = 0; $i < ($max); $i++) {
      if($selected == $i)
        { $str . = "<a class='active'>".($i + 1) ."</a> "; }
      else if($i >= $selected - $rightleft && $i <= $selected + $rightleft)
        { $str . = "<a href='$url/page/".($i)."'>".($i + 1) ."</a> "; }
    }

#
    if ( $max - 2 >= $selected) {
      $str . = "<a href='$url/page/".(($selected) + 1)."'>&gt;</a> ";
      $str . = "<a href='$url/page/".($max - 1)."'>&gt;&gt;</a> ";
    } else {
      $str . = "<span class='disabled'>&gt;</span> ";
      $str . = "<span class='disabled'>&gt;&gt;</span> ";
    }

    $str . = "</div>";
    return $str;
  }
  public function setPerPage($perpage) {
    $this->perpage = $perpage;
    return $this;
  }
  public function getPerPage() {
    return $this->perpage;
  }
  public function setRequestPage($requestpage) {
    if($requestpage < 0 || !is_numeric($requestpage) )
      { $requestpage = 0; }
    if($requestpage >= $this->numpages)
      { $requestpage = $this->numpages - 1; }
    $this->requestpage = $requestpage;
    return $this;
  }
  public function getRequestPage() {
    return $this->requestpage;
  }
  public function setElement($element) {
    $this->element = $element;
    $this->maxNumber = count($element);
    $this->numpages = ceil(count($element) / $this->perpage);
    return $this;
  }
  public function getStartItem() {
    return $this->requestpage * $this->perpage;
  }
  public function getElements() {
    return array_slice($this->element, $this->getStartItem(), $this->getPerPage(), true);
  }
  public function setPageUrl($url) {
    $this->pageUrl = $url;
    return $this;
  }

}