<?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;
	
	//если модуль форума установлен
	if (Db::i()->selectCell("SELECT module_id FROM ?_modules WHERE module='forum'")) {

		Db::i()->query("ALTER TABLE ?_forum_forums CHANGE `forum_name` `name` VARCHAR( 255 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '',
		CHANGE `forum_total_posts` `total_posts` INT( 11 ) NOT NULL DEFAULT '0',
		CHANGE `total_themes` `total_themes` INT( 11 ) NOT NULL DEFAULT '0',
		CHANGE `forum_last_post` `last_post` DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00',
		CHANGE `forum_last_user` `last_user` VARCHAR( 255 ) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ,
		CHANGE `forum_last_user_id` `last_user_id` INT( 11 ) NOT NULL,
		CHANGE `descr` `describe` TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL;");
		
		Db::i()->query("ALTER TABLE ?_forum_forums ADD `last_post_tmp` INT UNSIGNED NULL DEFAULT NULL AFTER `last_post`;");
		Db::i()->query("UPDATE ?_forum_forums SET last_post_tmp = UNIX_TIMESTAMP( last_post ) ;");
		Db::i()->query("ALTER TABLE ?_forum_forums DROP `last_post`;");
		Db::i()->query("ALTER TABLE ?_forum_forums CHANGE `last_post_tmp` `last_post` INT( 10 ) UNSIGNED NULL DEFAULT NULL ;");
		
		Db::i()->query("ALTER TABLE ?_forum_themes CHANGE `hidden` `hidden_by` INT( 11 ) NULL DEFAULT NULL; ");
		Db::i()->query("ALTER TABLE ?_forum_themes ADD `last_post_tmp` INT UNSIGNED NULL DEFAULT NULL AFTER `last_post` ;");
		Db::i()->query("UPDATE ?_forum_themes SET last_post_tmp = UNIX_TIMESTAMP( last_post ) ;");
		Db::i()->query("ALTER TABLE ?_forum_themes DROP `last_post`;");
		Db::i()->query("ALTER TABLE ?_forum_themes CHANGE `last_post_tmp` `last_post` INT( 10 ) UNSIGNED NULL DEFAULT NULL ;");
		
		Db::i()->query("ALTER TABLE ?_forum_hthemes CHANGE `hidden` `hidden_by` INT( 11 ) NULL DEFAULT NULL ;");
		Db::i()->query("ALTER TABLE ?_forum_hthemes ADD `last_post_tmp` INT UNSIGNED NULL DEFAULT NULL AFTER `last_post` ;");
		Db::i()->query("UPDATE ?_forum_hthemes SET last_post_tmp = UNIX_TIMESTAMP( last_post ) ;");
		Db::i()->query("ALTER TABLE ?_forum_hthemes DROP `last_post`;");
		Db::i()->query("ALTER TABLE ?_forum_hthemes CHANGE `last_post_tmp` `last_post` INT( 10 ) UNSIGNED NULL DEFAULT NULL ;");
		
		Db::i()->query("UPDATE ?_forum_posts p JOIN ?_users u ON edited=login SET edited=u.user_id;");
		Db::i()->query("ALTER TABLE ?_forum_posts CHANGE `edited` `edited_by` INT NULL DEFAULT NULL;");
		Db::i()->query("ALTER TABLE ?_forum_posts ADD `putdate_tmp` INT UNSIGNED NULL DEFAULT NULL AFTER `putdate` ;");
		Db::i()->query("UPDATE ?_forum_posts SET putdate_tmp = UNIX_TIMESTAMP( putdate ) ;");
		Db::i()->query("ALTER TABLE ?_forum_posts DROP `putdate`;");
		Db::i()->query("ALTER TABLE ?_forum_posts CHANGE `putdate_tmp` `putdate` INT( 10 ) UNSIGNED NULL DEFAULT NULL ;");
		
		
		Db::i()->query("UPDATE ?_forum_hposts p JOIN ?_users u ON edited=login SET edited=u.user_id;");
		Db::i()->query("ALTER TABLE ?_forum_hposts CHANGE `edited` `edited_by` INT NULL DEFAULT NULL ;");
		
		Db::i()->query("ALTER TABLE ?_forum_hposts ADD `putdate_tmp` INT UNSIGNED NULL DEFAULT NULL AFTER `putdate` ;");
		Db::i()->query("UPDATE ?_forum_hposts SET putdate_tmp = UNIX_TIMESTAMP( putdate ) ;");
		Db::i()->query("ALTER TABLE ?_forum_hposts DROP `putdate`;");
		Db::i()->query("ALTER TABLE ?_forum_hposts CHANGE `putdate_tmp` `putdate` INT( 10 ) UNSIGNED NULL DEFAULT NULL ;");
		
		Db::i()->query("UPDATE ?_forum_forums SET `last_user`=LEFT(last_user, 32);");
		Db::i()->query("UPDATE ?_forum_hposts SET `user_name`=LEFT(user_name, 32);");
		Db::i()->query("UPDATE ?_forum_posts SET `user_name`=LEFT(user_name, 32);");
		
		Db::i()->query("ALTER TABLE ?_forum_forums CHANGE `last_user` `last_user` VARCHAR( 32 ) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL;");
		Db::i()->query("ALTER TABLE ?_forum_hposts CHANGE `user_name` `user_name` VARCHAR( 32 ) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL;");
		Db::i()->query("ALTER TABLE ?_forum_posts CHANGE `user_name` `user_name` VARCHAR( 32 ) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL;");
		Db::i()->query("ALTER TABLE ?_forum_themes CHANGE `last_user` `last_user` VARCHAR( 32 ) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL;");
		
		Db::i()->query("UPDATE ?_forum_themes ft LEFT JOIN (SELECT theme_id, COUNT(post_id) as tp FROM ?_forum_posts GROUP BY theme_id) ct ON ft.theme_id=ct.theme_id SET total_posts=IF(ct.tp, ct.tp, 0);");
		Db::i()->query("UPDATE ?_forum_forums f LEFT JOIN (SELECT forum_id, COUNT(theme_id) as tt, SUM(total_posts) as tp FROM ?_forum_themes GROUP BY forum_id) cf ON f.forum_id=cf.forum_id SET total_posts=IF(cf.tp, cf.tp, 0), total_themes=IF(cf.tt, cf.tt, 0);");
		Db::i()->query("UPDATE ?_forum_themes ft, (SELECT * FROM (SELECT theme_id, IF(fp.user_id, login, user_name) as user_name, fp.user_id, putdate FROM ?_forum_posts fp LEFT JOIN ?_users u ON fp.user_id=u.user_id ORDER BY putdate DESC) fp GROUP BY theme_id) fc SET last_post=putdate, last_user=user_name, last_user_id=user_id WHERE ft.theme_id=fc.theme_id;");
		Db::i()->query("UPDATE ?_forum_forums ff, (SELECT * FROM (SELECT forum_id, last_user, last_user_id, last_post FROM ?_forum_themes ORDER BY last_post DESC) ft GROUP BY forum_id) fc SET ff.last_post=fc.last_post, ff.last_user=fc.last_user, ff.last_user_id=fc.last_user_id WHERE ff.forum_id=fc.forum_id;");
		
		Db::i()->query("ALTER TABLE ?_forum_posts ADD `first` TINYINT NOT NULL DEFAULT '0';");
		Db::i()->query("ALTER TABLE ?_forum_hposts ADD `first` TINYINT NOT NULL DEFAULT '0';");
		
		Db::i()->query("UPDATE ?_forum_posts SET first=1 WHERE post_id IN(SELECT post_id FROM (SELECT post_id, theme_id FROM ?_forum_posts ORDER BY putdate) fp GROUP BY theme_id);");
	}
	
	
