<?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
*/
$success=true;

set_time_limit(0);

if (Db::i()->selectCell("SELECT module_id FROM ?_modules WHERE module='forum'")) {
	//TODO перепроверить еще раз список полей
	Db::i()->query(
		"ALTER TABLE ?_forum_themes
		CHANGE `forum_id` `forum_id` int(11) NOT NULL AFTER `theme_id`,
		CHANGE `subject` `subject` varchar(60) COLLATE 'utf8_general_ci' NOT NULL AFTER `author_id`,
		CHANGE `pinup` `pinup` tinyint(4) NOT NULL DEFAULT '0' AFTER `last_user_id`,
		CHANGE `author` `author_name` varchar(32) COLLATE 'utf8_general_ci' NULL AFTER `forum_id`,
		CHANGE `last_post` `last_post_date` int(10) unsigned NULL AFTER `total_posts`,
		CHANGE `last_user` `last_user_name` varchar(32) COLLATE 'utf8_general_ci' NULL AFTER `last_post_date`,
		CHANGE `hidden_by` `hidden` int(11)  NOT NULL DEFAULT '0' AFTER `pinup`;"
	);

	Db::i()->query(
		"ALTER TABLE ?_forum_posts
		CHANGE `theme_id` `theme_id` int(11) NOT NULL AFTER `post_id`,
		CHANGE `msg` `msg` text COLLATE 'utf8_general_ci' NOT NULL DEFAULT '' AFTER `user_name`,
		CHANGE `putdate` `putdate` int(10) unsigned NOT NULL AFTER `email`,
		CHANGE `ip` `ip` varbinary(16) NOT NULL AFTER `putdate`,
		ADD `hidden` int(11)  NOT NULL DEFAULT '0' AFTER `ip`,
		CHANGE `edited_by` `edited` int(11)  NOT NULL DEFAULT '0' AFTER `hidden`;"
	);

	Db::i()->query(
		"INSERT INTO ?_forum_posts
		(`post_id`, `theme_id`, `user_id`, `user_name`, `msg`, `email`, `putdate`, `ip`,
		`hidden`, `edited`, `total_votes`, `vote_result`, `rating`, `first`)
		SELECT `post_id`, `theme_id`, `user_id`, `user_name`, `msg`, `email`,
		`putdate`, `ip`, `edited_by`, null,`total_votes`, `vote_result`, `rating`, `first`
		FROM ?_forum_hposts"
	);
	Db::i()->query(
		"INSERT INTO ?_forum_themes
		(`theme_id`, `forum_id`, `author_name`, `author_id`, `subject`, `total_posts`,
		`last_post_date`, `last_user_name`, `last_user_id`, `pinup`, `hidden`)
		SELECT `theme_id`, `forum_id`, `author`, `author_id`, `subject`, `total_posts`,
		`last_post`, `last_user`, `last_user_id`, `pinup`, `hidden_by`
		FROM ?_forum_hthemes;"
	);

	Db::i()->query(
		"DROP TABLE ?_forum_hposts, ?_forum_hthemes;"
	);

	Db::i()->query(
		"ALTER TABLE ?_forum_forums
		CHANGE `last_post` `last_post_date` int(10) unsigned NULL AFTER `total_themes`,
		CHANGE `last_user` `last_user_name` varchar(255) COLLATE 'utf8_general_ci' NULL AFTER `last_post_date`,
		CHANGE `name` `name` varchar(255) COLLATE 'utf8_general_ci' NOT NULL AFTER `forum_id`,
		CHANGE `describe` `describe` text COLLATE 'utf8_general_ci' NOT NULL DEFAULT '' AFTER `name`,
		CHANGE `last_user_id` `last_user_id` int(11) NULL AFTER `last_user_name`,
		CHANGE `closed` `closed` tinyint(1) NOT NULL DEFAULT '0' AFTER `last_user_id`;"
	);
}