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

class Users extends MY_Controller {

	function __construct(){

		parent::__construct();	
		$this->load->model('Users_model');
		$this->load->model('Home_model');		
		
	}


	
		public function index($param=null)
	{
		
	}
	
		public function savedarticles($param=null)
	{
		//echo $param;
		$head['canonicalurl'] = base_url();
		$data=array();
		
		$postsdata = $this->Users_model->getsavearticles(@$_SESSION['user']['userId']);
		//print_r($postsdata);
		$resultitems='';
		for($i=0;$i<count($postsdata);$i++){
			$posttype='<a class="post-category-color-text" style="background:#36c942" href="#">FREE</a>';
			
			if($postsdata[$i]->type=='PAID'){
				$posttype='<a class="post-category-color-text" style="background:#d80b0b" href="#">PAID</a>';
			}
			
			$postcategoryrsp = $this->Home_model->getpostcat($postsdata[$i]->articleId);
			$postcategory='';
			for($c=0;$c<count($postcategoryrsp);$c++){
				$postcategory.='<a href="'.base_url($postcategoryrsp[$c]['slug']).'">'.$postcategoryrsp[$c]['title'].'</a>';
			}
			
			$authorrsp = $this->Home_model->getpostauthor($postsdata[$i]->userId);
			
			
			
			$resultitems.='<div class=" jelly_homepage_builder homepage_builder_3grid_post jl_cus_grid2 jl_fontsize22 colstyle1">
                              <div class="col-md-12 blog_grid_post_style  jl_row_2">
                                <div class="jl_grid_box_wrapper">
                                  <div class="image-post-thumb">
                                    <a href="'.base_url($postsdata[$i]->slug).'" class="link_image featured-thumbnail" title="This big boss is work hard and also play hard">
                                      <img width="780" height="450" style="height:178px;width:260px;background-image: url('.base_url('uploads/articles/'.$postsdata[$i]->image).');background-position: center;background-size: cover;" class="attachment-disto_large_feature_image size-disto_large_feature_image wp-post-image" alt="" />
                                      <div class="background_over_image"></div>
                                    </a> <span class="meta-category-small">'.$posttype.'</span> 
                                  </div>
                                  <div class="post-entry-content">
                                    <h3 class="image-post-title"><a href="'.base_url($postsdata[$i]->slug).'">
                           '.$postsdata[$i]->title.'</a> '.checkbookmark($postsdata[$i]->articleId).'</h3>
                                    <span class="jl_post_meta">
									<span class="jl_author_img_w">
									<img src="'.base_url('uploads/authors/'.$authorrsp[0]['image']).'" width="30" height="30" alt="'.$authorrsp[0]['name'].'" class="avatar avatar-30 wp-user-avatar wp-user-avatar-30 alignnone photo" />
									<a href="'.base_url('authors/'.$authorrsp[0]['userId']).'" title="'.$postsdata[$i]->title.'" rel="author">'.$authorrsp[0]['name'].'</a></span>
										<span class="post-date"><i class="fa fa-eye"></i> '.$postsdata[$i]->readtime.' Min</span>
										<span class="post-date"><i class="fa fa-clock-o"></i> '.formatDate($postsdata[$i]->updatedon,'d M, Y').'</span>
									<span class="post-date" style="background: #f6f6f6;padding: 0px 8px;border: 1px solid #f3f3f3;">'.$postcategory.'</span>
                                    <div class="content_post_grid">
                                      <p>'.trimPara($postsdata[$i]->description,'100').' <a href="'.base_url($postsdata[$i]->slug).'">read more</a></p>
									  <p></p>
									  <p style="background: #f2f2f2;font-size: 10px;padding: 4px;margin-top: 10px;"><b>Saved On:</b> '.formatDate($postsdata[$i]->bookmarkdate,'F j, Y, g:i a').'</p>
                                    </div>
                                  </div>
                                </div>
                              </div>
                            </div>';
		}
		$data['articles']=$resultitems;
		//print_r($data['articles']);
		$head=array();
		$this->render('users/savedarticles',$data,$head);	
	}
	
