<?php
$mydirname = basename( dirname( dirname( __FILE__ ) ) ) ;

eval( ' function xoops_module_update_'.$mydirname.'( $module ) { return xpress_onupdate_base( $module , "'.$mydirname.'" ) ; } ' ) ;


if( ! function_exists( 'xpress_onupdate_base' ) ) :
function xpress_onupdate_base( $module , $mydirname )
{
	// transations on module update

	global $msgs ; // TODO :-D
	// for Cube 2.1
	if( defined( 'XOOPS_CUBE_LEGACY' ) ) {
		$root =& XCube_Root::getSingleton();
		$root->mDelegateManager->add( 'Legacy.Admin.Event.ModuleUpdate.' . ucfirst($mydirname) . '.Success', 'xpress_message_append_onupdate' ) ;
		$msgs = array() ;
	} else {
		if( ! is_array( $msgs ) ) $msgs = array() ;
	}

	$db =& Database::getInstance() ;
	$mid = $module->getVar('mid') ;

//XPress TABLE UPGRADE
	global $wpdb,$wp_rewrite, $wp_queries, $table_prefix, $wp_db_version, $wp_roles,$wp_query;
	define('WP_INSTALLING', true);
	$mydirpath = XOOPS_ROOT_PATH . '/modules/' . $mydirname;
	$path = $mydirpath . '/';
	if (file_exists($path . 'wp-load.php')) {
		require_once $path . 'wp-load.php';
	} else {
		require_once $path . 'wp-config.php';
	}
	require_once($mydirpath . '/wp-admin/upgrade-functions.php');

	if ( get_db_version($mydirname) != $wp_db_version ){
		if( function_exists( 'wp_upgrade' ) )	{	
			wp_upgrade();			
		} else {
			wp_cache_flush();
			make_db_current_silent();
			upgrade_all();
			wp_cache_flush();
		}				
			$msgs[] = 'Upgrade Wordpress Database Ver.' . get_db_version($mydirname) .' to' . $wp_db_version;
	}

	/* activate the tag plugin */
	$plugin_current = "xpressme/xpressme.php";
	update_option('active_plugins', array($plugin_current));
	include_once(dirname(__FILE__) . '/../wp-content/plugins/'.$plugin_current);
	do_action('activate_'.$plugin_current);
	$msgs[] = 'The xpressme plug-in was activated.';
	
	
//	update_option("blog_charset", wp_blog_charset());
	$xpress_version = $module->modinfo['version'];
	
	if (! enhanced_table_check($mydirname,'views')){
		$xp_prefix = $mydirname;
		if ($xp_prefix == 'wordpress'){
	 		$xp_prefix = 'wp';
		}
		
		$charset_collate = '';
		if ( version_compare(mysql_get_server_info(), '4.1.0', '>=') ) {
			if ( ! empty($wpdb->charset) )
			$charset_collate = "DEFAULT CHARACTER SET $wpdb->charset";
			if ( ! empty($wpdb->collate) )
			$charset_collate .= " COLLATE $wpdb->collate";
		}
		$views_table = XOOPS_DB_PREFIX . '_' . $xp_prefix .'_views' ;
		$views_queries ="CREATE TABLE $views_table (
  		post_id bigint(20) unsigned NOT NULL default '0',
  		post_views bigint(20) unsigned NOT NULL default '0',
  		KEY post_id (post_id)
		)$charset_collate;";

		dbDelta($views_queries);
		$msgs[] = "$views_table table of XPressME was made.";
	}
	
	if (! enhanced_table_check($mydirname,'d3forum_link')){
		$xp_prefix = $mydirname;
		if ($xp_prefix == 'wordpress'){
	 		$xp_prefix = 'wp';
		}
		
		$charset_collate = '';
		if ( version_compare(mysql_get_server_info(), '4.1.0', '>=') ) {
			if ( ! empty($wpdb->charset) )
			$charset_collate = "DEFAULT CHARACTER SET $wpdb->charset";
			if ( ! empty($wpdb->collate) )
			$charset_collate .= " COLLATE $wpdb->collate";
		}
	
		$d3forum_link = XOOPS_DB_PREFIX . '_' . $xp_prefix .'_d3forum_link' ;
		$queries ="CREATE TABLE $d3forum_link (
	  		comment_ID bigint(20) unsigned NOT NULL default '0',
	  		post_id int(10) unsigned NOT NULL default '0' ,
	  		wp_post_ID bigint(20) unsigned NOT NULL default '0',
	  		KEY post_id (post_id)
			)$charset_collate;";
		dbDelta($queries);
		$msgs[] = "$d3forum_link table of XPressME was made.";
	}

	if (! enhanced_table_check($mydirname,'group_role')){
		$xp_prefix = $mydirname;
		if ($xp_prefix == 'wordpress'){
	 		$xp_prefix = 'wp';
		}
		
		$charset_collate = '';
		if ( version_compare(mysql_get_server_info(), '4.1.0', '>=') ) {
			if ( ! empty($wpdb->charset) )
			$charset_collate = "DEFAULT CHARACTER SET $wpdb->charset";
			if ( ! empty($wpdb->collate) )
			$charset_collate .= " COLLATE $wpdb->collate";
		}
	
		$group_role = XOOPS_DB_PREFIX . '_' . $xp_prefix .'_group_role' ;
		$views_queries ="CREATE TABLE $group_role (
	  		groupid smallint(5) unsigned NOT NULL default '0',
	  		name varchar(50)  NOT NULL default '' ,
	  		description text  NOT NULL default '',
	  		group_type varchar(10)  NOT NULL default '' ,
			role varchar(20)  NOT NULL default '' ,
			login_all smallint(5) unsigned NOT NULL default '0' ,
	  		KEY groupid (groupid)
			)$charset_collate;";
		dbDelta($views_queries);
		$sql = "INSERT INTO $group_role (groupid, role) VALUES (1, 'administrator')";
		$wpdb->query($sql);
		$msgs[] = "$group_role table of XPressME was made.";
	}
	
	// make templates
	include_once XOOPS_ROOT_PATH . '/modules/' . $mydirname . '/include/xpress_templates_make.php' ;
	$t_mess = xpress_templates_make($mid,$mydirname);
	$msgs = array_merge($msgs,$t_mess);
	
	return true ;
}
endif;

