<?php 

class polls extends CI_Controller {

	function polls()	 

	{  

    parent::__construct();	

    $this->load->library('session');		

    $this->load->Model('functions', 'fun');  //function in model 

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

    $this->load->library('DX_Auth');

    $this->load->helper('form');

	$this->load->helper('url');

	$this->load->library('form_validation');

	$this->dx_auth->check_uri_permissions();



    } 

    

	

		

	function index()

	{   

	    $this->load->model('polls_model');  /// model file name

	    	

	   //////pagination styl and config

	    $config['base_url'] = base_url().'index.php/polls/index/';

		$config['total_rows'] = $this->polls_model->get_num_polls();

		$config['per_page'] = 20;

		$config['num_links'] = 3; 

		$config['uri_segment'] = 3;	

		$config['full_tag_open']='<div class="pagination">';

		$config['full_tag_close'] = '</div>';

		$config['next_link'] = 'Next';

		$config['prev_link'] = 'Prev';

		$config['cur_tag_open'] = '<span class="current">';

		$config['cur_tag_close'] = '</span>';		

		$offset = $this->uri->segment(3,0);

		$this->session->set_userdata('offset',$offset);

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

		

		$this->load->view('header');

		

		//// load left and send query to creat menu

		$status='all';

		

		$menu=array();

		$menu_q= $this->fun->get_modules();

		$menu['mods']=$menu_q;

		$menu['status']=$status;

		$this->load->view('left',$menu);

		

		////// load content send query to get post 

		$data=array();

		$q=$this->polls_model->get_polls($config['per_page']);   ///////model class --->model func		

		$data['records']=$q;

		

		$this->load->view('polls',$data);

		

		$this->load->view('footer');

	}

	

	

	function add_poll()

	{   

	

	           $data=array();

	             

	

	   $status='all';

	    

	    $this->load->view('header');

	    

	    $menu=array();

		$menu_q= $this->fun->get_modules();

		$menu['mods']=$menu_q;

		$menu['status']=$status;

		$this->load->view('left',$menu);

		$data['status']=$status;

		

		$this->load->view('add_poll',$data);

		$this->load->view('footer');



	}

	function save_add_poll()

	{	

	

	      $data=array();

	                $data['poll'] = $this->input->post('poll');

	                $data['answer'] = $this->input->post('answer');

	                $data['answer2'] = $this->input->post('answer2');

	                $data['answer3'] = $this->input->post('answer3');

	                $data['answer4'] = $this->input->post('answer4');

	                $data['answer5'] = $this->input->post('answer5');



	                

	                    $this->form_validation->set_error_delimiters('<font color="#ff0000">', '</font>');

						$this->form_validation->set_rules('poll', 'Poll ', 'trim|required|xss_clean');

						$this->form_validation->set_rules('answer', 'Answer ', 'trim|required|xss_clean');

						$this->form_validation->set_rules('answer2', 'Answer ', 'trim|required|xss_clean');

						$this->form_validation->set_rules('answer3', 'Answer ', 'trim|required|xss_clean');



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

					{

			

			              $status='all';

						   

						    $this->load->view('header');

						    

						    $menu=array();

							$menu_q= $this->fun->get_modules();

							$menu['mods']=$menu_q;

							$menu['status']=$status;

							$this->load->view('left',$menu);

							$data['status']=$status;

							

							$this->load->view('add_poll',$data);

							$this->load->view('footer');

					

					}

					else  // validation ture //

					{ 

					  $ans=array();

					  $ans['answer']=$this->input->post('answer');

					  $ans['answer2']=$this->input->post('answer2');

					  $ans['answer3']=$this->input->post('answer3');

					  $ans['answer4']=$this->input->post('answer4');

					  $ans['answer5']=$this->input->post('answer5');

					  

					 

					  

					  $data2=array('poll'=>$this->input->post('poll'));

					   $dt['poll'] = $data2;

					   $dt['answers'] = $ans;

					  

					  $this->load->model('polls_model');   ///model file name

	                  $this->polls_model->add_new_poll($data,'1');  /////model class --->model func

	                  

	                  echo "<script type=\"text/javascript\"> alert(\" Added Successfully \") ;window.location.href= \"".base_url()."index.php/polls\";</script>";









	                

					               

     			   }

					    

	}









	function edit_poll($id)

	{ 	

	     $status='all';

	     $data['status']=$status;

	     

	     $data['qry'] = $this->fun->getone("poll", $id);

	      			

	       $this->load->model('polls_model');   ///model file name

	       

	       $data['ans_qry'] =$this->polls_model->get_answers($id);	



	    $this->load->view('header');

	    

	    $menu=array();

		$menu_q= $this->fun->get_modules();

		$menu['mods']=$menu_q;

		$menu['status']=$status;

		$this->load->view('left',$menu);

						

	    $this->load->view('edit_poll', $data);

	    $this->load->view('footer');

	

	}

	

	function save_edite_poll($id)

	{

	

	

	      

	      $data=array();

	                $data['poll_id'] = $id;

	                $data['poll'] = $this->input->post('poll');

	                $data['answer'] = $this->input->post('answer');

	                $data['answer2'] = $this->input->post('answer2');

	                $data['answer3'] = $this->input->post('answer3');

	                $data['answer4'] = $this->input->post('answer4');

	                $data['answer5'] = $this->input->post('answer5');

	                

	                 $data['ans_id'] = $this->input->post('ans_id');

	                $data['ans_id2'] = $this->input->post('ans_id2');

	                $data['ans_id3'] = $this->input->post('ans_id3');

	                $data['ans_id4'] = $this->input->post('ans_id4');

	                $data['ans_id5'] = $this->input->post('ans_id5');





	                

	                    $this->form_validation->set_error_delimiters('<font color="#ff0000">', '</font>');

						$this->form_validation->set_rules('poll', 'Poll ', 'trim|required|xss_clean');

						$this->form_validation->set_rules('answer', 'Answer ', 'trim|required|xss_clean');

						$this->form_validation->set_rules('answer2', 'Answer ', 'trim|required|xss_clean');

						$this->form_validation->set_rules('answer3', 'Answer ', 'trim|required|xss_clean');



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

					{

			

						   $this->edit_poll($id);

					

					}

					else  // validation ture //

					{ 

					  					  

					  $this->load->model('polls_model');   ///model file name

	                  $this->polls_model->update_poll($data);  /////model class --->model func

	                  $this->edit_poll($id);

	                  

	                }  





	}

	

	function delete_poll($id)

	{ 	

	    $q= $this->fun->delete_one("poll", $id); 

	    

	    if($q)

	    {

	      $this->load->model('polls_model');   ///model file name

	      $this->polls_model->delete_answer($id);  /////model class --->model func



	     echo "<script type=\"text/javascript\"> alert(\" Deleted successfully \") ;window.location.href= \"".base_url()."index.php/polls/\";</script>";

	    }

	    else

	    {

	    echo "<script type=\"text/javascript\"> alert(\" Dont Deleted Please Try Again \") ;window.location.href= \"".base_url()."index.php/polls/\";</script>";

	    }

	   

	

	}

}

/* End of file welcome.php */

/* Location: ./application/controllers/welcome.php */