		public function purchasedarticles($param=null)
	{
		//echo $param;
		$head['canonicalurl'] = base_url();
		$data=array();
		
		$postsdata = $this->Users_model->getpurchasedarticles(@$_SESSION['user']['userId']);
		//print_r($postsdata);
		$resultitems='';
		for($i=0;$i<count($postsdata);$i++){
			$posttype='<a class="post-category-color-text" style="background:#36c942" href="#">FREE</a>';
			
			if($postsdata[$i]->type=='PAID'){
				$posttype='<a class="post-category-color-text" style="background:#d80b0b" href="#">PAID</a>';
			}
			
			$postcategoryrsp = $this->Home_model->getpostcat($postsdata[$i]->articleId);
			$postcategory='';
			for($c=0;$c<count($postcategoryrsp);$c++){
				$postcategory.='<a href="'.base_url($postcategoryrsp[$c]['slug']).'">'.$postcategoryrsp[$c]['title'].'</a>';
			}
			
			$authorrsp = $this->Home_model->getpostauthor($postsdata[$i]->userId);
			
			
			
			$resultitems.='<div class=" jelly_homepage_builder homepage_builder_3grid_post jl_cus_grid2 jl_fontsize22 colstyle1">
                              <div class="col-md-12 blog_grid_post_style  jl_row_2">
                                <div class="jl_grid_box_wrapper">
                                  <div class="image-post-thumb">
                                    <a href="'.base_url($postsdata[$i]->slug).'" class="link_image featured-thumbnail" title="This big boss is work hard and also play hard">
                                      <img width="780" height="450" style="height:178px;width:260px;background-image: url('.base_url('uploads/articles/'.$postsdata[$i]->image).');background-position: center;background-size: cover;" class="attachment-disto_large_feature_image size-disto_large_feature_image wp-post-image" alt="" />
                                      <div class="background_over_image"></div>
                                    </a> <span class="meta-category-small">'.$posttype.'</span> 
                                  </div>
                                  <div class="post-entry-content">
                                    <h3 class="image-post-title"><a href="'.base_url($postsdata[$i]->slug).'">
                           '.$postsdata[$i]->title.'</a> '.checkbookmark($postsdata[$i]->articleId).'</h3>
                                    <span class="jl_post_meta">
									<span class="jl_author_img_w">
									<img src="'.base_url('uploads/authors/'.$authorrsp[0]['image']).'" width="30" height="30" alt="'.$authorrsp[0]['name'].'" class="avatar avatar-30 wp-user-avatar wp-user-avatar-30 alignnone photo" />
									<a href="'.base_url('authors/'.$authorrsp[0]['userId']).'" title="'.$postsdata[$i]->title.'" rel="author">'.$authorrsp[0]['name'].'</a></span>
										<span class="post-date"><i class="fa fa-eye"></i> '.$postsdata[$i]->readtime.' Min</span>
										<span class="post-date"><i class="fa fa-clock-o"></i> '.formatDate($postsdata[$i]->updatedon,'d M, Y').'</span>
									<span class="post-date" style="background: #f6f6f6;padding: 0px 8px;border: 1px solid #f3f3f3;">'.$postcategory.'</span>
                                    <div class="content_post_grid">
                                      <p>'.trimPara($postsdata[$i]->description,'100').' <a href="'.base_url($postsdata[$i]->slug).'">read more</a></p>
									  <p></p>
									  <p style="background: #f2f2f2;font-size: 10px;padding: 4px;margin-top: 10px;">Purchased On: '.formatDate($postsdata[$i]->purchasedate,'F j, Y, g:i a').'</p>
                                    </div>
                                  </div>
                                </div>
                              </div>
                            </div>';
		}
		$data['articles']=$resultitems;
		//print_r($data['articles']);
		$head=array();
		$this->render('users/purchasedarticles',$data,$head);	
	}
	
	
	public function getposts($cat=NULL,$type=NULL,$limit=NULL,$offset=NULL){
		$type=$_POST['type'];
		$limit=$_POST['limit'];
		$offset=$_POST['offset'];
		$cat=$_POST['cat'];
		
		if($cat==0){
			$postsdata = $this->Home_model->getposts($type,$limit,$offset);
		}elseif($cat!=0){
			$postsdata = $this->Home_model->getcatposts($cat,$type,$limit,$offset);
		}
		//echo count($posts);
		//print_r($posts);
		
		$postitems=array();
		$postitems['posts']='';
		$resultitems='';
		for($i=0;$i<count($postsdata);$i++){
			$posttype='<a class="post-category-color-text" style="background:#36c942" href="#">FREE</a>';
			
			if($postsdata[$i]->type=='PAID'){
				$posttype='<a class="post-category-color-text" style="background:#d80b0b" href="#">PAID</a>';
			}
			
			$postcategoryrsp = $this->Home_model->getpostcat($postsdata[$i]->articleId);
			$postcategory='';
			for($c=0;$c<count($postcategoryrsp);$c++){
				$postcategory.='<a href="'.base_url($postcategoryrsp[$c]['slug']).'">'.$postcategoryrsp[$c]['title'].'</a>';
			}
			
			$authorrsp = $this->Home_model->getpostauthor($postsdata[$i]->userId);
			
			
			
			$resultitems.='<div class=" jelly_homepage_builder homepage_builder_3grid_post jl_cus_grid2 jl_fontsize22 colstyle1">
                              <div class="col-md-12 blog_grid_post_style  jl_row_2">
                                <div class="jl_grid_box_wrapper">
                                  <div class="image-post-thumb">
                                    <a href="'.base_url($postsdata[$i]->slug).'" class="link_image featured-thumbnail" title="This big boss is work hard and also play hard">
                                      <img width="780" height="450" style="height:178px;width:260px;background-image: url('.base_url('uploads/articles/'.$postsdata[$i]->image).');background-position: center;background-size: cover;" class="attachment-disto_large_feature_image size-disto_large_feature_image wp-post-image" alt="" />
                                      <div class="background_over_image"></div>
                                    </a> <span class="meta-category-small">'.$posttype.'</span> 
                                  </div>
                                  <div class="post-entry-content">
                                    <h3 class="image-post-title"><a href="'.base_url($postsdata[$i]->slug).'">
                           '.$postsdata[$i]->title.'</a> '.checkbookmark($postsdata[$i]->articleId).'</h3>
                                    <span class="jl_post_meta">
									<span class="jl_author_img_w">
									<img src="'.base_url('uploads/authors/'.$authorrsp[0]['image']).'" width="30" height="30" alt="'.$authorrsp[0]['name'].'" class="avatar avatar-30 wp-user-avatar wp-user-avatar-30 alignnone photo" />
									<a href="'.base_url('authors/'.$authorrsp[0]['userId']).'" title="'.$postsdata[$i]->title.'" rel="author">'.$authorrsp[0]['name'].'</a></span>
										<span class="post-date"><i class="fa fa-eye"></i> '.$postsdata[$i]->readtime.' Min</span>
										<span class="post-date"><i class="fa fa-clock-o"></i> '.formatDate($postsdata[$i]->updatedon,'d M, Y').'</span>
									<span class="post-date" style="background: #f6f6f6;padding: 0px 8px;border: 1px solid #f3f3f3;">'.$postcategory.'</span>
                                    <div class="content_post_grid">
                                      <p>'.trimPara($postsdata[$i]->description,'100').' <a href="'.base_url($postsdata[$i]->slug).'">read more</a></p>
                                    </div>
                                  </div>
                                </div>
                              </div>
                            </div>';
		}
		$postitems['posts']=$resultitems;
		$offset= $offset+$limit;
		if(count($postsdata)>0){
		$postitems['loadmore']='<a href="javascript:void(0);" id="loadmorebtn" data-type="'.$type.'" data-offset="'.$offset.'" data-cat="'.$cat.'" data-limit="'.$limit.'" class="jl_btn_load">Load more</a>';
		}else{
			$postitems['loadmore']='';
		}
		
		echo json_encode($postitems);
	}
	
	
	public function profile(){
		$head=array();
		$head['canonicalurl'] = base_url();
		$data=array();
		$this->render('users/profile',$data,$head);
	}
	
	public function writer(){
		$head=array();
		$head['canonicalurl'] = base_url();
		$data=array();
		$this->render('users/writerprofile',$data,$head);
	}
	public function writerstats(){
		$head=array();
		$head['canonicalurl'] = base_url();
		$data=array();
		$this->render('users/profile',$data,$head);
	}
}
