<?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;
	
	//устанавливаем значение по умолчанию для обновленного модуля переключения скина
	config::save('module.lc_skin_switcher.admin_skin', 'admin');
	
	//TODO добавить запросы устраняющие значения '0000-00-00 00:00:00'  и сделать поля NOT NULL
	
	if (Db::i()->selectCell("SELECT module_id FROM ?_modules WHERE module='news'")) {//проверяем что модуль установлен
		Db::i()->query("ALTER TABLE ?_news ADD `news_date_tmp` INT UNSIGNED NOT NULL DEFAULT 0 AFTER `news_id`");
		Db::i()->query("UPDATE ?_news SET news_date_tmp=UNIX_TIMESTAMP(news_date)");
		Db::i()->query("ALTER TABLE ?_news DROP `news_date`");
		Db::i()->query("ALTER TABLE ?_news CHANGE `news_date_tmp` `news_date` INT( 10 ) UNSIGNED NOT NULL ");
	}
	
	Db::i()->query("ALTER TABLE ?_users ADD `last_visit_tmp` INT UNSIGNED NULL DEFAULT NULL AFTER `last_visit`");
	Db::i()->query("UPDATE ?_users SET last_visit_tmp=UNIX_TIMESTAMP(last_visit)");
	Db::i()->query("ALTER TABLE ?_users DROP `last_visit`");
	Db::i()->query("ALTER TABLE ?_users CHANGE `last_visit_tmp` `last_visit` INT( 10 ) UNSIGNED NULL ");
	
	Db::i()->query("ALTER TABLE ?_users ADD `register_date_tmp` INT UNSIGNED DEFAULT 0 AFTER `register_date`");
	Db::i()->query("UPDATE ?_users SET register_date_tmp=UNIX_TIMESTAMP(register_date)");
	Db::i()->query("ALTER TABLE ?_users DROP `register_date`");
    //заглушка для очень старых инсталляций
	Db::i()->query("UPDATE ?_users SET register_date_tmp=0 WHERE register_date_tmp IS NULL");
	Db::i()->query("ALTER TABLE ?_users CHANGE `register_date_tmp` `register_date` INT( 10 ) UNSIGNED NOT NULL ");

    Db::i()->query("ALTER TABLE ?_tree CHANGE `lang_id` `lang_id` INT( 11 ) NOT NULL DEFAULT '0'");
    
	Db::i()->query("ALTER TABLE ?_comments ADD `putdate_tmp` INT UNSIGNED NOT NULL DEFAULT 0 AFTER `putdate`");
	Db::i()->query("UPDATE ?_comments SET putdate_tmp=UNIX_TIMESTAMP(putdate)");
	Db::i()->query("ALTER TABLE ?_comments DROP `putdate`");
	Db::i()->query("ALTER TABLE ?_comments CHANGE `putdate_tmp` `putdate` INT( 10 ) UNSIGNED NOT NULL ");
	
	if (Db::i()->selectCell("SELECT module_id FROM ?_modules WHERE module='bug_tracker'")) {//проверяем что модуль установлен
		//bug_date, putdate
		Db::i()->query("ALTER TABLE ?_bt_bugs ADD `last_update` INT UNSIGNED NOT NULL DEFAULT 0 AFTER `bug_date`");
		Db::i()->query("UPDATE ?_bt_bugs SET last_update=UNIX_TIMESTAMP(bug_date)");
		Db::i()->query("ALTER TABLE ?_bt_bugs DROP `bug_date`");

		Db::i()->query("ALTER TABLE ?_bt_posts ADD `putdate_tmp` INT UNSIGNED NOT NULL DEFAULT 0 AFTER `putdate`");
		Db::i()->query("UPDATE ?_bt_posts SET putdate_tmp=UNIX_TIMESTAMP(putdate)");
		Db::i()->query("ALTER TABLE ?_bt_posts DROP `putdate`");
		Db::i()->query("ALTER TABLE ?_bt_posts CHANGE `putdate_tmp` `putdate` INT( 10 ) UNSIGNED NOT NULL ");
	}
	
	if (Db::i()->selectCell("SELECT module_id FROM ?_modules WHERE module='admin_pages'")) {//проверяем что модуль установлен
		Db::i()->query("ALTER TABLE ?_pages ADD `date_created_tmp` INT UNSIGNED NOT NULL DEFAULT 0 AFTER `date_created`");
		Db::i()->query("UPDATE ?_pages SET date_created_tmp=UNIX_TIMESTAMP(date_created)");
		Db::i()->query("ALTER TABLE ?_pages DROP `date_created`");
		Db::i()->query("ALTER TABLE ?_pages CHANGE `date_created_tmp` `date_created` INT( 10 ) UNSIGNED NOT NULL ");
	}
	
	$module_id=Db::i()->query("INSERT INTO ?_modules (module, name, component, `static`, `disable`, runlevel) VALUES('lc_comments', 'Комментарии', 1, 0, 0, 1000)");
	Db::i()->query("UPDATE ?_rating_votes SET module_id=? WHERE `key`='comment'", $module_id);
	
	if ($data=Db::i()->selectRow("SELECT tree_id, parent_id FROM ?_tree WHERE path='/'")) {
		if ($data['parent_id']) $root_id=$data['parent_id'];
		else $root_id=$data['tree_id'];
		$tree_id=Db::i()->query("INSERT INTO ?_tree (parent_id, tree_name, menu, sitemap, path, pos, `show`, symlink) VALUES(?, 'Комментарии', 0, 0, '/comments', 500, 1, 0)", $root_id);
		Db::i()->query("INSERT INTO ?_includes (tree_id, module_id) VALUES(?, ?)", $tree_id, $module_id);
		Db::i()->query("ALTER TABLE ?_comments_items ADD `title` VARCHAR( 150 ) NOT NULL AFTER `url` ");
		Db::i()->query("ALTER TABLE ?_comments_items CHANGE `url` `url` VARCHAR( 150 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL");
		Db::i()->query("ALTER TABLE ?_comments_items CHANGE `item_key` `key` INT( 11 ) NOT NULL ");
	}
	
	Db::i()->query("ALTER TABLE ?_rating_votes DROP `key`");
	Db::i()->query("ALTER TABLE  ?_comments_items ADD  `type` TINYINT NOT NULL DEFAULT  '1' AFTER  `key`");

	//разделяем данные комментарием фотоальбома, если он установлен
	if ($module_id=Db::i()->selectCol("SELECT module_id FROM ?_modules WHERE module='lc_album'")) {
		Db::i()->query("UPDATE ?_comments_items SET `type`=2 WHERE `key`<10000 AND module_id=?", $module_id);
		Db::i()->query("UPDATE `l_comments_items` SET `key`=`key`-10000 WHERE `key`>10000 AND module_id=?", $module_id);
	}