<?php
	require_once( $GLOBALS["site_root"] . "/includes/migrate/AdminMigrate.inc.php" );
	require_once( $GLOBALS["site_root"] . "/includes/models/Forum.inc.php" );
	require_once( $GLOBALS["site_root"] . "/includes/models/User.inc.php" );

	class AdminMigrateForum extends AdminMigrate {
		protected $fm;
		protected $um;


		protected function migrateTopics( ) {
			$query = sprintf( "select * from fo_forumthemes order by id" );
			$ds = FNTH_db_get_resultset( $query , $this->olddb );

			$i=0;
			foreach ($ds as $row) {
				if ($this->fm->isTopicExistsOnOldid( $row["id"] )) continue;
				$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++;
				$this->fm->addTopic( $d );
			}
		}

		protected function migrateForumPost( $row ) {
			$post_id = $this->fm->getForumPostIdFromOldid( $row["id"] );
			if ($post_id) return true;	// mar migraltuk ezt a bejegyzest

			$ins = array();
			if (isset( $row["parent_entry_id"] ) && $row["parent_entry_id"])
				$ins["forum_post_parentid"]=$this->fm->getForumPostIdFromOldid( $row["parent_entry_id"] );

			$ins["forum_topic_id"]=$this->fm->getTopicIdFromOldid( $row["forum_theme_id"] );

			$user_id = 0;
			if (isset( $row["user_id"] )) $user_id = $this->um->getUserIdFromOldId( $row["user_id"] );
			if ($user_id) $ins["owner_user_id"]=$user_id;

			$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"];
			$this->fm->addTopicPost( $ins["forum_topic_id"] , $ins );
			return true;
		}


		public function handleMigratorEvent() {
			$this->fm = new ForumModel();
			$this->um = new UserModel();

			if (!isset( $this->aM["offset"] )) {
				$this->aM["offset"] = 0;
				$this->migrateTopics();
				$this->aM["records"] = FNTH_db_get_field( "select count(*) as num from fo_forumentries" , "num" , $this->olddb );
				$this->initAdminMigrate( $this->aM["records"] );
			}

			$batchsize = 20;
			$query = sprintf( "select * from fo_forumentries order by id limit %d offset %d" ,
				$batchsize,
				$this->aM["offset"] );
			$ds = FNTH_db_get_resultset( $query , $this->olddb );
			$clen = count( $ds );
			if ($batchsize>$clen) $batchsize=$clen;
			$i = 0;
			while ($batchsize) {
				$row = $ds[$i];
				$this->setProgressBarCurrent( "pb1" , $this->aM["offset"] );
				if (!$this->migrateForumPost( $row )) return false;

				$this->aM["offset"]++;
				if ($this->aM["offset"]>=$this->aM["records"]) break;
				$batchsize--;
				$i++;
			}
			$this->setProgressBarCurrent( "pb1" , $this->aM["offset"] );
			if ($this->aM["offset"]<$this->aM["records"]) return true;
			$this->finishAdminMigrate();
			return false;
		}	



	}

?>
