<?php

class Categories   extends CI_Controller {
    
    var $tablename = CATEGORIES;
    var $primarykey = "CategoryID";
        
    
    public function index(){               
        $data['current'] = "categories";          
        $this->session->unset_userdata('pid');      
        $data['customers'] = $this->general->getAllRecordsWhere($this->tablename,array("PCategoryID"=>0),"$this->primarykey","asc");
        $this->load->view(ADMINFOLDER."/categories",$data);
    }
    
    public function viewSubCategory($pid){               
        $data['current'] = "categories";      
        $this->session->set_userdata('pid', $pid);
        $data['customers'] = $this->general->getAllRecordsWhere($this->tablename,array("PCategoryID"=>$pid),"$this->primarykey","asc");
        $this->load->view(ADMINFOLDER."/categories",$data);
    }
    
    public function AddItem(){
        $this->load->library('form_validation');
         
        $this->form_validation->set_rules('txt_title', 'Category Title', 'trim|required');
        $this->form_validation->set_rules('txt_desc', 'Category Description', 'trim');
        
        $data['message'] = "";
		
        if ($this->form_validation->run() == TRUE)
	{         
            $slug = $this->general->SEF_URLS($this->input->post("txt_title"));
            
            $Data = array(            
                "CategoryName"          => $this->input->post("txt_title"),
                "Description"           => $this->input->post("txt_desc"),
                
                "slug"=>$slug,
                //"ar_slug"=>$this->general->SEF_URLS2($this->input->post("ar_txt_page_slug")),
                "FGID"          => $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")
            );
                   
            if($this->session->userdata("pid") != "")
            {
                $Data["PCategoryID"] = $this->session->userdata("pid");
            }
            
            $this->general->insertRecord($this->tablename, $Data);       
            
            
            
            $tablename = $this->tablename;
            $imagefieldid = $this->db->insert_id();
            $imagefieldslist = array("pic");


            // ==================== upload image
            $inputName      = "txt_image";            
            if(isset($_FILES[$inputName]) &&  $_FILES[$inputName]['name'] != ""){                                                                                  
            $oldimagename   = "old_image_image";                                    
            $imagefolder    = "categories";                                   
            $updatedfield   = "pic";                        
            $imageWidth     = "400";
            $imageHeight    = "400";
            $thumbimageWidth     = "100";
            $thumbimageHeight    = "100";             
            $resultimage = $this->ImageProcess->imageUpload($oldimagename,$slug,$imagefolder,$imageWidth,$imageHeight,$thumbimageWidth,$thumbimageHeight,$inputName,$tablename,$updatedfield,$imagefieldslist,$imagefieldid, $this->primarykey);            
            }
            // ===================================
                        
                        
                        
            $data['message'] = "Category Added Successfully";
        }
            
       
        $this->load->view(ADMINFOLDER."/addcategory",$data);
    }
    
    
    public function editItem($id){
        $this->load->library('form_validation');
         
        $this->form_validation->set_rules('txt_title', 'Category Title', 'trim|required');
        $this->form_validation->set_rules('txt_desc', 'Category Description', 'trim');
        
        $data['message'] = "";
		
        if ($this->form_validation->run() == TRUE)
	{                        
            
            $slug = $this->general->SEF_URLS($this->input->post("txt_title"));
            $Data = array(       
                "$this->primarykey" 	=> $id,
                "CategoryName"          => $this->input->post("txt_title"),
                "Description"           => $this->input->post("txt_desc"),
                "slug"                  => $slug,
                "FGID"                  => $this->input->post("txt_type"),
                "is_active"             => $this->input->post("txt_active"),                                 
                "date_modified"         => date("Y-m-d H:i:s")
            );
                        
            if($this->session->userdata("pid") != "")
            {
                $Data["PCategoryID"] = $this->session->userdata("pid");
            }
            
            $this->general->updateRecord($this->tablename, $Data , "$this->primarykey");       
                        
            
            $tablename = $this->tablename;
            $imagefieldid = $id;
            $imagefieldslist = array("pic");


            // ==================== upload image
            $inputName      = "txt_image";            
            if(isset($_FILES[$inputName]) &&  $_FILES[$inputName]['name'] != ""){                                                                                  
            $oldimagename   = "old_image_image";                                    
            $imagefolder    = "categories";                                   
            $updatedfield   = "pic";                        
            $imageWidth     = "400";
            $imageHeight    = "400";
            $thumbimageWidth     = "100";
            $thumbimageHeight    = "100";             
            $resultimage = $this->ImageProcess->imageUpload($oldimagename,$slug,$imagefolder,$imageWidth,$imageHeight,$thumbimageWidth,$thumbimageHeight,$inputName,$tablename,$updatedfield,$imagefieldslist,$imagefieldid, $this->primarykey);            
            }
            // ===================================
                        
                        
                        
            $data['message'] = "Category Updated Successfully";
        }
            
        $data['record'] = $this->general->getRecordWhere($this->tablename,array("$this->primarykey"=>$id), "$this->primarykey","desc");    
        $this->load->view(ADMINFOLDER."/editcategory",$data);                       
    }
    
                
    public function delete($id){        
        $newsRecord = $this->general->getRecordWhere($this->tablename, array("$this->primarykey"=>$id));
			if(count($newsRecord)){
				                            
                                
                                                if(file_exists("images/categories/".$newsRecord->pic)){
                                                @unlink("images/categories/".$newsRecord->pic);
                                                }
                                            
                                                if(file_exists("images/categories/".$this->ImageProcess->returnThumb($newsRecord->pic))){
                                                @unlink("images/categories/".$this->ImageProcess->returnThumb($newsRecord->pic));
                                                }
                                                                            

				$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");
			}        
                        
            if($this->session->userdata("pid") != "")
            {                
                $this->viewSubCategory($this->session->userdata("pid"));
            }
            else {  $this->index();  }
            
    }
    
                   
}
