summaryrefslogtreecommitdiff
path: root/demos/blog-tutorial/samples/day4/blog/protected/pages/posts/ListPost.php
blob: 7402dace0bf83dc19c4f26017a87e0eef32937c7 (plain)
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
<?php

class ListPost extends TPage
{
	/**
	 * Initializes the repeater.
	 * This method is invoked by the framework when initializing the page
	 * @param mixed event parameter
	 */
	public function onInit($param)
	{
		parent::onInit($param);
		if(!$this->IsPostBack)  // if the page is requested the first time
		{
			// get the total number of posts available
			$this->Repeater->VirtualItemCount=PostRecord::finder()->count();
			// populates post data into the repeater
			$this->populateData();
		}
	}

	/**
	 * Event handler to the OnPageIndexChanged event of the pager.
	 * This method is invoked when the user clicks on a page button
	 * and thus changes the page of posts to display.
	 */
	public function pageChanged($sender,$param)
	{
		// change the current page index to the new one
		$this->Repeater->CurrentPageIndex=$param->NewPageIndex;
		// re-populate data into the repeater
		$this->populateData();
	}

	/**
	 * Determines which page of posts to be displayed and
	 * populates the repeater with the fetched data.
	 */
	protected function populateData()
	{
		$offset=$this->Repeater->CurrentPageIndex*$this->Repeater->PageSize;
		$limit=$this->Repeater->PageSize;
		if($offset+$limit>$this->Repeater->VirtualItemCount)
			$limit=$this->Repeater->VirtualItemCount-$offset;
		$this->Repeater->DataSource=$this->getPosts($offset,$limit);
		$this->Repeater->dataBind();
	}

	/**
	 * Fetches posts from database with offset and limit.
	 */
	protected function getPosts($offset, $limit)
	{
		// Construts a query criteria
		$criteria=new TActiveRecordCriteria;
		$criteria->OrdersBy['create_time']='desc';
		$criteria->Limit=$limit;
		$criteria->Offset=$offset;
		// query for the posts with the above criteria and with author information
		return PostRecord::finder()->withAuthor()->findAll($criteria);
	}
}

?>