<?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();
		}				
	}
	/* activate the tag plugin */
	$plugin_current = "xpressme/xpressme.php";
	update_option('active_plugins', array($plugin_current));
	include(dirname(__FILE__) . '/../wp-content/plugins/'.$plugin_current);
	do_action('activate_'.$plugin_current);
	
//	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);
	}
	
	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);
	}
	
	clean_template($mydirname,$xpress_version);

	// update templates
//	include_once XOOPS_ROOT_PATH . '/modules/' . $mydirname . '/include/xpress_templates.php' ;
//	$msgs = xpress_update_templates($mid,$mydirname);
	
	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( 'clean_template' ) ) :
function clean_template($mydirname,$xpress_ver){
		global $xoopsModule;
		
		$var = floatval($xpress_ver);
		switch ($var){
			case ($var >= 0.62):
				$temp_file = "'". $mydirname . "_home.html','" . $mydirname. "_index.html','" . $mydirname . "_search.html','" . $mydirname . "_single.html'";
				break;
			case ($var >= 0.60):
				$temp_file = "'xpress_header.html','xpress_home.html','xpress_index.html','xpress_search.html','xpress_sidebar.html'";
				break;
			default:
				$temp_file = 'all';
		}
		
		if ($temp_file != 'all'){			
			$xoopsDB =& Database::getInstance();
			$db_tplfile = $xoopsDB->prefix('tplfile');
			$db_tplsource = $xoopsDB->prefix('tplsource');

			$sql = "SELECT * FROM $db_tplfile WHERE tpl_module = '$mydirname' AND NOT tpl_file IN($temp_file)";
			$res = $xoopsDB->query($sql, 0, 0);
			if ($res === false){
				return false;
			} else {
				$del_array = '';
				$i=0;
				while($row = $xoopsDB->fetchArray($res)){
					if (!empty($del_array))
					$del_array .= ',';
					$del_array .= $row['tpl_id'];
				}
				if(!empty($del_array)){
					$del_tplfile  = "DELETE FROM $db_tplfile WHERE tpl_id IN ($del_array)";
					$result = $xoopsDB->query($del_tplfile, 0, 0);
					$del_tplsource  = "DELETE FROM $del_tplsource WHERE tpl_id IN ($del_array)";
					$result = $xoopsDB->query($del_tplfile, 0, 0);
				}
			}
		}
}
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;

?>