<?php

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

class Static_pages extends CI_Controller
{
    function __construct()
    {
        parent::__construct();
        $this->load->model('Static_pages_model');
        $this->load->library('form_validation');
    }

    public function index()
    {
        $q = urldecode($this->input->get('q', TRUE));
        $start = intval($this->input->get('start'));
        
        if ($q <> '') {
            $config['base_url'] = base_url() . 'static_pages/?q=' . urlencode($q);
            $config['first_url'] = base_url() . 'static_pages/?q=' . urlencode($q);
        } else {
            $config['base_url'] = base_url() . 'static_pages/';
            $config['first_url'] = base_url() . 'static_pages/';
        }

        $config['per_page'] = 10;
        $config['page_query_string'] = TRUE;
        $config['total_rows'] = $this->Static_pages_model->total_rows($q);
        $static_pages = $this->Static_pages_model->get_limit_data($config['per_page'], $start, $q);

        $this->load->library('pagination');
        $this->pagination->initialize($config);

        $data = array(
            'static_pages_data' => $static_pages,
            'q' => $q,
            'pagination' => $this->pagination->create_links(),
            'total_rows' => $config['total_rows'],
            'start' => $start,
        );

        $pageData = array(
            'header'=>array('pageTitle'=>'CMS Pages'),
            'body'=>$data,
            'footer'=>array(),
        );

        loadView('static_pages/static_pages_list',$pageData);
        
    }


    public function create() 
    {
        $data = array(
            'button' => 'Create',
            'action' => site_url('static_pages/create_action'),
            'ID' => set_value('ID'),
            'fsTitle' => set_value('fsTitle'),
            'fsDescription' => set_value('fsDescription'),
            'fiStatus' => set_value('fiStatus'),
            'fdCreateDate' => set_value('fdCreateDate'),
            'fdUpdateDate' => set_value('fdUpdateDate'),
        );

        $pageData = array(
            'header'=>array('pageTitle'=>'CMS Pages'),
            'body'=>$data,
            'footer'=>array(),
        );

        loadView('static_pages/static_pages_form',$pageData);
    }
    
    public function create_action() 
    {
        $this->_rules();

        if ($this->form_validation->run() == FALSE) {
            $this->create();
        } else {
            // print_r($_REQUEST);
            // ECHO $this->input->post('fsDescription'); DIE();
            $data = array(
		'fsTitle' => $this->input->post('fsTitle',TRUE),
		'fsDescription' => $this->input->post('fsDescription',TRUE),
		'fiStatus' => $this->input->post('fiStatus',TRUE),
		'fdCreateDate' => date('Y-m-d H:i:s'),
	    );

            $this->Static_pages_model->insert($data);
            $this->session->set_flashdata('message', 'Create Record Success');
            redirect(site_url('static_pages'));
        }
    }
    
    public function update($id) 
    {
        $row = $this->Static_pages_model->get_by_id($id);

        if ($row) {
            $data = array(
                'button' => 'Update',
                'action' => site_url('static_pages/update_action'),
        		'ID' => set_value('ID', $row->ID),
        		'fsTitle' => set_value('fsTitle', $row->fsTitle),
        		'fsDescription' => set_value('fsDescription', $row->fsDescription),
        		'fiStatus' => set_value('fiStatus', $row->fiStatus),
        		'fdCreateDate' => set_value('fdCreateDate', $row->fdCreateDate),
        		'fdUpdateDate' => set_value('fdUpdateDate', $row->fdUpdateDate),
        	);

            $pageData = array(
            'header'=>array('pageTitle'=>'Static page'),
            'body'=>$data,
            'footer'=>array(),
            );

            loadView('static_pages/static_pages_form',$pageData);
        } else {
            $this->session->set_flashdata('message', 'Record Not Found');
            redirect(site_url('static_pages'));
        }
    }
    
    public function update_action() 
    {
        $this->_rules();

        if ($this->form_validation->run() == FALSE) {
            $this->update($this->input->post('ID', TRUE));
        } else {
            $data = array(
		'fsTitle' => $this->input->post('fsTitle',TRUE),
		'fsDescription' => $this->input->post('fsDescription',TRUE),
		'fiStatus' => $this->input->post('fiStatus',TRUE),
		'fdUpdateDate' => date('Y-m-d H:i:s'),
	    );

            $this->Static_pages_model->update($this->input->post('ID', TRUE), $data);
            $this->session->set_flashdata('message', 'Update Record Success');
            redirect(site_url('static_pages'));
        }
    }
    
    public function delete($id) 
    {
        $row = $this->Static_pages_model->get_by_id($id);

        if ($row) {
            $this->Static_pages_model->delete($id);
            $this->session->set_flashdata('message', 'Delete Record Success');
            redirect(site_url('static_pages'));
        } else {
            $this->session->set_flashdata('message', 'Record Not Found');
            redirect(site_url('static_pages'));
        }
    }

    public function _rules() 
    {
	$this->form_validation->set_rules('fsTitle', 'fstitle', 'trim|required');
	$this->form_validation->set_rules('fsDescription', 'fsdescription', 'trim|required');
	$this->form_validation->set_rules('ID', 'ID', 'trim');
	$this->form_validation->set_error_delimiters('<span class="text-danger">', '</span>');
    }

}
