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

class Category extends MY_Controller
{

	var $model ='Articles_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'] 			= 2000;
        $config['max_width'] 			= 1500;
        $config['max_height'] 			= 1500;
		$this->upload->initialize($config);
	}

	
/*######################################################################################################################################*/
/*###################################################### Blog Category START ########################################################*/
/*######################################################################################################################################*/
	
/*##########################################################*/
/*################ TO PRINT LIST ####################*/
/*##########################################################*/
	public function index(){
			$data['pagetitle']='Articles Category List';
			if(isset($_POST['status'])){$data['status'] = $_POST['status'];}else{$data['status']='-1';}
			
			$data['response'] = $this->Articles_model->get_category($data['status']);
			
			$this->adminview('articles/category_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' => 'Category Name',
											'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'])),'article_category')){
						$this->session->set_flashdata('image_error', '<div class="alert alert-danger text-center"> Article Category Already In Database...</div>');
							$formerror = 1; 
					}
				}
				
				if(!$formerror){
					$id = $_POST['id'];
					$data['title']=$_POST['title'];
					$data['description']=$_POST['description'];
					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->Articles_model->set_category($data,$id);
					$this->session->set_flashdata('page_message', '<div class="alert alert-success text-center"> Article Category Updated Successfully...</div>');

					redirect(base_url('admin/articles/category'));
				}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->Articles_model->get_single_category($para);
					$data['id']=$response[0]->articlecatId;
					$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['pagetitle']='Add Article Category';			
			$this->adminview('articles/category_publish',$data);
	}

/*##########################################################*/
/*################ TO DELETE SINGLE ENTRY ####################*/
/*##########################################################*/
	
	public function delete($para = null){
		$this->Articles_model->delete_category($para);
		$this->session->set_flashdata('page_message', '<div class="alert alert-danger text-center"> Selected Article Category Deleted Successfully</div>');
		redirect(base_url('admin/articles/category'));
	}
	

/*######################################################################################################################################*/
/*###################################################### Blog Category END ########################################################*/
/*######################################################################################################################################*/

/*######################################################################################################################################*/
/*###################################################### Blog Post START ########################################################*/
/*######################################################################################################################################*/
	
/*##########################################################*/
/*################ TO PRINT LIST ####################*/
/*##########################################################*/
	public function post(){
			$data['pagetitle']='Blog Posts List';
			if(isset($_POST['status'])){$data['status'] = $_POST['status'];}else{$data['status']='-1';}
			
			$data['response'] = $this->Articles_model->get_post($data['status']);
			
			$this->adminview('blogs/post_list',$data);
	}
	
/*##########################################################*/
/*################ TO ADD / UPDATE DATA ####################*/
/*##########################################################*/
	
	public function post_publish($para=null){
		
			$formerror=0;
			
			//print_r($_POST);
			if(isset($_POST['submit'])){
				$rules = array(
									array(
											'field' => 'title',
											'label' => 'Post Title',
											'rules' => 'required'
									),
									array(
											'field' => 'description',
											'label' => 'Post Description',
											'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'])),'blog_post')){
						$this->session->set_flashdata('image_error', '<div class="alert alert-danger text-center"> Blog Post Already In Database...</div>');
							$formerror = 1; 
					}
				}
				
				if(!$formerror){
					
					$id = $_POST['id'];
					$data['title']=$_POST['title'];
					$data['description']=$_POST['description'];
					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');
					$data['slug']=slugify($_POST['title']);						
					$id = $this->Articles_model->set_post($data,$id);
					
					$cat['blogcatIds']=$_POST['blogcatides'];
					$cat['blogId']=$id;
					$this->Articles_model->set_post_cat($cat);
					
					$this->session->set_flashdata('page_message', '<div class="alert alert-success text-center"> Blog Post Updated Successfully...</div>');
					redirect(base_url('admin/blogs/post'));
				}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->Articles_model->get_single_post($para);
					$data['id']=$response[0]->blogId;
					$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;
					$catresponse = $this->Articles_model->get_post_cat($response[0]->blogId);
					$data['catids']=$catresponse;
			}
			$data['pagetitle']='Add Blog Category';			
			$this->adminview('blogs/post_publish',$data);
	}

/*##########################################################*/
/*################ TO DELETE SINGLE ENTRY ####################*/
/*##########################################################*/
	
	public function post_delete($para = null){
		$this->Articles_model->delete_post($para);
		$this->session->set_flashdata('page_message', '<div class="alert alert-danger text-center"> Selected Entry Deleted Successfully</div>');
		redirect(base_url('admin/blogs/post'));
	}
	

/*######################################################################################################################################*/
/*###################################################### Blog Post END ########################################################*/
/*######################################################################################################################################*/

public function seo($para=null){
			$this->load->model('Helper_model');
			$formerror=0;
			
			//print_r($_POST);
			if(isset($_POST['submit'])){
				$rules = array(
									array(
											'field' => 'slug',
											'label' => 'Article Category 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']='article_category';	
					$data['slug']=slugify($_POST['slug']);						
					$data['updatedon']=date('Y-m-d H:i:s');	
					$data['table']='article_category';
					$data['tableId']=$id;
					$data['type']='article_category';
					$data['parentId']='articlecatId';
					$id = $this->Helper_model->set_seo($data);				
					$this->session->set_flashdata('page_message', '<div class="alert alert-success text-center"> Article Category SEO Updated Successfully...</div>');
					redirect(base_url('admin/articles/category'));
				}else{
					$data['seotitle']=$_POST['seotitle'];
					$data['seodescription']=$_POST['seodescription'];
					$data['seokeywords']=$_POST['seokeywords'];
					$data['slug']=slugify($_POST['slug']);	
				}
			
			}
			
			if($para >0){

					$type='article_category';
					$response = $this->Helper_model->get_seo($para,$type);					
					$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);
	}
	
}	

?>