<?php

if(!defined('BASEPATH')) exit('Hacking Attempt : Get Out of the system ..!');

class Users_model extends CI_Model
{
	public function __construct(){
		parent::__construct();
	}
	
	public function getsinglepagedetails($slug){
		$this->db->select('*');
		$where = array('pages.slug='=>$slug,"pages.status"=>1);
		$this->db->where($where);
		$query = $this->db->get('pages');
		return $query->result_array();
	}
	
	public function getsavearticles($userId)
	{
		$this->db->select('articles.*,user_bookmarks.addedon as bookmarkdate');
		$this->db->join('user_bookmarks',"user_bookmarks.articleId=articles.articleId","left");
		$this->db->where(array('articles.status'=>1,'user_bookmarks.userId'=>$userId));
		$this->db->order_by('user_bookmarks.addedon', 'DESC');
		$query = $this->db->get('articles');		
		//echo $this->db->last_query();
		return $query->result();
	}
	
	public function getpurchasedarticles($userId)
	{
		$this->db->select('articles.*,user_purchased_articles.purchasedate as purchasedate');
		$this->db->join('user_purchased_articles',"user_purchased_articles.articleId=articles.articleId","left");
		$this->db->where(array('articles.status'=>1,'user_purchased_articles.userId'=>$userId));
		$this->db->order_by('user_purchased_articles.purchasedate', 'DESC');
		$query = $this->db->get('articles');		
		//echo $this->db->last_query();
		return $query->result();
	}
	
	public function getcatposts($cat,$type,$limit,$offset)
	{
		  $this->db->reset_query();
		$this->db->select('articles.*');
		$this->db->join('article_to_category',"article_to_category.articleId=articles.articleId","left");
		$this->db->where(array('articles.status'=>1,'article_to_category.articlecatId'=>$cat));	
		//$this->db->limit($limit, $offset);	
		$query = $this->db->get('articles');		
		//echo $this->db->last_query();
		return $query->result();
	}
	
	
	public function getpostcat($articleId){
		$this->db->select('article_category.articlecatId,article_category.title,article_category.slug');
		$this->db->join('article_to_category',"article_to_category.articlecatId=article_category.articlecatId","left");
		$where = array('article_to_category.articleId='=>$articleId,"article_category.status="=>1);
		$this->db->where($where);
		$query = $this->db->get('article_category');
		$this->db->order_by('article_category.title', 'ASC');
		return $query->result_array();
	}
	
	public function getpostauthor($userId){
		  $this->db->reset_query();
		$this->db->select('*');
		$this->db->join('user_author',"user_author.userId=user.userId","left");
		$where = array('user.userId='=>$userId,"user.status="=>1);
		$this->db->where($where);
		$query = $this->db->get('user');
		return $query->result_array();
	}
	
	public function getcatdetails($slug){
		$this->db->select('*');
		$where = array('article_category.slug='=>$slug,"article_category.status"=>1);
		$this->db->where($where);
		$query = $this->db->get('article_category');
		return $query->result_array();
	}
	
	public function getpostdetails($slug)
	{
		$this->db->reset_query();
		$this->db->select('*');
		$this->db->where(array('articles.status'=>1,'articles.slug'=>$slug));	
		$query = $this->db->get('articles');		
		//echo $this->db->last_query();
		return $query->result();
	}
	
	public function getranpost($limit){
		$this->db->reset_query();
		$this->db->select('articleId,title,slug');
		$this->db->where(array('articles.status'=>1));
		$this->db->order_by('rand()');	
		$this->db->limit($limit);
		$query = $this->db->get('articles');		
		//echo $this->db->last_query();
		return $query->result();
	}
	
	public function getplandetails(){
		$this->db->reset_query();
		$this->db->select('*');
		$this->db->where(array('user_plan.status'=>1,'user_plan.userId'=>@$_SESSION['user']['userId']));
		$query = $this->db->get('user_plan');	
		return $query->result();
	}
	
	public function purchsedetails($articleId,$userId){
		$this->db->reset_query();
		$this->db->select('*');
		$this->db->where(array('articleId'=>$articleId,'userId'=>$userId));
		$query = $this->db->get('user_purchased_articles');
		return $query->num_rows();
	}
	
	public function purchasearticle($articleId,$userId){
		$this->db->reset_query();
		$data['articleId']= $articleId;
		$data['userId']= $userId;
		$data['purchasedate']=date('Y-m-d H:i:s');
		$this->db->insert('user_purchased_articles', $data);
	}
	
	public function updateuserplan($data){
		$this->db->reset_query();
		$this->db->where(array('userId'=>@$_SESSION['user']['userId']));
		$this->db->update('user_plan', $data);
	}
}

?>