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

class Data 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'] 			= 12000;
        $config['max_width'] 			= 15500;
        $config['max_height'] 			= 15500;
		$this->upload->initialize($config);
	}

/*######################################################################################################################################*/
/*###################################################### General START ########################################################*/
/*######################################################################################################################################*/
	
/*##########################################################*/
/*################ TO PRINT LIST ####################*/
/*##########################################################*/
	public function index(){
			$data['pagetitle']='Articles List';
			if(isset($_POST['status'])){$data['status'] = $_POST['status'];}else{$data['status']='-1';}
			
			$data['response'] = $this->Articles_model->get_articles($data['status']);
			
			$this->adminview('articles/articles_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' => 'Article Title',
											'rules' => 'required'
									),
									array(
											'field' => 'description',
											'label' => 'Article 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['id'] ==''){
					if(checkunique('slug',array('slug'=>slugify($_POST['title'])),'articles')){
						$this->session->set_flashdata('image_error', '<div class="alert alert-danger text-center"> Article Already In Database...</div>');
							$formerror = 1; 
					}
				}
				
				if(!$formerror){
					
					$id = $_POST['id'];
					$data['title']=$_POST['title'];
					$data['subtitle']=$_POST['subtitle'];
					$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_post($data,$id);
					
					$cat['articlecatIds']=$_POST['articlescatides'];
					$cat['articleId']=$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/articles/data'));
				}else{
					$data['title']=$_POST['title'];
					$data['description']=$_POST['description'];
					$data['title']=$_POST['title'];
					$data['subtitle']=$_POST['subtitle'];
					$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]->articleId;
					$data['title']=$response[0]->title;
					$data['subtitle']=$response[0]->subtitle;
					$data['description']=$response[0]->description;
					$data['image']=$response[0]->image;
					$data['status']=$response[0]->status;
					$catresponse = $this->Articles_model->get_post_cat($response[0]->articleId);
					$data['catids']=$catresponse;
			}
			$data['pagetitle']='Add Articles';			
			$this->adminview('articles/articles_publish',$data);
	}

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

/*######################################################################################################################################*/
/*###################################################### General 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 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']='articles';
					$data['tableId']=$id;
					$data['type']='articles';
					$data['parentId']='articleId';
					$id = $this->Helper_model->set_seo($data);				
					$this->session->set_flashdata('page_message', '<div class="alert alert-success text-center"> Article SEO Updated Successfully...</div>');
					redirect(base_url('admin/articles/data'));
				}else{
					$data['seotitle']=$_POST['seotitle'];
					$data['seodescription']=$_POST['seodescription'];
					$data['seokeywords']=$_POST['seokeywords'];
					$data['slug']=slugify($_POST['slug']);	
				}
			
			}
			
			if($para >0){

					$type='articles';
					$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);
	}
	
}	

?>