<?php

class Homeslider   extends CI_Controller {
    
    var $tablename = SLIDER;
    var $primarykey = "id";
        
    
    public function index(){               
        $data['current'] = "slider";                   
        $data['customers'] = $this->general->getAllRecordsWhere($this->tablename,array(""),"$this->primarykey","desc");
        $this->load->view(ADMINFOLDER."/homeslider",$data);
    }
    
   
    
    public function AddItem(){
        $this->load->library('form_validation');
         
        $this->form_validation->set_rules('txt_title', 'Slider Title', 'trim|required');
        $this->form_validation->set_rules('txt_link', 'Slider Link', 'trim');
        
        $data['message'] = "";
		
        if ($this->form_validation->run() == TRUE)
	{         
            $slug = $this->general->SEF_URLS($this->input->post("txt_title"));
            
            $Data = array(            
                "sli_title"          => $this->input->post("txt_title"),
                "sli_url"           => $this->input->post("txt_link"),                
                "slug"=>$slug,          
                
                
                "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);       
            
            
            
            $tablename = $this->tablename;
            $imagefieldid = $this->db->insert_id();
            $imagefieldslist = array("sli_img");


            // ==================== upload image
            $inputName      = "txt_image";            
            if(isset($_FILES[$inputName]) &&  $_FILES[$inputName]['name'] != ""){                                                                                  
            $oldimagename   = "old_image_image";                                    
            $imagefolder    = "homeslider";                                   
            $updatedfield   = "sli_img";                        
            $imageWidth     = "1920";
            $imageHeight    = "855";
            $thumbimageWidth     = "300";
            $thumbimageHeight    = "200";             
            $resultimage = $this->ImageProcess->imageUpload($oldimagename,$slug,$imagefolder,$imageWidth,$imageHeight,$thumbimageWidth,$thumbimageHeight,$inputName,$tablename,$updatedfield,$imagefieldslist,$imagefieldid, $this->primarykey);            
            }
            // ===================================
                        
                        
                        
            $data['message'] = "Slider Added Successfully";
        }
            
       
        $this->load->view(ADMINFOLDER."/addslider",$data);
    }
    
    
    public function editItem($id){
        $this->load->library('form_validation');
         
        $this->form_validation->set_rules('txt_title', 'Slider Title', 'trim|required');
        $this->form_validation->set_rules('txt_link', 'Slider Link', 'trim');
        
        $data['message'] = "";
		
        if ($this->form_validation->run() == TRUE)
	{                        
            
            $slug = $this->general->SEF_URLS($this->input->post("txt_title"));
            $Data = array(       
                "$this->primarykey" 	=> $id,
                
                "sli_title"          => $this->input->post("txt_title"),
                "sli_url"           => $this->input->post("txt_link"),                
                "slug"=>$slug,          
                 
                "is_active"             => $this->input->post("txt_active"),                                 
                "date_modified"         => date("Y-m-d H:i:s")
            );
                        
            
            $this->general->updateRecord($this->tablename, $Data , "$this->primarykey");                               
            
            $tablename = $this->tablename;
            $imagefieldid = $id;
            $imagefieldslist = array("sli_img");


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

				$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();  
            
    }
    
                   
}
