| 1 |
<?php |
|---|
| 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 |
abstract class sfPager |
|---|
| 28 |
{ |
|---|
| 29 |
protected |
|---|
| 30 |
$page = 1, |
|---|
| 31 |
$maxPerPage = 0, |
|---|
| 32 |
$lastPage = 1, |
|---|
| 33 |
$nbResults = 0, |
|---|
| 34 |
$class = '', |
|---|
| 35 |
$tableName = '', |
|---|
| 36 |
$objects = null, |
|---|
| 37 |
$cursor = 1, |
|---|
| 38 |
$parameters = array(), |
|---|
| 39 |
$currentMaxLink = 1, |
|---|
| 40 |
$parameterHolder = null, |
|---|
| 41 |
$maxRecordLimit = false; |
|---|
| 42 |
|
|---|
| 43 |
public function __construct($class, $maxPerPage = 10) |
|---|
| 44 |
{ |
|---|
| 45 |
$this->setClass($class); |
|---|
| 46 |
$this->setMaxPerPage($maxPerPage); |
|---|
| 47 |
$this->parameterHolder = new sfParameterHolder(); |
|---|
| 48 |
} |
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 |
abstract public function init(); |
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 |
abstract public function getResults(); |
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 |
abstract protected function retrieveObject($offset); |
|---|
| 58 |
|
|---|
| 59 |
public function getCurrentMaxLink() |
|---|
| 60 |
{ |
|---|
| 61 |
return $this->currentMaxLink; |
|---|
| 62 |
} |
|---|
| 63 |
|
|---|
| 64 |
public function getMaxRecordLimit() |
|---|
| 65 |
{ |
|---|
| 66 |
return $this->maxRecordLimit; |
|---|
| 67 |
} |
|---|
| 68 |
|
|---|
| 69 |
public function setMaxRecordLimit($limit) |
|---|
| 70 |
{ |
|---|
| 71 |
$this->maxRecordLimit = $limit; |
|---|
| 72 |
} |
|---|
| 73 |
|
|---|
| 74 |
public function getLinks($nb_links = 5) |
|---|
| 75 |
{ |
|---|
| 76 |
$links = array(); |
|---|
| 77 |
$tmp = $this->page - floor($nb_links / 2); |
|---|
| 78 |
$check = $this->lastPage - $nb_links + 1; |
|---|
| 79 |
$limit = ($check > 0) ? $check : 1; |
|---|
| 80 |
$begin = ($tmp > 0) ? (($tmp > $limit) ? $limit : $tmp) : 1; |
|---|
| 81 |
|
|---|
| 82 |
$i = (int) $begin; |
|---|
| 83 |
while (($i < $begin + $nb_links) && ($i <= $this->lastPage)) |
|---|
| 84 |
{ |
|---|
| 85 |
$links[] = $i++; |
|---|
| 86 |
} |
|---|
| 87 |
|
|---|
| 88 |
$this->currentMaxLink = count($links) ? $links[count($links) - 1] : 1; |
|---|
| 89 |
|
|---|
| 90 |
return $links; |
|---|
| 91 |
} |
|---|
| 92 |
|
|---|
| 93 |
public function haveToPaginate() |
|---|
| 94 |
{ |
|---|
| 95 |
return (($this->getMaxPerPage() != 0) && ($this->getNbResults() > $this->getMaxPerPage())); |
|---|
| 96 |
} |
|---|
| 97 |
|
|---|
| 98 |
public function getCursor() |
|---|
| 99 |
{ |
|---|
| 100 |
return $this->cursor; |
|---|
| 101 |
} |
|---|
| 102 |
|
|---|
| 103 |
public function setCursor($pos) |
|---|
| 104 |
{ |
|---|
| 105 |
if ($pos < 1) |
|---|
| 106 |
{ |
|---|
| 107 |
$this->cursor = 1; |
|---|
| 108 |
} |
|---|
| 109 |
else if ($pos > $this->nbResults) |
|---|
| 110 |
{ |
|---|
| 111 |
$this->cursor = $this->nbResults; |
|---|
| 112 |
} |
|---|
| 113 |
else |
|---|
| 114 |
{ |
|---|
| 115 |
$this->cursor = $pos; |
|---|
| 116 |
} |
|---|
| 117 |
} |
|---|
| 118 |
|
|---|
| 119 |
public function getObjectByCursor($pos) |
|---|
| 120 |
{ |
|---|
| 121 |
$this->setCursor($pos); |
|---|
| 122 |
|
|---|
| 123 |
return $this->getCurrent(); |
|---|
| 124 |
} |
|---|
| 125 |
|
|---|
| 126 |
public function getCurrent() |
|---|
| 127 |
{ |
|---|
| 128 |
return $this->retrieveObject($this->cursor); |
|---|
| 129 |
} |
|---|
| 130 |
|
|---|
| 131 |
public function getNext() |
|---|
| 132 |
{ |
|---|
| 133 |
if (($this->cursor + 1) > $this->nbResults) |
|---|
| 134 |
{ |
|---|
| 135 |
return null; |
|---|
| 136 |
} |
|---|
| 137 |
else |
|---|
| 138 |
{ |
|---|
| 139 |
return $this->retrieveObject($this->cursor + 1); |
|---|
| 140 |
} |
|---|
| 141 |
} |
|---|
| 142 |
|
|---|
| 143 |
public function getPrevious() |
|---|
| 144 |
{ |
|---|
| 145 |
if (($this->cursor - 1) < 1) |
|---|
| 146 |
{ |
|---|
| 147 |
return null; |
|---|
| 148 |
} |
|---|
| 149 |
else |
|---|
| 150 |
{ |
|---|
| 151 |
return $this->retrieveObject($this->cursor - 1); |
|---|
| 152 |
} |
|---|
| 153 |
} |
|---|
| 154 |
|
|---|
| 155 |
public function getFirstIndice() |
|---|
| 156 |
{ |
|---|
| 157 |
if ($this->page == 0) |
|---|
| 158 |
{ |
|---|
| 159 |
return 1; |
|---|
| 160 |
} |
|---|
| 161 |
else |
|---|
| 162 |
{ |
|---|
| 163 |
return ($this->page - 1) * $this->maxPerPage + 1; |
|---|
| 164 |
} |
|---|
| 165 |
} |
|---|
| 166 |
|
|---|
| 167 |
public function getLastIndice() |
|---|
| 168 |
{ |
|---|
| 169 |
if ($this->page == 0) |
|---|
| 170 |
{ |
|---|
| 171 |
return $this->nbResults; |
|---|
| 172 |
} |
|---|
| 173 |
else |
|---|
| 174 |
{ |
|---|
| 175 |
if (($this->page * $this->maxPerPage) >= $this->nbResults) |
|---|
| 176 |
{ |
|---|
| 177 |
return $this->nbResults; |
|---|
| 178 |
} |
|---|
| 179 |
else |
|---|
| 180 |
{ |
|---|
| 181 |
return ($this->page * $this->maxPerPage); |
|---|
| 182 |
} |
|---|
| 183 |
} |
|---|
| 184 |
} |
|---|
| 185 |
|
|---|
| 186 |
public function getClass() |
|---|
| 187 |
{ |
|---|
| 188 |
return $this->class; |
|---|
| 189 |
} |
|---|
| 190 |
|
|---|
| 191 |
public function setClass($class) |
|---|
| 192 |
{ |
|---|
| 193 |
$this->class = $class; |
|---|
| 194 |
} |
|---|
| 195 |
|
|---|
| 196 |
public function getNbResults() |
|---|
| 197 |
{ |
|---|
| 198 |
return $this->nbResults; |
|---|
| 199 |
} |
|---|
| 200 |
|
|---|
| 201 |
protected function setNbResults($nb) |
|---|
| 202 |
{ |
|---|
| 203 |
$this->nbResults = $nb; |
|---|
| 204 |
} |
|---|
| 205 |
|
|---|
| 206 |
public function getFirstPage() |
|---|
| 207 |
{ |
|---|
| 208 |
return 1; |
|---|
| 209 |
} |
|---|
| 210 |
|
|---|
| 211 |
public function getLastPage() |
|---|
| 212 |
{ |
|---|
| 213 |
return $this->lastPage; |
|---|
| 214 |
} |
|---|
| 215 |
|
|---|
| 216 |
protected function setLastPage($page) |
|---|
| 217 |
{ |
|---|
| 218 |
$this->lastPage = $page; |
|---|
| 219 |
if ($this->getPage() > $page) |
|---|
| 220 |
{ |
|---|
| 221 |
$this->setPage($page); |
|---|
| 222 |
} |
|---|
| 223 |
} |
|---|
| 224 |
|
|---|
| 225 |
public function getPage() |
|---|
| 226 |
{ |
|---|
| 227 |
return $this->page; |
|---|
| 228 |
} |
|---|
| 229 |
|
|---|
| 230 |
public function getNextPage() |
|---|
| 231 |
{ |
|---|
| 232 |
return min($this->getPage() + 1, $this->getLastPage()); |
|---|
| 233 |
} |
|---|
| 234 |
|
|---|
| 235 |
public function getPreviousPage() |
|---|
| 236 |
{ |
|---|
| 237 |
return max($this->getPage() - 1, $this->getFirstPage()); |
|---|
| 238 |
} |
|---|
| 239 |
|
|---|
| 240 |
public function setPage($page) |
|---|
| 241 |
{ |
|---|
| 242 |
$this->page = intval($page); |
|---|
| 243 |
if ($this->page <= 0) |
|---|
| 244 |
{ |
|---|
| 245 |
|
|---|
| 246 |
$this->page = $this->getMaxPerPage() ? 1 : 0; |
|---|
| 247 |
} |
|---|
| 248 |
} |
|---|
| 249 |
|
|---|
| 250 |
public function getMaxPerPage() |
|---|
| 251 |
{ |
|---|
| 252 |
return $this->maxPerPage; |
|---|
| 253 |
} |
|---|
| 254 |
|
|---|
| 255 |
public function setMaxPerPage($max) |
|---|
| 256 |
{ |
|---|
| 257 |
if ($max > 0) |
|---|
| 258 |
{ |
|---|
| 259 |
$this->maxPerPage = $max; |
|---|
| 260 |
if ($this->page == 0) |
|---|
| 261 |
{ |
|---|
| 262 |
$this->page = 1; |
|---|
| 263 |
} |
|---|
| 264 |
} |
|---|
| 265 |
else if ($max == 0) |
|---|
| 266 |
{ |
|---|
| 267 |
$this->maxPerPage = 0; |
|---|
| 268 |
$this->page = 0; |
|---|
| 269 |
} |
|---|
| 270 |
else |
|---|
| 271 |
{ |
|---|
| 272 |
$this->maxPerPage = 1; |
|---|
| 273 |
if ($this->page == 0) |
|---|
| 274 |
{ |
|---|
| 275 |
$this->page = 1; |
|---|
| 276 |
} |
|---|
| 277 |
} |
|---|
| 278 |
} |
|---|
| 279 |
|
|---|
| 280 |
public function getParameterHolder() |
|---|
| 281 |
{ |
|---|
| 282 |
return $this->parameterHolder; |
|---|
| 283 |
} |
|---|
| 284 |
|
|---|
| 285 |
public function getParameter($name, $default = null) |
|---|
| 286 |
{ |
|---|
| 287 |
return $this->parameterHolder->get($name, $default); |
|---|
| 288 |
} |
|---|
| 289 |
|
|---|
| 290 |
public function hasParameter($name) |
|---|
| 291 |
{ |
|---|
| 292 |
return $this->parameterHolder->has($name); |
|---|
| 293 |
} |
|---|
| 294 |
|
|---|
| 295 |
public function setParameter($name, $value) |
|---|
| 296 |
{ |
|---|
| 297 |
return $this->parameterHolder->set($name, $value); |
|---|
| 298 |
} |
|---|
| 299 |
} |
|---|
| 300 |
|
|---|