<?php
class ControllerMyWishlist extends Controller
{
    public function index()
    {
        $config   =& $this->locator->get('config');
        $currency =& $this->locator->get('currency');
        $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');
        $language =& $this->locator->get('language');
        $session  =& $this->locator->get('session');
        $tax      =& $this->locator->get('tax');
        $template =& $this->locator->get('template');
        $url      =& $this->locator->get('url');
        $cart     =& $this->locator->get('cart');
        $image    =& $this->locator->get('image');
        if (!$customer->isLogged()) {
            $session->set('redirect', $url->ssl('my_wishlist'));
            $response->redirect($url->ssl('account_login'));
        }

       $view = $this->locator->create('template');

      if($request->has('page')){
		   $page=$request->get('page');
		}else{
			$page = 1;
		}    
		
        $customer_id  = $customer->getId();

        $sql = "select * from wishlist where customer_id=$customer_id";

        $wishlist_rows = $database->getRows($database->splitQuery($sql, $page, 5));

        $products_data = array();
        foreach ($wishlist_rows as $key=>$wishlist_row) {

            $product_option_key=explode(':', $wishlist_row['product_id']);
            $product_info = $database->getRow("select p.product_id,p.tax_class_id,p.price,p.quantity,p.sold,i.filename,pd.name as product_title from product p left join image i on (p.image_id=i.image_id) left join product_description pd on (p.product_id = pd.product_id) where p.product_id=".$product_option_key[0]);

            $products_data [] = array (
            'wishlist_id'=> $wishlist_row['wishlist_id'],
            'product_id'=> $wishlist_row['product_id'],
            'product_options'=> $product_option_key[1],
            'product_single_id'=> $product_option_key[0],
            'product_image_filename'=> $image->resize($product_info['filename'], $config->get('config_image_width'), $config->get('config_image_height')),
            'product_href' => $url->ssl('product', FALSE, array('product_id' => $product_option_key[0])),
            'remove_product_href' => $url->ssl('my_wishlist', 'delete', array('wishlist_id' => $wishlist_row['wishlist_id'])),
            'product_title'=> $product_info['product_title'],
            'price'=> $currency->format($tax->calculate($product_info['price'], $product_info['tax_class_id'])),
            'market_price'=> $currency->format($tax->calculate(1.7*$product_info['price'], $product_info['tax_class_id'])),
            'sold'=>$product_info['sold'],
            'quantity'=>$product_info['quantity'],
            'review'=>$database->getRow("select avg(rating) as review_rating_avg, count(review_id) as review_count from `review` where product_id=".$product_option_key[0])
            );
        }
        $view->set('my_wishlists', $products_data);
        $view->set('href_cart', $url->href('cart'));

       $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('page', $page);	
  		$view->set('text_results', $language->get('text_results',$page, $database->getPages()));		
  		$view->set('action', $url->href('my_wishlist'));
  		
       
        $template->set('content', $view->fetch('content/my_wishlist.tpl'));
        $template->set($module->fetchAccount());
        $response->set($template->fetch('layout.tpl'));
    }

    //add wishlist
    public function addtowishlist()
    {
        $currency =& $this->locator->get('currency');
        $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');
        $template =& $this->locator->get('template');
        $session  =& $this->locator->get('session');
        $url      =& $this->locator->get('url');
        if (!$customer->isLogged()) {
            $session->set('redirect', $url->ssl('my_wishlist'));
            $response->redirect($url->ssl('account_login'));
        }
        $customer_id  = $customer->getId();
        $product_id = $request->get('product_id');

        $wishlist_row= $database->getRow("select wishlist_id from wishlist where product_id =$product_id and customer_id = $customer_id");
        $wishlist_count = $database->countRows("select wishlist_id from wishlist where customer_id = customer_id");
        if ($wishlist_row) {
            echo "<script>alert('Sorry, The same product has exsited in your wishlist.');location.href='/index.php?controller=my_wishlist';</script>";
            exit;
        } elseif ($wishlist_count >= 200) {
            echo "<script>alert('Sorry, Your wishlist is full. only 200 wish items are allowed to add in to your wishList.');location.href='/index.php?controller=my_wishlist';</script>";
            exit;
        } else {
            $database->query("insert into wishlist set product_id = $product_id, customer_id = $customer_id, date_added = now()");
            exit;
        }
        $response->redirect($_SERVER['HTTP_REFERER']);
    }

    //delete wishlist
    public function delete()
    {
        $currency =& $this->locator->get('currency');
        $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');
        $template =& $this->locator->get('template');
        $url      =& $this->locator->get('url');
        if (!$customer->isLogged()) {
            $session->set('redirect', $url->ssl('my_wishlist'));
            $response->redirect($url->ssl('account_login'));
        }
        $customer_id  = $customer->getId();
        $wishlist_id = $request->get('wishlist_id');
        $database->query("delete from wishlist where wishlist_id = $wishlist_id and customer_id = $customer_id");
        $response->redirect($url->ssl('my_wishlist'));
    }

    public function ajax_addtocart()
    {
        $response  =& $this->locator->get('response');
        $request = &$this->locator->get('request');
        $calculate =& $this->locator->get('calculate');
        if ($request->has('product_id')) {
            $product_array=explode(':', $request->get('product_id'));
            if (count($product_array)>1) {
                $product_id = $product_array[0];
                $option_string = $product_array[2];
            } else {
                $product_id = $product_array[0];
                $option_string = '';
            }
            $options = array();
            if (!empty($option_string)) {
                $options = explode('.', $option_string);
            }
            $cart = &$this->locator->get('cart');
            $cart->add($product_id, 1, $options);

            return true;
        } else {
            return false;
        }
    }

}
