Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/*
3
 *  $Id: PropelPager.php 1262 2009-10-26 20:54:39Z francois $
4
 *
5
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
6
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
7
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
8
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
9
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
10
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
11
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
12
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
13
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
14
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
15
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
16
 *
17
 * This software consists of voluntary contributions made by many individuals
18
 * and is licensed under the LGPL. For more information please see
19
 * <http://propel.phpdb.org>.
20
 */
21
 
22
/**
23
 *  PropelPager
24
 *
25
 *  Example Usage:
26
 *
27
 *  require_once 'propel/util/PropelPager.php';
28
 *  require_once 'PEACH/Propel/Poem/poemPeer.php';
29
 *
30
 *  $c = new Criteria();
31
 *  $c->addDescendingOrderByColumn(poemPeer::SID);
32
 *
33
 *  // with join
34
 *  $pager = new PropelPager($c, 'poemPeer', 'doSelectJoinPoemUsers', 1, 50);
35
 *
36
 *  // without Join
37
 *
38
 *  $pager = new PropelPager($c, 'poemPeer', 'doSelect', 1, 50);
39
 *
40
 * Some template:
41
 *
42
 * <p>
43
 * Total Pages: <?=$pager->getTotalPages()?>  Total Records: <?=$pager->getTotalRecordCount()?>
44
 * </p>
45
 * <table>
46
 * <tr>
47
 * <td>
48
 * <?if ($link = $pager->getFirstPage):?>
49
 * <a href="somescript?page=<?=$link?>"><?=$link?></a>|
50
 * <?endif?>
51
 * </td>
52
 * <td>
53
 * <?if ($link = $pager->getPrev()):?>
54
 * <a href="somescript?page=<?=$link?>">Previous</a>|
55
 * <?endif?>
56
 * </td>
57
 * <td>
58
 * <?foreach ($pager->getPrevLinks() as $link):?>
59
 * <a href="somescript?page=<?=$link?>"><?=$link?></a>|
60
 * <?endforeach?>
61
 * </td>
62
 * <td><?=$pager->getPage()?></td>
63
 * <td>
64
 * <?foreach ($pager->getNextLinks() as $link):?>
65
 * | <a href="somescript?page=<?=$link?>"><?=$link?></a>
66
 * <?endforeach?>
67
 * </td>
68
 * <td>
69
 * <?if ($link = $pager->getNext()):?>
70
 * <a href="somescript?page=<?=$link?>">Last</a>|
71
 * <?endif?>
72
 * </td>
73
 * <td>
74
 * <?if ($link = $pager->getLastPage()):?>
75
 * <a href="somescript?page=<?=$link?>"><?=$link?></a>|
76
 * <?endif?>
77
 * </td>
78
 * </tr>
79
 * </table>
80
 * <table id="latestPoems">
81
 * <tr>
82
 * <th>Title</th>
83
 * <th>Auteur</th>
84
 * <th>Date</th>
85
 * <th>comments</th>
86
 * </tr>
87
 * <?foreach ($pager->getResult() as $poem):?>
88
 * <tr>
89
 * <td><?=$poem->getTitle()?></td>
90
 * <td><?=$poem->getPoemUsers()->getUname()?></td>
91
 * <td><?=$poem->getTime()?></td>
92
 * <td><?=$poem->getComments()?></td>
93
 * </tr>
94
 * <?endforeach?>
95
 * </table>
96
 *
97
 *
98
 * @author     Rob Halff <info@rhalff.com>
99
 * @author	   Niklas Närhinen <niklas@narhinen.net>
100
 * @version    $Revision: 1262 $
101
 * @copyright  Copyright (c) 2004 Rob Halff: LGPL - See LICENCE
102
 * @package    propel.util
103
 */
