<?php
if(!defined('BASEPATH')) exit('Hacking Attempt : Will Kill You Do Not Worry..!!');

class Pages extends MY_Controller
{

	var $model ='Pages_model';
	
	public function __construct(){
		parent::__construct();
		if($this->session->userdata('isLogin') == FALSE ){ redirect(base_url('admin/login/login_form'));}	
		$this->load->model('admin/'.$this->model); 
		
		$config['upload_path']          = 'uploads/';
		$config['allowed_types']        = 'gif|jpg|png|jpeg';
		$config['max_size'] 			= 12000;
        $config['max_width'] 			= 13500;
        $config['max_height'] 			= 13500;
		$this->upload->initialize($config);
	}

/*##########################################################*/
/*################ TO PRINT LIST ####################*/
/*##########################################################*/
	public function index(){
			$data['pagetitle']='Pages List';
			if(isset($_POST['status'])){$data['status'] = $_POST['status'];}else{$data['status']='-1';}
			
			$data['response'] = $this->Pages_model->get_general($data['status']);
			
			$this->adminview('pages/list',$data);
	}
	
/*##########################################################*/
/*################ TO ADD / UPDATE DATA ####################*/
/*##########################################################*/
	
	public function publish($para=null){
		
			$formerror=0;
			
			//print_r($_POST);
			if(isset($_POST['submit'])){
				$rules = array(
									array(
											'field' => 'title',
											'label' => 'Page Title',
											'rules' => 'required'
									)
							);

				$this->form_validation->set_rules($rules);
				if ($this->form_validation->run() == FALSE) { 
					$formerror = 1; 
					$this->session->set_flashdata('form_error', '<div class="alert alert-danger text-center"> '.validation_errors().'</div>');
				}else{$formerror = 0; }
				
				if($_POST['imageold'] != $_FILES['image']['name'] && $_FILES['image']['name'] !=''){
					if (!$this->upload->do_upload('image')) {    
						$this->session->set_flashdata('image_error', '<div class="alert alert-danger text-center"> '.$this->upload->display_errors().'</div>');
						$formerror = 1; 
					} 				
					else {  
						$imgdata = $this->upload->data('file_name');  $formerror = 0; 
						$data['image']=$imgdata;		
					}
				}
				
				if($_POST['bannerold'] != $_FILES['banner']['name'] && $_FILES['banner']['name'] !=''){
					if (!$this->upload->do_upload('banner')) {    
						$this->session->set_flashdata('image_error', '<div class="alert alert-danger text-center"> '.$this->upload->display_errors().'</div>');
						$formerror = 1; 
					} 				
					else {  
						$imgdata = $this->upload->data('file_name');  $formerror = 0; 
						$data['banner']=$imgdata;		
					}
				}
				
				if($_POST['id'] ==''){
					if(checkunique('slug',array('slug'=>slugify($_POST['title'])),'pages')){
						$this->session->set_flashdata('image_error', '<div class="alert alert-danger text-center"> Page Already In Database...</div>');
							$formerror = 1; 
					}
				}
				
				if(!$formerror){
					
					$id = $_POST['id'];
					$data['title']=$_POST['title'];
					$data['description']=$_POST['description'];
					$data['template']=$_POST['template'];
					if(isset($_POST['status'])){$data['status']=1;}else{$data['status'] = 0;}
					if($id ==''){$data['createdon']=date('Y-m-d H:i:s');}					
					$data['updatedon']=date('Y-m-d H:i:s');
					if($id ==''){$data['slug']=slugify($_POST['title']);}						
					$id = $this->Pages_model->set_general($data,$id);
					
					$this->session->set_flashdata('page_message', '<div class="alert alert-success text-center"> General Page Updated Successfully...</div>');
					redirect(base_url('admin/pages'));
				}else{
					$data['title']=$_POST['title'];
					$data['description']=$_POST['description'];
					if(@$_POST['status'] == 'on'){$data['status']=1;}else{$data['status'] = 0;}
				}
			
			}
			
			if($para >0){
					
					$response = $this->Pages_model->get_single_general($para);
					$data['id']=$response[0]->pageId;
					$data['title']=$response[0]->title;
					$data['description']=$response[0]->description;
					$data['image']=$response[0]->image;
					$data['banner']=$response[0]->banner;
					$data['status']=$response[0]->status;
					$data['template']=$response[0]->template;
			}
			$data['pagetitle']='Add Pages';	
			$data['templates']=array('blank','home','about','contact','escapades','epiphany','press');	
			$this->adminview('pages/publish',$data);
	}

/*##########################################################*/
/*################ TO DELETE SINGLE ENTRY ####################*/
/*##########################################################*/
	
	public function delete($para = null){
		$this->Pages_model->delete_general($para);
		$this->session->set_flashdata('page_message', '<div class="alert alert-danger text-center"> Selected Entry Deleted Successfully</div>');
		redirect(base_url('admin/pages'));
	}
	
public function seo($para=null){
		
			$formerror=0;
			
			//print_r($_POST);
			if(isset($_POST['submit'])){
				$rules = array(
									array(
											'field' => 'slug',
											'label' => 'Page Slug',
											'rules' => 'required'
									)
							);

				$this->form_validation->set_rules($rules);
				if ($this->form_validation->run() == FALSE) { 
					$formerror = 1; 
					$this->session->set_flashdata('form_error', '<div class="alert alert-danger text-center"> '.validation_errors().'</div>');
				}else{$formerror = 0; }
				
				if($_POST['seoimageold'] != $_FILES['seoimage']['name'] && $_FILES['seoimage']['name'] !=''){
					if (!$this->upload->do_upload('seoimage')) {    
						$this->session->set_flashdata('image_error', '<div class="alert alert-danger text-center"> '.$this->upload->display_errors().'</div>');
						$formerror = 1; 
					} 				
					else {  
						$imgdata = $this->upload->data('file_name');  $formerror = 0; 
						$data['seoimage']=$imgdata;		
					}
				}
				
				if(!$formerror){
					
					$id = $_POST['id'];
					$data['seotitle']=$_POST['seotitle'];
					$data['seodescription']=$_POST['seodescription'];
					$data['seokeywords']=$_POST['seokeywords'];
					$data['table']='pages';	
					$data['slug']=slugify($_POST['slug']);						
					$data['updatedon']=date('Y-m-d H:i:s');					
					$id = $this->Pages_model->set_page_seo($data,$id);				
					$this->session->set_flashdata('page_message', '<div class="alert alert-success text-center"> Page SEO Updated Successfully...</div>');
					redirect(base_url('admin/pages'));
				}else{
					$data['seotitle']=$_POST['seotitle'];
					$data['seodescription']=$_POST['seodescription'];
					$data['seokeywords']=$_POST['seokeywords'];
					$data['slug']=slugify($_POST['slug']);	
				}
			
			}
			
			if($para >0){
					
					$response = $this->Pages_model->get_page_seo($para);
					$data['id']=$response[0]->itemId;
					$data['seotitle']=$response[0]->seotitle;
					$data['seodescription']=$response[0]->seodescription;
					$data['seokeywords']=$response[0]->seokeywords;
					$data['slug']=$response[0]->slug;
					$data['seoimage']=$response[0]->seoimage;
			}
			$data['pagetitle']='Update SEO';			
			$this->adminview('seo_publish',$data);
	}

}	

?>