<?php

/**
 * The <strong>Loader</strong> class loads the model(s) and/or view(s) given by the developer.<br />
 * Loader object must be used within controllers only.
 *
 * @copyright Trusto Tech 2015-2016
 * @package System\Core
 */
class Loader {

    /**
     * Takes a model name and checkes whether or not the model exists in its respective folder.
     * If the model class exists, the model method will then create an instance of the Registry
     * class and wraps the model object in it.
     *
     * @author  Taha Sayyad <info@trustotech.com>
     * @param string $model The model name you want to load
     * @uses Registry::getInstance() to create an instance of the registry class then wraps the model
     * 		object in it.
     * @throws Exception if there was an error in loading the model
     * @return boolean True on success.
     */
    public function model($model) {
        $full_model = MODELS . $model . 'Model.php';
        $model_class = $model . 'Model';
        $custom_model = include_custom_path('Models' . DIRECTORY_SEPARATOR . $model . 'Model.php', false);
        if ($custom_model) {
            $full_model = $custom_model;
            $model_class = $model . 'ModelCustom';
            include $full_model;
        }
        //
        if (is_readable($full_model) && class_exists($model_class)) {
            $registry = Registry::getInstance();
            $registry->$model = new $model_class;
            return true;
        }
        throw new Exception('Error in loading model: ' . $model_class);
    }

    /**
     * Takes a view name and checkes whether or not the view exists in its respective folder.
     * If the view file exists, the view method will then extract the vars returned from the 
     * controllers. If the 'error' index is set to TRUE, the default template files and the view
     * file will be included and displayed to the user.
     *
     * @param string $model The view name you want to load
     * @param array $vars An array of arguments returned from the current view controller.
     * 		 The default value of $vars is NULL.
     * @throws Exception if there was an error in loading the view
     * @return boolean True on success.
     */
    public function view($view, array $vars = null, $error = null, $ignore_login = false) {
        //
        if ($ignore_login)
            $_GET['__trusto__head_ignore_login'] = 1;
        //
        $full_view = VIEWS . $view . '.php';
        $custom_view = include_custom_path('Views' . DIRECTORY_SEPARATOR . $view . '.php', false);
        if ($custom_view)
            $full_view = $custom_view;
        //
        if (is_readable($full_view)) {
            if (isset($vars))
                extract($vars);
            if (!$error) {
                include HTML . 'head.php';
                
                include HTML . 'content.php';
            }
            include $full_view;
            if (!$error)
                include HTML . 'footer.php';
            return true;
        }
        throw new Exception($view . ' did not load.');
    }

    public function view_html($html, array $vars = null, $error = null, $ignore_login = false) {
        if ($ignore_login)
            $_GET['__trusto__head_ignore_login'] = 1;
        //
        if (isset($vars)) {//needed for page name permission in head
            extract($vars);
        }
        if (!$error) {
            include HTML . 'head.php';
            include HTML . 'content.php';
        }
        echo $html;
        if (!$error) {
            include HTML . 'footer.php';
        }
        return true;
    }

}
