<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Blogs extends CI_Controller {
	
	public function __construct()
    {
	    parent::__construct();
		 
	}
	
	
//*******************blog page start *******************//
	
	public function index()
	{
		$all_blog_count =$this->Data_access_model->select_all('hs_blogs');
		$all_blogs_data = $all_blog_count->num_rows(); 
		$no_of_blogs_per_page = 2;

		// config pagination  
		$config = array();
		$config['base_url'] = base_url('blogs/index');
		$config['total_rows'] = $all_blogs_data;
		$config['per_page'] = $no_of_blogs_per_page;
		$config["uri_segment"] = 3;
		// custom paging configuration
		$config['use_page_numbers'] = TRUE;
		$config['full_tag_open'] ='<div class="fd_pagination"><nav aria-label="Page navigation example"> <ul class="pagination">';
		$config['full_tag_close'] ='</ul></nav></div>';
		$config['first_link'] ='first';
		$config['first_tag_open'] ='<li class="page-item"> ';
		$config['first_tag_close'] ='</li>';
		$config['last_link'] = 'last';
		$config['last_tag_open'] ='<li class="page-item"> ';
		$config['last_tag_close'] ='</li>';
		$config['next_link'] ='<img src="'.base_url().'themeassets/images/svg/rightarrow_hover.svg">';
		$config['next_tag_open'] ='<li class="page-item fd_pgborder"><span aria-hidden="true"> ';
		$config['next_tag_close'] ='</span></li>';
		$config['prev_link'] = '<img src="'. base_url().'themeassets/images/svg/leftarrow_hover.svg">';
		$config['prev_tag_open'] ='<li class="page-item fd_pgborder"><span aria-hidden="true"> ';
		$config['prev_tag_close'] ='</span></li>';
		$config['cur_tag_open'] ='<li class="page-item active">';
		$config['cur_tag_close'] ='</li>';
		$config['num_tag_open'] = '<li class="page-item"> ';
		$config['num_tag_close'] = '</li>'; 
		$config['attributes'] = array('class' => 'page-link');  
		$this->pagination->initialize($config);

		if($this->uri->segment(3)){
			
			$page =($this->uri->segment(3));
			
		}else
		{
			$page = 1;
		}

		$offset=($page-1)* $no_of_blogs_per_page; 
		$user_data['serial_no'] = $offset;

		$blog_query = $this->Data_access_model->count_items_withlimit('*','comment_id','countcomment','hs_comment','hs_blogs','blog_id = comment_blog_id',$config['per_page'],$offset,'blog_id');
		if($blog_query->num_rows() > 0)
		{
			$data['all_blogs'] =$blog_query->result();
		}else{ 
			$data['all_blogs'] = "";
		}
		
		$opening_time_query =$this->Data_access_model->select_all('hs_opening_schedule');
		if($opening_time_query->num_rows() > 0){
			$setting_data['opening_time'] =$opening_time_query->result();
		}else{ 
			$setting_data['opening_time'] = "";
		}
		
		$partner_query=$this->Data_access_model->select_data('*','hs_partners',array( 'partner_status' =>1,));
		if($partner_query->num_rows() > 0){
			   
			$setting_data['all_partners'] =$partner_query->result();
			 
		}else{ 
			$setting_data['all_partners'] = "";
			  
		}
		
		$count_blogs = $this->Data_access_model->count_items_where('*','blog_id','countblog','hs_blogs',array('cate_status' =>1),'hs_blog_category','cate_id =blog_category','cate_id');
		if(!empty($count_blogs)){
			$data['all_blogcategory']=$count_blogs->result();
		}else{
			$data['all_blogcategory']= 0;
		} 
		
		$offer_query=$this->Data_access_model->select_data('*','hs_offers',array( 'offer_status' =>1,));
		if($offer_query->num_rows() > 0){
			   
			$data['all_offer'] =$offer_query->result();
			 
		}else{ 
			$data['all_offer'] = "";
			  
		}
		$data['page_title'] = 'Blogs';
		$data['page_descri'] ='All Blogs';
		$data['add_active'] = 6;
		$this->load->view('theme/common/header',$data);
		$this->load->view('theme/blog',$data);
		$this->load->view('theme/common/footer',$setting_data);
	}
//*******************blog page end *******************//
	
	
//*******************blog single page start ****************//	
	public function single()
	{
		$rand_testimonial = $this->Data_access_model->select_random_record('*','hs_testimonial','test_id',1);
		if($rand_testimonial->num_rows() >0){
			 $data['randtestimonial']= $rand_testimonial->result();
		}else{
			$data['randtestimonial']= '';
		}		 
		 
		$opening_time_query =$this->Data_access_model->select_all('hs_opening_schedule');
		if($opening_time_query->num_rows() > 0){
			   
		    $setting_data['opening_time'] =$opening_time_query->result();
			 
		}else{ 
			$setting_data['opening_time'] = "";
		}
		
		$partner_query=$this->Data_access_model->select_data('*','hs_partners',array( 'partner_status' =>1,));
		if($partner_query->num_rows() > 0){
			$setting_data['all_partners'] =$partner_query->result();
		}else{ 
			$setting_data['all_partners'] = "";
		}
		
		if($this->uri->segment(3) !== ''){
	      
			$blog_slug = $this->uri->segment(3);
			$query_blog=$this->Data_access_model->select_data('*','hs_blogs',array('blog_slug' =>$blog_slug));
			if($query_blog->num_rows() > 0)
			{
				$blog_details = $query_blog->row();
				$blog_id = $blog_details->blog_id;
				$data['page_title'] = $blog_details->blog_title;
				$data['page_descri'] = $blog_details->blog_content;
				$data['single_blog'] = $query_blog->result();
			     
			}else{
				$data['single_blog'] = ''; 
			}
			if(isset($blog_id))
			{
				$query_comment=$this->Data_access_model->select_data('*','hs_comment',array('comment_blog_id' =>$blog_id));
				if($query_comment->num_rows() > 0){
					$data['singleblog_comment'] =$query_comment->result();
				}else{
					$data['singleblog_comment'] = ''; 
				}
		    }
		}
		
		$count_blogs = $this->Data_access_model->count_items_where('*','blog_id','countblog','hs_blogs',array('cate_status' =>1),'hs_blog_category','cate_id =blog_category','cate_id');
		if($count_blogs->num_rows()){
			$data['all_blogcategory']=$count_blogs->result();
		}else{
			$data['all_blogcategory']= 0;
		}
	 
		$offer_query=$this->Data_access_model->select_data('*','hs_offers',array( 'offer_status' =>1,));
		if($offer_query->num_rows() > 0){
			   
		    $data['all_offer'] =$offer_query->result();
			 
		}else{ 
			$data['all_offer'] = "";
			  
		}
		$data['add_active'] = 6;
		$this->load->view('theme/common/header',$data);
		$this->load->view('theme/blog-single',$data);
		$this->load->view('theme/common/footer',$setting_data);
	}
//*******************blog single page end *******************//	
	
//*******************display blog bycategory section start *******************//	
	
	public function display_blog_bycategory()
	{
		$blog_cate_id = $this->input->post('blogcate_id');
	 
		if(isset($blog_cate_id) && ($blog_cate_id !==''))
		{
			if($blog_cate_id ==0){
				$blog_query=$this->Data_access_model->select_data('*','hs_blogs',array('blog_status' =>1,)); 
			}else{
				$blog_query=$this->Data_access_model->select_data('*','hs_blogs',array('blog_category'=>$blog_cate_id, 'blog_status' =>1,));
			}  
		
			if($blog_query->num_rows() > 0)
			{
				$data = $blog_query->result();  
				$response ='';
				foreach($data as $blogs)
				{   
					$query_comment=$this->Data_access_model->select_data('count(*) as totalcomment','hs_comment',array('comment_blog_id' =>$blogs->blog_id));
					if(!empty($query_comment))
					{
						$data['singleblog_comment'] =$query_comment->result(); 
						$total_singleblog_comment = $data['singleblog_comment'][0]->totalcomment;
					}else{
						$total_singleblog_comment = 0;					
					}
					
					$blogdate = strtotime($blogs->blog_date);
					$day = date('d', $blogdate);
					$month = date('M', $blogdate);
					$response .='<div class="fd_blog_slider btm_padder60"><div class="fd_post_box">
								<div class="fd_post_img">
								<img src="'.base_url().$blogs->blogsingle_image.'" alt="">
								<div class="fd_post_date_box">
								<div class="fd_date_inner">
								<span class="pst_date">'.$day.'</span>
								<span class="pst_month">'.$month.'</span>
								</div>
								</div>
								</div>
								<div class="fd_post_data">
								<h2><a href="'.base_url().'blogs/single/'.$blogs->blog_slug.'">'.$blogs->blog_title.'</a></h2>
								<ul class="fd_pst_info">
								<li><a href="'.base_url().'blogs/single/'.$blogs->blog_slug.'"><img src="'. base_url().'themeassets/images/userpic.png" alt=""> - '.$this->hs->get_userdetails('user_name',$blogs->blog_userid).' </a></li>
								<li><a href="'.base_url().'blogs/single/'.$blogs->blog_slug.'"><i class="fa fa-commenting-o" aria-hidden="true"></i> '.$total_singleblog_comment.' Comment</a></li>
								</ul>
								<p>'.$blogs->blog_content.'</p>
								<a href="'.base_url().'blogs/single/'.$blogs->blog_slug.'" class="view_link">Read More <i class="fa fa-long-arrow-right" aria-hidden="true"></i></a>
							</div>
						</div>
					</div>';  
				} 
				echo  json_encode($response);   
			}else{ 
				$response = 0;
				echo json_encode($response); 
			} 
		} 
	}
	//***********display blog bycategory section end *********//	


}