<?php

//TODO dodizajnovat info o spustani a mazani

error_reporting(E_ALL ^ E_NOTICE);

$oForm = new cForm();

$ret = "";

$ret .= ' <script type="text/javascript" src="'.$pluginURI.'js/nextStep.js"></script>';

/*
 * vyberie vsetky kluby a vypise ich
*/
$stmt = $this->oDb->query("SELECT * FROM `klub`");
$values[0]['name']="Vyber klub posobenia!";
$values[0]['value']="-1";
$i=1;
while ($row = $stmt->fetch()){
	$values[$i]['value']=$row['id'];
	$values[$i]['name']=$row['nazov'].'('.$row['sport'].')';
	$i++;
}
$values[$i]['name']="Nový klub";
$values[$i]['value']="0";


$ret .= '<h2 id="kroky">'."\n". 'Krok 1 z 1'.'</h2>'."\n";
$ret .= $oForm->selectBox("Klub", "klub", $values );

$ret .= '<label id="login2_label" for="login2">Login</label> <input id="login2" class="input_large" type="text" maxlength="64" value="" name="login">';

if (isset($_POST['loginname']) && !empty($_POST['loginname']))  {
	$pieces = explode(" ", $_POST['loginname']);
	$sMeno=htmlspecialchars($pieces[0]);
	$sPriezvisko=htmlspecialchars($pieces[1]);
}
else {
	$sMeno="";
	$sPriezvisko="";
}


$ret .= $oForm->inputBox("Meno", "meno", 64, isset($_POST['meno'])? htmlspecialchars($_POST['meno']): $sMeno);
$ret .= $oForm->inputBox("Priezvisko", "priezvisko", 32, isset($_POST['priezvisko'])? htmlspecialchars($_POST['priezvisko']): $sPriezvisko);


$ret .= $oForm->inputBox("Vek", "vek", 32, isset($_POST['vek'])? htmlspecialchars($_POST['vek']): "");
$ret .= $oForm->passwordBox("Password", "p1");
$ret .= $oForm->passwordBox("Password retype", "p2");
//$ret .= $this->oForm->inputBox("Prava", "prava", 32, $this->pp($this->row, "prava"));
//$ret .= $this->oForm->inputBox("Respekt", "respekt", 64, "");
$ret .= $oForm->inputBox("Email", "email", 96, isset($_POST['email'])? htmlspecialchars($_POST['email']): "");


$ret .= '<div class="submit">'."\n";
$ret .= $oForm->submitButton("", "Registruj", false);
$ret .= '</div>'."\n";


$form = "";
$form .= ' <form method="post" action="'.$this->oUrl->href().'" enctype="multipart/form-data"'.'> <div id="krok1">' ."\n";
$form .= $ret;
$form .= "\n".'</div>';

////////// FORMULAR PRE KLUB
$retKlub = "<div id=\"krok2\">";
$retKlub .= "<h2> Krok 2 z 2</h2>";
$retKlub .= "<h3> Registrácia tvojho klubu </h3>";
$retKlub .= $oForm->inputBox("Meno", "menoK", 64, isset($_POST['menoK'])? $_POST['menoK']: "");
$retKlub .= $oForm->inputBox("Špot", "sport", 64, isset($_POST['sport'])? $_POST['sport']: "");
////////// FORMULAR PRE KLUB


//potrebujem repostnut minule data preto ich vlozim do hidden fields
foreach ( $_POST as $a => $b ) {
	$retKlub .= $oForm->hiddenF($a, $b);
}

$retKlub .= '<div class="submit">'."\n";
$retKlub .= $oForm->submitButton("", "Vytvor nový klub a dokonci registraciu ", false);
$retKlub .= '</div>'."\n";

$retKlub .= "\n"."</div>";


$form.=$retKlub.'</form>';
$ok=false;

