<?php
// Author: Sting_Band
// URL: http://www.bluish.jp/

require( '../../mainfile.php' );
require( 'header.php' );

if (isset($_POST['action']) && $_POST['action'] == "upgrade"){
	$error = array();
	//カラム名の変更
	$sql = "ALTER TABLE `".$xoopsDB->prefix('bluesbb')."`
	 CHANGE `sread_id` `thread_id` int(6) unsigned NOT NULL default '1'";
	$result = $xoopsDB->query($sql);
	if (!$result) {
		$error[] = "Failed ALTER TABLE ".$xoopsDB->prefix('bluesbb')."";
	}
	$sql = "ALTER TABLE `".$xoopsDB->prefix('bluesbb_topic')."`
	 CHANGE `sread_per_page` `thread_per_page` tinyint(3) unsigned NOT NULL default '5',
	 CHANGE `res_per_sread` `res_per_thread` tinyint(3) unsigned NOT NULL default '10'";
	$result = $xoopsDB->query($sql);
	if (!$result) {
		$error[] = "Failed ALTER TABLE ".$xoopsDB->prefix('bluesbb_topic')."";
	}
	//カラムの追加
	$result = $xoopsDB->query("ALTER TABLE `".$xoopsDB->prefix('bluesbb')."` ADD `hig_id` smallint(3) UNSIGNED DEFAULT '0' NOT NULL AFTER `res_id`");
	if (!$result) {
		$error[] = "".$xoopsDB->prefix('bluesbb')."はアップデート済みです。";
	} else {
		$result = $xoopsDB->query("ALTER TABLE `".$xoopsDB->prefix('bluesbb')."` ADD INDEX topic_id( `topic_id` )");
		if (!$result) {
			$error[] = "Failed ALTER TABLE ".$xoopsDB->prefix('bluesbb')." topic_id";
		}
		$result = $xoopsDB->query("ALTER TABLE `".$xoopsDB->prefix('bluesbb')."` ADD INDEX thread_id( `thread_id` )");
		if (!$result) {
			$error[] = "Failed ALTER TABLE ".$xoopsDB->prefix('bluesbb')." thread_id";
		}
		$result = $xoopsDB->query("ALTER TABLE `".$xoopsDB->prefix('bluesbb')."` ADD INDEX uid( `uid` )");
		if (!$result) {
			$error[] = "Failed ALTER TABLE ".$xoopsDB->prefix('bluesbb')." uid";
		}
		//レス投稿にとりあえずデータ放り込み
		$sql = 'update '.$xoopsDB->prefix('bluesbb').' set hig_id = 1 where res_id != 0';
		$result = $xoopsDB->query($sql);
		if (!$result) {
			$error[] = "error hig_id　0 to 1";
		}
	}
	$result = $xoopsDB->query("ALTER TABLE `".$xoopsDB->prefix('bluesbb_topic')."` ADD `list_display` ENUM('0','1') DEFAULT '1' NOT NULL AFTER `allow_sig`");
	if (!$result) {
		$error[] = "".$xoopsDB->prefix('bluesbb_topic')."はアップデート済みです。";
	} else {
		$result = $xoopsDB->query("ALTER TABLE `".$xoopsDB->prefix('bluesbb_topic')."` ADD `tree_per_page` tinyint(3) UNSIGNED DEFAULT '5' NOT NULL AFTER `res_per_thread`");
		if (!$result) {
			$error[] = "Failed ALTER TABLE ".$xoopsDB->prefix('bluesbb_topic')." tree_per_page";
		}
		$result = $xoopsDB->query("ALTER TABLE `".$xoopsDB->prefix('bluesbb_topic')."` ADD `order_per_page` tinyint(3) UNSIGNED DEFAULT '20' NOT NULL AFTER `tree_per_page`");
		if (!$result) {
			$error[] = "Failed ALTER TABLE ".$xoopsDB->prefix('bluesbb_topic')." order_per_page";
		}
		$result = $xoopsDB->query("ALTER TABLE `".$xoopsDB->prefix('bluesbb_topic')."` ADD `topic_style` ENUM('1','2','3') DEFAULT '1' NOT NULL AFTER `res_limit`");
		if (!$result) {
			$error[] = "Failed ALTER TABLE ".$xoopsDB->prefix('bluesbb_topic')." topic_style";
		}
		$result = $xoopsDB->query("ALTER TABLE `".$xoopsDB->prefix('bluesbb_topic')."` ADD `style_choice` varchar(5) DEFAULT '1:2:3' NOT NULL AFTER `topic_style`");
		if (!$result) {
			$error[] = "Failed ALTER TABLE ".$xoopsDB->prefix('bluesbb_topic')." style_choice";
		}
		$result = $xoopsDB->query("ALTER TABLE `".$xoopsDB->prefix('bluesbb_topic')."` ADD `topic_order` smallint(3) DEFAULT '1' NOT NULL AFTER `style_choice`");
		if (!$result) {
			$error[] = "Failed ALTER TABLE ".$xoopsDB->prefix('bluesbb_topic')." topic_order";
		}
	}
}

include XOOPS_ROOT_PATH."/header.php";

if (isset($_POST['action']) && $_POST['action'] == "upgrade") {
	if (count($error)) {
		foreach( $error as $err ) {
			echo $err."<br />";
		}
	}
	echo "<br />BluesBB-1.xx用テーブルへのアップデートは完了しました。<br />";
	echo "<font color=\"#ff0000\" size=\"+1\">「update_0.xx_to_1.xx.php」は用済みです。サーバ上から削除してください。</font><br />";
} else {
	echo "<p>以下の「アップデート開始」をクリックすると、BluesBB-0.xx用のテーブルがBluesBB-1.xx用へとアップデートされます。</p>";
	echo "<form action='".BLUESBB_URL."/update_0.xx_to_1.xx.php' method='post'><input type='submit' name='button' id='button' value='アップデート開始' /><input type='hidden' value='upgrade' name='action' id='action' /></form>";
}

include XOOPS_ROOT_PATH."/footer.php";
?>