<?php

/*
 * awframework from aw.inc present aw controller
 * it will help you develop easy and fast with secure tools
 * you can edit and add what you feel right
 * don't change in strucure of file
 */

require_once("imp/_imp.php");

class Controllers {

    //public vars
    public $smarty;
    public $hosturl;

    // it will run in everytime with
    // default requried
    public function __construct() {
        require_once("../public/aw_config.php");
        $this->smarty = $smarty;
        $this->hosturl = $hosturl;

        $this->smarty->assign('thisdisply', 0);
    }

    //here the frist page and
    //the frist action in this class
    public function index() {

        // include the to-be-used language
        require_once('../../library/login_system/translations/ar.php');
        // include the PHPMailer library
        require_once('../../library/plugins/PHPMailer/PHPMailer.php');
        // load the registration class
        require_once('../../library/login_system/classes/Login.php');
        // create the registration object. when this object is created, it will do all registration stuff automatically
        // so this single line handles the entire registration process.
        $login = new Login();
        if ($login->isUserLoggedIn() == true) {
            // the user is logged in. you can do whatever you want here.
            // for demonstration purposes, we simply show the "you are logged in" view.
            header('Location:' . $this->hosturl . '/index');
        } else {
            // the user is not logged in. you can do whatever you want here.
            // for demonstration purposes, we simply show the "you are not logged in" view.
            if (isset($login)) {
                if ($login->errors) {
                    foreach ($login->errors as $error) {
                        $this->smarty->assign('error', $error);
                    }
                }
            }


            $this->smarty->display('_login/head.html');
            $this->smarty->display('_login/login.html');
            $this->smarty->display('_login/js.html');
        }
    }

    public function logout() {
        // include the to-be-used language
        require_once('../../library/login_system/translations/ar.php');
        // include the PHPMailer library
        require_once('../../library/plugins/PHPMailer/PHPMailer.php');
        // load the registration class
        require_once('../../library/login_system/classes/Login.php');
        // create the registration object. when this object is created, it will do all registration stuff automatically
        // so this single line handles the entire registration process.
        $login = new Login();

        if ($login->isUserLoggedIn() == true) {
            // the user is logged in. you can do whatever you want here.
            // for demonstration purposes, we simply show the "you are logged in" view.
            header('Location:' . $this->hosturl . '/index');
        } else {
            // the user is not logged in. you can do whatever you want here.
            // for demonstration purposes, we simply show the "you are not logged in" view.
            header('Location:' . $this->hosturl . '/login');
        }
    }

    public function fgpass() {
        // include the to-be-used language
        require_once('../../library/login_system/translations/ar.php');
        // include the PHPMailer library
        require_once('../../library/plugins/PHPMailer/PHPMailer.php');
        // load the registration class
        require_once('../../library/login_system/classes/Login.php');
        // create the registration object. when this object is created, it will do all registration stuff automatically
        // so this single line handles the entire registration process.
        $login = new Login();


        if ($login->passwordResetWasSuccessful() == true && $login->passwordResetLinkIsValid() != true) {
            //not use just redirect if happend
            header('Location:' . $this->hosturl . '/login');
        } else {
            //sucess  good
            header('Location:' . $this->hosturl . '/login');
        }
    }

    public function passrest() {
        // include the to-be-used language
        require_once('../../library/login_system/translations/ar.php');
        // include the PHPMailer library
        require_once('../../library/plugins/PHPMailer/PHPMailer.php');
        // load the registration class
        require_once('../../library/login_system/classes/Login.php');
        // create the registration object. when this object is created, it will do all registration stuff automatically
        // so this single line handles the entire registration process.
        $login = new Login();

        if ($login->passwordResetWasSuccessful() == true && $login->passwordResetLinkIsValid() != true) {
            //not use just redirect if happend
            header('Location:' . $this->hosturl . '/login');
        } else {
            //sucess  good
            $this->smarty->assign('user_name', $_GET['id']);
            $this->smarty->assign('verification_code', $_GET['other']);

            $this->smarty->display('_login/head.html');
            $this->smarty->display('_login/resetpass.html');
            $this->smarty->display('_login/js.html');
        }
    }

    public function __destruct() {
        //colse connection
        R::close();
    }

}
