<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
	
	class Mod_blog extends CI_Model{
		
		function getarticle($p=1,$id="")
		{	
			$this->db->SELECT("*");
			$this->db->from("blog_posts");
			$this->db->where("postStatus",1);
			$this->db->join("tbluser","tbluser.userid = blog_posts.postBy","left");	
			$this->db->join("blog_cats","blog_cats.catID = blog_posts.postBy","left");	
			$this->db->order_by('postID','DESC');	
			
			if($p==1){
				$start=0;	
			}else{
				$start=($p*10)-10;			
			}
			
			$count=10;
			if($id==""){
				$this->db->limit($count,$start);
			} 	
			
			return $this->db->get();
		}
		
		
		function article_countAll()
		{
			$this->db->where("postStatus",1);
			return $this->db->count_all_results('blog_posts');
		}
		
		
		function tags_countAll($id)
		{
			$this->db->where("tagID",$id);
			return $this->db->count_all_results('blog_tags');
		}
		
		
		function getTags($id)
		{
			$this->db->SELECT("*");
			$this->db->from("blog_post_tags");
			$this->db->where("tagSlug",$id);
			//return $this->db->get();
			$q = $this->db->get();
			if($q->num_rows() > 0)
			{
				return $q->row();
			}
			return false;	
			
			
		}
		
		
		/*function getByTags($id,$b=1,$ID="")
		{
			$this->db->SELECT("*");
			$this->db->from("blog_posts_seo");
			$this->db->where("postTags",$id);
			$this->db->join("blog_members","blog_members.memberID = blog_posts_seo.postBy","left");	
			$this->db->order_by('postID','DESC');
			
			if($b==1){
				$start=0;	
			}else{
			$start=($b*10)-10;			
			}
			$count=10;
			if($ID==""){
			$this->db->limit($count,$start);
			
			} 	
			
			return $this->db->get();
			
		}*/
		
		function getByTags($tagID,$b=1,$id="")
		{
			$this->db->SELECT("*");
			$this->db->from("blog_tags");
			$this->db->join('blog_posts_seo', 'blog_posts_seo.postID= blog_tags.postID','left');
			$this->db->join("blog_members","blog_members.memberID = blog_posts_seo.postBy","left");	
			$this->db->where("blog_tags.tagID",$tagID);
			$this->db->order_by('blog_posts_seo.postID','DESC');
			
			if($this->uri->segment(3))
			{
				$b = ($this->uri->segment(3)) ;
			}
			
			if($b==1){
				$start=0;	
			}else{
				$start=($b*10)-10;			
			}
			$count=10;
			
			if($id==""){
				$this->db->limit($count,$start);
			} 	
			
			return $this->db->get();	
		}
		
		/*function showTags($id)
		{
			$this->db->SELECT("*");
			$this->db->from("blog_posts_seo");
			$this->db->where("postStatus",1);
			$this->db->where("postID",$id);
			$this->db->join('blog_members', 'blog_members.memberID = blog_posts_seo.postBy','left');
			$this->db->order_by('postID','DESC');
			return $this->db->get();		
		}*/
		
		
		function getById($title)
		{
			//$this->db->like("postSlug",$title);
			$this->db->where("postSlug",$title);
			$this->db->or_where("postCode",$title);
			$this->db->from("blog_posts");	
			$this->db->join('tbluser','tbluser.userid = blog_posts.postBy','left');
			$q = $this->db->get();
			if($q->num_rows() > 0)
			{
				return $q->row();
			}
			return false;
		}
		
		
		function related_posts($id)
		{
			$q = $this->db->query('
					SELECT 
						blog_posts_seo.postID, blog_posts_seo.postTitle, blog_posts_seo.postSlug, blog_posts_seo.postCont, blog_posts_seo.postDate,blog_posts_seo.postBy,blog_posts_seo.postCont
					FROM 
						blog_posts_seo,
						blog_post_cats
					WHERE
						 blog_posts_seo.postID = blog_post_cats.postID
						 AND blog_post_cats.catID = '.$id.'
					ORDER BY 
						postID DESC LIMIT 4');
		
			if($q->num_rows() > 4){
				return $q;
			}else{
				$q = $this->db->query("SELECT `postTitle`, `postSlug`,`postDate`, `postBy`,`postCont` FROM `blog_posts_seo` WHERE `postStatus` = 1 ORDER BY RAND() LIMIT 4");
				return $q;
			}	
		}
		
		
		function view_article($postID,$ip_address,$date)
		{
			$this->db->select('*');
			$this->db->from('user_view');
			$this->db->where('postID',$postID);
			$this->db->where('ip_address',$ip_address);
			$this->db->where('date',$date);
			return $this->db->get();
			
		} 
		
		
		function insert_view($postID,$ip_address,$date)
		{
			$data = array(
						'postID'=>$postID,
						'ip_address'=>$ip_address,
						'date'=>$date,
					);
			$this->db->insert('user_view',$data);
		}
		
		
		function update_view($postID)
		{
			$this->db->set('postView','postView+1', FALSE);
			$this->db->where("postID",$postID);
			$this->db->update('blog_posts_seo');
		}
		
		
		function delete_view($date)
		{
			$this->db->where('date <',$date);
			$this->db->delete('user_view');
		}
		
	}