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

class Blog_m extends CI_Model {
	public function __construct(){
		parent::__construct();
		$this->db->query("SET sql_mode = ''");
	}
	public function get_all_post()
	{
		$this->db->select('p.*');
		$this->db->select('cat.cat_name, cat.id as cat_id');
		$this->db->from('post as p');
		$this->db->join('categories as cat ','cat.id = p.cat_id','LEFT');
        $this->db->where('p.user_id',auth('id'));
		$query = $this->db->get();
		$query = $query->result_array();
		foreach ($query as $key => $value) {
			$this->db->select('tg.id as tag_id,t.tag_name');
            $this->db->from('post_tags as tg');
            $this->db->join('tags as t ','t.id = tg.tag_id','LEFT');
            $this->db->where('post_id',$value['id']);
            $query2 = $this->db->get();
            $query2 = $query2->result_array();
            $query[$key]['tags'] = $query2;
        }
		return $query;

	}

	public function get_post_tags($id)
	{
		$this->db->select('pt.*,t.tag_name');
		$this->db->from('post_tags as pt');
		$this->db->join('tags as t ','t.id = pt.tag_id','LEFT');
		$this->db->where('post_id',$id);
		$query = $this->db->get();
		$query = $query->result_array();
		return $query;
	}



	function tag_delete($id,$table){
		$this->db->delete($table,array('post_id'=>$id));
		return $id;
	}



	public function insert($data,$table)
	{
		$this->db->insert($table,$data);
		return $this->db->insert_id();
	}
	public function update($data,$id,$table)
	{
		$this->db->where('id',$id);
		$this->db->update($table,$data);
		return $id;
	}

	/*  blog user font page
	================================================== */

	public function get_my_all_post($id,$limit,$type,$order)
	{
		$this->db->select('p.*');
		$this->db->select('cat.cat_name, cat.id as cat_id,cat.slug as cat_slug');
		$this->db->from('post as p');
		$this->db->join('categories as cat ','cat.id = p.cat_id','LEFT');
		$this->db->where('p.status',1);
		$ids = array(1,2);
		$this->db->where_in('p.post_type',$ids);
		$this->db->where('p.is_schedule',0);
		$this->db->where('p.user_id',$id);
		$this->db->order_by($type,$order);
		if($limit !=0){
			$this->db->limit($limit);
		}
		$query = $this->db->get();
		$query = $query->result_array();
		foreach ($query as $key => $value) {
			$this->db->select('tg.id as tag_id,t.tag_name,t.slug as tag_slug');
	        $this->db->from('post_tags as tg');
	        $this->db->join('tags as t ','t.id = tg.tag_id','LEFT');
	        $this->db->where('post_id',$value['id']);
	        $query2 = $this->db->get();
	        $query2 = $query2->result_array();
	        $query[$key]['tags'] = $query2;
	    }
		return $query;

	}

	public function all_posts_by_category($id,$cat_slug,$limit,$type,$order)
	{
		$this->db->select('p.*');
		$this->db->select('cat.cat_name, cat.id as cat_id');
		$this->db->from('post as p');
		$this->db->join('categories as cat ','cat.id = p.cat_id','LEFT');
		$this->db->where('p.status',1);
		$ids = array(1,2);
		$this->db->where_in('p.post_type',$ids);
		$this->db->where('p.is_schedule',0);
		$this->db->where('p.user_id',$id);
		$this->db->where('cat.slug',$cat_slug);
		$this->db->order_by($type,$order);
		if($limit !=0){
			$this->db->limit($limit);
		}
		$query = $this->db->get();
		$query = $query->result_array();
		foreach ($query as $key => $value) {
			$this->db->select('tg.id as tag_id,t.tag_name,t.slug as tag_slug');
	        $this->db->from('post_tags as tg');
	        $this->db->join('tags as t ','t.id = tg.tag_id','LEFT');
	        $this->db->where('post_id',$value['id']);
	        $query2 = $this->db->get();
	        $query2 = $query2->result_array();
	        $query[$key]['tags'] = $query2;
	    }
		return $query;

	}

	public function get_single_post_by_tag($id,$tag_slug,$limit,$type,$order)
	{	

	$this->db->select('p.*');
	$this->db->select('cat.cat_name, cat.id as cat_id,cat.slug as cat_slug');
	$this->db->select('pt.id as post_tag_id,pt.post_id');
	$this->db->select('tg.id as tag_id,tg.slug as tag_slug,tg.tag_name');
	$this->db->from('post as p');
	$this->db->join('categories as cat ','cat.id = p.cat_id','LEFT');
	$this->db->join('post_tags as pt ','pt.post_id = p.id','LEFT');
	$this->db->join('tags as tg ','tg.id = pt.tag_id','LEFT');
	$this->db->where('p.user_id',$id);
	$this->db->where('tg.slug',$tag_slug);
	$ids = array(1,2);
	$this->db->where_in('p.post_type',$ids);
	$this->db->order_by($type,$order);
	if($limit !=0){
		$this->db->limit($limit);
	}
	$query = $this->db->get();
	$query = $query->result_array();
	foreach ($query as $key => $value) {
		$this->db->select('tg.id as tag_id,t.tag_name,t.slug as tag_slug');
        $this->db->from('post_tags as tg');
        $this->db->join('tags as t ','t.id = tg.tag_id','LEFT');
        $this->db->where('post_id',$value['id']);
        $query2 = $this->db->get();
        $query2 = $query2->result_array();
        $query[$key]['tags'] = $query2;
    }
	return $query;
}

	public function get_single_post_by_slug($slug)
	{
		$this->db->select('p.*');
		$this->db->select('cat.cat_name, cat.id as cat_id,cat.slug as cat_slug');
		$this->db->from('post as p');
		$this->db->join('categories as cat ','cat.id = p.cat_id','LEFT');
		$this->db->where('p.slug',utf8_encode($slug));
		$this->db->limit(1);
		$query = $this->db->get();
		$query = $query->result_array();
		foreach ($query as $key => $value) {
			$this->db->select('tg.id as tag_id,t.tag_name,t.slug as tag_slug');
	        $this->db->from('post_tags as tg');
	        $this->db->join('tags as t ','t.id = tg.tag_id','LEFT');
	        $this->db->where('post_id',$value['id']);
	        $query2 = $this->db->get();
	        $query2 = $query2->result_array();
	        $query[$key]['tags'] = $query2;
	    }
		return $query;

	}

/** 
  ***  get all schedule post
**/ 

public function get_schedule_post()
{
	$this->db->select('p.*');
	$this->db->from('post as p');
	$this->db->where('p.is_schedule',1);
	$this->db->where('p.schedule_time !=','0000-00-00 00:00:00');
	$query = $this->db->get();
	$query = $query->result_array();
	return $query;
}
	

}

