<?php 

class ControllerInstocknotice extends Controller {
	function index(){
		$customer =& $this->locator->get('customer');
		$database =& $this->locator->get('database');
		$currency =& $this->locator->get('currency');
		$tax      =& $this->locator->get('tax');
		$module   =& $this->locator->get('module');
		$request  =& $this->locator->get('request');
		$response =& $this->locator->get('response');
		$session  =& $this->locator->get('session');
		$template =& $this->locator->get('template');
		$url      =& $this->locator->get('url');
		$language =& $this->locator->get('language');
		$view = $this->locator->create('template');
		if (!$customer->isLogged()) {
	  		$session->set('redirect', $url->ssl('instocknotice'));
	  		$response->redirect($url->ssl('account_login'));
    	} 
		$template->set('title', 'My in stock notice List');
		$user_id  = $customer->getId();		//get login user_id
		$sql = "select id,product_id from in_stock_notice where user_id='$user_id'";	//selecd all users
		$notice_user = $database->getRows($database->splitQuery($sql,($request->has('path') ? $session->get('instocknotice.' . $request->get('path') . '.page')  : $session->get('instocknotice.page')), 10));
		$notice_list = array();
		if($notice_user){
			foreach ($notice_user  as $key=>$pro_id){
				
				$query = array(
            			'path'       => $request->get('path'),
            			'product_id' => $pro_id['product_id']
          			);
				$product_id = $pro_id['product_id'];
				
				$notice_list[$key] = $database->getRow("select p.product_id,p.tax_class_id,p.quantity,p.price,p.sold,image.filename,product_description.name from product as p left join image on p.image_id=image.image_id left join product_description on p.product_id = product_description.product_id where p.product_id='$product_id'");
				$notice_list[$key]['id'] = $pro_id['id'];
				$notice_list[$key]['market_price'] = $currency->format($tax->calculate($notice_list[$key]['price']*1.4, $notice_list[$key]['tax_class_id']));
				$notice_list[$key]['price'] = $currency->format($tax->calculate($notice_list[$key]['price'], $notice_list[$key]['tax_class_id']));
				$notice_list[$key]['sold'] = $notice_list[$key]['sold'];
			}
			foreach ($notice_list as $k=>$v){					
					$notice_list[$k]['feedback']=$database->getRow("select sum(rating) as sum,avg(rating) as avg,count(feedback_id) as total from `feedback` where product_id='".$v['product_id']."'");
		    }	
		}
		if($session->get('instocknotice.' . $request->get('path') . '.page')==null)
		{
			$page = 1;
		}else{
			$page = ($request->has('path') ? $session->get('instocknotice.' . $request->get('path') . '.page') : $session->get('instocknotice.page'));
		}
		$view->set('text_results', $language->get('text_results', $page, $database->getPages()));
  		$view->set('entry_page', $language->get('entry_page'));
			
		$view->set('action', $url->href('instocknotice', 'page', array('path' => $request->get('path'))));
		
		// record last viewed wish, it can be use to return back 
		$session->set('last_view_wish',$url->href('instocknotice', 'page', array('path' => $request->get('path'))));
		$view->set('page', ($request->has('path') ? $session->get('instocknotice.' . $request->get('path') . '.page') : $session->get('instocknotice.page')));
		$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('notice_list', $notice_list);
		$template->set('content', $view->fetch('content/my_in_stock_notice.tpl'));
			$template->set($module->fetchAccount());	
    	$response->set($template->fetch('layout.tpl')); 
	}
	
	function addstock()
	{
		$customer =& $this->locator->get('customer');
		$database =& $this->locator->get('database');
		$module   =& $this->locator->get('module');
		$request  =& $this->locator->get('request');
		$response =& $this->locator->get('response');
		$session  =& $this->locator->get('session');
		$template =& $this->locator->get('template');
		$url      =& $this->locator->get('url');
		$product_id = $request->get('pro_id','get');
		if (!$customer->isLogged()) {
	  		$session->set('redirect', $url->ssl('instocknotice&action=addstock&pro_id='.$product_id));
	  		$response->redirect($url->ssl('account_login'));
    	}
    	$user_id  = $customer->getId();
    	$sql = "select * from in_stock_notice where product_id =".$product_id." and user_id = ".$user_id;
    	$limit_product_count = $database->countRows("select id from in_stock_notice where product_id =".$product_id);	//limit add product count
    	if($limit_product_count>500)
    	{
    		echo "<script>alert('Sorry, The same product only can be attention for 500 customers.');window.history.back(-1);</script>";
    		exit();
    	}
    	$result = $database->getRow($sql);
    	if ($result['id'] > 0){
    		echo '0';
    	}else{
    		$sql = "insert into in_stock_notice set product_id = ".$product_id.","." user_id = ".$user_id.","."add_time = now()";
    		$database->query("update product set attentions = attentions + 1 where product_id = ".$product_id);
    		$database->query($sql);
    		echo '1';
    	}
	}
	
		//delete in_stock_notice
	function delete()
	{
		$customer =& $this->locator->get('customer');
		$database =& $this->locator->get('database');
		$module   =& $this->locator->get('module');
		$request  =& $this->locator->get('request');
		$response =& $this->locator->get('response');
		$session  =& $this->locator->get('session');
		$template =& $this->locator->get('template');
		$url      =& $this->locator->get('url');
		if (!$customer->isLogged()) {
	  		$session->set('redirect', $url->ssl('instocknotice'));
	  		$response->redirect($url->ssl('account_login'));
    	}
		$id= $request->get('id','get');
    	$user_id  = $customer->getId();
    	$sql = "delete from in_stock_notice where id = ".$id." and user_id = ".$user_id;
    	$database->query($sql);
    	$response->redirect($url->ssl('instocknotice'));
	}
	
	function page() {
		$request  =& $this->locator->get('request');
		$response =& $this->locator->get('response');
		$session  =& $this->locator->get('session');
		$url      =& $this->locator->get('url');
		
		if ($request->has('page', 'post')) {
			$session->set(($request->has('path') ? 'instocknotice.' . $request->get('path') . '.page' : 'instocknotice.page'), $request->get('page', 'post'));
		}

		$response->redirect($url->ssl('instocknotice', FALSE, array('path' => $request->get('path'))));
	}
}
?>