<?php
	require_once( $GLOBALS["site_root"]."/includes/models/User.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/models/Job.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/models/Member.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/migrate/migrate.job_languages.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/migrate/migrate.job_servicetypes.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/migrate/migrate.job_custom_recipients.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/migrate/migrate.job_quotes.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/migrate/migrate.job_notifications.inc.php" );

	$this->setMigrateTitle( "Munkaajánlatok" );

	$query = sprintf( "select count(*) as num from fo_offer" );
	$row = FNTH_db_get_row( $query , $this->olddb );
	$numofrecs = $row["num"];

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

	// batch of jobs
	$query = sprintf( "select * from fo_offer order by id limit 100 offset %d" , $offset );
	$data = FNTH_db_get_resultset( $query , $this->olddb );
	$user = new UserModel();
	$job = new JobModel();

	foreach ($data as $row) {
		$p = array();
		$p["oldid"]=$row["id"];
		if (isset( $row["ctstamp"] ) && $row["ctstamp"]>="1970-01-01 00:00:00")
			$p["ctstamp"]=$row["ctstamp"];
		if (isset( $row["userid"] )) $p["poster_user_id"]=$user->getUserIdFromOldId( $row["userid"] );
		if (isset( $row["oldid"] ) && $row["oldid"] ) {
			$p["job_version"]=1.0;
		} else {
			$p["job_version"]=2.0;
		}
		$p["poster_name"]=$row["name"];
		$p["poster_email"]=$row["email"];
		$p["poster_phone"]=$row["phone"];
		$p["poster_cellular"]=$row["cellular"];
		$p["poster_show_phone"]=$row["show_phone"];
		$p["job_title"]=$row["title"];
		$p["job_size"]=$row["sizeofoffer"];
		if (isset( $row["deadline"] ) && $row["deadline"]!="0000-00-00")
			$p["job_quote_deadline"]=$row["deadline"];

		if (isset( $p["job_quote_deadline"] ))
			$p["job_deadline"]=$p["job_quote_deadline"];
		$p["job_description"]=$row["description"];
		$p["job_mainaspects"]=$row["mainaspects"];
		$p["job_recipients"]=array();
		switch ($row["offertype"]) {
		case "translators":$p["job_recipients"]["type"]="freelancers";break;
		case "offices":$p["job_recipients"]["type"]="agencies";break;
		case "all":
		case "custom":
			$p["job_recipients"]["type"]=$row["offertype"];
			break;
		default:
			$p["job_recipients"]["type"]="all";
			break;
		}
		$p["poster_acknowledged"]=$row["acknowledged"];
		$p["admin_acknowledged"]=$row["admin_acknowledged"];
		$p["is_revoked"]=$row["revoked"];
		if (isset( $row["rtstamp"] ) && $row["rtstamp"]>="1970-01-01 00:00:00") $p["revoked_at"]=$row["rtstamp"];
		$p["is_sent"]=$row["sent"];
		if (isset( $row["ststamp"] ) && $row["rtstamp"]>="1970-01-01 00:00:00") $p["sent_at"]=$row["ststamp"];
		if (isset( $row["ipaddress"] ) && strlen( trim( $row["ipaddress"] ))) $p["ipaddress"]=$row["ipaddress"];
		if (isset( $row["agent"] ) && strlen( trim( $row["agent"] ))) $p["useragent"]=$row["agent"];
		$p["hash"]=$row["hash"];
		$p["send_status"]=$row["send_status"];
		$p["source"]=$row["source"];


		$p = fo3_migrate_job_languages( $this->olddb , $p , $row["id"] );

		$job_id = $job->addJob( $p );
		// fo3_migrate_job_languages( $this->olddb , $job_id , $row["id"] );
		fo3_migrate_job_servicetypes( $this->olddb , $job_id , $row["id"] );
		fo3_migrate_job_custom_recipients( $this->olddb , $job_id , $row["id"] );
		fo3_migrate_job_quotes( $this->olddb , $job_id , $row["id"] );
		fo3_migrate_job_notifications( $this->olddb , $job_id , $row["id"] );
		$offset++;
	}
	$this->setProgressValue( $offset );
	if ($offset<$numofrecs) {
		// advance
		$u = array();
		$u["migrate"]="\"jobs\"";
		$u["offset"]=$offset;
		$this->doMigrateProcedure( $u );
	} else {
		$u = array();
		$u["migrate"]="\"mail_templates\"";
		exit();
		$this->doMigrateProcedure( $u );
	}
?>
