<?php

	defined('BASEPATH') or exit('No Direct Script is Allowed');

	/**
	 * 
	 */
    class BlogController extends CI_Controller
	    {
		    function __construct(){
                parent::__construct();
                    if(!$this->session->id){
                    redirect(site_url('admin/login'));
                }
                $this->load->library('upload');
            }
            
        public function blog(){
                $data['blogs'] = $this->db->where('blog_status', '1')->order_by('blog_title', 'ASC')->get('tbl_blog')->result();
                $this->db->where('adm_id', $this->session->id);
			    $user['data'] = $this->db->get('tbl_admin')->row();	
                    $this->load->view('backend/global/header',$user);
                    $this->load->view('backend/global/menu');
                    $this->load->view('backend/blog/blog_list',$data);
                    $this->load->view('backend/global/footer');
            } 
            
            public function blog_add(){
                $data['blogs'] = $this->db->where('blog_status', '1')->order_by('blog_title', 'ASC')->get('tbl_blog')->result();
                $this->db->where('adm_id', $this->session->id);
			    $user['data'] = $this->db->get('tbl_admin')->row();	
                    $this->load->view('backend/global/header',$user);
                    $this->load->view('backend/global/menu');
                    $this->load->view('backend/blog/blog',$data);
                    $this->load->view('backend/global/footer');
            }
            
            public function blog_add_action(){
                 @mkdir('./uploads/blog/');
				$config['upload_path'] = './uploads/blog/';
				$config['allowed_types'] = 'gif|jpg|png|jpeg';
				$this->load->library('upload', $config);
				$config['file_name'] = strtolower($_FILES['blog_image']['name']);
				$this->upload->initialize($config);
				$this->upload->do_upload('blog_image');
				$upload_data    = $this->upload->data();
				$mainimagefilename  = $upload_data['file_name'];
				$slug = strtolower(trim(preg_replace('/[^A-Za-z0-9-]+/', '-', $this->input->post('blog_title'))));
                $data = array(
						'blog_title' => $this->input->post('blog_title'),
						'blog_short_description' => $this->input->post('blog_short_description'),
						'blog_description' => $this->input->post('blog_description'),
						'blog_slug' => $slug,
						'blog_image' => $mainimagefilename,
					);
				$query= $this->db->insert('tbl_blog', $data);
					if($query){
                        $this->session->set_flashdata('success', 'Sucessfully Added');
                            	redirect('admin/blog');
                    }else{
                        $this->session->set_flashdata('error', 'Something is wrong. Error!!');
                       	redirect('admin/blog');
                    }
                
            } 
            
            public function blog_edit(){
                $this->db->where('blog_id', $this->input->get('bid'));
				$data['blogE'] = $this->db->get('tbl_blog')->row();
			    $this->db->where('adm_id', $this->session->id);
			    $user['data'] = $this->db->get('tbl_admin')->row();		
                    $this->load->view('backend/global/header',$user);
		            $this->load->view('backend/global/menu');
		            $this->load->view('backend/blog/blog',$data);
			        $this->load->view('backend/global/footer');
            } 
            
            public function blog_update(){
                  if(!empty($_FILES['blog_image']['name'])){
            @mkdir('./uploads/blog/');
			    $config['upload_path'] = './uploads/blog/';
				$config['allowed_types'] = 'gif|jpg|png|jpeg';
				$this->load->library('upload', $config);
				$config['file_name'] = strtolower($_FILES['blog_image']['name']);
				$this->upload->initialize($config);
				$this->upload->do_upload('blog_image');
				$upload_data    = $this->upload->data();
				$mainimagefilename  = $upload_data['file_name'];
                $data = array(
						'blog_title' => $this->input->post('blog_title'),
						'blog_short_description' => $this->input->post('blog_short_description'),
						'blog_description' => $this->input->post('blog_description'),
						'blog_image' => $mainimagefilename,
						'blog_status' => $this->input->post('blog_status'),
						'meta_title' => $this->input->post('meta_title'),
						'meta_keyword' => $this->input->post('meta_keyword'),
						'meta_description' => $this->input->post('meta_description'),
					);
						$this->db->where('blog_id', $this->input->post('blog_id'));
							$query =	$this->db->update('tbl_blog', $data);
				 if($query){
                        $this->session->set_flashdata('success', 'Sucessfully Updated');
                            	redirect('admin/blog');
                    }else{
                        $this->session->set_flashdata('error', 'Something is wrong. Error!!');
                       	redirect('admin/blog');
                    }
               
            }else{
                $data = array(
							'blog_title' => $this->input->post('blog_title'),
						'blog_short_description' => $this->input->post('blog_short_description'),
						'blog_description' => $this->input->post('blog_description'),
						'blog_status' => $this->input->post('blog_status'),
						'meta_title' => $this->input->post('meta_title'),
						'meta_keyword' => $this->input->post('meta_keyword'),
						'meta_description' => $this->input->post('meta_description'),
					);
						$this->db->where('blog_id', $this->input->post('blog_id'));
							$query =	$this->db->update('tbl_blog', $data);
				 if($query){
                        $this->session->set_flashdata('success', 'Sucessfully Updated');
                            	redirect('admin/blog');
                    }else{
                        $this->session->set_flashdata('error', 'Something is wrong. Error!!');
                       	redirect('admin/blog');
                    }
            }
            }
           
            public function blog_delete(){
              $this->db->where('blog_id', $this->input->get('bid'));
			    $this->db->delete('tbl_blog');
				redirect('admin/blog');
               
            }    
            
          
	} 
 
 