<?php

class Fields   extends CI_Controller {
    
    var $tablename = FIELDS;
    var $primarykey = "FieldID";
        
    var $tablename2 = CHOICES;
    var $primarykey2 = "ChoiceID";
    
    var $tablename3 = CHOICESJOIN;
    
    public function index(){               
        $data['current'] = "fields";                  
        $data['fields'] = $this->general->getAllRecordsWhere($this->tablename,array(),"$this->primarykey","desc");
        $this->load->view(ADMINFOLDER."/fields",$data);
    }
    
    
    
    public function AddItem(){
        $this->load->library('form_validation');
        
        $this->form_validation->set_rules('txt_title', 'Field Label', 'trim|required');
        
        $data['choices'] = $this->general->getAllRecordsWhere($this->tablename2,array(),"$this->primarykey2","desc");
        
        $data['message'] = "";
		
        if ($this->form_validation->run() == TRUE)
	{                     
            
            $Data = array(            
                "FieldLabel"          => $this->input->post("txt_title"),
                "FieldType"           => $this->input->post("txt_type"),
                                                               
                "is_active"             => $this->input->post("txt_active"),                 
                "date_added"            => date("Y-m-d H:i:s"),
                "date_modified"         => date("Y-m-d H:i:s")
            );
                   
           
            
            $this->general->insertRecord($this->tablename, $Data);       
            $fid = $this->db->insert_id();
            
            // add choices values ==============
               $choices = $this->input->post("txt_choices");
                foreach ($choices as $ids)  
                {
                    $Data2= array( 
                        "FID"          => $fid,
                        "CHID"          => $ids,
                        "date_added"            => date("Y-m-d H:i:s"),
                        "date_modified"         => date("Y-m-d H:i:s")
                        );
                    $this->general->insertRecord($this->tablename3, $Data2); 
                }               
            // =================================
            
            
                                                                                                
            $data['message'] = "Field Added Successfully";
        }
            
       
        $this->load->view(ADMINFOLDER."/addfield",$data);
    }
    
    
    public function editItem($id){
        $this->load->library('form_validation');
         
        $this->form_validation->set_rules('txt_title', 'Field Label', 'trim|required');
        
        $data['choices'] = $this->general->getAllRecordsWhere($this->tablename2,array(),"$this->primarykey2","desc");
        
        $data['message'] = "";
		
        if ($this->form_validation->run() == TRUE)
	{                        
            
           
            $Data = array(       
                "$this->primarykey" 	=> $id,
                "FieldLabel"            => $this->input->post("txt_title"),
                "FieldType"             => $this->input->post("txt_type"),
                                                               
                "is_active"             => $this->input->post("txt_active"),                                 
                "date_modified"         => date("Y-m-d H:i:s")
            );
                        
           
            
            $this->general->updateRecord($this->tablename, $Data , "$this->primarykey");       
                        
            
           // update choices =============================
            
            $this->db->query("DELETE FROM ".$this->tablename3." WHERE FID = $id");
            
            $choices = $this->input->post("txt_choices");
            if(isset($choices))
            {
                foreach ($choices as $ids)  
                {
                    $Data2= array( 
                        "FID"          => $id,
                        "CHID"          => $ids,
                        "date_added"            => date("Y-m-d H:i:s"),
                        "date_modified"         => date("Y-m-d H:i:s")
                        );
                    $this->general->insertRecord($this->tablename3, $Data2); 
                }           
            }
            //============================================
                        
                        
            $data['message'] = "Field Updated Successfully";
        }
            
        $data['record'] = $this->general->getRecordWhere($this->tablename,array("$this->primarykey"=>$id), "$this->primarykey","desc");    
        $this->load->view(ADMINFOLDER."/editfield",$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();  
            
    }
    
                   
}
