00001 <?php
00002
00016
00017
00018
00019 import('comment.Comment');
00020
00021 define ('ARTICLE_COMMENT_RECURSE_ALL', -1);
00022
00023
00024 define ('COMMENTS_DISABLED', 0);
00025 define ('COMMENTS_AUTHENTICATED', 1);
00026 define ('COMMENTS_ANONYMOUS', 2);
00027 define ('COMMENTS_UNAUTHENTICATED', 3);
00028
00029 class CommentDAO extends DAO {
00035 function &getRootCommentsByArticleId($articleId, $childLevels = 0) {
00036 $comments = array();
00037
00038 $result =& $this->retrieve('SELECT * FROM comments WHERE article_id = ? AND parent_comment_id IS NULL ORDER BY date_posted', (int) $articleId);
00039
00040 while (!$result->EOF) {
00041 $comments[] =& $this->_returnCommentFromRow($result->GetRowAssoc(false), $childLevels);
00042 $result->moveNext();
00043 }
00044
00045 $result->Close();
00046 unset($result);
00047
00048 return $comments;
00049 }
00050
00056 function &getCommentsByParentId($parentId, $childLevels = 0) {
00057 $comments = array();
00058
00059 $result =& $this->retrieve('SELECT * FROM comments WHERE parent_comment_id = ? ORDER BY date_posted', (int) $parentId);
00060
00061 while (!$result->EOF) {
00062 $comments[] =& $this->_returnCommentFromRow($result->GetRowAssoc(false), $childLevels);
00063 $result->moveNext();
00064 }
00065
00066 $result->Close();
00067 unset($result);
00068
00069 return $comments;
00070 }
00071
00077 function &getCommentsByUserId($userId) {
00078 $comments = array();
00079
00080 $result =& $this->retrieve('SELECT * FROM comments WHERE user_id = ?', (int) $userId);
00081
00082 while (!$result->EOF) {
00083 $comments[] =& $this->_returnCommentFromRow($result->GetRowAssoc(false));
00084 $result->moveNext();
00085 }
00086
00087 $result->Close();
00088 unset($result);
00089
00090 return $comments;
00091 }
00092
00098 function attributedCommentsExistForUser($userId) {
00099 $result =& $this->retrieve('SELECT count(*) FROM comments WHERE user_id = ?', (int) $userId);
00100 $returner = $result->fields[0]?true:false;
00101 $result->Close();
00102 return $returner;
00103 }
00104
00110 function &getComment($commentId, $articleId, $childLevels = 0) {
00111 $result =& $this->retrieve(
00112 'SELECT * FROM comments WHERE comment_id = ? and article_id = ?', array((int) $commentId, (int) $articleId)
00113 );
00114
00115 $comment = null;
00116 if ($result->RecordCount() != 0) {
00117 $comment =& $this->_returnCommentFromRow($result->GetRowAssoc(false), $childLevels);
00118 }
00119
00120 $result->Close();
00121 unset($result);
00122
00123 return $comment;
00124 }
00125
00131 function &_returnCommentFromRow($row, $childLevels = 0) {
00132 $userDao =& DAORegistry::getDAO('UserDAO');
00133
00134 $comment =& new Comment();
00135 $comment->setCommentId($row['comment_id']);
00136 $comment->setArticleId($row['article_id']);
00137 $comment->setUser($userDao->getUser($row['user_id']), true);
00138 $comment->setPosterIP($row['poster_ip']);
00139 $comment->setPosterName($row['poster_name']);
00140 $comment->setPosterEmail($row['poster_email']);
00141 $comment->setTitle($row['title']);
00142 $comment->setBody($row['body']);
00143 $comment->setDatePosted($this->datetimeFromDB($row['date_posted']));
00144 $comment->setDateModified($this->datetimeFromDB($row['date_modified']));
00145 $comment->setParentCommentId($row['parent_comment_id']);
00146 $comment->setChildCommentCount($row['num_children']);
00147
00148 if (!HookRegistry::call('CommentDAO::_returnCommentFromRow', array(&$comment, &$row, &$childLevels))) {
00149 if ($childLevels>0) $comment->setChildren($this->getCommentsByParentId($row['comment_id'], $childLevels-1));
00150 else if ($childLevels==ARTICLE_COMMENT_RECURSE_ALL) $comment->setChildren($this->getCommentsByParentId($row['comment_id'], ARTICLE_COMMENT_RECURSE_ALL));
00151 }
00152
00153 return $comment;
00154 }
00155
00161 function insertComment(&$comment) {
00162 $comment->setDatePosted(Core::getCurrentDate());
00163 $comment->setDateModified($comment->getDatePosted());
00164 $user = $comment->getUser();
00165 $this->update(
00166 sprintf('INSERT INTO comments
00167 (article_id, num_children, parent_comment_id, user_id, poster_ip, date_posted, date_modified, title, body, poster_name, poster_email)
00168 VALUES
00169 (?, ?, ?, ?, ?, %s, %s, ?, ?, ?, ?)',
00170 $this->datetimeToDB($comment->getDatePosted()), $this->datetimeToDB($comment->getDateModified())),
00171 array(
00172 $comment->getArticleId(),
00173 $comment->getChildCommentCount(),
00174 $comment->getParentCommentId(),
00175 (isset($user)?$user->getUserId():null),
00176 $comment->getPosterIP(),
00177 $comment->getTitle(),
00178 $comment->getBody(),
00179 $comment->getPosterName(),
00180 $comment->getPosterEmail()
00181 )
00182 );
00183
00184 $comment->setCommentId($this->getInsertCommentId());
00185
00186 if ($comment->getParentCommentId()) $this->incrementChildCount($comment->getParentCommentId());
00187
00188 return $comment->getCommentId();
00189 }
00190
00195 function getInsertCommentId() {
00196 return $this->getInsertId('comments', 'comment_id');
00197 }
00198
00203 function incrementChildCount($commentId) {
00204 $this->update('UPDATE comments SET num_children=num_children+1 WHERE comment_id = ?', $commentId);
00205 }
00206
00211 function decrementChildCount($commentId) {
00212 $this->update('UPDATE comments SET num_children=num_children-1 WHERE comment_id = ?', $commentId);
00213 }
00214
00219 function deleteComment(&$comment, $isRecursing = false) {
00220 $result = $this->update('DELETE FROM comments WHERE comment_id = ?', $comment->getCommentId());
00221 if (!$isRecursing) $this->decrementChildCount($comment->getParentCommentId());
00222 foreach ($comment->getChildren() as $child) {
00223 $this->deleteComment($child, true);
00224 }
00225 }
00226
00231 function deleteCommentsByArticle($articleId) {
00232 return $this->update('DELETE FROM comments WHERE article_id = ?', $articleId);
00233 }
00234
00239 function updateComment(&$comment) {
00240 $comment->setDateModified(Core::getCurrentDate());
00241 $user = $comment->getUser();
00242 $this->update(
00243 sprintf('UPDATE article_comments
00244 SET
00245 article_id = ?,
00246 num_children = ?,
00247 parent_comment_id = ?,
00248 user_id = ?,
00249 poster_ip = ?,
00250 date_posted = %s,
00251 date_modified = %s,
00252 title = ?,
00253 body = ?,
00254 poster_name = ?,
00255 poster_email = ?
00256 WHERE comment_id = ?',
00257 $this->datetimeToDB($comment->getDatePosted()), $this->datetimeToDB($comment->getDateModified())),
00258 array(
00259 $comment->getArticleId(),
00260 $comment->getChildCommentCount(),
00261 $comment->getParentCommentId(),
00262 (isset($user)?$user->getUserId():null),
00263 $comment->getPosterIP(),
00264 $comment->getTitle(),
00265 $comment->getBody(),
00266 $comment->getPosterName(),
00267 $comment->getPosterEmail(),
00268 $comment->getCommentId()
00269 )
00270 );
00271 }
00272 }
00273
00274 ?>