Open Journal Systems  3.0.0
 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  return $comments;
61  }
62 
68  function &getCommentsByParentId($parentId, $childLevels = 0) {
69  $comments = array();
70 
71  $result = $this->retrieve('SELECT * FROM comments WHERE parent_comment_id = ? ORDER BY date_posted', (int) $parentId);
72 
73  while (!$result->EOF) {
74  $comments[] = $this->_returnCommentFromRow($result->GetRowAssoc(false), $childLevels);
75  $result->MoveNext();
76  }
77 
78  $result->Close();
79  return $comments;
80  }
81 
87  function getByUserId($userId) {
88  $comments = array();
89 
90  $result = $this->retrieve('SELECT * FROM comments WHERE user_id = ?', (int) $userId);
91 
92  while (!$result->EOF) {
93  $comments[] = $this->_returnCommentFromRow($result->GetRowAssoc(false));
94  $result->MoveNext();
95  }
96 
97  $result->Close();
98  return $comments;
99  }
100 
106  function attributedCommentsExistForUser($userId) {
107  $result = $this->retrieve('SELECT count(*) FROM comments WHERE user_id = ?', (int) $userId);
108  $returner = $result->fields[0]?true:false;
109  $result->Close();
110  return $returner;
111  }
112 
120  function getById($commentId, $submissionId, $childLevels = 0) {
121  $result = $this->retrieve(
122  'SELECT * FROM comments WHERE comment_id = ? and submission_id = ?',
123  array((int) $commentId, (int) $submissionId)
124  );
125 
126  $comment = null;
127  if ($result->RecordCount() != 0) {
128  $comment = $this->_returnCommentFromRow($result->GetRowAssoc(false), $childLevels);
129  }
130 
131  $result->Close();
132  return $comment;
133  }
134 
139  function newDataObject() {
140  return new Comment();
141  }
142 
148  function _returnCommentFromRow($row, $childLevels = 0) {
149  $userDao = DAORegistry::getDAO('UserDAO');
150 
151  $comment = $this->newDataObject();
152  $comment->setId($row['comment_id']);
153  $comment->setSubmissionId($row['submission_id']);
154  $comment->setUser($userDao->getById($row['user_id']), true);
155  $comment->setPosterIP($row['poster_ip']);
156  $comment->setPosterName($row['poster_name']);
157  $comment->setPosterEmail($row['poster_email']);
158  $comment->setTitle($row['title']);
159  $comment->setBody($row['body']);
160  $comment->setDatePosted($this->datetimeFromDB($row['date_posted']));
161  $comment->setDateModified($this->datetimeFromDB($row['date_modified']));
162  $comment->setParentCommentId($row['parent_comment_id']);
163  $comment->setChildCommentCount($row['num_children']);
164 
165  if (!HookRegistry::call('CommentDAO::_returnCommentFromRow', array(&$comment, &$row, &$childLevels))) {
166  if ($childLevels>0) $comment->setChildren($this->getCommentsByParentId($row['comment_id'], $childLevels-1));
167  else if ($childLevels==SUBMISSION_COMMENT_RECURSE_ALL) $comment->setChildren($this->getCommentsByParentId($row['comment_id'], SUBMISSION_COMMENT_RECURSE_ALL));
168  }
169 
170  return $comment;
171  }
172 
178  function insertObject($comment) {
179  $comment->setDatePosted(Core::getCurrentDate());
180  $comment->setDateModified($comment->getDatePosted());
181  $user = $comment->getUser();
182  $this->update(
183  sprintf('INSERT INTO comments
184  (submission_id, num_children, parent_comment_id, user_id, poster_ip, date_posted, date_modified, title, body, poster_name, poster_email)
185  VALUES
186  (?, ?, ?, ?, ?, %s, %s, ?, ?, ?, ?)',
187  $this->datetimeToDB($comment->getDatePosted()), $this->datetimeToDB($comment->getDateModified())),
188  array(
189  $comment->getSubmissionId(),
190  $comment->getChildCommentCount(),
191  $comment->getParentCommentId(),
192  (isset($user)?$user->getId():null),
193  $comment->getPosterIP(),
194  String::substr($comment->getTitle(), 0, 255),
195  $comment->getBody(),
196  String::substr($comment->getPosterName(), 0, 90),
197  String::substr($comment->getPosterEmail(), 0, 90)
198  )
199  );
200 
201  $comment->setId($this->getInsertId());
202 
203  if ($comment->getParentCommentId()) $this->incrementChildCount($comment->getParentCommentId());
204 
205  return $comment->getId();
206  }
207 
212  function getInsertId() {
213  return $this->_getInsertId('comments', 'comment_id');
214  }
215 
220  function incrementChildCount($commentId) {
221  $this->update('UPDATE comments SET num_children=num_children+1 WHERE comment_id = ?', $commentId);
222  }
223 
228  function decrementChildCount($commentId) {
229  $this->update('UPDATE comments SET num_children=num_children-1 WHERE comment_id = ?', $commentId);
230  }
231 
236  function deleteObject($comment, $isRecursing = false) {
237  $this->update('DELETE FROM comments WHERE comment_id = ?', $comment->getId());
238  if (!$isRecursing) $this->decrementChildCount($comment->getParentCommentId());
239  foreach ($comment->getChildren() as $child) {
240  $this->deleteObject($child, true);
241  }
242  }
243 
248  function deleteBySubmissionId($submissionId) {
249  return $this->update(
250  'DELETE FROM comments WHERE submission_id = ?',
251  (int) $submissionId
252  );
253  }
254 
259  function updateObject($comment) {
260  $comment->setDateModified(Core::getCurrentDate());
261  $user = $comment->getUser();
262  $this->update(
263  sprintf('UPDATE comments
264  SET
265  submission_id = ?,
266  num_children = ?,
267  parent_comment_id = ?,
268  user_id = ?,
269  poster_ip = ?,
270  date_posted = %s,
271  date_modified = %s,
272  title = ?,
273  body = ?,
274  poster_name = ?,
275  poster_email = ?
276  WHERE comment_id = ?',
277  $this->datetimeToDB($comment->getDatePosted()), $this->datetimeToDB($comment->getDateModified())),
278  array(
279  (int) $comment->getSubmissionId(),
280  $comment->getChildCommentCount(),
281  $comment->getParentCommentId(),
282  (isset($user)?$user->getId():null),
283  $comment->getPosterIP(),
284  String::substr($comment->getTitle(), 0, 255),
285  $comment->getBody(),
286  String::substr($comment->getPosterName(), 0, 90),
287  String::substr($comment->getPosterEmail(), 0, 90),
288  (int) $comment->getId()
289  )
290  );
291  }
292 }
293 
294 ?>
static & getDAO($name, $dbconn=null)
Operations for retrieving and modifying objects from a database.
Definition: DAO.inc.php:30
static substr($string, $start, $length=false)
Definition: String.inc.php:187
attributedCommentsExistForUser($userId)
& retrieve($sql, $params=false, $callHooks=true)
Definition: DAO.inc.php:84
& getCommentsByParentId($parentId, $childLevels=0)
_returnCommentFromRow($row, $childLevels=0)
Class for public Comment associated with submission.
Definition: Comment.inc.php:22
_getInsertId($table= '', $id= '')
Definition: DAO.inc.php:249
datetimeFromDB($dt)
Definition: DAO.inc.php:313
static call($hookName, $args=null)
deleteBySubmissionId($submissionId)
getById($commentId, $submissionId, $childLevels=0)
insertObject($comment)
incrementChildCount($commentId)
Operations for retrieving and modifying Comment objects.
getByUserId($userId)
updateObject($comment)
datetimeToDB($dt)
Definition: DAO.inc.php:293
update($sql, $params=false, $callHooks=true, $dieOnError=true)
Definition: DAO.inc.php:208
deleteObject($comment, $isRecursing=false)
decrementChildCount($commentId)
& getRootCommentsBySubmissionId($submissionId, $childLevels=0)
static getCurrentDate($ts=null)
Definition: Core.inc.php:95