<?php
/**
 * User: David Camacho
 * Date: 30/03/2015
 * Time: 17:18
 */
include_once "./controllers/Core.php";

class RegistrationController extends Core{

    var $user;
    var $errors_output;
    var $errors;

    public function RegistrationController()
    {

        parent::Core();

        $this->errors_output = "";
        $this->errors = array();
        $this->user = new User();

        if (isset($_POST["user_selected"]) && isset($_POST["pass_selected"]) && isset($_POST["role_selected"]) && isset($_POST["name_selected"])) {

            $this->user->registerUser($_POST["role_selected"], $_POST["name_selected"], $_POST["user_selected"], $_POST["pass_selected"]);
            header('Location: ./login.php');

        }else
            $this->displayTemplate();


        $this->user->destroy();
    }




    private function displayTemplate()
    {

        if( count($this->errors)>0){
            $this->errors_output = '<div data-alert class="alert-box alert">';
            foreach($this->errors as $err)
                $this->errors_output = $this->errors_output  . $err. '<br>';
         $this->errors_output = $this->errors_output . ' <a href="#" class="close">&times;</a></div>';

        }

        include_once('./templates/' . _template . '/views/registration.html');
    }



}