<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Pages extends CI_Controller {


    protected $data = Array();
    protected $jsData = Array();

	public function index()
	{
		$this->load->view('home');
	}

	public function view($page = "home", $data = array()){
        if (!file_exists(APPPATH . 'views/pages/' . $page . '.php')) {
            // Whoops, we don't have a page for that!
            show_404();
        }


        $this->load->library(array('ion_auth'));

        $this->data = $data;
        $this->loadResource($page);

        $this->data['my_user'] = $this->ion_auth->user()->row();

        if(file_exists(APPPATH . 'views/includes/head/' . $page . '.php')){
            $this->load->view('includes/head/' . $page);
        }else{
            $this->load->view('includes/head/_default.php');
        }
        $this->load->view('pages/' . $page, $this->data);
        if(file_exists(APPPATH . 'views/includes/footer/' . $page . '.php')){
            $this->load->view('includes/footer/' . $page, $this->jsData);
        }else{
            $this->load->view('includes/footer/_default.php', $this->jsData);
        }
    }

    private function loadResource($page = "home"){
        if (file_exists(APPPATH . 'controllers/resources/' . $page . '.php')) {
            include_once(APPPATH . 'controllers/resources/' . $page . '.php');
            return TRUE;
        }
        return FALSE;
    }

}
