<?php

require_once '../incfiles/core.php';
require_once '../incfiles/func.php';
require_once '../incfiles/auth.php';
require_once '../incfiles/user.php';
if (empty($user_id)) {go(URL.'/');}

if (!empty($_GET['id']) && is_numeric($_GET['id']))
{
	$id = abs(intval($_GET['id']));
	$result = mysql_query("SELECT * FROM `blog` WHERE `id` = '".$id."' AND `user_id` = '".$user_id."' LIMIT 1");
	if (mysql_num_rows($result) == true)
	{
		$arr_blog = mysql_fetch_array(mysql_query("SELECT * FROM `blog` WHERE `id` = '".$id."' AND `user_id` = '".$user_id."' LIMIT 1"));
	}
	else
	{
		go(URL.'/response/?id='.$user_id);
	}
}
else
{
	go(URL.'/response/?id='.$user_id);
}

if (!empty($_POST['del']))
{
	mysql_query("UPDATE `users` SET count_blog=count_blog-1 WHERE `id` = '".$user_id."'");
	mysql_query("DELETE FROM `blog` WHERE `id` = '".$id."'");
	mysql_query("DELETE FROM `blog_comm` WHERE `fid` = '".$id."'");
	mysql_query("DELETE FROM `blog_like` WHERE `fid` = '".$id."'");
	go(URL.'/response/?id='.$user_id);
}
else if (!empty($_POST['text']))
{
	$text = check($_POST['text']);
	$text = substr($text, 0, 10000);
	if (empty($text)) {go(URL.'/response/post.php?id='.$id);}

	if (mysql_query("UPDATE `blog` SET `text` = '".$text."' WHERE `id` = '".$id."' AND `user_id` = '".$user_id."'") == true)
	{
		go(URL.'/response/post.php?id='.$id);
	}
}

	$title = ''.lang('Редактировать вопрос','Редагувати питання').'';
	require_once('../incfiles/head.php');
?>
<script type="text/javascript">
$(function(){
	$('#form').on('submit', function(e){
	$('#form_content').html('');
	Page.Loading('start');
    e.preventDefault();
    var $that = $(this),
        formData = new FormData($that.get(0));
    $.ajax({
      url: $that.attr('action'),
      type: $that.attr('method'),
      contentType: false,
      processData: false,
      data: formData,
      success: function(response){
        if(response){
 			Page.Go($that.attr('action'));
       }
     }
    });
  });
});
</script>
<?
	echo '<div class="title">'.lang('Редактировать вопрос','Редагувати питання').'</div>
	
	

		<form id="form" action="'.URL.'/response/edit.php?id='.$id.'" method="post">
	<div class="block2">
		<textarea id="textarea" name="text" class="input">'.no_tags($arr_blog['text']).'</textarea>
	</div><div class="block2">

	
<input class="button" type="submit" value="'.lang('Сохранить','Зберегти').'"/> 

	<input class="button" type="submit" name="del" value="'.lang('Удалить','Видалити').'"/>
	</div>
	</form>';

	require_once('../incfiles/foot.php');
	exit;


?>