00001 <?php
00002
00016
00017
00018 import('comment.Comment');
00019
00020 define ('PAPER_COMMENT_RECURSE_ALL', -1);
00021
00022 class CommentDAO extends DAO {
00028 function &getRootCommentsByPaperId($paperId, $childLevels = 0) {
00029 $comments = array();
00030
00031 $result =& $this->retrieve('SELECT * FROM comments WHERE paper_id = ? AND parent_comment_id IS NULL ORDER BY date_posted', $paperId);
00032
00033 while (!$result->EOF) {
00034 $comments[] =& $this->_returnCommentFromRow($result->GetRowAssoc(false), $childLevels);
00035 $result->moveNext();
00036 }
00037
00038 $result->Close();
00039 unset($result);
00040
00041 return $comments;
00042 }
00043
00049 function &getCommentsByParentId($parentId, $childLevels = 0) {
00050 $comments = array();
00051
00052 $result =& $this->retrieve('SELECT * FROM comments WHERE parent_comment_id = ? ORDER BY date_posted', $parentId);
00053
00054 while (!$result->EOF) {
00055 $comments[] =& $this->_returnCommentFromRow($result->GetRowAssoc(false), $childLevels);
00056 $result->moveNext();
00057 }
00058
00059 $result->Close();
00060 unset($result);
00061
00062 return $comments;
00063 }
00064
00070 function &getCommentsByUserId($userId) {
00071 $comments = array();
00072
00073 $result =& $this->retrieve('SELECT * FROM comments WHERE user_id = ?', $userId);
00074
00075 while (!$result->EOF) {
00076 $comments[] =& $this->_returnCommentFromRow($result->GetRowAssoc(false));
00077 $result->moveNext();
00078 }
00079
00080 $result->Close();
00081 unset($result);
00082
00083 return $comments;
00084 }
00085
00091 function &getComment($commentId, $paperId, $childLevels = 0) {
00092 $result =& $this->retrieve(
00093 'SELECT * FROM comments WHERE comment_id = ? and paper_id = ?', array($commentId, $paperId)
00094 );
00095
00096 $comment = null;
00097 if ($result->RecordCount() != 0) {
00098 $comment =& $this->_returnCommentFromRow($result->GetRowAssoc(false), $childLevels);
00099 }
00100
00101 $result->Close();
00102 unset($result);
00103
00104 return $comment;
00105 }
00106
00112 function &_returnCommentFromRow($row, $childLevels = 0) {
00113 $userDao =& DAORegistry::getDAO('UserDAO');
00114
00115 $comment = new Comment();
00116 $comment->setId($row['comment_id']);
00117 $comment->setPaperId($row['paper_id']);
00118 $comment->setUser($userDao->getUser($row['user_id']), true);
00119 $comment->setPosterIP($row['poster_ip']);
00120 $comment->setPosterName($row['poster_name']);
00121 $comment->setPosterEmail($row['poster_email']);
00122 $comment->setTitle($row['title']);
00123 $comment->setBody($row['body']);
00124 $comment->setDatePosted($this->datetimeFromDB($row['date_posted']));
00125 $comment->setDateModified($this->datetimeFromDB($row['date_modified']));
00126 $comment->setParentCommentId($row['parent_comment_id']);
00127 $comment->setChildCommentCount($row['num_children']);
00128
00129 if (!HookRegistry::call('CommentDAO::_returnCommentFromRow', array(&$comment, &$row, &$childLevels))) {
00130 if ($childLevels>0) $comment->setChildren($this->getCommentsByParentId($row['comment_id'], $childLevels-1));
00131 else if ($childLevels==SUBMISSION_COMMENT_RECURSE_ALL) $comment->setChildren($this->getCommentsByParentId($row['comment_id'], SUBMISSION_COMMENT_RECURSE_ALL));
00132 }
00133
00134 return $comment;
00135 }
00136
00142 function insertComment(&$comment) {
00143 $comment->setDatePosted(Core::getCurrentDate());
00144 $comment->setDateModified($comment->getDatePosted());
00145 $user = $comment->getUser();
00146 $this->update(
00147 sprintf('INSERT INTO comments
00148 (paper_id, num_children, parent_comment_id, user_id, poster_ip, date_posted, date_modified, title, body, poster_name, poster_email)
00149 VALUES
00150 (?, ?, ?, ?, ?, %s, %s, ?, ?, ?, ?)',
00151 $this->datetimeToDB($comment->getDatePosted()), $this->datetimeToDB($comment->getDateModified())),
00152 array(
00153 $comment->getPaperId(),
00154 $comment->getChildCommentCount(),
00155 $comment->getParentCommentId(),
00156 (isset($user)?$user->getId():null),
00157 $comment->getPosterIP(),
00158 String::substr($comment->getTitle(), 0, 255),
00159 $comment->getBody(),
00160 String::substr($comment->getPosterName(), 0, 90),
00161 String::substr($comment->getPosterEmail(), 0, 90)
00162 )
00163 );
00164
00165 $comment->setId($this->getInsertCommentId());
00166
00167 if ($comment->getParentCommentId()) $this->incrementChildCount($comment->getParentCommentId());
00168
00169 return $comment->getId();
00170 }
00171
00176 function getInsertCommentId() {
00177 return $this->getInsertId('comments', 'comment_id');
00178 }
00179
00184 function incrementChildCount($commentId) {
00185 $this->update('UPDATE comments SET num_children=num_children+1 WHERE comment_id = ?', $commentId);
00186 }
00187
00192 function decrementChildCount($commentId) {
00193 $this->update('UPDATE comments SET num_children=num_children-1 WHERE comment_id = ?', $commentId);
00194 }
00195
00200 function deleteComment(&$comment, $isRecursing = false) {
00201 $result = $this->update('DELETE FROM comments WHERE comment_id = ?', $comment->getId());
00202 if (!$isRecursing) $this->decrementChildCount($comment->getParentCommentId());
00203 foreach ($comment->getChildren() as $child) {
00204 $this->deleteComment($child, true);
00205 }
00206 }
00207
00212 function deleteCommentsByPaper($paperId) {
00213 return $this->update('DELETE FROM comments WHERE paper_id = ?', $paperId);
00214 }
00215
00220 function updateComment(&$comment) {
00221 $comment->setDateModified(Core::getCurrentDate());
00222 $user = $comment->getUser();
00223 $this->update(
00224 sprintf('UPDATE paper_comments
00225 SET
00226 paper_id = ?,
00227 num_children = ?,
00228 parent_comment_id = ?,
00229 user_id = ?,
00230 poster_ip = ?,
00231 date_posted = %s,
00232 date_modified = %s,
00233 title = ?,
00234 body = ?,
00235 poster_name = ?,
00236 poster_email = ?
00237 WHERE comment_id = ?',
00238 $this->datetimeToDB($comment->getDatePosted()), $this->datetimeToDB($comment->getDateModified())),
00239 array(
00240 $comment->getPaperId(),
00241 $comment->getChildCommentCount(),
00242 $comment->getParentCommentId(),
00243 (isset($user)?$user->getId():null),
00244 $comment->getPosterIP(),
00245 String::substr($comment->getTitle(), 0, 255),
00246 $comment->getBody(),
00247 String::substr($comment->getPosterName(), 0, 90),
00248 String::substr($comment->getPosterEmail(), 0, 90),
00249 $comment->getId()
00250 )
00251 );
00252 }
00253 }
00254
00255 ?>