Файл: housetrack/modules/status/controllers/status.php
Строк: 120
<?
defined('IN_SYSTEM') or die('<b>403<br />Запрет доступа!</b>');
Class Status_Controller extends Controller
{
public $access_level=5;
public function __construct()
{
parent::__construct();
if ((ROUTE_ACTION == 'view' || ROUTE_ACTION == 'index' || ROUTE_ACTION == '') && is_numeric($_GET['user_id']))
$user_id = intval($_GET['user_id']);
else $user_id = USER_ID;
// Получаем профайл пользователя
if ($user_id == -1) a_notice('Гости не имеют анкеты на сайте.', URL);
if ( ! $this->profile = $this->db->get_row("SELECT * FROM #__users_profiles JOIN #__users USING(user_id) WHERE user_id = '". $user_id ."'")) a_error('Анкета пользователя не найдена.');
}
public function action_add(){
$status=$this->db->get_one("SELECT msg FROM #__status WHERE user_id='".USER_ID."' ORDER BY message_id DESC LIMIT 1");
if (isset($_POST['submit'])) {
If (empty($_POST['add'])){
$this->error='Введите статут';
}
If (strlen($_POST['add'])<4 || strlen($_POST['add'])>100 ){
$this->error='Неправильная длинна (4-100)';}
If (!$this->error)
{
$this->db->query("INSERT INTO #__status SET
user_id = '".USER_ID."',
msg = '".a_safe($_POST['add'])."',
time = UNIX_TIMESTAMP()
");
header("Location: ". a_url('user/profile/view', '', true));
}
}
if (!isset($_POST['submit']) OR $this->error){
$this->tpl->assign(array(
'error' => $this->error,
'status' => $status));
$this->tpl->display('add');
}
}
public function action_index()
{
$sql = "SELECT SQL_CALC_FOUND_ROWS #__status.*,(SELECT COUNT(*) FROM #__status_comm WHERE #__status_comm.status_id=#__status.message_id) AS count_msg,
(SELECT COUNT(*) FROM #__like WHERE id_src = #__status.message_id AND types='status') as count_status
FROM #__status WHERE user_id='".$this->profile['user_id']."'
ORDER BY message_id DESC LIMIT $this->start, $this->per_page";
// Выполнение запроса
$result = $this->db->query($sql);
// Получение кол-ва сообщений
$total = $this->db->get_one("SELECT FOUND_ROWS()");
// Подключение помощника смайлов
if ( ! class_exists('smiles')) a_import('modules/smiles/helpers/smiles');
// Форматирование сообщений
while($status = $this->db->fetch_array($result)) {
$status['msg'] = smiles::smiles_replace(main::bbcode(nl2br($status['msg'])));
$statuss[] = $status;
}
// Конфигурация пагинации
$pg_conf['base_url'] = a_url('status', 'user_id='.$this->profile['user_id'].'&start=');
$pg_conf['total_rows'] = $total;
$pg_conf['per_page'] = $this->per_page;
// Пагинация
a_import('libraries/pagination');
$pg = new CI_Pagination($pg_conf);
// Назначение переменных
$this->tpl->assign(array(
'profile'=>$this->profile,
'total' => $total,
'statuss' => $statuss,
'pagination' => $pg->create_links()
));
// Вывод шаблона
$this->tpl->display('listing');
}
public function action_view()
{
If (is_numeric($_GET['status_id']) && is_numeric($_GET['user_id']))
{
if (!$this->stats=$this->db->get_row("SELECT #__status.*,(SELECT COUNT(*) FROM #__like WHERE id_src = #__status.message_id AND types='status') as count_status,(SELECT COUNT(*) FROM #__status_comm WHERE #__status_comm.status_id=#__status.message_id) as count FROM #__status WHERE user_id='".$this->profile['user_id']."' AND message_id='".intval($_GET['status_id'])."'"))
a_error('Статус не найден');
}
else
a_error('Статус не найден');
// Запрос для вывода сообщений
$sql = "SELECT SQL_CALC_FOUND_ROWS #__status_comm.*, #__users.status AS user_status, up.avatar AS avatar_exists, #__users.last_visit, #__users.username AS user_name
FROM #__status_comm LEFT JOIN #__users USING(user_id) LEFT JOIN #__users_profiles AS up USING(user_id) WHERE status_id='".$this->stats['message_id']."'
ORDER BY message_id DESC LIMIT $this->start, $this->per_page";
// Выполнение запроса
$result = $this->db->query($sql);
// Получение кол-ва сообщений
$total = $this->db->get_one("SELECT FOUND_ROWS()");
// Подключение помощника смайлов
if ( ! class_exists('smiles')) a_import('modules/smiles/helpers/smiles');
// Форматирование сообщений
while($message = $this->db->fetch_array($result)) {
$message['msg'] = smiles::smiles_replace(main::bbcode(nl2br($message['msg'])));
$messages[] = $message;
}
// Конфигурация пагинации
$pg_conf['base_url'] = a_url('status/view', 'user_id='.$this->profile['user_id'].'&status_id='.intval($_GET['status_id']).'&start=');
$pg_conf['total_rows'] = $total;
$pg_conf['per_page'] = $this->per_page;
// Пагинация
a_import('libraries/pagination');
$pg = new CI_Pagination($pg_conf);
if(isset($_POST['submit'])) {
if (USER_ID == -1) {
$this->error .= 'Для написания сообщений вам необходимо авторизироваться на сайте<br />';
}
if(empty($_POST['message'])) {
$this->error .= 'Укажите сообщение<br />';
}
if(!$this->error) {
a_antiflud();
setcookie('username', $_POST['username'], time() + 999999999, '/');
$repl=NULL;
if (isset($_GET['reply']))
{
if(!$this->reply = $this->db->get_row("SELECT * FROM #__users WHERE username = '".a_safe($_GET['reply'])."'"))
a_error("Пользователь не найден");
else
{
$repl=$this->reply['username'];
}
}
$this->db->query("INSERT INTO #__status_comm SET
user_id = '".USER_ID."',
status_id = '".$this->stats['message_id']."',
msg = '". a_safe($_POST['message']) ."',
time = UNIX_TIMESTAMP(),
reply= '$repl'
");
$_SESSION['captcha_code'] = main::get_unique_code(4);
user::rating_update();
header("Location: ". a_url('status/view', 'user_id='.$this->profile['user_id'].'&status_id='.$this->stats['message_id'], true));
exit;
}
}
if(!isset($_POST['submit']) OR $this->error) {
// Назначение переменных
$this->tpl->assign(array(
'profile'=>$this->profile,
'error'=>$this->error,
'stats'=>$this->stats,
'total' => $total,
'messages' => $messages,
'pagination' => $pg->create_links(),
));
// Вывод шаблона
$this->tpl->display('view');
}
}
public function action_status_del(){
if (!$this->stats=$this->db->get_row("SELECT * FROM #__status WHERE message_id='".intval($_GET['status_id'])."'"))
a_error('Статус не найден');
if ($this->stats['user_id']!=USER_ID)
a_error('У вас нет прав');
if(!empty($_GET['confirm'])) {
$this->db->query("DELETE FROM #__status WHERE message_id = '". $this->stats['message_id'] ."' AND user_id='".USER_ID."'");
header("Location: ". a_url('status/', '', true));
}
else {
a_confirm('Подтверждаете удаление данного сообщения?', a_url('status/status_del', 'status_id='. $this->stats['message_id'].'&confirm=ok'), a_url('status/view/', 'user_id='.$this->stats['user_id'].'status_id='.$this->stats['message_id']));
}
}
public function action_comm_del(){
if (!$this->comm=$this->db->get_row("SELECT * FROM #__status_comm WHERE message_id='".intval($_GET['message_id'])."'"))
a_error('Сообщение не найдено');
$pravo=$this->db->get_one("SELECT user_id FROM #__status WHERE message_id='".$this->comm['status_id']."'");
if ($pravo!=USER_ID)
a_error('У вас нет прав');
if(!empty($_GET['confirm'])) {
$this->db->query("DELETE FROM #__status_comm WHERE message_id = '". $this->comm['message_id'] ."'");
header("Location: ". a_url('status/view', 'user_id='.$pravo.'&status_id='.$this->comm['status_id'], true));
}
else {
a_confirm('Подтверждаете удаление данного сообщения?', a_url('status/comm_del', 'message_id='. $this->comm['message_id'].'&confirm=ok'), a_url('status/view/', 'user_id='.$pravo.'status_id='.$this->comm['status_id']));
}
}
public function action_edit(){
if (!$this->stats=$this->db->get_row("SELECT * FROM #__status WHERE message_id='".intval($_GET['status_id'])."'"))
a_error('Статус не найден');
if ($this->stats['user_id']!=USER_ID)
a_error('У вас нет прав');
if ($this->stats['time']+300<time())
a_error('Редактировать запись можно только 5 мин с момента её создания!');
$status=$this->db->get_one("SELECT msg FROM #__status WHERE message_id='".$this->stats['message_id']."' ");
if (isset($_POST['submit'])) {
If (empty($_POST['add'])){
$this->error='Введите статут';
}
If (strlen($_POST['add'])<4 || strlen($_POST['add'])>100 ){
$this->error='Неправильная длинна (4-100)';}
If (!$this->error)
{
$this->db->query("UPDATE #__status SET
`msg` = '".a_safe($_POST['add'])."'
WHERE `message_id`='".$this->stats['message_id']."' AND `user_id`='".$this->stats['user_id']."'");
header("Location: ". a_url('user/profile/view', '', true));
}
}
if (!isset($_POST['submit']) OR $this->error){
$this->tpl->assign(array(
'error' => $this->error,
'status' => $status));
$this->tpl->display('edit');
}
}
public function action_like()
{
if (is_numeric($_GET['status_id']))
{
if(!$message = $this->db->get_row("SELECT * FROM #__status WHERE message_id = '". intval($_GET['status_id']) ."'"))
a_error('Сообщение не найдено!');
else
$id=intval($_GET['status_id']);
$type='status';
}
else
{
rederect('/');
exit();
}
#Проверяем ставил ли юзер лайк на какюто запись и т.п если нет то ставим
$result = $this->db->query("SELECT * FROM `#__like` WHERE id_us='".USER_ID."' AND id_src='$id' AND types= '$type'");
if($this->db->num_rows($result)==0)
{
#записываем лайк
$this->db->query("INSERT INTO #__like (`id_us`,`id_src`, `types`)
VALUES ('".USER_ID."','$id','$type')");
if (isset($_SERVER['HTTP_REFERER']) && $_SERVER['HTTP_REFERER']!=NULL)
header("Location: ".$_SERVER['HTTP_REFERER']);
else
header("Location: index.php");
#проверяем есть ли лайк если есть то удаляем
}
elseif ($this->db->num_rows($result)!=0)
{
#удаляем лайк
$this->db->query("DELETE FROM #__like WHERE id_src='$id' AND id_us='".USER_ID."' AND types='$type'");
#возвращаем юзера обратно откуда он пришел
if (isset($_SERVER['HTTP_REFERER']) && $_SERVER['HTTP_REFERER']!=NULL)
header("Location: ".$_SERVER['HTTP_REFERER']);
else
header("Location: /");
}
}
}
?>