summaryrefslogtreecommitdiff
path: root/demos/quickstart/protected/comments/QuickStartComments.php
blob: c138bd949f3e768141154dd56df0c18783765fe4 (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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php

/**
 * QuickStartComments class.
 *
 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
 * @version : $  Sat May 27 16:49:19 AZOST 2006 $
 * @package Demos.QuickStart.comments
 * @since 3.0
 */
class QuickStartComments
{
	/**
	 * @var string sqlite database source.
	 */
	private $_database;
	/**
	 * @var sqlite connection.
	 */
	private $_connection;
	
	/**
	 * Sets the sqlite comment database file.
	 */
	public function __construct()
	{
		$this->_database = realpath(dirname(__FILE__).'/comments.db');
	}
	
	/**
	 * Closed the database connection.
	 */
	public function __destruct()
	{
		if(!is_null($this->_connection))
			sqlite_close($this->_connection);
	}
	
	/**
	 * @return resource sqlite database connection.
	 */
	protected function getConnection()
	{
		if(is_null($this->_connection))
			$this->_connection = sqlite_open($this->_database);
		return $this->_connection;
	}
	
	/**
	 * Quote database input data.
	 */
	protected function quote($value)
	{
		return sqlite_escape_string($value);
	}
	
	/**
	 * Executes an sqlite query.
	 * @param string SQL
	 * @return mixed query results.
	 */
	protected function query($sql)
	{
		return sqlite_query($this->getConnection(), $sql);
	}
	
	/**
	 * Returns a row from the sqlite result.
	 * @param resource sqlite result
	 * @return array database record.
	 */
	protected function fetch($resource)
	{
		if($resource !== false)
			return sqlite_fetch_array($resource);
		else
			return false;
	}
	
	/**
	 * Fetch all the records for given SQL query.
	 * @param string SQL query.
	 * @return array result set.
	 */
	protected function fetchAll($sql)
	{
		$rs = $this->query($sql);
		$rows = array();
		while($row = $this->fetch($rs))
			$rows[] = $row;
		return $rows;
	}
	
	/**
	 * Returns all the comments for a given page.
	 * @param string specific page comments
	 * @return array list of comments
	 */
	public function getComments($pageID)
	{
		$page = $this->quote($pageID);
		$sql = "SELECT * FROM comments WHERE page=\"$page\" AND approved = 1 ORDER BY date_added ASC";
		return $this->fetchAll($sql);
	}
	
	/**
	 * Adds a new comment for moderation.
	 * @param string ID of the page to comment belongs
	 * @param string email address of the commenter
	 * @param string comment contents
	 */
	public function addNewComment($pageID, $email, $comment)
	{
		$page = $this->quote($pageID);
		$email = $this->quote($email);
		$comment = $this->quote($comment);
		$date_added = time();
		$sql = <<<EOD
		INSERT INTO comments(page, email, comment, date_added)
				VALUES ("$page", "$email", "$comment", "$date_added")
EOD;
		return $this->query($sql);
	}
	
	/**
	 * Update an existing comment.
	 * @param string comment ID
	 * @param string page ID
	 * @param string email address
	 * @param string updated comment.
	 */
	public function updateComment($commentID, $page, $email, $content)
	{
		$ID = intval($commentID);
		$email = $this->quote($email);
		$comment = $this->quote($content);
		$page = $this->quote($page);
		$sql = <<<EOD
		UPDATE comments SET 
			email = "$email", comment = "$comment", page = "$page"
			WHERE id = $ID;
EOD;
		$this->query($sql);
	}
	
	/**
	 * Delete a comment.
	 * @param string comment ID
	 */
	public function deleteComment($commentID)
	{
		$ID = intval($commentID);
		$this->query("DELETE FROM comments WHERE id=$ID");
	}
		
	/**
	 * @return array all the quequed comments.
	 */
	public function getQuequedComments()
	{
		return $this->fetchAll("SELECT * FROM comments WHERE approved != 1");
	}
	
	/**
	 * Approve a quequed comment.
	 * @param string comment ID.
	 */
	public function approveComment($commentID)
	{
		$ID = intval($commentID);
		$this->query("UPDATE comments SET approved = 1 WHERE id=$ID");
	}
}

?>