<?php

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

class Whywork extends CI_Controller 
{
	 function __construct() 
	 {
        parent::__construct();
        $this->load->database();
        $this->load->model('User_Model');
        $this->load->model('Common');
         $x = $this->session->userdata('logged');
         if ($x['email'] == '') 
         {
                return redirect('Welcome/index', 'refresh');
          }
    }

    public function index()
    {
        $page =(isset($_GET['page'])) ? $_GET['page'] : 0;
        $query=$this->db->select('whywork.*')->from('whywork')->order_by('id','desc')->limit(20,($page*20))->get();
        $num_rows=$this->db->order_by('id', 'desc')->get('whywork')->num_rows();
        $data['whywork']=$query->result_array();
        $data['links']=$this->pagi->pagination1('admin/whywork',$num_rows,20);
        $this->load->view('whywork/index',$data);
    }

    public function add()
    {
        if ($this->input->server('REQUEST_METHOD') == 'POST') 
        {
         $this->load->helper(array('form'));
         $this->load->library('form_validation');
         $this->form_validation->set_rules('title', 'Title', 'required'); 
         $this->form_validation->set_rules('description', 'Description', 'required'); 
            if ($this->form_validation->run() == FALSE) 
             { 
                 $this->load->view('whywork/add');
             } 
             else 
             { 
               
                if($this->db->insert('whywork',$_POST))
                {
                    $this->session->set_flashdata('msg', 'whywork Successfully Added');
                    return redirect(base_url('admin/whywork'),'refresh');
                }
                else
                {
                    $this->session->set_flashdata('err', 'Please try Again After SomeTimes');
                    return redirect(base_url('admin/whywork/add'),'refresh');
                }
             } 
        }
        else
        {
            $this->load->view('whywork/add');
        }
    }

    public function edit($id='')
    {
        if ($this->input->server('REQUEST_METHOD') == 'POST') 
        {
        $this->load->helper(array('form'));
         $this->load->library('form_validation');
         $this->form_validation->set_rules('title', 'Title', 'required'); 
         $this->form_validation->set_rules('description', 'Description', 'required'); 
            if ($this->form_validation->run() == FALSE) 
             { 
                 $data['whywork']=$this->db->where('id',$id)->get('whywork')->row_array();
                 $this->load->view('whywork/edit',$data);
             } 
             else 
             { 
                
                if($this->db->where('id',$id)->update('whywork',$_POST))
                {
                    $this->session->set_flashdata('msg', 'whywork Successfully Added');
                    return redirect(base_url('admin/whywork'),'refresh');
                }
                else
                {
                    $this->session->set_flashdata('err', 'Please try Again After SomeTimes');
                    return redirect(base_url('admin/whywork/edit/'.$id),'refresh');
                }
             }
        }
        else
        {
            $data['whywork']=$this->db->where('id',$id)->get('whywork')->row_array();
            $this->load->view('whywork/edit',$data);
        }
    }

    public function view($id='')
    {
         $data['whywork']=$this->db->where('id',$id)->get('whywork')->row_array();
         $this->load->view('whywork/view',$data);
    }

    public function delete($id='')
    {
        if($this->db->where('id',$id)->delete('whywork'))
        {
            $this->session->set_flashdata('msg', 'Success Deleted whywork');
        }
        else
        {
            $this->session->set_flashdata('err', 'Please try After Sometimes...');
        }
        return redirect(base_url('admin/whywork'),'refresh');
    }

}
