<?php
	require_once( $GLOBALS["site_root"]."/includes/pages/BackendPage.inc.php" );

	FNTH_class( "ProgressBar" );
	FNTH_class( "Button" );

	class MigratePage extends BackendPage {
		protected $progressbar = null;
		protected $startbutton = null;
		protected $olddb = null;

		public function connectOldDatabase() {
			$db = array();
			$db["dbtype"]="MySQL";
			$db["database"]="fordit_v2";
			$db["username"]="fordit_v2";
			$db["password"]="kakukk321";
			$this->olddb = FNTH_db_connect( $db );
			FNTH_db_free_result( FNTH_db_query( "set names utf8" , $this->olddb ) );
		}

		public function setMigrateTitle( $s ) {
			FNTH_direct_echo( sprintf( "$(\"#MigrateTitle\").html('%s');" , $s ) );
		}

		public function setProgressValue( $v ) {
			FNTH_direct_echo( sprintf( "FNTHProgressBarSetValue( '%s','%d' );",
				$this->progressbar->getId(),$v));		
		}

		public function setProgressMax( $v ) {
			FNTH_direct_echo( sprintf( "FNTHProgressBarSetMax( '%s','%d' );",
				$this->progressbar->getId(),$v));		
		}

		public function doMigrateProcedure( $p ) {
			$retval="\$.ajax(";
				$retval.="{";
					$retval.="type: \"POST\",";
					$retval.="cache: false,";
					$retval.="data: (";
						$retval.="{";
							$retval.="inajax:1";
							foreach ($p as $k=>$v)
								$retval.=",".$k.":".$v;
						$retval.="}";
					$retval.="),";
					$retval.="success: function(ret) { eval( ret ); }";
				$retval.="}";
			$retval.=");";
			FNTH_direct_echo( $retval );
			fo_exit();
		}

		public function handleEvents() {
			if (!isset( $this->aP["inajax"] )) {
				parent::handleEvents();
				return;
			}
			if (!isset( $this->aP["migrate"] )) {
				parent::handleEvents();
				return;
			}

			$this->connectOldDatabase();
			
			switch ($this->aP["migrate"]) {
			/*
			case "mail_templates":
				require_once( $GLOBALS["site_root"] . "/includes/migrate/migrate.mail_templates.inc.php" );
				break;
			case "articles":
				require_once( $GLOBALS["site_root"] . "/includes/migrate/migrate.articles.inc.php" );
				break;
			*/
			case "forum":
				require_once( $GLOBALS["site_root"] . "/includes/migrate/migrate.forum.inc.php" );
				break;
			}
			fo_exit();
		}

		public function __construct( $id = "MigratePage" , $parent = null ) {
			parent::__construct( $id , $parent );

			/*
			$this->connectOldDatabase();
			$query = sprintf( "select * from fo_user order by id" );
			$data = FNTH_db_get_resultset( $query , $this->olddb );
			echo nl2br( print_r( $data , true ));exit();
			*/

			$this->progressbar = new FNTHProgressBar( "progressbar" , $this );
			$this->progressbar->setWidth( 600 );

			$this->startbutton = new FNTHButton( "startbutton" , $this );
			$this->startbutton->setValue( "Start!" );
			$this->startbutton->setAttribute( "onclick" , "StartMigration();" );
		}
	}
?>
