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

/**
 * 
 * Landing page functions
 * @author Teamtweaks
 *
 */

class Landing extends MY_Controller {
	function __construct(){
        parent::__construct();
		$this->load->helper(array('cookie','date','form','email'));
		$this->load->library('session');
		$this->load->library(array('encrypt','form_validation'));		
		$this->load->model('product_model');
		$this->load->model('user_model');
		//echo $this->checkLogin('U'); die;
		$this->data['loginCheck'] = $this->checkLogin('U');
		$this->data['likedProducts'] = array();
	 	if ($this->data['loginCheck'] != ''){
	 		$this->data['likedProducts'] = $this->product_model->get_all_details(PRODUCT_LIKES,array('user_id'=>$this->checkLogin('U')));
	 	}
		$UserCookieData = $this->input->cookie("Shopsy_NewUser");

		//if($UserCookieData == ''){
		//	$CookieVal = array( 'name'   => 'Shopsy_NewUser','value'  => 2,'expire' => time()+3600,'secure' => FALSE);
		//	$this->input->set_cookie($CookieVal);
			//redirect('signup');
	//	}elseif($UserCookieData == 2){
			//redirect('signup');
	//	}
	 	#echo $UserCookieData; die;
		if($UserCookieData != ''){
			$condition = array('id'=>$UserCookieData);
			$checkUser = $this->user_model->get_all_details(USERS,$condition);
			//echo '<pre>'; print_r($checkUser); die;
			if ($checkUser->num_rows() == 1){ 
				$userdata = array(
								'shopsy_session_user_id' => $checkUser->row()->id,
								'shopsy_session_user_name' => $checkUser->row()->user_name,
								'shopsy_session_full_name' => $checkUser->row()->full_name,
								'shopsy_session_user_email' => $checkUser->row()->email,
								'shopsy_session_user_confirm' => $checkUser->row()->is_verified,
								'userType'=>$checkUser->row()->group
							);
				
				$this->session->set_userdata($userdata);
			}
			//redirect('signup');
		}
		
				
    }
    
    /**
     * Site Index Page
     * 
     */
   	public function index(){	 	
		$recentFavorites= $this->user_model->get_resent_favorite_list()->result_array();
		$newFavArr[]=$recentFavorites[0]['shop_id'];
		$recentnewFavorites[0]=$recentFavorites[0];
		$j=0;$l=1;
		for($k=1;$k<count($recentFavorites);$k++){
			if(!in_array($recentFavorites[$k]['shop_id'],$newFavArr)){
				$j++;$l++;
				$newFavArr[]=$recentFavorites[$k]['shop_id'];
				$recentnewFavorites[$l-1]=$recentFavorites[$k];
				if($l==4){break;}
			}
		}
		$this->data['recentFavorites']=$recentnewFavorites;
		$FavoriteShopsProducts=array();
		$FavoriteShops='';
		foreach($recentnewFavorites as $listFav){	
			if($listFav['shop_id'] !=''){		
				$products=$this->product_model->get_product_from_favorite_shop('p.user_id='.$listFav['shop_id'].' GROUP BY p.id')->result_array();
				$FavoriteShopsProducts[$listFav['shop_id']]=$products; 
				$FavoriteShops.=$listFav['shop_id'].',';	
			}
		}
		$pickedUsers=$this->user_model->get_pickedItems();
		#echo "<pre>"; print_r($pickedUsers->result_array()); die;
		foreach($pickedUsers->result_array() as $UsersPick){
			$pickedUsersFavorites[$UsersPick['user_id']]=$this->user_model->get_userfav_products($UsersPick['user_id'])->result();
		}
		$this->data['pickedUsers']=$pickedUsers->result_array();
		$this->data['pickedUsersFavorites']=$pickedUsersFavorites;
		$this->data['FavoriteShops']=$FavoriteShops ;
		$this->data['FavoriteShopsProducts']=$FavoriteShopsProducts ;
		###$this->data['testimonials'] = $testimonials= $this->user_model->get_testimonials()->result();
		$this->data['testimonials'] =$recentBlogPosts = $this->user_model->getrecentSellerBlog();
		###echo "<pre>"; print_r($recentBlogPosts); die;
		$this->load->view('site/landing/landing',$this->data);
	}
	
