<?php

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



class SliderController extends CI_Controller 

{



	function __construct() 

	{ 

		parent::__construct();

		$this->load->database();

		$x = $this->session->userdata('logged');

         if ($x['email'] == '') 

         {

                return redirect('Welcome/index', 'refresh');

          }

		

	}



	public function index()

	{

		$data['slider']=$this->db->get('slider')->result_array();

		$this->load->view('slider/view_slider',$data);

	}



	public function add_slider()

	{

		if($this->input->server('REQUEST_METHOD') == 'POST')

		{

			$this->form_validation->set_rules('status', 'Status', 'required');
			

			 	if ($this->form_validation->run() == FALSE)

                {

                        $this->load->view('slider/add_slider');

                }

                else

                {

                	$journalName = str_replace(' ', '_', $_FILES['image']['name']);

		            $config['upload_path']          = './uploads/slider/';

		            $config['allowed_types']        = 'gif|jpg|png|JPEG|jpeg|JPG';

		           $config['file_name'] = time().$journalName;



                 $this->upload->initialize($config);



		            if ( ! $this->upload->do_upload('image'))

		            {

		                    $error = array('error' => $this->upload->display_errors());



		                    $this->load->view('slider/add_slider', $error);

		            }

		            else

		            {

		                    $inserted_data=$this->input->post();

		                    $inserted_data['image']=$config['file_name'];

							if($this->db->insert('slider',$inserted_data))

							{

									$this->session->set_flashdata('msg', 'Successfully Inserted Slider Image');

							}

							else

							{

								$this->session->set_flashdata('err', 'Not Inserted please Try Again');

							}

							return redirect(base_url('view_slider'),'refresh');

		            } 

                }

		}

		else

		{

			$this->load->view('slider/add_slider');

		}

	}



	public function edit_slider($id='')

	{



		if($this->input->server('REQUEST_METHOD') == 'POST')

		{

			$this->form_validation->set_rules('status', 'Status', 'required');

				if ($this->form_validation->run() == FALSE)

                {

					$data['slider']=$this->db->where('id',$id)->get('slider')->row_array();

					$this->load->view('slider/edit_slider',$data);

                }

                else

                {

                	$data=$this->db->where('id',$id)->get('slider')->row_array();

                	$updated_data=$this->input->post();

                	if($_FILES['image']['name']!='')

                	{

							$journalName = str_replace(' ', '_', $_FILES['image']['name']);

		            $config['upload_path']          = './uploads/slider/';

		            $config['allowed_types']        = 'gif|jpg|png|JPEG|jpeg|JPG';

		           $config['file_name'] = time().$journalName;



						 $this->upload->initialize($config);



						if ( ! $this->upload->do_upload('image'))

						{

						   return redirect(base_url('view_slider'));

						}

						else

						{

							$updated_data['image']=$config['file_name'];

						}



                	}

                         

		                    

							if($this->db->where('id',$id)->update('slider',$updated_data))

							{

								unlink(base_url("uploads/slider/".$data['image']));

									$this->session->set_flashdata('msg', 'Successfully Updates Slider Image');

							}

							else

							{

								$this->session->set_flashdata('err', 'Not Updated please Try Again');

							}

							return redirect(base_url('view_slider'),'refresh');

                }

		}

		else

		{

			$data['slider']=$this->db->where('id',$id)->get('slider')->row_array();

			$this->load->view('slider/edit_slider',$data);

		}

	}



	public function delete_slider($id='')

	{

		$data=$this->db->where('id',$id)->get('slider')->row_array();



			if($this->db->where('id',$id)->delete('slider'))

			{

				unlink(base_url($_SERVER['DOCUMENT_ROOT']."/uploads/slider/".$data['image']));

				$this->session->set_flashdata('msg', 'Slider Image Successfully Deleted');

			}

			else

			{

				$this->session->set_flashdata('err', 'SLider Image Not Deleted Please Try Again After Some Time');

			}



			//return redirect(base_url('view_slider'),'refresh');

	}

}