if ( intval($_POST['klub'])>0 && !empty($_POST['email']) && !empty($_POST['priezvisko']) && !empty($_POST['meno'])
		 && !empty($_POST['login']) && !empty($_POST['vek']) && !empty($_POST['p1']) 
		&& $_POST['p1']==$_POST['p2'] && kontrolaemail($_POST['email']) && je_cislo($_POST['vek'])  )
{
		//vloz hraca
		$insertId= vlozHraca($this->oDb, $this->oMenu, $this->oUrl ,$_POST['login'],$_POST['meno'], $_POST['priezvisko'],
	 $_POST['vek'], $_POST['klub'], $_POST['p1'], $_POST['email'] );	
		$ok=true;
		/*
		 * prava
		*/
		$this->oDb->query("INSERT INTO `group_user` (id_group,id_user) VALUES ('2','".$insertId."')");
		$this->oDb->query("INSERT INTO `group_user` (id_group,id_user) VALUES ('1','".$insertId."')");
		
		echo '<h2> registracia uspesna! mozete sa prihlasit! </h2>';
		
		include_once  $this->path['core_tmpl'] . "_login.tmpl.php";
	}

if ( !empty($_POST['sport']) && !empty($_POST['menoK']) ) {
		//pridam novy klub a user_id dam == 0 
		$ok=true;
		$sql='INSERT INTO `klub` (
					`id` ,
					`nazov` ,
					`admin_id` ,
					`sport` ,
					`reg_time`
					)
					VALUES ( NULL , ?, ?, ?, ?);';
		$this->oDb->queryWithBids($sql, array( $_POST['menoK'],0,$_POST['sport'],date('Y-m-d H:i:s')));
		
		$idKlubu= $this->oDb->insertID();
		//pri$q->dam hraca a ulozim si jeho id
		$_POST['id']=vlozHraca($this->oDb, $this->oMenu, $this->oUrl , $_POST['login'],$_POST['meno'], $_POST['priezvisko'],
				$_POST['vek'], $idKlubu, $_POST['p1'], $_POST['email'] );
		
		/*
		 * tento clovek sa automaticky stava veducim klubu a potom si uz vie pridat vsetky ostatne funkcie
		 */
		$id = $this->oDb->obtain("SELECT id_group as id FROM `group` WHERE title = 'Veduci Klubu'");
		$this->oDb->query("INSERT INTO `group_user` (id_group,id_user) VALUES ('".$id['id']."','".$_POST['id']."')");		
		
		$sql="UPDATE `klub` SET `admin_id` = ? WHERE `klub`.`id` = ?;";
		$this->oDb->queryWithBids($sql, array($_POST['id'], $idKlubu ) );
		
		
		echo 'registracia klubu a hraca uspesna!';
	}
	
//este nic nepisal
if (!$ok)
	echo $form;

function vlozHraca( $db, $menu ,  $url ,$login, $meno, $priezvisko, $vek, $klub, $pass,$email ) {
	$sql ="INSERT INTO `users` (`login`, `meno`, `priezvisko`, `vek`, `klub_id`, `salt`, `heslo`, `reg_time`, `last_login`, `email`, `respekt`, `prava`, `foto`)
				VALUES (?, ?, ?, ?, ?, ?, ?, ?, NULL, ?, 0.1 , ?, NULL);";
	$salt=uniqid();
	//respektu na zaciatku 1 nech sa to potom meni s update

	$q= $db->queryWithBids($sql, array( $login,$meno,$priezvisko,$vek,$klub,
			$salt, MD5($salt.$pass), date('Y-m-d H:i:s'),$email, 1 ));
	
	$insertID= $db->insertID();
	
	//pridat kontrulu ci to dobre zbehlo
	return $insertID;
}



/*
 * regex
 */

function kontrolaemail($text) {
	return preg_match("	/^(([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5}){1,25})+([;.](([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5}){1,25})+)*$/", $text );
}

function je_cislo($text) {
	return preg_match("/^[0-9]{1,}$/", $text );
}
?>
<script>

$(function() {
	kontrolaMeno('#meno');
	kontrolaMeno('#login');
	kontrolaMeno('#priezvisko');
	kontrolaCislo('#vek');
	kontrolaPass('#p1');
	kontrolaPass('#p2');
	kontrolaPass('email');
});

</script>


