<?php

function xoops_module_update_tadnews(&$module, $old_version) {
    GLOBAL $xoopsDB;

		if(!chk_chk9()) go_update9();
		if(!chk_chk10()) go_update10();

		$old_fckeditor=XOOPS_ROOT_PATH."/modules/tadnews/fckeditor";
		if(is_dir($old_fckeditor)){
			delete_directory($old_fckeditor);
			delete_directory(XOOPS_ROOT_PATH."/modules/tadnews/dhtmlgoodies_calendar");
			
		}
    return true;
}

//新增置頂日期欄位
function chk_chk9(){
	global $xoopsDB;
	$sql="select count(`always_top_date`) from ".$xoopsDB->prefix("tad_news");
	$result=$xoopsDB->query($sql);
	if(empty($result)) return false;
	return true;
}

function go_update9(){
	global $xoopsDB;
	$sql="ALTER TABLE ".$xoopsDB->prefix("tad_news")." ADD `always_top_date` DATETIME NOT NULL";
	$xoopsDB->queryF($sql) or redirect_header(XOOPS_URL."/modules/system/admin.php?fct=modulesadmin",30,  mysql_error());
}


//建立搬移檔案新表格
function chk_chk10(){
	global $xoopsDB;
	$sql="SHOW FIELDS FROM ".$xoopsDB->prefix("tadnews_files_center");
	$result=$xoopsDB->query($sql);
	if(empty($result)) return false;
	return true;
}

function go_update10(){
	global $xoopsDB;
	mk_dir(XOOPS_ROOT_PATH."/uploads/tadnews");
	mk_dir(XOOPS_ROOT_PATH."/uploads/tadnews/cate");
	mk_dir(XOOPS_ROOT_PATH."/uploads/tadnews/file");
	mk_dir(XOOPS_ROOT_PATH."/uploads/tadnews/image");
	mk_dir(XOOPS_ROOT_PATH."/uploads/tadnews/image/.thumbs");

	//建立電子報佈景
	mk_dir(XOOPS_ROOT_PATH."/uploads/tadnews/themes");
	if(!is_dir(XOOPS_ROOT_PATH."/uploads/tadnews/themes/bluefreedom2")){
		full_copy(XOOPS_ROOT_PATH."/modules/tadnews/images/bluefreedom2", XOOPS_ROOT_PATH."/uploads/tadnews/themes/bluefreedom2");
	}
	
	//表格改名
	$sql="RENAME TABLE ".$xoopsDB->prefix("tad_news_files")."  TO ".$xoopsDB->prefix("tadnews_files_center");

	$xoopsDB->queryF($sql) or redirect_header(XOOPS_URL."/modules/system/admin.php?fct=modulesadmin",30,  mysql_error());

	//修改表格
	$sql="ALTER TABLE `".$xoopsDB->prefix("tadnews_files_center")."`
	CHANGE `fsn` `files_sn` SMALLINT( 5 ) UNSIGNED NOT NULL AUTO_INCREMENT,
	CHANGE `nsn` `col_sn` SMALLINT( 5 ) UNSIGNED NOT NULL,
	ADD `col_name` VARCHAR( 255 ) NOT NULL AFTER `files_sn` ,
	ADD `sort` SMALLINT UNSIGNED NOT NULL AFTER `col_sn` ,
	ADD `kind` ENUM( 'img', 'file' ) NOT NULL AFTER `sort`,
	ADD `description` TEXT NOT NULL AFTER `file_type`";
	$xoopsDB->queryF($sql) or redirect_header(XOOPS_URL."/modules/system/admin.php?fct=modulesadmin",30,  mysql_error());
	
	//套入描述以及欄位名稱
	$sql="update ".$xoopsDB->prefix("tadnews_files_center")." set `col_name`='nsn', `description`=`file_name`";

	$xoopsDB->queryF($sql) or redirect_header(XOOPS_URL."/modules/system/admin.php?fct=modulesadmin",30,  mysql_error());


	$sql="select files_sn,file_name,file_type,description,col_name,col_sn from ".$xoopsDB->prefix("tadnews_files_center")."";
	$result=$xoopsDB->queryF($sql) or redirect_header(XOOPS_URL."/modules/system/admin.php?fct=modulesadmin",30,  mysql_error());
	while(list($files_sn,$file_name,$file_type,$description,$col_name,$col_sn)=$xoopsDB->fetchRow($result)){
	  $kind=(substr($file_type,0,5)=="image")?"img":"file";
	  $new_file_name="{$col_name}_{$col_sn}_{$files_sn}".substr($description,-4);
	  if($kind=="file"){
			rename_win(XOOPS_ROOT_PATH."/uploads/tadnews/file/{$col_sn}_{$description}",XOOPS_ROOT_PATH."/uploads/tadnews/file/$new_file_name");
		}else{
			rename_win(XOOPS_ROOT_PATH."/uploads/tadnews/file/{$col_sn}_{$description}",XOOPS_ROOT_PATH."/uploads/tadnews/image/$new_file_name");
		}

		//更新檔名
		$sql1="update ".$xoopsDB->prefix("tadnews_files_center")." set `file_name`='{$new_file_name}',kind='{$kind}' where files_sn='{$files_sn}'";

		$xoopsDB->queryF($sql1);
	}


}


//建立目錄
function mk_dir($dir=""){
    //若無目錄名稱秀出警告訊息
    if(empty($dir))return;
    //若目錄不存在的話建立目錄
    if (!is_dir($dir)) {
        umask(000);
        //若建立失敗秀出警告訊息
        mkdir($dir, 0777);
    }
}

//拷貝目錄
function full_copy( $source="", $target=""){
	if ( is_dir( $source ) ){
		@mkdir( $target );
		$d = dir( $source );
		while ( FALSE !== ( $entry = $d->read() ) ){
			if ( $entry == '.' || $entry == '..' ){
				continue;
			}

			$Entry = $source . '/' . $entry;
			if ( is_dir( $Entry ) )	{
				full_copy( $Entry, $target . '/' . $entry );
				continue;
			}
			copy( $Entry, $target . '/' . $entry );
		}
		$d->close();
	}else{
		copy( $source, $target );
	}
}


function rename_win($oldfile,$newfile) {
   if (!rename($oldfile,$newfile)) {
      if (copy ($oldfile,$newfile)) {
         unlink($oldfile);
         return TRUE;
      }
      return FALSE;
   }
   return TRUE;
}

function delete_directory($dirname) {
    if (is_dir($dirname))
        $dir_handle = opendir($dirname);
    if (!$dir_handle)
        return false;
    while($file = readdir($dir_handle)) {
        if ($file != "." && $file != "..") {
            if (!is_dir($dirname."/".$file))
                unlink($dirname."/".$file);
            else
                delete_directory($dirname.'/'.$file);
        }
    }
    closedir($dir_handle);
    rmdir($dirname);
    return true;
}

?>
