<?php

class Pages   extends CI_Controller {
    
    var $tablename = PAGES;
    var $primarykey = "id";
        
    
    public function index(){               
        $data['current'] = "pages";       
        $this->session->unset_userdata('pid');      
        $data['customers'] = $this->general->getAllRecordsWhere($this->tablename,array("pid"=>0),"$this->primarykey","asc");
        $this->load->view(ADMINFOLDER."/pages",$data);
    }
    
   public function viewSubCategory($pid){               
        $data['current'] = "pages";      
        $this->session->set_userdata('pid', $pid);
        $data['customers'] = $this->general->getAllRecordsWhere($this->tablename,array("pid"=>$pid),"$this->primarykey","asc");
        $this->load->view(ADMINFOLDER."/pages",$data);
    }
    
    public function AddItem(){
        $this->load->library('form_validation');
         
        $this->form_validation->set_rules('txt_title', 'Page Title', 'trim|required');
        $this->form_validation->set_rules('txt_desc', 'Page Description', 'trim');
        
        $data['message'] = "";
		
        if ($this->form_validation->run() == TRUE)
	{         
            $slug = $this->general->SEF_URLS($this->input->post("txt_title"));
            
            $Data = array(            
                "menu_title"          => $this->input->post("txt_title"),
                "page_content"           => $this->input->post("txt_desc"),                
                "menu_slug"=>$slug,          
                
                "page_title"          => $this->input->post("txt_pagetitle"),
                "page_keywords"          => $this->input->post("txt_pagekeywords"),
                "page_description"          => $this->input->post("txt_pagedesc"),                                                                                                                                               
                
                
                "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["pid"] = $this->session->userdata("pid");
            }
            
            $this->general->insertRecord($this->tablename, $Data);       
            
            
            
            $tablename = $this->tablename;
            $imagefieldid = $this->db->insert_id();
            $imagefieldslist = array("banner_image");


            // ==================== upload image
            $inputName      = "txt_image";            
            if(isset($_FILES[$inputName]) &&  $_FILES[$inputName]['name'] != ""){                                                                                  
            $oldimagename   = "old_image_image";                                    
            $imagefolder    = "pages";                                   
            $updatedfield   = "banner_image";                        
            $imageWidth     = "1920";
            $imageHeight    = "300";
            $thumbimageWidth     = "300";
            $thumbimageHeight    = "100";             
            $resultimage = $this->ImageProcess->imageUpload($oldimagename,$slug,$imagefolder,$imageWidth,$imageHeight,$thumbimageWidth,$thumbimageHeight,$inputName,$tablename,$updatedfield,$imagefieldslist,$imagefieldid, $this->primarykey);            
            }
            // ===================================
                        
                        
                        
            $data['message'] = "Page Added Successfully";
        }
            
       
        $this->load->view(ADMINFOLDER."/addpage",$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,
                
                "menu_title"          => $this->input->post("txt_title"),
                "page_content"           => $this->input->post("txt_desc"),                
                "menu_slug"=>$slug,                                
                "page_title"          => $this->input->post("txt_pagetitle"),
                "page_keywords"          => $this->input->post("txt_pagekeywords"),
                "page_description"          => $this->input->post("txt_pagedesc"),                                                                                                                                                                                                       
                
                "is_active"             => $this->input->post("txt_active"),                                 
                "date_modified"         => date("Y-m-d H:i:s")
            );
                        
            if($this->session->userdata("pid") != "")
            {
                $Data["pid"] = $this->session->userdata("pid");
            }
            
            $this->general->updateRecord($this->tablename, $Data , "$this->primarykey");                               
            
            $tablename = $this->tablename;
            $imagefieldid = $id;
            $imagefieldslist = array("banner_image");


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

				$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");
			}        
                        
             // check if category have sub before delete           
                        
            if($this->session->userdata("pid") != "")
            {                
                $this->viewSubCategory($this->session->userdata("pid"));
            }
            else {  $this->index();  }
            
    }
    
                   
}
