. ', comment_of_url = :ofUrl'
. ', comment_type = :type'
. ', comment_json = :json'
+ . ', comment_pingstate = "0"'
);
$ofUrl = '';
}
/**
- * @return null|object NULL if not found, comment object otherwise
+ * @return null|object NULL if not found, JSON comment object otherwise
+ * - "Xrow" property contains the database row object
+ * - "user" property contains the user db row object
*/
- public function getComment($id)
+ public function getJsonComment($id)
{
$stmt = $this->db->prepare(
'SELECT * FROM comments WHERE comment_id = ?'
$json->Xrow = $row;
//FIXME: load user
- $stmt = $this->db->prepare(
- 'SELECT * FROM users WHERE user_id = ?'
- );
+ $stmt = $this->db->prepare('SELECT * FROM users WHERE user_id = ?');
$stmt->execute([$row->comment_user_id]);
$rowUser = $stmt->fetchObject();
if ($rowUser === false) {
$rowUser = (object) array(
- 'user_id' => 0,
- 'user_name' => 'Anonymous',
+ 'user_id' => 0,
+ 'user_name' => 'Anonymous',
'user_imageurl' => '',
);
}
return $json;
}
+ /**
+ * @return null|object NULL if not found, JSON comment object otherwise
+ * - "Xrow" property contains the database row object
+ * - "user" property contains the user db row object
+ */
+ public function listLatest()
+ {
+ $stmt = $this->db->prepare(
+ 'SELECT comment_id, comment_user_id, comment_published'
+ . ', comment_of_url, comment_type'
+ . ' FROM comments'
+ . ' ORDER BY comment_published DESC'
+ . ' LIMIT 20'
+ );
+ $stmt->execute();
+ $rows = $stmt->fetchAll(\PDO::FETCH_OBJ);
+ if (!count($rows)) {
+ return [];
+ }
+ $userIds = array_values(
+ array_unique(array_column($rows, 'comment_user_id'))
+ );
+
+
+ $placeholders = implode(',', array_fill(0, count($userIds), '?'));
+ $stmt = $this->db->prepare(
+ 'SELECT * FROM users WHERE user_id IN (' . $placeholders . ')'
+ );
+ $stmt->execute($userIds);
+
+ $users = $stmt->fetchAll(\PDO::FETCH_OBJ);
+ $users = array_combine(
+ array_column($users, 'user_id'),
+ $users
+ );
+
+ foreach ($rows as $row) {
+ $row->user = $users[$row->comment_user_id];
+ }
+
+ return $rows;
+ }
+
/**
* @return null|object NULL if not found, user database row otherwise
*/
);
return $this->db->lastInsertId();
}
+
+ public function setPostPingState($postId, $pingstate)
+ {
+ $stmt = $this->db->prepare(
+ 'UPDATE comments SET comment_pingstate = ? WHERE comment_id = ?'
+ );
+ $stmt->execute(array($pingstate, $postId));
+ }
}
?>