<?php

	require_once( $GLOBALS["site_root"]."/includes/models/Articles.inc.php" );

	$this->setMigrateTitle( "Cikkek" );


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

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

	$this->setProgressValue( $offset );

	$query = sprintf( "select * from cikk order by id limit 10 offset %d" , $offset );
	$data = FNTH_db_get_resultset( $query , $this->olddb );
	$article = new ArticlesModel();
		
	function fo3_migrate_article_preparebody( $body ) {
		// boxing
		$pos = strpos( $body , "[box:" );
		while ($pos!==false) {
			$pos2 = strpos( $body , "]" , $pos );
			$realstuff = substr( $body , $pos + 1 , $pos2 - $pos - 1 );
			$exp = explode( ":" , $realstuff );
			switch ($exp[1]) {
			case "video":
				$width = 460;
				$height = 345;
				$filename = $exp[2];
				if (count( $exp )>3) {
					$width = $exp[3];
					$height = $exp[4];
				}
				$newstuff = sprintf( "<span class=\"rmpvideo\">[{video width=%d height=%d}]%s[{/video}]</span>",
					$width , $height , $filename );
				$body = str_replace( "[".$realstuff."]" , $newstuff , $body );
				break;
			default:
				FNTH_dacl( htmlspecialchars( $realstuff ) );
				break;
			}
			$pos = strpos( $body , "[box:" );
		}
		return $body;
	}

	foreach ($data as $row) {
		$p = array();
		$query = sprintf( "select * from lcikk where cikkid=%d" , $row["id"] );
		$lcikk = FNTH_db_get_row( $query , $this->olddb );
		if (!$lcikk) {
			$offset++;
			$this->setProgressValue( $offset );
			continue;
		}

		switch ($row["rovat"]) {
		case 0:$article_category_id=4;break;	// nemtudni
		case 1:$article_category_id=4;break;	// hirek
		case 2:$article_category_id=4;break;	// cikkek
		case 3:$article_category_id=5;break;	// honlap
		}
		$p["article_category_id"]=$article_category_id;
		$p["article_ctstamp"]=$lcikk["cdate"];
		if (isset( $lcikk["pdate"] )) $p["article_publication_tstamp"]=$lcikk["pdate"];
		$p["article_name"]=$row["name"];
		$p["is_active"]=true;
		$p["is_public"]=($lcikk["public"]>0);
		$p["is_deleted"]=false;
		$p["is_memberonly"]=($lcikk["is_activeonly"]>0);
		// $p["article_ordering_tstamp"]=
		// $p["article_leadpicture"]=

		$article_id = $article->addArticle( $p );

		// language article
		$p = array();
		$p["article_id"]=$article_id;
		$p["site_language_id"]=1;
		$p["larticle_title"]=$lcikk["title"];
		$p["larticle_lead"]=$lcikk["lead"];
		$p["larticle_body"]="";
		$url = (isset( $lcikk["url"] ) && strlen( trim( $lcikk["url"] )) ) ? trim( $lcikk["url"] ) : false;
		if ($url) $p["larticle_url"]=$url;
		$p["is_url_used"]=$url;
		$p["is_active"]=true;
		$p["is_public"]=($lcikk["public"]>0);
		$p["is_deleted"]=false;

		$query = sprintf( "select * from lpage where lcikkid=%d order by pageno" , $lcikk["id"] );
		$pages = FNTH_db_get_resultset( $query , $this->olddb );
		foreach ($pages as $i => $page) {
			if ($i) {
				$p["larticle_body"].=sprintf( "<div class=\"divpagebreak\">%s</div>%s",
					$page["pagetitle"] , $page["pagetext"] );
				continue;
			}
			$p["larticle_body"].=$page["pagetext"];
		}
		$p["larticle_body"]=fo3_migrate_article_preparebody( $p["larticle_body"] );
		$p["larticle_ctstamp"]=$lcikk["cdate"];
		if (isset( $lcikk["pdate"] )) $p["larticle_publication_tstamp"]=$lcikk["pdate"];
		
		$larticle_id = $article->addLanguageArticle( $article_id , $p );
		$offset++;
		$this->setProgressValue( $offset );
	}
	$this->setProgressValue( $offset );
	
	if ($offset<$numofrecs) {
		// advance
		$u = array();
		$u["migrate"]="\"articles\"";
		$u["offset"]=$offset;
		$this->doMigrateProcedure( $u );
	} else {
		/*
		// advance to next migrational step: orders
		$u = array();
		$u["migrate"]="\"jobs\"";
		// $this->doMigrateProcedure( $u );
		*/
	}

?>
