<?php
	require_once( $GLOBALS["site_root"]."/includes/models/Forum.inc.php" );
	
	function fo3_get_topic_id_from_oldid( $oldid ) {
		return FNTH_db_get_field( sprintf( "select forum_topic_id from forum_topics where oldid=%d" , $oldid ),
			"forum_topic_id" );
	}

	function fo3_get_user_id_from_oldid( $oldid ) {
		$num = (int)FNTH_db_get_field( sprintf( "select user_id from members where oldid=%d", $oldid ) , "user_id" );
		if (!$num) return false;
		return $num;
	}

	function fo3_get_forum_post_id_from_oldid( $oldid ) {
		$num = (int)FNTH_db_get_field( sprintf( "select forum_post_id from forum_posts where oldid=%d", $oldid ) , "forum_post_id" );
		if (!$num) return false;
		return $num;
	}

	function fo3_migrate_forum_topics( $olddb ) {
		$query = sprintf( "select count(*) as num from forum_topics" );
		$num = FNTH_db_get_field( $query , "num" );
		if ($num) return;
		// not any topics found
		$fm = new ForumModel();
		$query = sprintf( "select * from fo_forumthemes order by id" );
		$ds = FNTH_db_get_resultset( $query , $olddb );
		$i=1;
		foreach ($ds as $row) {
			$d = array();
			$d["ctstamp"]="2000-01-01 00:00:00";
			$d["forum_topic_title"]=$row["title"];
			$d["forum_topic_level"]=1;
			$d["forum_topic_ordering"]=$i;
			$d["is_public"]=$row["is_public"];
			$d["is_active"]=$row["is_active"];
			$d["is_admin_acknowledged"]=true;
			$d["oldid"]=$row["id"];
			$i++;
			$fm->addTopic( $d );
		}

		// add the topic
		$d = array();
		$d["ctstamp"]=fo3_date("Y-m-d H:i:s");
		$d["forum_topic_title"]="Fordítóknak";
		$d["forum_topic_level"]=1;
		$d["forum_topic_ordering"]=$i;
		$d["is_public"]=false;
		$d["is_active"]=true;
		$d["is_admin_acknowledged"]=true;
		$fm->addTopic( $d );
	}
	
	$this->setMigrateTitle( "Forum" );
	$fm = new ForumModel();

	$query = sprintf( "select count(*) as num from fo_forumentries" );
	$numofrecs = FNTH_db_get_field( $query , "num" , $this->olddb );

	$offset = (isset( $this->aP["offset"] )) ? $this->aP["offset"] : 0;
	if (!$offset) {
		fo3_migrate_forum_topics( $this->olddb );
		$this->setProgressmax( $numofrecs );
	}

	$this->setprogressValue( $offset );

	$query = sprintf( "select * from fo_forumentries order by id limit 100 offset %d" , $offset );
	$ds = FNTH_db_get_resultset( $query , $this->olddb );
	foreach ($ds as $row) {
		$ins = array();
		if (isset( $row["parent_entry_id"] ) && $row["parent_entry_id"])
			$ins["forum_post_parentid"]=fo3_get_forum_post_id_from_oldid( $row["parent_entry_id"] );
		$ins["forum_topic_id"]=fo3_get_topic_id_from_oldid( $row["forum_theme_id"] );
		$ins["owner_user_id"]=(isset( $row["user_id"] )) ? fo3_get_user_id_from_oldid( $row["user_id"] ) : "null";
		$ins["ctstamp"]=$row["ctstamp"];
		$ins["poster_name"]=$row["name"];
		$ins["poster_email"]=$row["email"];
		$ins["post"]=$row["entry"];
		$ins["ipaddress"]=$row["ipaddress"];
		$ins["hostname"]=gethostbyaddr( $row["ipaddress"] );
		$ins["is_admin_acknowledged"]=true;
		$ins["is_admin_moderated"]=(isset( $row["moderated"] ) && $row["moderated"]=="Y");
		$ins["oldid"]=$row["id"];
		$fm->addTopicPost( $ins["forum_topic_id"] , $ins );
		$offset++;
	}
	
	$this->setprogressValue( $offset );
	if ($offset<$numofrecs) {
		// advance
		$u = array();
		$u["migrate"]="\"forum\"";
		$u["offset"]=$offset;
		$this->doMigrateProcedure( $u );
	} else {
		$u = array();
		$u["migrate"]="\"end\"";
		exit();
		$this->doMigrateProcedure( $u );
	}
?>
