<?php
session_start ();
include_once 'includes/inter.php';
if (! isConnected ()) {
	header ( 'Location:401.php' );
	goto end;
} else if (getAdminLevel () < 16) {
	header ( 'Location:403.php' );
	goto end;
} else {
	try {
		echo '[' . date ( 'H:i:s' ) . ']' . 'Start sorting bdd<br/>';
		/*-------------------- USERS --------------------*/
		$idAssos = array();
		echo '[' . date ( 'H:i:s' ) . ']' . 'Table users<br/>';
		$rep = $GLOBALS ['bdd']->query ( 'SELECT * FROM users ORDER BY ID' );
		$users = array ();
		while ( $user = $rep->fetch () ) {
			echo '----' . $user ['ID'] . ':"' . $user ['pseudo'] . '";"' . $user ['mdp'] . '";' . $user ['date_creation'] . ';' . $user ['administration'] . '<br/>';
			$users [] = $user;
		}
		$rep->closeCursor ();
		$GLOBALS ['bdd']->exec ( 'TRUNCATE TABLE users' );
		echo '[' . date ( 'H:i:s' ) . ']' . 'Table users effacée<br/>';
		$currentID = 1;
		foreach ( $users as $u ) {
			$req = $GLOBALS['bdd']->prepare('INSERT INTO users(pseudo,mdp,date_creation,administration) VALUES (?,?,?,?)');
			$req->execute(array($u ['pseudo'],$u ['mdp'], $u ['date_creation'], $u ['administration']));
			$idAssos[$u['ID']] = $currentID;
			$currentID +=1;
			echo '----' . $u ['ID'] . '->' . $idAssos[$u['ID']] . '<br/>';
		}
		echo '[' . date ( 'H:i:s' ) . ']' . 'Pseudos réinsérés triés par ID<br/>';
		
		/*-------------------- PROJETS --------------------*/
		
		$pidAssos = array();
		echo '[' . date ( 'H:i:s' ) . ']' . 'Table projets<br/>';
		$rep = $GLOBALS ['bdd']->query ( 'SELECT * FROM projets ORDER BY ID' );
		$projects = array ();
		while ( $project = $rep->fetch () ) {
			echo '----' . $project ['ID'] . ':"' . $project['name'] . '";"' . $project ['ownersID'] . '";' . $project ['permissions'] . '<br/>';
			$projects[] = $project;
		}
		$rep->closeCursor();
		$GLOBALS ['bdd']->exec ( 'TRUNCATE TABLE projets' );
		echo '[' . date ( 'H:i:s' ) . ']' . 'Table projets effacée<br/>';
		$currentID = 1;
		foreach ( $projects as $p ) {
			$ownersID = explode(';',$p['ownersID']);
			$ownersNewID = array();
			foreach($ownersID AS $o){
				if(isset($idAssos[$o])){
					$ownersNewID[] = $idAssos[$o];
				}
			}
			$newOwnersID = implode(';',$ownersNewID);
			if(count($ownersNewID)<=0){	
				echo '####################' . 'Le projet '.print_r($p,TRUE).' n\'a aucun poss&eacute;sseur<br/>';
			}else{
				echo '--------' . 'Owners :'.$p['ownersID'].' &agrave; '.$newOwnersID.'<br/>';
			}
			
			$req = $GLOBALS['bdd']->prepare('INSERT INTO projets(name,ownersID,permissions) VALUES (?,?,?)');
			$req->execute(array($p ['name'],$newOwnersID,$p['permissions']));
			$pidAssos[$p['ID']] = $currentID;
			$currentID +=1;
			echo '----' . $p ['ID'] . '->' . $pidAssos[$p['ID']] . '<br/>';
		}
		echo '[' . date ( 'H:i:s' ) . ']' . 'Projets réinsérés triés par ID<br/>';
		
		
		/*-------------------- VERSIONS --------------------*/
		
		$vidAssos=array();
		echo '[' . date ( 'H:i:s' ) . ']' . 'Table versions<br/>';
		$rep = $GLOBALS ['bdd']->query ( 'SELECT * FROM versions ORDER BY id' );
		$versions = array ();
		while ( $version = $rep->fetch () ) {
			echo '----' . $version ['id'] . ':' . $version['project_id'] . ';"' . $version ['version'] . '";"' . $version ['versionAbs'].'";'.$version['language'] . '<br/>';
			$versions[] = $version;
		}
		$rep->closeCursor();
		$GLOBALS ['bdd']->exec ( 'TRUNCATE TABLE versions' );
		echo '[' . date ( 'H:i:s' ) . ']' . 'Table versions effacée<br/>';
		$currentID = 1;
		foreach ( $versions as $v ) {
			
			$newProjectId=$pidAssos[$v['project_id']];
			
			$req = $GLOBALS['bdd']->prepare('INSERT INTO versions(project_id,version,versionAbs,language) VALUES (?,?,?,?)');
			$req->execute(array($pidAssos,$v ['version'],$v['versionAbs'],$v ['language']));
			$vidAssos[$v['id']] = $currentID;
			$currentID +=1;
			echo '----' . $v ['id'] . '->' . $vidAssos[$v['id']] . '<br/>';
		}
		echo '[' . date ( 'H:i:s' ) . ']' . 'Versions réinsérés triés par ID<br/>';
		
		
		
	
	} catch ( Exception $e ) {
		echo $GLOBALS['bdd']->error;
	}
}

end:
?>