<?php	
final class Reg
	{
	
	private $sql;
	var $cleaner = '<div style="clear: both;"></div>';
    var $form_action;
	
	public function __construct() {
		global $sql;
		$this->sql = &$sql;
        $this->form_action = str_replace('&','&amp;',$_SERVER['REQUEST_URI']);
	}

	public function return_heading_text($text) {
		echo '<h3>' . $text . '</h3>';
	}

	/*** REGISTRATION: START ***/

	public function reg_check_enque($nick) {
		$this->sql->sql_connect('login');
			$sql_check = "SELECT nick FROM users WHERE LOWER(nick)='" . strtolower($nick) . "'";
			$result_check = mysql_query($sql_check);
			if ( mysql_num_rows($result_check) > 0 ) {
				return false;
			} else {
				return true;
			}
		$this->sql->sql_free_result($result_check);
		$this->sql->sql_close();
	}
	
	public function return_reg_form() {
		if ( $this->reg_check_enque(trim(strip_tags($_POST['nick']))) ) {$enque = '';}
		else {$enque = '<h3 style="color: red;">Použite iný nick. "' . $_POST['nick'] . '" je už použitý.</h3>';}
		if	(
            isset($_POST['register']) && ($_POST['odbor'] != '---') &&
            ($_SESSION['security_code'] == $_POST['security_code']) &&
			isset($_POST['nick']) && (trim(strip_tags($_POST['nick']))!='') &&
			isset($_POST['pass']) && (trim(strip_tags($_POST['pass']))!='') &&
			isset($_POST['repass']) && (trim(strip_tags($_POST['repass']))!='') &&
			$_POST['pass'] == $_POST['repass'] &&
			isset($_POST['first_name']) && (trim(strip_tags($_POST['first_name']))!='') &&
			isset($_POST['last_name']) && (trim(strip_tags($_POST['last_name']))!='') &&
			isset($_POST['mail']) && (trim(strip_tags($_POST['mail']))!='') && empty($enque)
			) {
            $this->execute_reg( strip_tags($_POST['nick']), strip_tags($_POST['pass']), strip_tags($_POST['first_name']), strip_tags($_POST['last_name']), strip_tags($_POST['mail']) );
		} else {
            if ( isset($_POST['register']) ) {
                $this->return_heading_text('<span style="color: red;">Niektoré polia neboli vyplnené správne.</span>');
            }
            if ( isset($_POST['register']) && ($_SESSION['security_code'] != $_POST['security_code']) ) {
                $this->return_heading_text('<span style="color: red;">Zlý bezpečnostný kód.</span>');
            }
            if ( isset($_POST['odbor']) && ($_POST['odbor'] = '---') ) {
                $this->return_heading_text('<span style="color: red;">Musíte mať vyplnený odbor.</span>');
            }
			$this->return_heading_text('Zaregistrujte sa prosím:');
			echo $enque;
            echo '
                <form class="registration" action="' . $this->form_action. '" method="post">
                    <div style="float: left; width: 80px; margin: 3px;">nick:</div>
                    <div style="margin-bottom: 5px;"><input type="text" name="nick" value="' . $_POST['nick'] . '" /></div>' . $this->cleaner . '
                    <div style="float: left; width: 80px; margin: 3px;">heslo:</div>
                    <div style="margin-bottom: 5px;"><input type="password" name="pass" value="' . $_POST['pass'] . '" /></div>' . $this->cleaner . '
                    <div style="float: left; width: 80px; margin: 3px;">overenie hesla:</div>
                    <div style="margin-bottom: 5px;"><input type="password" name="repass" value="' . $_POST['repass'] . '" /></div>' . $this->cleaner . '
                    <div style="float: left; width: 80px; margin: 3px;">meno:</div>
                    <div style="margin-bottom: 5px;"><input type="text" name="first_name" value="' . $_POST['first_name'] . '" /></div>' . $this->cleaner . '
                    <div style="float: left; width: 80px; margin: 3px;">priezvisko:</div>
                    <div style="margin-bottom: 5px;"><input type="text" name="last_name" value="' . $_POST['last_name'] . '" /></div>' . $this->cleaner . '
                    <div style="float: left; width: 80px; margin: 3px;">e-mail:</div>
                    <div style="margin-bottom: 5px;"><input type="text" name="mail" value="' . $_POST['mail'] . '" /></div>' . $this->cleaner;
            echo '
                    <div style="float: left; width: 80px; margin: 3px;">odbor:</div>
                    <div style="margin-bottom: 5px;">
                        <select name="odbor" style="font-size: 10px;">
            ';
                        $sql = "SELECT * FROM e_index.portal_odbory ORDER BY id_o ASC;";
                        if ( $result = mysql_query($sql) ) {
                            echo '<option>---</option>';
                            while ($row = mysql_fetch_assoc($result)) {
                                echo '<option>' . $row['o_kod'] . '</option>';
                            }
                        }
            echo '
                        </select>
                    </div>
            ';
            echo '
                    <div style="float: left; width: 80px; margin: 3px;">Bezpečnostný kód: </div>
                    <div style="margin-bottom: 5px;"><img src="CaptchaSecurityImages.php?width=100&height=40&characters=5" /></div>' . $this->cleaner . '
                    <div style="float: left; width: 80px; margin: 3px;">Sem vložte kód z obrázku:</div>
                    <div style="margin-bottom: 5px;"><input id="security_code" name="security_code" type="text" /></div>' . $this->cleaner . '
                    <div style="margin-bottom: 5px;"><input type="submit" name="register" value="Registruj"  /></div>
                </form>
            ';
		}
	}
		
	public function execute_reg($nick, $pass, $first_name, $last_name, $mail) {
		$this->sql->sql_connect('login');
			$sql_reg = "INSERT INTO users (nick, pass, first_name, last_name, mail, department) VALUES
			('" . $nick . "', '" . $pass . "', '" . $first_name . "', '" . $last_name . "', '" . $mail . "', '" . $_POST['odbor'] . "');";
			if ( mysql_query($sql_reg) ) {
                if ( $this->copy_subjects($nick) ) {
                    $this->return_heading_text('Registrácia prebehla úspešne.');
                } else {
                    $this->return_heading_text('Registrácia prebehla úspešne. <span style="color:;">Ale nastal problém pri zápise povinných predmetov v časti E-index.</span>');
                }
			} else {
				$this->return_heading_text('<span style="color: red;">Problém pri zápise do databázy, registrácia NEprebehla úspešne.</span>');
			}
		$this->sql->sql_close();
	}

    private function copy_subjects($nick) {
        $sql_get_user_id = "SELECT id_user FROM login.users WHERE nick = '" . $nick ."';";
        if ( $result_get_user_id = mysql_query($sql_get_user_id) ) {
            $row_get_user_id = mysql_fetch_assoc($result_get_user_id);
            $id_user = $row_get_user_id['id_user'];
        }
        //spojenie db s predmetmy daneho odboru
        $sql_get_subject = "SELECT * FROM e_index.portal_predmety WHERE odbor = '" . $_POST['odbor'] . "' AND druh='PP';";
        if ( $result_get_subject = mysql_query($sql_get_subject) ) {
            //vyberanie predmetov povinnych
            while ( $row_get_subject = mysql_fetch_assoc($result_get_subject) ) {
                $sql_add_subject = "INSERT INTO e_index.portal_main
                  (id_p, student, odbor, druh, cislo, predmet, ucitel,
                   prerekvizity, rok, semester, typ, kredity, hodnotenie, znamka) VALUES ('"
                   . $row_get_subject['id_p'] . "', '" . $id_user . "', '" . $row_get_subject['odbor'] . "', '"
                   . $row_get_subject['druh'] . "', '" . $row_get_subject['cislo'] . "', '" . $row_get_subject['predmet'] . "', '"
                   . $row_get_subject['ucitel'] . "', '" . $row_get_subject['prerekvizity'] . "', '" . $row_get_subject['rok'] . "', '"
                   . $row_get_subject['semester'] . "', '" . $row_get_subject['typ'] . "', '" . $row_get_subject['kredity'] . "', '"
                   . $row_get_subject['hodnotenie'] . "', '--');";
                if ( $result_add_subject = mysql_query($sql_add_subject) ) {
                    //
                } else {
                    return false;
                }
            }
            return true;
        } else {
            return false;
        }
    }

	/*** REGISTRATION: END ***/

}
?>