if( ! function_exists( 'xpress_message_append_onupdate' ) ) :
function xpress_message_append_onupdate( &$module_obj , &$log )
{
	if( is_array( @$GLOBALS['msgs'] ) ) {
		foreach( $GLOBALS['msgs'] as $message ) {
			$log->add( strip_tags( $message ) ) ;
		}
	}

	// use mLog->addWarning() or mLog->addError() if necessary
}
endif;

if( ! function_exists( 'get_db_version' ) ) :
function get_db_version($mydirname){
		global $xoopsModule;
		$wp_prefix = $mydirname;
		if ($wp_prefix == 'wordpress'){
			$wp_prefix = 'wp';
		}
		$xoopsDB =& Database::getInstance();
		$db_xpress_option = $xoopsDB->prefix($wp_prefix . '_options');

		$sql = "SELECT option_value FROM $db_xpress_option WHERE option_name = 'db_version'";
		$res = $xoopsDB->query($sql, 0, 0);
		if ($res === false){
			return false;
		} else {
			$row = $xoopsDB->fetchArray($res);
			return $row['option_value'];
		}
}
endif;


if( ! function_exists( 'enhanced_table_check' ) ) :
function enhanced_table_check($mydirname,$table_name){
		global $xoopsModule;
		
		$xoopsDB =& Database::getInstance();
		if ($mydirname == 'wordpress'){
			$xpress_prefix=  $xoopsDB->prefix('wp_');
		} else {
			$xpress_prefix=  $xoopsDB->prefix($mydirname . '_');
		}
		$db_enhanced = $xpress_prefix . $table_name;

		$sql = "show tables like '$db_enhanced'";
		$res = $xoopsDB->query($sql, 0, 0);
		if ($res === false){
			return false;
		} else {
			if ($xoopsDB->getRowsNum($res)  > 0)
				return true;
			else
				return false;
		}
}
endif;

?>