<?php

class Fieldsgroups   extends CI_Controller {
    
    var $tablename = FIELDSGROUPS;
    var $primarykey = "FieldCatID";
        
    var $tablename2 = FIELDS;
    var $primarykey2 = "FieldID";
    
    var $tablename3 = FIELDSJOIN;
    
    public function index(){               
        $data['current'] = "fieldsgroups";                  
        $data['groups'] = $this->general->getAllRecordsWhere($this->tablename,array(),"$this->primarykey","desc");
        $this->load->view(ADMINFOLDER."/fieldsgroups",$data);
    }
    
    
    
    public function AddItem(){
        $this->load->library('form_validation');
        
        $this->form_validation->set_rules('txt_title', 'Group Name', 'trim|required');
        
        $data['choices'] = $this->general->getAllRecordsWhere($this->tablename2,array(),"$this->primarykey2","desc");
        
        $data['message'] = "";
		
        if ($this->form_validation->run() == TRUE)
	{                     
            
            $Data = array(            
                "CatName"          => $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);       
            $fid = $this->db->insert_id();
            
            // add fields values ==============
               $choices = $this->input->post("txt_choices");
                foreach ($choices as $ids)  
                {
                    $Data2= array( 
                        "CID"          => $fid,
                        "FID"          => $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'] = "Group Added Successfully";
        }
            
       
        $this->load->view(ADMINFOLDER."/addgroup",$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,
               "CatName"          => $this->input->post("txt_title"),
                                                                               
                                
               "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 CID = $id");
                                  
            $choices = $this->input->post("txt_choices");
            if(isset($choices))
            {
                foreach ($choices as $ids)  
                {
                    $Data2= array( 
                        "CID"          => $id,
                        "FID"          => $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'] = "Group Updated Successfully";
        }
            
        $data['record'] = $this->general->getRecordWhere($this->tablename,array("$this->primarykey"=>$id), "$this->primarykey","desc");    
        $this->load->view(ADMINFOLDER."/editgroup",$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();  
            
    }
    
                   
}
