<?php 
class ControllerCart extends Controller {
	function index() {
		$cart     =& $this->locator->get('cart');
		$config   =& $this->locator->get('config');
		$currency =& $this->locator->get('currency');
		$customer =& $this->locator->get('customer');
		$database =& $this->locator->get('database');
		$language =& $this->locator->get('language');
		$image    =& $this->locator->get('image');
		$module   =& $this->locator->get('module');
		$request  =& $this->locator->get('request');
		$response =& $this->locator->get('response');
		$shipping =& $this->locator->get('shipping');
		$tax      =& $this->locator->get('tax');
		$template =& $this->locator->get('template');
		$url      =& $this->locator->get('url');
		    	
    	if ($request->isPost()) {
      		if ($request->get('product_id', 'post')) {
        		$cart->add($request->get('product_id', 'post'), '1', $request->get('option', 'post'));
      		}

      		if ($request->get('quantity', 'post')) {
	    		foreach ($request->get('quantity', 'post') as $key => $value) {
	      			$cart->update($key, $value);
				}
      		}

      		if ($request->get('remove', 'post')) {
	    		foreach (array_keys($request->get('remove', 'post')) as $key) {
          			$cart->remove($key);
				}
      		}
      
	  		$response->redirect($url->ssl('cart'));
    	}

		$language->load('controller/cart.php');

    	$template->set('title', $language->get('heading_title'));
	
    	if ($cart->hasProducts()) {
      		$view = $this->locator->create('template');

      		$view->set('heading_title', $language->get('heading_title'));

      		$view->set('text_subtotal', $language->get('text_subtotal'));

      		$view->set('column_remove', $language->get('column_remove'));
      		$view->set('column_image', $language->get('column_image'));
      		$view->set('column_name', $language->get('column_name'));
      		$view->set('column_model', $language->get('column_model'));
      		$view->set('column_quantity', $language->get('column_quantity'));
			$view->set('column_price', $language->get('column_price'));
      		$view->set('column_total', $language->get('column_total'));

      		$view->set('button_update', $language->get('button_update'));
      		$view->set('button_shopping', $language->get('button_shopping'));
      		$view->set('button_checkout', $language->get('button_checkout'));

      		$view->set('error', ((!$cart->hasStock()) && ($config->get('config_stock_check')) ? $language->get('error_stock') : NULL));

      		$view->set('action', $url->href('cart'));

      		$product_data = array();

      		foreach ($cart->getProducts() as $result) {
        		$option_data = array();

        		foreach ($result['option'] as $option) {
          			$option_data[] = array(
            			'name'  => $option['name'],
            			'value' => $option['value'],
          			);
        		}

        		$product_data[] = array(
          			'key'      => $result['key'],
          			'name'     => $result['name'],
          			'model'    => $result['model'],
          			'thumb'    => $image->resize($result['image'], 50, 50),
          			'option'   => $option_data,
          			'quantity' => $result['quantity'],
          			'stock'    => $result['stock'],
					'price'    => $currency->format($tax->calculate($result['price'], $result['tax_class_id'], $config->get('config_tax'))),
          			'discount' => ($result['discount'] ? $currency->format($tax->calculate($result['price'] - $result['discount'], $result['tax_class_id'], $config->get('config_tax'))) : NULL),
					'total'    => $currency->format($tax->calculate($result['total'], $result['tax_class_id'], $config->get('config_tax'))),
					'href'     => $url->href('product', FALSE, array('product_id' => $result['product_id']))
        		);
      		}

      		$view->set('products', $product_data);

      		$view->set('subtotal', $currency->format($cart->getSubtotal()));

      		$view->set('continue', $url->href('home'));

      		$view->set('checkout', $url->href('checkout_shipping'));

	  		$template->set('content', $view->fetch('content/cart.tpl'));
	  
	  		$template->set($module->fetch());

      		$response->set($template->fetch('layout.tpl'));
    	} else {
      		$view = $this->locator->create('template');

      		$view->set('heading_title', $language->get('heading_title'));

      		$view->set('text_error', $language->get('text_error'));

      		$view->set('button_continue', $language->get('button_continue'));

      		$view->set('continue', $url->href('home'));

	  		$template->set('content', $view->fetch('content/error.tpl'));
	  
	  		$template->set($module->fetch());

      		$response->set($template->fetch('layout.tpl'));
    	}
  	}
}
?>
