summaryrefslogtreecommitdiff
path: root/demos/blog/protected/Pages/Posts/ListPost.php
blob: 51e581d35fae3a07a49a4f9bb200d587758b8456 (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
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php
/**
 * ListPost class file
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @link http://www.pradosoft.com/
 * @copyright Copyright &copy; 2006 PradoSoft
 * @license http://www.pradosoft.com/license/
 */

/**
 * ListPost class
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @link http://www.pradosoft.com/
 * @copyright Copyright &copy; 2006 PradoSoft
 * @license http://www.pradosoft.com/license/
 */
class ListPost extends BlogPage
{
	private $_posts;
	private $_category;

	public function onInit($param)
	{
		parent::onInit($param);
		$this->_posts=$this->DataAccess->queryPosts(
				$this->getPostFilter(),
				$this->getCategoryFilter(),
				'ORDER BY a.status DESC, create_time DESC',
				'LIMIT '.$this->getPageOffset().','.$this->getPageSize());
		if($this->Request['cat']!==null)
		{
			$catID=TPropertyValue::ensureInteger($this->Request['cat']);
			$this->_category=$this->DataAccess->queryCategoryByID($catID);
			$this->CategoryPanel->Visible=true;
		}
		$this->Title=$this->Application->Parameters['SiteTitle'];
	}

	private function getPageOffset()
	{
		if(($offset=TPropertyValue::ensureInteger($this->Request['offset']))<=0)
			$offset=0;
		return $offset;
	}

	private function getPageSize()
	{
		if(($limit=TPropertyValue::ensureInteger($this->Request['limit']))<=0)
			$limit=TPropertyValue::ensureInteger($this->Application->Parameters['PostPerPage']);
		return $limit;
	}

	private function getTimeFilter()
	{
		if(($time=TPropertyValue::ensureInteger($this->Request['time']))>0)
		{
			$year=(integer)($time/100);
			$month=$time%100;
			$startTime=mktime(0,0,0,$month,1,$year);
			if(++$month>12)
			{
				$month=1;
				$year++;
			}
			$endTime=mktime(0,0,0,$month,1,$year);
			return "create_time>=$startTime AND create_time<$endTime";
		}
		else
			return '';
	}

	private function getPostFilter()
	{
		$filter='(a.status=0 OR a.status=3)';
		if(($timeFilter=$this->getTimeFilter())!=='')
			return "$filter AND $timeFilter";
		else
			return $filter;
	}

	private function getCategoryFilter()
	{
		if(($catID=$this->Request['cat'])!==null)
		{
			$catID=TPropertyValue::ensureInteger($catID);
			return "category_id=$catID";
		}
		else
			return '';
	}

	private function formUrl($newOffset)
	{
		$gets=array();
		$gets['offset']=$newOffset;
		if($this->Request['limit']!==null)
			$gets['limit']=$this->Request['limit'];
		if($this->Request['time']!==null)
			$gets['time']=$this->Request['time'];
		if($this->Request['cat']!==null)
			$gets['cat']=$this->Request['cat'];
		return $this->Service->constructUrl('Posts.ListPost',$gets);
	}

	public function getCategory()
	{
		return $this->_category;
	}

	public function onLoad($param)
	{
		parent::onLoad($param);
		$this->PostList->DataSource=$this->_posts;
		$this->PostList->dataBind();
		if($this->getPageOffset()>0)
		{
			if(($offset=$this->getPageOffset()-$this->getPageSize())<0)
				$offset=0;
			$this->PrevPage->NavigateUrl=$this->formUrl($offset);
			$this->PrevPage->Visible=true;
		}
		if(count($this->_posts)===$this->getPageSize())
		{
			$this->NextPage->NavigateUrl=$this->formUrl($this->getPageOffset()+$this->getPageSize());
			$this->NextPage->Visible=true;
		}
	}

	public function deleteButtonClicked($sender,$param)
	{
		if($this->User->IsAdmin)
		{
			$this->DataAccess->deleteCategory($this->Category->ID);
			$this->gotoDefaultPage();
		}
	}
}