<?php
class Post {
public $id;
public $post;
public $userId;
public $threadId;
public $Attachments;
public $User;
public $ip;
public $updated;
public $created;
public $autoLoad;
function __construct($id, $post=NULL, $userId, $threadId, $ip=NULL, $updated, $created, $username=NULL, $autoLoad=array()) {
$this->id = $id;
$this->post = stripslashes($post);
$this->userId = $userId;
$this->username = !is_null($username) ? $username : NULL;
$this->threadId = $threadId;
$this->ip = $ip;
$this->updated = $updated;
$this->created = $created;
$this->autoLoad = $autoLoad;
$this->AutoLoad($this->autoLoad);
}
public static function CleanPosts($posts, $smileArray, $keywords=NULL) {
if (is_array($posts)) {
foreach($posts as $post) {
$post->post = nl2br($post->post);
$post->post = Forum_Code::ReturnFinalString($post->post,$post->User->username);
$post->post = Smile::FindAndReplace($post->post, $smileArray);
$post->User->Reputation = isset($post->User->Reputation) ? Reputation::Display($post->User->Reputation) : Reputation::Display(0);
if (!is_null($keywords)) {
$post->post = preg_replace("/$keywords/i", '<span class="highlightString">'.$keywords.'</span>', $post->post);
}
}
return $posts;
}
}
public static function CleanPost($post, $smileArray) {
$post->post = nl2br($post->post);
$username = isset($post->User->username) ? $post->User->username : $post->username;
$post->post = Forum_Code::ReturnFinalString($post->post, $username);
$post->post = Smile::FindAndReplace($post->post, $smileArray);
$post->User->Reputation = isset($post->User->Reputation) ? Reputation::Display($post->User->Reputation) : Reputation::Display(0);
return $post;
}
public static function Load($id, $autoLoad=array()) {
try {
$db = Database::GetInstance();
$query = $db->query("SELECT p.*, u.username FROM Post p JOIN User u ON p.user_id = u.id WHERE p.id = $id ") or die($db->error);
if ($query && $query->num_rows > 0) {
$q = $query->fetch_object();
if ($Object = new self($q->id, $q->post, $q->user_id, $q->thread_id, $q->ip, $q->updated, $q->created, $q->username, $autoLoad)) {
return $Object;
}
} else {
throw new Exception_Application('Could not load postId '.$id);
}
} catch(Exception_Database $e) {
throw new Exception_Application($e->getMessage(), $e->getCode());
}
}
public function AutoLoad($autoLoad) {
if (is_array($autoLoad)) {
foreach($autoLoad as $load):
switch($load):
case 'attachments':
$this->LoadAttachments();
break;
case 'is_first_post':
$this->LoadIsFirstPost();
break;
endswitch;
endforeach;
} else if ($autoLoad === true) {
$this->LoadAttachments();
$this->LoadIsFirstPost();
}
}
public function LoadAttachments() {
$this->Attachments = Attachment::LoadAllByPostId($this->id);
}
public function LoadIsFirstPost() {
$db = Database::GetInstance();
$query = $db->query("SELECT id FROM Post WHERE thread_id = ".$this->threadId." ORDER BY id ASC LIMIT 1");
$result = $query->fetch_object();
$this->isFirstPost = ($result->id == $this->id) ? true : false;
}
public function Delete() {
try {
$db = Database::GetInstance();
$result = $db->query("DELETE FROM Post WHERE id = ".$this->id);
if (!$result) {
return false;
} else if ($db->affected_rows >= 0) {
Thread::RemovePost($this->threadId);
User::RemovePost($this->userId);
return true;
} else {
return false;
}
} catch(Exception_Database $e) {
throw new Exception_Application($e->getMessage(), $e->getCode());
}
}
public function DeleteByThreadId($threadId) {
try {
$db = Database::GetInstance();
$query = $db->query("SELECT id, user_id FROM Post WHERE thread_id = ".$threadId);
while($q = $query->fetch_object()) {
User::RemovePost($q->user_id);
}
return $db->query("DELETE FROM Post WHERE thread_id = ".$threadId);
} catch(Exception_Database $e) {
throw new Exception_Application($e->getMessage(), $e->getCode());
}
}
public function Save() {
try {
$db = Database::GetInstance();
$post = Forum_Code::Prepare_Post($this->post);
$result = $db->query("UPDATE Post SET post = ".$db->safe($this->post).", user_id = ".$this->userId.", thread_id = ".$this->threadId.", ip = '".$this->ip."', updated = ".time()." WHERE id = ".$this->id);
if (!$result) {
return false;
} else if ($db->affected_rows >= 0) {
return true;
} else {
return false;
}
} catch(Exception_Database $e) {
throw new Exception_Application($e->getMessage(), $e->getCode());
}
}
public static function NewPostsCount($userId, $timestamp) {
try {
$db = Database::GetInstance();
$query = $db->query("SELECT p.id
FROM Post p
INNER JOIN Thread t ON t.id = p.thread_id
INNER JOIN Forum f ON f.id = t.forum_id
WHERE ((f.require_access = 1 AND EXISTS (SELECT 1 FROM User_Data u WHERE u.user_id = $userId AND u.value = f.id AND u.key = 'ForumAccess' ))
OR f.require_access = 0 )
AND p.created > $timestamp ");
if ($query) {
return $query->num_rows;
}
} catch(Exception_Database $e) {
throw new Exception_Application($e->getMessage(), $e->getCode());
}
}
public static function ReturnByUserId($userId, $limit, $autoLoad=array()) {
global $User;
try {
$db = Database::GetInstance();
$query = $db->query("SELECT p.*,
t.id as t_id, t.title as t_title, t.forum_id as t_forum_id, t.group_id as t_group_id, t.icon_id as t_icon_id, t.views as t_views, t.posts as t_posts, t.locked as t_locked, t.sticky as t_sticky
FROM Post p
JOIN Thread t ON t.id = p.thread_id
JOIN Forum f ON f.id = t.forum_id
WHERE ((f.require_access = 1 AND EXISTS (SELECT 1 FROM User_Data u WHERE u.user_id = $User->id AND u.value = f.id AND u.key = 'ForumAccess' ))
OR f.require_access = 0)
AND p.user_id = $userId
ORDER BY p.updated DESC LIMIT $limit");
if ($query && $query->num_rows > 0) {
$posts = array();
while ($q = $query->fetch_object()) {
$Object = new self($q->id, $q->post, $q->user_id, $q->thread_id, $q->ip, $q->updated, $q->created, NULL, $autoLoad);
$Object->Thread = new Thread($q->t_id, $q->t_title, $q->t_forum_id, $q->t_group_id, $q->t_icon_id, $q->t_views, $q->t_posts, $q->t_locked, $q->t_sticky);
$posts[] = $Object;
}
return $posts;
}
return false;
} catch(Exception_Database $e) {
throw new Exception_Application($e->getMessage(), $e->getCode());
}
}
public static function LoadAllByThreadId($id, $from, $to, $autoLoad=array()) {
try {
$db = Database::GetInstance();
$query = $db->query("SELECT p.*, u.username as u_username, u.postcount as u_postcount, u.last_seen as u_last_seen, u.created as u_created, u.status as u_status, (SELECT COUNT(*) FROM Attachment WHERE post_id = p.id) as attachmentCount
FROM Post p
JOIN User u ON p.user_id = u.id
WHERE p.thread_id = $id
ORDER BY p.id ASC
LIMIT $from, $to") or die($db->error);
if ($query && $query->num_rows > 0) {
$results = array();
while ($q = $query->fetch_object()) {
$Object = new self($q->id, $q->post, $q->user_id, $q->thread_id, $q->ip, $q->updated, $q->created, $q->u_username, $autoLoad);
$Object->User = new User($q->user_id, $q->u_username, NULL, NULL, $q->u_status, $q->u_postcount, NULl, NULL, NULL, $q->u_last_seen, $q->u_created, $autoLoad);
if ($q->attachmentCount > 0 && in_array('attachments', $autoLoad)) {
$Object->Attachments = Attachment::LoadAllByPostId($q->id);
}
$posts[] = $Object;
}
return $posts;
}
return false;
} catch(Exception_Database $e) {
throw new Exception_Application($e->getMessage(), $e->getCode());
}
}
public static function LoadLastPostInThread($id, $autoLoad=array()) {
global $User;
try {
$db = Database::GetInstance();
$query = $db->query("SELECT p.*, u.username as u_username, u.postcount as u_postcount, u.last_seen as u_last_seen, u.created as u_created, u.status as u_status, (SELECT COUNT(*) FROM Attachment WHERE post_id = p.id) as attachmentCount
FROM Post p
JOIN User u ON p.user_id = u.id
WHERE p.id = $id
ORDER BY p.id DESC
LIMIT 1") or die($db->error);
if ($query && $query->num_rows > 0) {
$results = array();
while ($q = $query->fetch_object()) {
$Object = new self($q->id, $q->post, $q->user_id, $q->thread_id, $q->ip, $q->updated, $q->created, $q->u_username, $autoLoad);
$Object->User = new User($q->user_id, $q->u_username, NULL, NULL, $q->u_status, $q->u_postcount, NULl, NULL, NULL, $q->u_last_seen, $q->u_created, $autoLoad);
if ($q->attachmentCount > 0 && in_array('attachments', $autoLoad)) {
$Object->Attachments = Attachment::LoadAllByPostId($q->id);
}
return $Object;
}
}
return false;
} catch(Exception_Database $e) {
throw new Exception_Application($e->getMessage(), $e->getCode());
}
}
public static function Create($body, $userId, $threadId, $ip) {
try {
$db = Database::GetInstance();
$post = Forum_Code::Prepare_Post($body);
$time = time();
$return = $db->query("INSERT INTO Post (post, user_id, thread_id, ip, updated, created) VALUES (".$db->safe($body).", $userId, $threadId, '$ip', $time, $time ) ");
$id = $db->insert_id;
if ($id > 0) {
User::AddPost($userId);
Thread::AddPost($threadId);
return $id;
} else {
return false;
}
} catch(Exception_Database $e) {
throw new Exception_Application($e->getMessage(), $e->getCode());
}
}
public function GetPageNumber($perPage) {
$db = Database::GetInstance();
$query = $db->query("SELECT * FROM Post WHERE created < ".$this->created." AND thread_id = ".$this->threadId);
$count = $query->num_rows;
if ($perPage == $count) {
return 2;
} else {
$page = ceil($count / $perPage);
return ($page == 0) ? 1 : $page;
}
}
}
?>
Bookmarks