<?php
class Controller {

    public function __construct() {
        // Start session if not already started
        if (session_status() == PHP_SESSION_NONE) {
            session_start();
        }

        // Don't redirect if current controller is LoginController
        $currentController = get_class($this); // get actual controller class

        if ($currentController !== 'LoginController') {
            if (empty($_SESSION['admin_logged_in'])) {
                header('Location: ' . BASE_URL . '/login');
                exit;
            }
        }
    }

    // View loader
    public function view($view, $data = []) {
        extract($data);
        $viewPath = 'admin/app/views/';

        if (file_exists($viewPath . 'header.php')) {
            include $viewPath . 'header.php';
        }

        include $viewPath . $view . '.php';

        if (file_exists($viewPath . 'footer.php')) {
            include $viewPath . 'footer.php';
        }
    }

    // Model loader
    public function model($model) {
        require_once "admin/app/models/$model.php";
        return new $model();
    }
}
