<?php
/*
* @package LabCMS
* @copyright (c) 2006 Alexander Levin aka Loki (loki_angel@mail.ru)
* @support http://labcms.ru
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*/
	if ($mode=='delete')
	{
		User::checkToken();
		$files=Db::i()->selectCol("SELECT file_id FROM ?_bt_files WHERE post_id=?", $post_info['post_id']);
		if ($files) delete_files($files);
		Db::i()->query("DELETE p, f FROM ?_bt_posts p LEFT JOIN ?_bt_files f ON p.post_id=f.post_id WHERE p.post_id=?", $_GET['var1']);
		Db::i()->query("UPDATE ?_bt_bugs SET bug_date=(SELECT MAX(putdate) FROM ?_bt_posts WHERE bug_id=?) WHERE bug_id=?", $post_info['bug_id'], $post_info['bug_id']);
		//TODO тут надо будет обновить счетчик постов, если он будет
		refresh('', LabCMS::i()->url($post_info['bug_id'], 'item'));
	}
	
	if ($mode=='delete_bug')
	{
		User::checkToken();
		$files=Db::i()->selectCol("SELECT file_id FROM ?_bt_files f JOIN ?_bt_posts p ON f.post_id=p.post_id WHERE bug_id=?", $_GET['var1']);
		if ($files) delete_files($files);
		Db::i()->query("DELETE b, p FROM ?_bt_bugs b LEFT JOIN ?_bt_posts p ON b.bug_id=p.bug_id WHERE b.bug_id=?", $_GET['var1']);
		refresh();
	}
	
	
	if ($mode=='edit') $is_first=($post_info['post_id']==Db::i()->selectCell("SELECT MIN(post_id) FROM ?_bt_posts WHERE bug_id=?", $post_info['bug_id']));
	if ($is_first)
	{
		$versions=Db::i()->selectCol("SELECT version_id AS ARRAY_KEY, version FROM ?_bt_versions WHERE project_id=? ORDER BY version_id", $project_id);
		Tpl::i()->assign('versions', $versions);
	}
	
	
	if ($_SERVER['REQUEST_METHOD']=='POST')
	{
		User::checkToken();
		if ($is_first && !trim($_POST["summary"])) LabCMS::i()->error("Не заполнена тема сообщения");
		if (!trim($_POST["msg"])) LabCMS::i()->error("Не заполнен текст сообщения");
		$ids=array();
		
		if (is_array($_POST['del_img']))
		{
			$files=Db::i()->selectCol("SELECT file_id FROM ?_bt_files WHERE post_id=? AND file_id IN (?a)", $post_info['post_id'], array_keys($_POST['del_img']));
			if ($files) delete_files($files);
		}

		if (LabCMS::i()->no_errors)
		{
			foreach($_FILES['shot']['name'] as $key=>$value)
			{
				if (LabConfig::$mcfg['max_upload_size'] && ($_FILES['shot']['size'][$key]>(LabConfig::$mcfg['max_upload_size']*1024)))
				{
					LabCMS::i()->error(sprintf(_m("Файл %s слишком большой"), $_FILES['shot']['name'][$key]), true);
					break;
				}
				if ($_FILES['shot']['tmp_name'][$key]) $ids[]=upload_file($_FILES['shot']['name'][$key], $_FILES['shot']['tmp_name'][$key]);
			}
		}

		if (LabCMS::i()->no_errors)
		{
			if ($is_first) Db::i()->query("UPDATE ?_bt_bugs SET summary=?, version_id=? WHERE bug_id=?", $_POST['summary'], $_POST['version_id'], $post_info['bug_id']);
			if ($mode=='edit')
			{
				Db::i()->query("UPDATE ?_bt_posts SET msg=? WHERE post_id=?", $_POST['msg'], $post_info['post_id']);
			}
			
			if ($mode=='reply')
			{
				$post_info['post_id']=Db::i()->query("INSERT INTO ?_bt_posts (msg, bug_id, user_id, putdate) VALUES(?, ?, ?, NOW())", $_POST['msg'], $post_info['bug_id'], User::i()->id);
				Db::i()->query("UPDATE ?_bt_bugs SET bug_date=NOW() WHERE bug_id=?", $post_info['bug_id']);
			}
			if (count($ids)) Db::i()->query("UPDATE ?_bt_files SET post_id=? WHERE file_id IN (?a)", $post_info['post_id'], $ids);
			refresh('', LabCMS::i()->url($post_info['bug_id'], 'item'));
		}
		else
		{
			if (count($ids)) delete_files($ids);
		}
	}
	
	if ($mode=='edit')
	{
		Tpl::i()->assign('msg', $post_info['msg']);
		if ($is_first)
		{
			Tpl::i()->assign('summary', $post_info['summary']);
			Tpl::i()->assign('version_id', $post_info['version_id']);
		}
		
		$comment=array();
		$all_files=Db::i()->select("SELECT file_id AS ARRAY_KEY, file_type, file_name FROM ?_bt_files WHERE post_id=?", $post_info['post_id']);
		if ($all_files)
		foreach($all_files as $file_id=>$file)
		{
			if (in_array($file['file_type'], array('gif', 'jpg', 'jpeg', 'png'))) $files['images'][$file_id]=array('type'=>$file['file_type'], 'name'=>$file['file_name']);
			else $files['files'][$file_id]=array('type'=>$file['file_type'], 'name'=>$file['file_name']);
		}
		Tpl::i()->assign($files);
	}
	
	if ($mode=='reply')
	{
		Tpl::i()->assign('msg', '[quote="'.str_replace('"', '', $post_info['login']).'"]'.$post_info['msg'].'[/quote]');
	}

	$mod_template="edit_post.tpl";
	//вывод смайликов
	Tpl::i()->assign("js_smiles", bbcode_smiles_js(4,4));
