<?php 
class ControllerNewArrivals extends Controller { 

	function index() {
		$config   = $this->locator->get('config');
		$customer =& $this->locator->get('customer');      
		$language =& $this->locator->get('language');
		$module   =& $this->locator->get('module');
		$response =& $this->locator->get('response');
		$session  =& $this->locator->get('session');
		$template =& $this->locator->get('template');
		$url      =& $this->locator->get('url');
		$currency =& $this->locator->get('currency');
		$database =& $this->locator->get('database');	
		$image    = $this->locator->get('image');
		$request   =& $this->locator->get('request');
		
    	$view = $this->locator->create('template');
    	$view->set('heading_title', "New Arrivals");   	
    	      				
    	$selected_date = $request->get('date');
		if($request->has('page')){
		  	$session->set(($request->has('date') ? 'new_arrivals.' . $request->get('date') . '.page' : 'category.page'), $request->get('page'));
		} 
        $page =$request->has('date') ? $session->get('new_arrivals.' . $request->get('date') . '.page') : $session->get('category.page');
        $page=($page==NULL)?1:$page;
        $first_date = $last_date = ''; 			 
    	$release10_date_rows=$database->getRows("select date_added from product where status>0 group by TO_DAYS(date_added) order by date_added desc limit 0,10");	
    	
    	$release10_date_array2 = array ();	
    	foreach($release10_date_rows as $key=>$release_date) {
    	   $date = date('Y-m-d', strtotime($release_date['date_added'])); 
    	   $product_count=$database->countRows("SELECT product_id FROM product WHERE STATUS >0 AND `date_added` LIKE '%$date%'");	 	
    	   $release10_date_array2[]=array (
    	   		  'date'  => $date,
    	   		  'href'  => $url->ssl('new_arrivals', FALSE, array('date' => $date)),
    	   		  'product_count'  => $product_count
    	   );
    	   if($key==(count($release10_date_rows)-1)){
    	   	$first_date = $date;
    	   	$item_count = $database->countRows("SELECT product_id FROM `product` WHERE date_added >= '$first_date 00:00:00'");
    	   	$view->set('item_count', $item_count);  
    	   }
    	   if($key==0){$last_date = $date;}
    	}
		 if(!empty($release10_date_array2)){
				  
		 	$sql = "SELECT p.product_id, p.price, p.sold, i.filename, pd.name FROM `product` p LEFT JOIN `product_description` pd ON (p.product_id=pd.product_id) LEFT JOIN `image` i on (p.image_id=i.image_id)";
		 	if($selected_date){
		     	$sql .=  " WHERE p.date_added LIKE '%$selected_date%' ORDER BY p.date_added desc";	 
		     	$show_date = $selected_date;
		 	}else{
		 		$sql .= " WHERE p.date_added >= '$first_date 00:00:00' ORDER BY p.date_added desc";
		 		$show_date = "[" . $first_date . "]"." - " . "[" . $last_date . "]";
		 	}
             $exe_sql = $database->splitQuery($sql, $page, $config->get('config_max_rows'));
             $date_product_rows = $database->cache('new_arrivals-'.$selected_date.$page, $exe_sql);
             $show_date.=" (".count($date_product_rows).")";
             $view->set('show_date', $show_date);
             
             $date_product_array2 = array ();	
             foreach($date_product_rows as $key=>$f_product){
             	$date_product_array2 [] = array(
             		'product_id' => $f_product['product_id'],
      				'href'  =>  $url->ssl('product', FALSE, array('product_id' => $f_product['product_id'])),
      				'title' => $f_product['name'],
      				'thumb' => $image->resize($f_product['filename'], $config->get('config_image_width'), $config->get('config_image_height')),
      				'price' => $currency->format($f_product['price']), 
      				'market_price' => $currency->format(1.7*$f_product['price']),
      				'sold'  => $f_product['sold'],                    
                    'review_info'=>$database->getRow("select avg(rating) as avg, count(review_id) as count from `review` where product_id=".$f_product['product_id'])          				
      				);
             }
             
			$page_data = array();
	      	for ($i = 1; $i <= $database->getPages(); $i++) {
				$page_data[] = array(
	  				'text'  => $language->get('text_pages', $i, $database->getPages()),
	  				'value' => $i
				);
	      	}
	      	$view->set('pages', $page_data);  
	      	$view->set('action', $url->href('new_arrivals', NULL, array('date' => $request->get('date'))));		        		
  			$view->set('page', $page);	
  			 
  			 $view->set('text_results', $language->get('text_results',$page, $database->getPages()));				
			 $view->set('release_10_dates', $release10_date_array2);
			 $view->set('date_product_list', $date_product_array2);
			 $view->set('selected_date', $selected_date);			
			
	  		 $template->set('content', $view->fetch('content/new_arrivals.tpl'));    	
			 $template->set($module->fetchPublic());
    		 $response->set($template->fetch('layout.tpl')); 
  	      }
	} 	

	}
?>