Open Journal Systems  2.4.4
 All Classes Namespaces Functions Variables Groups Pages
CommentDAO.inc.php
1 <?php
2 
18 import('lib.pkp.classes.comment.Comment');
19 
20 define ('SUBMISSION_COMMENT_RECURSE_ALL', -1);
21 
22 // Comment system configuration constants
23 define ('COMMENTS_DISABLED', 0); // All comments disabled
24 define ('COMMENTS_AUTHENTICATED', 1); // Can be posted by authenticated users
25 define ('COMMENTS_ANONYMOUS', 2); // Can be posted anonymously by authenticated users
26 define ('COMMENTS_UNAUTHENTICATED', 3); // Can be posted anonymously by anyone
27 
28 class CommentDAO extends DAO {
32  function CommentDAO() {
33  parent::DAO();
34  }
35 
42  function &getRootCommentsBySubmissionId($submissionId, $childLevels = 0) {
43  $comments = array();
44 
45  $result =& $this->retrieve(
46  'SELECT *
47  FROM comments
48  WHERE submission_id = ? AND
49  parent_comment_id IS NULL
50  ORDER BY date_posted',
51  (int) $submissionId
52  );
53 
54  while (!$result->EOF) {
55  $comments[] =& $this->_returnCommentFromRow($result->GetRowAssoc(false), $childLevels);
56  $result->MoveNext();
57  }
58 
59  $result->Close();
60  unset($result);
61 
62  return $comments;
63  }
64 
70  function &getCommentsByParentId($parentId, $childLevels = 0) {
71  $comments = array();
72 
73  $result =& $this->retrieve('SELECT * FROM comments WHERE parent_comment_id = ? ORDER BY date_posted', (int) $parentId);
74 
75  while (!$result->EOF) {
76  $comments[] =& $this->_returnCommentFromRow($result->GetRowAssoc(false), $childLevels);
77  $result->MoveNext();
78  }
79 
80  $result->Close();
81  unset($result);
82 
83  return $comments;
84  }
85 
91  function &getByUserId($userId) {
92  $comments = array();
93 
94  $result =& $this->retrieve('SELECT * FROM comments WHERE user_id = ?', (int) $userId);
95 
96  while (!$result->EOF) {
97  $comments[] =& $this->_returnCommentFromRow($result->GetRowAssoc(false));
98  $result->MoveNext();
99  }
100 
101  $result->Close();
102  unset($result);
103 
104  return $comments;
105  }
106 
112  function attributedCommentsExistForUser($userId) {
113  $result =& $this->retrieve('SELECT count(*) FROM comments WHERE user_id = ?', (int) $userId);
114  $returner = $result->fields[0]?true:false;
115  $result->Close();
116  return $returner;
117  }
118 
126  function &getById($commentId, $submissionId, $childLevels = 0) {
127  $result =& $this->retrieve(
128  'SELECT * FROM comments WHERE comment_id = ? and submission_id = ?',
129  array((int) $commentId, (int) $submissionId)
130  );
131 
132  $comment = null;
133  if ($result->RecordCount() != 0) {
134  $comment =& $this->_returnCommentFromRow($result->GetRowAssoc(false), $childLevels);
135  }
136 
137  $result->Close();
138  unset($result);
139 
140  return $comment;
141  }
142 
147  function newDataObject() {
148  return new Comment();
149  }
150 
156  function &_returnCommentFromRow($row, $childLevels = 0) {
157  $userDao =& DAORegistry::getDAO('UserDAO');
158 
159  $comment = $this->newDataObject();
160  $comment->setId($row['comment_id']);
161  $comment->setSubmissionId($row['submission_id']);
162  $comment->setUser($userDao->getById($row['user_id']), true);
163  $comment->setPosterIP($row['poster_ip']);
164  $comment->setPosterName($row['poster_name']);
165  $comment->setPosterEmail($row['poster_email']);
166  $comment->setTitle($row['title']);
167  $comment->setBody($row['body']);
168  $comment->setDatePosted($this->datetimeFromDB($row['date_posted']));
169  $comment->setDateModified($this->datetimeFromDB($row['date_modified']));
170  $comment->setParentCommentId($row['parent_comment_id']);
171  $comment->setChildCommentCount($row['num_children']);
172 
173  if (!HookRegistry::call('CommentDAO::_returnCommentFromRow', array(&$comment, &$row, &$childLevels))) {
174  if ($childLevels>0) $comment->setChildren($this->getCommentsByParentId($row['comment_id'], $childLevels-1));
175  else if ($childLevels==SUBMISSION_COMMENT_RECURSE_ALL) $comment->setChildren($this->getCommentsByParentId($row['comment_id'], SUBMISSION_COMMENT_RECURSE_ALL));
176  }
177 
178  return $comment;
179  }
180 
186  function insertComment(&$comment) {
187  $comment->setDatePosted(Core::getCurrentDate());
188  $comment->setDateModified($comment->getDatePosted());
189  $user = $comment->getUser();
190  $this->update(
191  sprintf('INSERT INTO comments
192  (submission_id, num_children, parent_comment_id, user_id, poster_ip, date_posted, date_modified, title, body, poster_name, poster_email)
193  VALUES
194  (?, ?, ?, ?, ?, %s, %s, ?, ?, ?, ?)',
195  $this->datetimeToDB($comment->getDatePosted()), $this->datetimeToDB($comment->getDateModified())),
196  array(
197  $comment->getSubmissionId(),
198  $comment->getChildCommentCount(),
199  $comment->getParentCommentId(),
200  (isset($user)?$user->getId():null),
201  $comment->getPosterIP(),
202  String::substr($comment->getTitle(), 0, 255),
203  $comment->getBody(),
204  String::substr($comment->getPosterName(), 0, 90),
205  String::substr($comment->getPosterEmail(), 0, 90)
206  )
207  );
208 
209  $comment->setId($this->getInsertCommentId());
210 
211  if ($comment->getParentCommentId()) $this->incrementChildCount($comment->getParentCommentId());
212 
213  return $comment->getId();
214  }
215 
220  function getInsertCommentId() {
221  return $this->getInsertId('comments', 'comment_id');
222  }
223 
228  function incrementChildCount($commentId) {
229  $this->update('UPDATE comments SET num_children=num_children+1 WHERE comment_id = ?', $commentId);
230  }
231 
236  function decrementChildCount($commentId) {
237  $this->update('UPDATE comments SET num_children=num_children-1 WHERE comment_id = ?', $commentId);
238  }
239 
244  function deleteComment(&$comment, $isRecursing = false) {
245  $result = $this->update('DELETE FROM comments WHERE comment_id = ?', $comment->getId());
246  if (!$isRecursing) $this->decrementChildCount($comment->getParentCommentId());
247  foreach ($comment->getChildren() as $child) {
248  $this->deleteComment($child, true);
249  }
250  }
251 
256  function deleteBySubmissionId($submissionId) {
257  return $this->update(
258  'DELETE FROM comments WHERE submission_id = ?',
259  (int) $submissionId
260  );
261  }
262 
267  function updateComment(&$comment) {
268  $comment->setDateModified(Core::getCurrentDate());
269  $user = $comment->getUser();
270  $this->update(
271  sprintf('UPDATE comments
272  SET
273  submission_id = ?,
274  num_children = ?,
275  parent_comment_id = ?,
276  user_id = ?,
277  poster_ip = ?,
278  date_posted = %s,
279  date_modified = %s,
280  title = ?,
281  body = ?,
282  poster_name = ?,
283  poster_email = ?
284  WHERE comment_id = ?',
285  $this->datetimeToDB($comment->getDatePosted()), $this->datetimeToDB($comment->getDateModified())),
286  array(
287  $comment->getSubmissionId(),
288  $comment->getChildCommentCount(),
289  $comment->getParentCommentId(),
290  (isset($user)?$user->getId():null),
291  $comment->getPosterIP(),
292  String::substr($comment->getTitle(), 0, 255),
293  $comment->getBody(),
294  String::substr($comment->getPosterName(), 0, 90),
295  String::substr($comment->getPosterEmail(), 0, 90),
296  $comment->getId()
297  )
298  );
299  }
300 }
301 
302 ?>
Operations for retrieving and modifying objects from a database.
Definition: DAO.inc.php:29
& getByUserId($userId)
deleteComment(&$comment, $isRecursing=false)
attributedCommentsExistForUser($userId)
& retrieve($sql, $params=false, $callHooks=true)
Definition: DAO.inc.php:83
& getCommentsByParentId($parentId, $childLevels=0)
getInsertId($table= '', $id= '', $callHooks=true)
Definition: DAO.inc.php:252
getCurrentDate($ts=null)
Definition: Core.inc.php:94
Class for public Comment associated with submission.
Definition: Comment.inc.php:21
datetimeFromDB($dt)
Definition: DAO.inc.php:316
deleteBySubmissionId($submissionId)
updateComment(&$comment)
substr($string, $start, $length=false)
Definition: String.inc.php:179
& _returnCommentFromRow($row, $childLevels=0)
& getDAO($name, $dbconn=null)
incrementChildCount($commentId)
Operations for retrieving and modifying Comment objects.
call($hookName, $args=null)
datetimeToDB($dt)
Definition: DAO.inc.php:296
update($sql, $params=false, $callHooks=true, $dieOnError=true)
Definition: DAO.inc.php:211
& getById($commentId, $submissionId, $childLevels=0)
insertComment(&$comment)
decrementChildCount($commentId)
& getRootCommentsBySubmissionId($submissionId, $childLevels=0)