<?php

class ModuleCart extends Controller
{
    public function fetch()
    {
        $config   =& $this->locator->get('config');

        $cart     =& $this->locator->get('cart');

        $currency =& $this->locator->get('currency');

        $customer =& $this->locator->get('customer');

        $language =& $this->locator->get('language');

        $url      =& $this->locator->get('url');

        $database =& $this->locator->get('database');

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

        $calculate =& $this->locator->get('calculate');
        $image    =& $this->locator->get('image');

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

        $template->set('text_cart', $language->get('text_cart'));

            $cart_quantity = 0;
            $cart_product_data=array();
            foreach ($cart->getProducts() as $key => $result) {
                $product_image_info=$database->getRow("select p.product_id,p.image_id,i.filename from product p left join image i on i.image_id=p.image_id where p.product_id='".$result['product_id']."' ");
                $cart_quantity = $cart_quantity + $result['quantity'];

                $cart_product_data[]=array(
                     'product_id'=>$result['product_id'],
                     'thumb' =>$image->resize($product_image_info['filename'], 100, 100),
                     'price' =>$currency->format($result['price']),
                     'name' =>$result['name'],
                     'quantity'=>$result['quantity'],
                     'href_remove'=>$cart->remove($result['product_id']),
                     'product_key' => $key,
                );
            }
            $template->set('cart_quantity', $cart_quantity);
            $cart_subtotal = $calculate->getTotalText();
            $template->set('cart_subtal', $cart_subtotal);
            $template->set('cart_products', $cart_product_data);

            return $template->fetch('module/cart.tpl');

    }

}