104
class PropelPager implements Countable, Iterator {
105
 
106
	private $recordCount;
107
	private $pages;
108
	private $peerClass;
109
	private $peerSelectMethod;
110
	private $peerCountMethod;
111
	private $criteria;
112
	private $countCriteria;
113
	private $page;
114
	private $rs = null;
115
 
116
	//Iterator vars
117
	private $currentKey = 0;
118
 
119
	/** @var        int Start row (offset) */
120
	protected $start = 0;
121
 
122
	/** @var        int Max rows to return (0 means all) */
123
	protected $max = 0;
124
 
125
	/**
126
	 * Create a new Propel Pager.
127
	 * @param      Criteria $c
128
	 * @param      string $peerClass The name of the static Peer class.
129
	 * @param      string $peerSelectMethod The name of the static method for selecting content from the Peer class.
130
	 * @param      int $page The current page (1-based).
131
	 * @param      int $rowsPerPage The number of rows that should be displayed per page.
132
	 */
133
	public function __construct($c = null, $peerClass = null, $peerSelectMethod = null, $page = 1, $rowsPerPage = 25)
134
	{
135
		if (!isset($c)) {
136
			$c = new Criteria();
137
		}
138
		$this->setCriteria($c);
139
		$this->setPeerClass($peerClass);
140
		$this->setPeerSelectMethod($peerSelectMethod);
141
		$this->guessPeerCountMethod();
142
		$this->setPage($page);
143
		$this->setRowsPerPage($rowsPerPage);
144
	}
145
 
146
	/**
147
	 * Set the criteria for this pager.
148
	 * @param      Criteria $c
149
	 * @return     void
150
	 */
151
	public function setCriteria(Criteria $c)
152
	{
153
		$this->criteria = $c;
154
	}
155
 
156
	/**
157
	 * Return the Criteria object for this pager.
158
	 * @return     Criteria
159
	 */
160
	public function getCriteria()
161
	{
162
		return $this->criteria;
163
	}
164
 
165
	/**
166
	 * Set the Peer Classname
167
	 *
168
	 * @param      string $class
169
	 * @return     void
170
	 */
171
	public function setPeerClass($class)
172
	{
173
		$this->peerClass = $class;
174
	}
175
 
176
	/**
177
	 * Return the Peer Classname.
178
	 * @return     string
179
	 */
180
	public function getPeerClass()
181
	{
182
		return $this->peerClass;
183
	}
184
 
185
	/**
186
	 * Set the Peer select method.
187
	 * This exists for legacy support, please use setPeerSelectMethod().
188
	 * @param      string $method The name of the static method to call on the Peer class.
189
	 * @return     void
190
	 * @see        setPeerSelectMethod()
191
	 * @deprecated
192
	 */
193
	public function setPeerMethod($method)
194
	{
195
		$this->setPeerSelectMethod($method);
196
	}
197
 
198
	/**
199
	 * Return the Peer select method.
200
	 * This exists for legacy support, please use getPeerSelectMethod().
201
	 * @return     string
202
	 * @see        getPeerSelectMethod()
203
	 * @deprecated
204
	 */
205
	public function getPeerMethod()
206
	{
207
		return $this->getPeerSelectMethod();
208
	}
209
 
210
	/**
211
	 * Set the Peer select method.
212
	 *
213
	 * @param      string $method The name of the static method to call on the Peer class.
214
	 * @return     void
215
	 */
216
	public function setPeerSelectMethod($method)
217
	{
218
		$this->peerSelectMethod = $method;
219
	}
220
 
221
	/**
222
	 * Return the Peer select method.
223
	 * @return     string
224
	 */
225
	public function getPeerSelectMethod()
226
	{
227
		return $this->peerSelectMethod;
228
	}
229
 
230
	/**
231
	 * Sets the Count method.
232
	 * This is set based on the Peer method, for example if Peer method is doSelectJoin*() then the
233
	 * count method will be doCountJoin*().
234
	 * @param      string $method The name of the static method to call on the Peer class.
235
	 */
236
	public function setPeerCountMethod($method)
237
	{
238
		$this->peerCountMethod = $method;
239
	}
240
 
241
	/**
242
	 * Return the Peer count method.
243
	 */
244
	public function getPeerCountMethod()
245
	{
246
		return $this->peerCountMethod;
247
	}
248
 
249
	/**
250
	 * Guesses the Peer count method based on the select method.
251
	 */
252
	private function guessPeerCountMethod()
253
	{
254
		$selectMethod = $this->getPeerSelectMethod();
255
		if ($selectMethod == 'doSelect') {
256
			$countMethod = 'doCount';
257
		} elseif ( ($pos = stripos($selectMethod, 'doSelectJoin')) === 0) {
258
			$countMethod = 'doCount' . substr($selectMethod, strlen('doSelect'));
259
		} else {
260
			// we will fall back to doCount() if we don't understand the join
261
			// method; however, it probably won't be accurate.  Maybe triggering an error would
262
			// be appropriate ...
263
			$countMethod = 'doCount';
264
		}
265
		$this->setPeerCountMethod($countMethod);
266
	}
267
 
268
	/**
269
	 * Get the paged resultset
270
	 *
271
	 * @return     mixed $rs
272
	 */
273
	public function getResult()
274
	{
275
		if (!isset($this->rs)) {
276
			$this->doRs();
277
		}
278
 
279
		return $this->rs;
280
	}
281
 
282
	/**
283
	 * Get the paged resultset
284
	 *
285
	 * Main method which creates a paged result set based on the criteria
286
	 * and the requested peer select method.
287
	 *
288
	 */
289
	private function doRs()
290
	{
291
		$this->criteria->setOffset($this->start);
292
		$this->criteria->setLimit($this->max);
293
		$this->rs = call_user_func(array($this->getPeerClass(), $this->getPeerSelectMethod()), $this->criteria);
294
	}
295
 
296
	/**
297
	 * Get the first page
298
	 *
299
	 * For now I can only think of returning 1 always.
300
	 * It should probably return 0 if there are no pages
301
	 *
302
	 * @return     int 1
303
	 */
304
	public function getFirstPage()
305
	{
306
		return '1';
307
	}
308
 
309
	/**
310
	 * Convenience method to indicate whether current page is the first page.
311
	 *
312
	 * @return     boolean
313
	 */
314
	public function atFirstPage()
315
	{
316
		return $this->getPage() == $this->getFirstPage();
317
	}
318
 
319
	/**
320
	 * Get last page
321
	 *
322
	 * @return     int $lastPage
323
	 */
324
	public function getLastPage()
325
	{
326
		$totalPages = $this->getTotalPages();
327
		if ($totalPages == 0) {
328
			return 1;
329
		} else {
330
			return $totalPages;
331
		}
332
	}
333
 
334
	/**
335
	 * Convenience method to indicate whether current page is the last page.
336
	 *
337
	 * @return     boolean
338
	 */
339
	public function atLastPage()
340
	{
341
		return $this->getPage() == $this->getLastPage();
342
	}
343
 
344
	/**
345
	 * get total pages
346
	 *
347
	 * @return     int $this->pages
348
	 */
349
	public function getTotalPages() {
350
		if (!isset($this->pages)) {
351
			$recordCount = $this->getTotalRecordCount();
352
			if ($this->max > 0) {
353
					$this->pages = ceil($recordCount/$this->max);
354
			} else {
355
					$this->pages = 0;
356
			}
357
		}
358
		return $this->pages;
359
	}
360
 
361
	/**
362
	 * get an array of previous id's
363
	 *
364
	 * @param      int $range
365
	 * @return     array $links
366
	 */
367
	public function getPrevLinks($range = 5)
368
	{
369
		$total = $this->getTotalPages();
370
		$start = $this->getPage() - 1;
371
		$end = $this->getPage() - $range;
372
		$first =  $this->getFirstPage();
373
		$links = array();
374
		for ($i=$start; $i>$end; $i--) {
375
			if ($i < $first) {
376
					break;
377
			}
378
			$links[] = $i;
379
		}
380
 
381
		return array_reverse($links);
382
	}
383
 
384
	/**
385
	 * get an array of next id's
386
	 *
387
	 * @param      int $range
388
	 * @return     array $links
389
	 */
390
	public function getNextLinks($range = 5)
391
	{
392
		$total = $this->getTotalPages();
393
		$start = $this->getPage() + 1;
394
		$end = $this->getPage() + $range;
395
		$last =  $this->getLastPage();
396
		$links = array();
397
		for ($i=$start; $i<$end; $i++) {
398
			if ($i > $last) {
399
					break;
400
			}
401
			$links[] = $i;
402
		}
403
 
404
		return $links;
405
	}
406
 
407
	/**
408
	 * Returns whether last page is complete
409
	 *
410
	 * @return     bool Last page complete or not
411
	 */
412
	public function isLastPageComplete()
413
	{
414
		return !($this->getTotalRecordCount() % $this->max);
415
	}
416
 
417
	/**
418
	 * get previous id
419
	 *
420
	 * @return     mixed $prev
421
	 */
422
	public function getPrev() {
423
		if ($this->getPage() != $this->getFirstPage()) {
424
				$prev = $this->getPage() - 1;
425
		} else {
426
				$prev = false;
427
		}
428
		return $prev;
429
	}
430
 
431
	/**
432
	 * get next id
433
	 *
434
	 * @return     mixed $next
435
	 */
436
	public function getNext() {
437
		if ($this->getPage() != $this->getLastPage()) {
438
				$next = $this->getPage() + 1;
439
		} else {
440
				$next = false;
441
		}
442
		return $next;
443
	}
444
 
445
	/**
446
	 * Set the current page number (First page is 1).
447
	 * @param      int $page
448
	 * @return     void
449
	 */
450
	public function setPage($page)
451
	{
452
		$this->page = $page;
453
		// (re-)calculate start rec
454
		$this->calculateStart();
455
	}
456
 
457
	/**
458
	 * Get current page.
459
	 * @return     int
460
	 */
461
	public function getPage()
462
	{
463
		return $this->page;
464
	}
465
 
466
	/**
467
	 * Set the number of rows per page.
468
	 * @param      int $r
469
	 */
470
	public function setRowsPerPage($r)
471
	{
472
		$this->max = $r;
473
		// (re-)calculate start rec
474
		$this->calculateStart();
475
	}
476
 
477
	/**
478
	 * Get number of rows per page.
479
	 * @return     int
480
	 */
481
	public function getRowsPerPage()
482
	{
483
		return $this->max;
484
	}
485
 
486
	/**
487
	 * Calculate startrow / max rows based on current page and rows-per-page.
488
	 * @return     void
489
	 */
490
	private function calculateStart()
491
	{
492
		$this->start = ( ($this->page - 1) * $this->max );
493
	}
494
 
495
	/**
496
	 * Gets the total number of (un-LIMITed) records.
497
	 *
498
	 * This method will perform a query that executes un-LIMITed query.
499
	 *
500
	 * @return     int Total number of records - disregarding page, maxrows, etc.
501
	 */
502
	public function getTotalRecordCount()
503
	{
504
 
505
				if (!isset($this->rs)) {
506
					$this->doRs();
507
				}
508
 
509
				if (empty($this->recordCount)) {
510
						$this->countCriteria = clone $this->criteria;
511
						$this->countCriteria->setLimit(0);
512
						$this->countCriteria->setOffset(0);
513
 
514
						$this->recordCount = call_user_func(
515
								        array(
516
								                $this->getPeerClass(),
517
												$this->getPeerCountMethod()
518
								             ),
519
								        $this->countCriteria
520
								        );
521
 
522
				}
523
 
524
				return $this->recordCount;
525
 
526
	}
527
 
528
	/**
529
	 * Sets the start row or offset.
530
	 * @param      int $v
531
	 */
532
	public function setStart($v)
533
	{
534
		$this->start = $v;
535
	}
536
 
537
	/**
538
	 * Sets max rows (limit).
539
	 * @param      int $v
540
	 * @return     void
541
	 */
542
	public function setMax($v)
543
	{
544
		$this->max = $v;
545
	}
546
 
547
	/**
548
	 * Returns the count of the current page's records
549
	 * @return 	int
550
	 */
551
	public function count()
552
	{
553
		return count($this->getResult());
554
	}
555
 
556
	/**
557
	 * Returns the current element of the iterator
558
	 * @return mixed
559
	 */
560
	public function current()
561
	{
562
		if (!isset($this->rs)) {
563
			$this->doRs();
564
		}
565
		return $this->rs[$this->currentKey];
566
	}
567
 
568
	/**
569
	 * Returns the current key of the iterator
570
	 * @return int
571
	 */
572
	public function key()
573
	{
574
		return $this->currentKey;
575
	}
576
 
577
	/**
578
	 * Advances the iterator to the next element
579
	 * @return void
580
	 */
581
	public function next()
582
	{
583
		$this->currentKey++;
584
	}
585
 
586
	/**
587
	 * Resets the iterator to the first element
588
	 * @return void
589
	 */
590
	public function rewind()
591
	{
592
		$this->currentKey = 0;
593
	}
594
 
595
	/**
596
	 * Checks if the current key exists in the container
597
	 * @return boolean
598
	 */
599
	public function valid()
600
	{
601
		if (!isset($this->rs)) {
602
			$this->doRs();
603
		}
604
		return in_array($this->currentKey, array_keys($this->rs));
605
	}
606
 
607
}