<?php

/**
 * This class plays a role in giving life to all controllers across the system.<br />
 * The $_registry var will hold an instance of the Registry class in which will be used
 * to store the needed model for a controller inside it.
 * The $load var will hold an instance of the Loader class for which to load the needed
 * models and views.
 *
 * @copyright Trusto Tech 2015-2016
 * @package System\Core
 */
abstract class mainController {

    /**
     * @var obj Holds an instance of the Registry class
     */
    protected $_registry;

    /**
     * @var obj Holds an instance of the Loader class
     */
    protected $load;

    /**
     * Create and save an instance of the Registry and Loader classes
     *
     * @uses Registry::getInstance() to create an instance of the Registry class
     * @uses Loader to create an instance of the Loader class
     */
    public function __construct($check_login = true) {
        if ($check_login && Session::isAlive() == false) {
            if (!empty($_GET['smc__auto_login'])) {
                $user_pass = CommonFunctions::decryptAutoLogin($_GET['smc__auto_login']);
                $login_arr = array('Username' => $user_pass[0], 'Password' => $user_pass[1]);
                $person = new Person();
                $statusCode = $person->setLoginInformation($login_arr)->login();    //login and fill session/permissions
                //if login not success
                if ($statusCode != 1) {
                    $person->redirectFromStatusCode($statusCode);
                    exit;
                }
            } else {
                Session::kill();
                exit();
            }
        }
        $this->_registry = Registry::getInstance();
        $this->load = new Loader;
    }

    /**
     * An abstract method that must be implemented by all classes that extend mainController class
     *
     */
    abstract public function landing();

    public function get_registry() {
        return $this->_registry;
    }

}