	public function ajax_load_more(){
		$pageloaded = $this->input->post('group_no');
		$limit = 39;
		$start = $limit*$pageloaded;
		$productDetails = $this->product_model->view_product_details(" where p.status='Publish' and p.quantity > 0 and u.group='Seller' and u.status='Active' or p.status='Publish' and p.quantity > 0 and p.user_id=0 order by p.created desc limit ".$start.','.$limit);
		$resultVal = '';
		if ($productDetails->num_rows()>0){
			$productArr = $productDetails->result_array();
			for ($i=0;$i<count($productArr);$i=$i+3){
	          	if (isset($productArr[$i]['id'])){
	          		$imgArr = explode(',', $productArr[$i]['image']);
	          		$img = 'dummyProductImage.jpg';
	          		foreach ($imgArr as $imgVal){
	          			if ($imgVal != ''){
							$img = $imgVal;
							break;
	          			}
	          		}
	          		$fancyClass = 'fancy';
	          		$fancyText = LIKE_BUTTON;
	          		if (count($this->data['likedProducts'])>0 && $this->data['likedProducts']->num_rows()>0){
	          			foreach ($this->data['likedProducts']->result() as $likeProRow){
	          				if ($likeProRow->product_id == $productArr[$i]['seller_product_id']){
	          					$fancyClass = 'fancyd';$fancyText = LIKED_BUTTON;break;
	          				}
	          			}
	          		}
	          		$resultVal .= '<li class="big clear" tid="'.$productArr[$i]['seller_product_id'].'" tuserid="'.$productArr[$i]['user_id'].'">
              <div class="figure-item">
                <!-- span class="pre"></span -->
                <a href="';
	          		if ($productArr[$i]['user_id'] != '0'){
	          			$resultVal .= base_url().'user/'.$productArr[$i]['user_name'];
	          		}else {
	          			$resultVal .= base_url().'user/administrator';
	          		}
	          		$resultVal.='" class="vcard">';
                	if ($productArr[$i]['thumbnail'] == ''){
                	$resultVal .= '<img src="images/users/user-thumb1.png">';
                	}else {
                	$resultVal .= '<img src="images/users/'.$productArr[$i]['thumbnail'].'">';
                	}
                $resultVal .= '</a>
                <a href="things/'.$productArr[$i]['id'].'/'.url_title($productArr[$i]['product_name'],'-').'" class="figure-img"> 
                	<span class="figure grid" style="background-size:cover;" data-ori-url="images/product/thumb/'.$img.'" data-310-url="images/product/thumb/'.$img.'"><em class="back"></em></span>
                	<span class="figure classic"> <em class="back"></em> 
                		<img src="images/product/'.$img.'" data-width="640" data-height="640"> 
                	</span> 
                	<span class="figure vertical"> <em class="back"></em> 
                		<img src="images/product/'.$img.'" data-width="310" data-height="310"> 
                	</span> 
                	<span class="figcaption">'.$productArr[$i]['product_name'].'</span> 
                </a> 
                <em class="figure-detail"> 
                	<span class="price">'.$this->data['currencySymbol'].' '.$productArr[$i]['sale_price'].' <small>'.$this->data['currencyType'].'</small></span> 
                	<span class="username"><em><i> by </i><a href="';
                	if ($productArr[$i]['user_id'] != '0'){
                		$resultVal .= base_url().'user/'.$productArr[$i]['user_name'];
                	}else {
                		$resultVal .= base_url().'user/administrator';
                	}
	          	$resultVal .= '">';
	          	if ($productArr[$i]['user_id'] != '0'){
	          		$resultVal .= $productArr[$i]['full_name'];
	          	}else {
	          		$resultVal .= 'administrator';
	          	}
	          	$resultVal .= '</a> + '.$productArr[$i]['likes'].'</em></span> 
                </em>
                <ul class="function">
                  <li class="list"><a href="#">Add to List</a></li>
                  <li class="cmt"><a href="#">Comment</a></li>
                  <li class="share">
                    <button type="button" ';
	          	if ($this->data['loginCheck']==''){
	          		$resultVal .= 'require_login="true"';
	          	}
	          	$resultVal .= ' data-timage="images/product/thumb/'.$img.'" class="btn-share" tname="'.$productArr[$i]['product_name'].'"  username="';
	          	 if ($productArr[$i]['user_id'] != '0'){
	          	 	$resultVal .= $productArr[$i]['full_name'];
	          	 }else {
	          	 	$resultVal .= 'administrator';
	          	 }
	          	$resultVal .= '"><i class="ic-share"></i></button>
                  </li>
                  <li class="view-cmt"><a href="#">5 comments</a></li>
                </ul>
                <a href="#" item_img_url="images/product/'.$img.'" tid="'.$productArr[$i]['seller_product_id'].'" class="button '.$fancyClass.'" ';
	          	if ($this->data['loginCheck']==''){$resultVal .= 'require_login="true"'; }
	          	$resultVal .= '><span><i></i></span> '.$fancyText.'</a> </div>
            </li>';
	          	}
          	if (isset($productArr[$i+1]['id'])){
          		$imgArr = explode(',', $productArr[$i+1]['image']);
          		$img = 'dummyProductImage.jpg';
          		foreach ($imgArr as $imgVal){
          			if ($imgVal != ''){
						$img = $imgVal;
						break;
          			}
          		}
          		$fancyClass = 'fancy';
          		$fancyText = LIKE_BUTTON;
          		if (count($this->data['likedProducts'])>0 && $this->data['likedProducts']->num_rows()>0){
          			foreach ($this->data['likedProducts']->result() as $likeProRow){
          				if ($likeProRow->product_id == $productArr[$i+1]['seller_product_id']){
          					$fancyClass = 'fancyd';$fancyText = LIKED_BUTTON;break;
          				}
          			}
          		}
          		$resultVal .= '<li  class="mid clear" tid="'.$productArr[$i+1]['seller_product_id'].'" tuserid="'.$productArr[$i+1]['user_id'].'">
              <div class="figure-item">
                <!-- span class="pre"></span -->
                <a href="';
          		if ($productArr[$i+1]['user_id'] != '0'){$resultVal .= 'user/'.$productArr[$i+1]['user_name'];}else {$resultVal .= base_url().'user/administrator';}
          		$resultVal .= '" class="vcard">';
                	if ($productArr[$i+1]['thumbnail'] == ''){
                	$resultVal .= '<img src="images/users/user-thumb1.png">';
	                }else {
	                $resultVal .= '<img src="images/users/'.$productArr[$i+1]['thumbnail'].'">';
	                }
                $resultVal .= '</a> 
                <a href="things/'.$productArr[$i+1]['id'].'/'.url_title($productArr[$i+1]['product_name'],'-').'" class="figure-img"> 
                	<span class="figure grid" style="background-size:cover;" data-ori-url="images/product/thumb/'.$img.'" data-310-url="images/product/thumb/'.$img.'"><em class="back"></em></span> 
                	<span class="figure classic"> <em class="back"></em> 
                		<img src="images/product/'.$img.'" data-width="310" data-height="310"> 
                	</span> 
                	<span class="figure vertical"> <em class="back"></em> 
                		<img src="images/product/'.$img.'" data-width="310" data-height="310"> 
                	</span> 
                	<span class="figcaption">'.$productArr[$i+1]['product_name'].'</span> 
                </a> 
                <em class="figure-detail"> 
                	<span class="price">'.$this->data['currencySymbol'].' '.$productArr[$i+1]['sale_price'].' <small>'.$this->data['currencyType'].'</small></span> 
                	<span class="username"><em><i> by </i><a href="';
                if ($productArr[$i+1]['user_id'] != '0'){$resultVal .= base_url().'user/'.$productArr[$i+1]['user_name'];}else {$resultVal .= base_url().'user/administrator';}
                $resultVal .= '">';
                if ($productArr[$i+1]['user_id'] != '0'){$resultVal .= $productArr[$i+1]['full_name'];}else {$resultVal .= 'administrator';}
                $resultVal .= '</a> + '.$productArr[$i+1]['likes'].'</em></span> 
                </em>
                <ul class="function">
                  <li class="list"><a href="#">Add to List</a></li>
                  <li class="cmt"><a href="#">Comment</a></li>
                  <li class="share">
                    <button type="button" ';
                if ($this->data['loginCheck']==''){$resultVal .= 'require_login="true"';}
                $resultVal .= ' data-timage="images/product/'.$img.'" class="btn-share"  tname="'.$productArr[$i+1]['product_name'].'"  username="';
                if ($productArr[$i+1]['user_id'] != '0'){$resultVal .= $productArr[$i+1]['full_name'];}else {$resultVal .= 'administrator';}
                $resultVal .= '"><i class="ic-share"></i></button>
                  </li>
                  <li class="view-cmt"><a href="#">5 comments</a></li>
                </ul>
                <a href="#" item_img_url="images/product/'.$img.'" tid="'.$productArr[$i+1]['seller_product_id'].'" class="button '.$fancyClass.'" ';
                if ($this->data['loginCheck']==''){$resultVal .= 'require_login="true"';}
                $resultVal .= '><span><i></i></span> '.$fancyText.'</a> </div>
            </li>';
          	}
          	if (isset($productArr[$i+2]['id'])){
          		$imgArr = explode(',', $productArr[$i+2]['image']);
          		$img = 'dummyProductImage.jpg';
          		foreach ($imgArr as $imgVal){
          			if ($imgVal != ''){
						$img = $imgVal;
						break;
          			}
          		}
          		$fancyClass = 'fancy';
          		$fancyText = LIKE_BUTTON;
          		if (count($this->data['likedProducts'])>0 && $this->data['likedProducts']->num_rows()>0){
          			foreach ($this->data['likedProducts']->result() as $likeProRow){
          				if ($likeProRow->product_id == $productArr[$i+2]['seller_product_id']){
          					$fancyClass = 'fancyd';$fancyText = LIKED_BUTTON;break;
          				}
          			}
          		}
          		$resultVal .= '<li  class="mid "  tid="'.$productArr[$i+2]['seller_product_id'].'" tuserid="'.$productArr[$i+2]['user_id'].'">
              <div class="figure-item">
                <!-- span class="pre"></span -->
                <a href="things/'.$productArr[$i+2]['id'].'/'.url_title($productArr[$i+2]['product_name'],'-').'" class="figure-img"> 
                	<span class="figure grid" style="background-size:cover;" data-ori-url="images/product/thumb/'.$img.'" data-310-url="images/product/thumb/'.$img.'"><em class="back"></em></span> 
                	<span class="figure classic"> <em class="back"></em> 
                		<img src="images/product/'.$img.'" data-width="310" data-height="310"> 
                	</span> 
                	<span class="figure vertical"> <em class="back"></em> 
                		<img src="images/product/'.$img.'" data-width="310" data-height="310"> 
                	</span> 
                	<span class="figcaption">'.$productArr[$i+2]['product_name'].'</span> 
                </a> 
                <em class="figure-detail"> 
                	<span class="price">'.$this->data['currencySymbol'].' '.$productArr[$i+2]['sale_price'].' <small>'.$this->data['currencyType'].'</small></span> 
                	<span class="username"><em><i> by </i><a href="';
          		if ($productArr[$i+2]['user_id'] != '0'){$resultVal .= base_url().'user/'.$productArr[$i+2]['user_name'];}else {$resultVal .= base_url().'user/administrator';}
  	    		$resultVal .= '">';
          		if ($productArr[$i+2]['user_id'] != '0'){$resultVal .= $productArr[$i+2]['full_name'];}else {$resultVal .= 'administrator';}
          		$resultVal .= '</a> + '.$productArr[$i+2]['likes'].'</em></span> 
                </em>
                <ul class="function">
                  <li class="list"><a href="#">Add to List</a></li>
                  <li class="cmt"><a href="#">Comment</a></li>
                  <li class="share">
                    <button type="button" ';
          		if ($this->data['loginCheck']==''){$resultVal .= 'require_login="true"';}
          		$resultVal .= ' data-timage="images/product/'.$img.'" class="btn-share" tname="'.$productArr[$i+2]['product_name'].'"  username="';
          		if ($productArr[$i+2]['user_id'] != '0'){$resultVal .= $productArr[$i+2]['full_name'];}else {$resultVal .= 'administrator';}
          		$resultVal .= '"><i class="ic-share"></i></button>
                  </li>
                  <li class="view-cmt"><a href="#">5 comments</a></li>
                </ul>
                <a href="#" item_img_url="images/product/'.$img.'" tid="'.$productArr[$i+2]['seller_product_id'].'" class="button '.$fancyClass.'" ';
          		if ($this->data['loginCheck']==''){$resultVal .= 'require_login="true"'; }
          		$resultVal .= '><span><i></i></span> '.$fancyText.'</a> </div>
            </li>';
				}
			}
		}
		
		echo $resultVal;
	}
	
	
}

/* End of file landing.php */
/* Location: ./application/controllers/site/landing.php */