<?php

class Home extends CIF_Controller {

    public $layout = 'full';
    public $module = 'home';
    public $model = 'Sessions_model';

    public function __construct() {
        parent::__construct();
        $this->load->model($this->model);
        $this->_primary_key = $this->{$this->model}->_primary_keys[0];
    }

    public function index() {
        $data['sliders'] = $this->db->get('sliders')->result();
        $data['about'] = $this->db->get('aboutme')->result();
        $data['testimonials'] = $this->db->get('testimonials')->result();
        $this->{$this->model}->custom_select = 'sessions.*, categories.title as category';
        $this->{$this->model}->joins = array(
            'categories' => array('categories.category_id = sessions.category_id', 'inner')
        );
        $data['categories'] = $this->db->select('categories.*, (SELECT COUNT(*) FROM sessions where categories.category_id = sessions.category_id) as count')->order_by('order')->get('categories')->result();

       $data['items'] = $this->db->limit('8')
                        ->order_by('session_id', 'desc')->get('sessions')->result();
         $this->load->view($this->module, $data);
    }

    public function contactme() {
        $this->load->library('form_validation');
        $this->form_validation->set_rules('name', 'Name', 'trim|required');
        $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
        $this->form_validation->set_rules('message', 'Message', 'trim|required');
        $data['success'] = false;

        if ($this->form_validation->run() == TRUE) {
            @mail(config('webmaster_email'), 'Message from ' . config('title'), ""
                            . "Full Name: $_POST[name]\n"
                            . "Email: $_POST[email]\n"
                            . "Message: $_POST[message]\n"
                            . "");
            $data['success'] = true;
        }
        $this->load->view(__FUNCTION__, $data);
    }

}
