| 1 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
class ListPost extends TPage
|
|
|
4 |
{
|
|
|
5 |
/**
|
|
|
6 |
* Initializes the repeater.
|
|
|
7 |
* This method is invoked by the framework when initializing the page
|
|
|
8 |
* @param mixed event parameter
|
|
|
9 |
*/
|
|
|
10 |
public function onInit($param)
|
|
|
11 |
{
|
|
|
12 |
parent::onInit($param);
|
|
|
13 |
if(!$this->IsPostBack) // if the page is requested the first time
|
|
|
14 |
{
|
|
|
15 |
// get the total number of posts available
|
|
|
16 |
$this->Repeater->VirtualItemCount=PostRecord::finder()->count();
|
|
|
17 |
// populates post data into the repeater
|
|
|
18 |
$this->populateData();
|
|
|
19 |
}
|
|
|
20 |
}
|
|
|
21 |
|
|
|
22 |
/**
|
|
|
23 |
* Event handler to the OnPageIndexChanged event of the pager.
|
|
|
24 |
* This method is invoked when the user clicks on a page button
|
|
|
25 |
* and thus changes the page of posts to display.
|
|
|
26 |
*/
|
|
|
27 |
public function pageChanged($sender,$param)
|
|
|
28 |
{
|
|
|
29 |
// change the current page index to the new one
|
|
|
30 |
$this->Repeater->CurrentPageIndex=$param->NewPageIndex;
|
|
|
31 |
// re-populate data into the repeater
|
|
|
32 |
$this->populateData();
|
|
|
33 |
}
|
|
|
34 |
|
|
|
35 |
/**
|
|
|
36 |
* Determines which page of posts to be displayed and
|
|
|
37 |
* populates the repeater with the fetched data.
|
|
|
38 |
*/
|
|
|
39 |
protected function populateData()
|
|
|
40 |
{
|
|
|
41 |
$offset=$this->Repeater->CurrentPageIndex*$this->Repeater->PageSize;
|
|
|
42 |
$limit=$this->Repeater->PageSize;
|
|
|
43 |
if($offset+$limit>$this->Repeater->VirtualItemCount)
|
|
|
44 |
$limit=$this->Repeater->VirtualItemCount-$offset;
|
|
|
45 |
$this->Repeater->DataSource=$this->getPosts($offset,$limit);
|
|
|
46 |
$this->Repeater->dataBind();
|
|
|
47 |
}
|
|
|
48 |
|
|
|
49 |
/**
|
|
|
50 |
* Fetches posts from database with offset and limit.
|
|
|
51 |
*/
|
|
|
52 |
protected function getPosts($offset, $limit)
|
|
|
53 |
{
|
|
|
54 |
// Construts a query criteria
|
|
|
55 |
$criteria=new TActiveRecordCriteria;
|
|
|
56 |
$criteria->OrdersBy['create_time']='desc';
|
|
|
57 |
$criteria->Limit=$limit;
|
|
|
58 |
$criteria->Offset=$offset;
|
|
|
59 |
// query for the posts with the above criteria and with author information
|
|
|
60 |
return PostRecord::finder()->withAuthor()->findAll($criteria);
|
|
|
61 |
}
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
?>
|