<?php
class Forum{
static private $instance = NULL;
static function a(){
if(self::$instance==NULL)self::$instance=new Forum();
return self::$instance;
}
public function getForums(){
$forums = DB::a()->d->query("SELECT * FROM `forums` ORDER BY `id`")->fetchAll();
$tops = count(DB::a()->d->query("SELECT * FROM `forumTopics`")->fetchAll());
$mess = count(DB::a()->d->query("SELECT * FROM `forumMessage`")->fetchAll());
DB::a()->d->query("UPDATE `setting` SET `countTopics` = '".$tops."',`countMessage` = '".$mess."' WHERE `id` = '1' LIMIT 1");
$counts = json_decode(User::a()->user['counts'],true);
$counts['forums'] = $tops;
$counts['topics'] = $mess;
User::a()->user['counts'] = json_encode($counts);
$newSort = array();
foreach($forums as $key=>$value){
$newSort[$value['id']]['data'] = $value;
/*Новые сообщения на форуме*/
if($value['users']=='')$newSort[$value['id']]['star'] = true;
else{
$users = json_decode($value['users'],true);
if(!in_array(User::a()->user['id'], $users ,true))$newSort[$value['id']]['star'] = true;
else $newSort[$value['id']]['star'] = false;
}
}
return $newSort;
}
public function getForum($id){
$id = abs(intval($id));
$forum = DB::a()->d->query("SELECT * FROM `forums` WHERE `id` = '".$id."' LIMIT 1")->fetch();
if($forum===false)return false;
return $forum;
}
public function getTopic($id){
$id = abs(intval($id));
$topic = DB::a()->d->query("SELECT * FROM `forumTopics` WHERE `id` = '".$id."' LIMIT 1")->fetch();
if($topic===false)return false;
return $topic;
}
public function closed($id){
$id = abs(intval($id));
$topic = $this->getTopic($id);
if($topic === false)return false;
if($topic['closed']==1)$closed = 0;
else $closed = 1;
DB::a()->d->query("UPDATE `forumTopics` SET `closed` = '".$closed."' WHERE `id` = '".$id."' LIMIT 1");
return true;
}
public function delete($id){
$id = abs(intval($id));
$topic = $this->getTopic($id);
if($topic === false)return false;
DB::a()->d->query("DELETE FROM `forumTopics` WHERE `id` = '".$id."' LIMIT 1");
return true;
}
public function delMessage($id){
$id = abs(intval($id));
$message = DB::a()->d->query("SELECT * FROM `forumMessage` WHERE `id` = '".$id."' LIMIT 1")->fetch();
if($message === false)return false;
DB::a()->d->query("DELETE FROM `forumMessage` WHERE `id` = '".$id."' LIMIT 1");
return true;
}
public function getTopics($id,$page){
if(!$page) $page = 1;
$page = abs(intval($page));
$start = $page * 15 - 15;
$forum = $this->getForum($id);
if($forum === false) return false;
if($forum['users'] == ''){$users=array(User::a()->user['id']);}
else{
$users = json_decode($forum['users'],true);
if(!in_array(User::a()->user['id'], $users ,true))$users[] = User::a()->user['id'];
}
$newUsers = json_encode($users);
DB::a()->d->query("UPDATE `forums` SET `users` = '".$newUsers."' WHERE `id` = '".$id."' LIMIT 1");
$tops = DB::a()->d->query("SELECT * FROM `forumTopics` WHERE `forum` = '".$forum['id']."' ORDER BY `imp` DESC,`lastMessage` DESC")->fetchAll();
if(count($tops)<1)return array('topics'=>NULL,'forum'=>$forum);
$count = count($tops);
$topics = array();
for($i=$start;$i<=(15*$page-1);$i++){
if(array_key_exists($i,$tops) == true)$topics[$i]=$tops[$i];
}
$newSort = array();
foreach($topics as $key=>$value){
$newSort[$value['id']]['data'] = $value;
$newSort[$value['id']]['message'] = DB::a()->d->query("SELECT * FROM `forumMessage` WHERE `topic` = '".$value['id']."'")->fetchAll();
/*Новые сообщения на форуме*/
if($value['users']=='')$newSort[$value['id']]['star'] = true;
else{
$users = json_decode($value['users'],true);
if(!in_array(User::a()->user['id'], $users ,true))$newSort[$value['id']]['star'] = true;
else $newSort[$value['id']]['star'] = false;
}
}
return array('count'=>$count, 'countMess'=> $countMess, 'topics'=>$newSort,'forum'=>$forum);
}
public function getMessages($id,$page){
if(!$page) $page = 1;
$page = abs(intval($page));
$start = $page * 10 - 10;
$topic = $this->getTopic($id);
if($topic === false) return false;
if($topic['users'] == ''){$users=array(User::a()->user['id']);}
else{
$users = json_decode($topic['users'],true);
if(!in_array(User::a()->user['id'], $users ,true))$users[] = User::a()->user['id'];
}
$newUsers = json_encode($users);
DB::a()->d->query("UPDATE `forumTopics` SET `users` = '".$newUsers."' WHERE `id` = '".$id."' LIMIT 1");
$result = DB::a()->d->query("SELECT `forumMessage`.* ,".User::a()->join." FROM `forumMessage` JOIN `users` ON `users`.`id` = `forumMessage`.`user` WHERE `forumMessage`.`topic` = '".$topic['id']."' ORDER BY `forumMessage`.`id`")->fetchAll();
if(count($result)<1)return array('mess'=>NULL,'topic'=>$topic);
$count = count($result);
$mess = array();
for($i=$start;$i<=(10*$page-1);$i++){
if(array_key_exists($i,$result) == true)$mess[$i]=$result[$i];
}
$newSort = array();
foreach($mess as $key=>$value){
$newSort[$value['id']]['data'] = $value;
/*Новые сообщения на форуме*/
if($value['users']=='')$newSort[$value['id']]['star'] = true;
else{
$users = json_decode($value['users'],true);
if(!in_array(User::a()->user['id'], $users ,true))$newSort[$value['id']]['star'] = true;
else $newSort[$value['id']]['star'] = false;
}
}
return array('count'=>$count,'mess'=>$newSort,'topic'=>$topic);
}
public function addForum($name,$admin,$new){
$forums = DB::a()->d->query("INSERT INTO `forums` SET `name` = '".$name."' , `admin` = '".$admin."' , `newTop` = '".$new."'");
return true;
}
public function addTopic($forum,$name,$user,$text){
DB::a()->d->query("UPDATE `forums` SET `users` = '' WHERE `id` = '".$forum."' LIMIT 1");
DB::a()->d->query("INSERT INTO `forumTopics` SET `user` = '".$user."' ,`forum` = '".$forum."' ,`name` = '".$name."' , `lastMessage` = '".time()."'");
$id = DB::a()->d->lastInsertId();
$tops = count(DB::a()->d->query("SELECT * FROM `forumTopics`")->fetchAll());
DB::a()->d->query("UPDATE `setting` SET `countTopics` = '".$tops."' WHERE `id` = '1' LIMIT 1");
$counts = json_decode(User::a()->user['counts'],true);
$counts['forums'] = $tops;
User::a()->user['counts'] = json_encode($counts);
DB::a()->d->query("INSERT INTO `forumMessage` SET `forum` = '".$forum."' ,`topic` = '".$id."' ,`user` = '".$user."' , `message` = '".$text."' , `time` = '".time()."'");
return $id;
}
public function addMessage($forum,$topic,$user,$text){
DB::a()->d->query("INSERT INTO `forumMessage` SET `forum` = '".$forum."' ,`topic` = '".$topic."' ,`user` = '".$user."' , `message` = '".$text."' , `time` = '".time()."'");
DB::a()->d->query("UPDATE `forumTopics` SET `users` = '' ,`lastMessage` = '".time()."' WHERE `id` = '".$topic."' LIMIT 1");
DB::a()->d->query("UPDATE `forums` SET `users` = '' WHERE `id` = '".$forum."' LIMIT 1");
$mess = count(DB::a()->d->query("SELECT * FROM `forumMessage` ")->fetchAll());
$thisMess = count(DB::a()->d->query("SELECT * FROM `forumMessage` WHERE `topic` = ".$topic)->fetchAll());
DB::a()->d->query("UPDATE `setting` SET `countMessage` = '".$mess."' WHERE `id` = '1' LIMIT 1");
$counts = json_decode(User::a()->user['counts'],true);
$counts['topics'] = $mess;
User::a()->user['counts'] = json_encode($counts);
return $thisMess;
}
public function folderForum($star){
if($star===true)return "<img src='/img/icons/new.png' style='margin-right:2px;width:16px'>";
return "<img src='/img/icons/folder.png' style='margin-right:2px;width:16px'>";
}
public function folderTopic($closed,$star){
if($closed==1)return "<img src='/img/icons/topic_closed.png' style='margin-right:2px;width:16px'>";
if($star===true)return "<img src='/img/icons/topic_new.png' style='margin-right:2px;width:16px'>";
return "<img src='/img/icons/topic.png' style='margin-right:2px;width:16px'>";
}
}