<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Aboutme extends CIF_Controller {

    public $layout = 'full';
    public $module = 'aboutme';
    public $model = 'Aboutme_model';

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

    public function manage($id = FALSE) {
        $data = array();

        if ($id) {
            $this->{$this->model}->{$this->_primary_key} = $id;
            $data['item'] = $this->{$this->model}->get();
            if (!$data['item'])
                show_404();
        } else {
            $data['item'] = new Std();
        }
        $this->load->library("form_validation");
        $this->form_validation->set_rules('about', 'About', 'trim|required');
        $this->form_validation->set_rules('name', 'Name', 'trim|required');
        $this->form_validation->set_rules('established', 'established', 'trim|required');
        $this->form_validation->set_rules('sessions', 'sessions', 'trim|required');
        $this->form_validation->set_rules('meetings', 'meetings', 'trim|required');
        $this->form_validation->set_rules('clients', 'clients', 'trim|required');
        $this->form_validation->set_rules("image", 'Image', "trim|callback_image[$id]");


        if ($this->form_validation->run() == FALSE)
            $this->load->view($this->module . '/manage', $data);

        else {
            $this->{$this->model}->about = $this->input->post('about');
            $this->{$this->model}->name = $this->input->post('name');
            $this->{$this->model}->established = $this->input->post('established');
            $this->{$this->model}->sessions = $this->input->post('sessions');
            $this->{$this->model}->clients = $this->input->post('clients');
            $this->{$this->model}->meetings = $this->input->post('meetings');

            $this->{$this->model}->save();
            redirect('admin/aboutme/manage/1');
        }
    }

    public function image($var, $id) {
        $config['upload_path'] = './cdn/about/';
        $config['allowed_types'] = 'gif|jpg|png|jpeg';
        $this->load->library('upload', $config);
        if ($this->upload->do_upload('image')) {
            $data = $this->upload->data();
            if ($data['file_name'])
                $this->{$this->model}->image = $data['file_name'];
        }
        return true;
    }

}
