<?php

class Choices   extends CI_Controller {
       
    var $tablename = CHOICES;
    var $primarykey = "ChoiceID";
    
    public function index(){               
        $data['current'] = "choices";                  
        $data['choices'] = $this->general->getAllRecordsWhere($this->tablename,array(),"$this->primarykey","desc");
        $this->load->view(ADMINFOLDER."/choices",$data);
    }
    
    
    
    public function AddItem(){
        $this->load->library('form_validation');
        
        $this->form_validation->set_rules('txt_title', 'Choice', 'trim|required');               
        
        $data['message'] = "";
		
        if ($this->form_validation->run() == TRUE)
	{         
            $slug = $this->general->SEF_URLS($this->input->post("txt_title"));
            
            $Data = array(            
                "Choice"          => $this->input->post("txt_title"),
                
                                                                                       
                "date_added"            => date("Y-m-d H:i:s"),
                "date_modified"         => date("Y-m-d H:i:s")
            );
                   
           
            
            $this->general->insertRecord($this->tablename, $Data);       
                                                                                                                                    
            $data['message'] = "Choice Added Successfully";
        }
            
       
        $this->load->view(ADMINFOLDER."/addchoice",$data);
    }
    
    
    public function editItem($id){
        $this->load->library('form_validation');
         
        $this->form_validation->set_rules('txt_title', 'Category Title', 'trim|required');        
        
        $data['message'] = "";
		
        if ($this->form_validation->run() == TRUE)
	{                        
            
            
            $Data = array(       
                "$this->primarykey" 	=> $id,
                "Choice"          => $this->input->post("txt_title"),
                                               
                "date_modified"         => date("Y-m-d H:i:s")
            );
                        
            
            
            $this->general->updateRecord($this->tablename, $Data , "$this->primarykey");       
                        
            
            
                                                                        
            $data['message'] = "Choice Updated Successfully";
        }
            
        $data['record'] = $this->general->getRecordWhere($this->tablename,array("$this->primarykey"=>$id), "$this->primarykey","desc");    
        $this->load->view(ADMINFOLDER."/editchoice",$data);                       
    }
    
                
    public function delete($id){        
        $newsRecord = $this->general->getRecordWhere($this->tablename, array("$this->primarykey"=>$id));
			if(count($newsRecord)){
				                            
                                
                                               
                                                                            

				$this->db->query("DELETE FROM ".$this->tablename." WHERE $this->primarykey = $id");
				$data['message'] = $this->general->generateMessage("Record Deleted Successfully !!!", "green");
			}else{
				$data['message'] = $this->general->generateMessage("No record found with the specified id.", "red");
			}        
                        
            $this->index();            
    }
    
                   
}
