<?php

defined('_JEXEC') or die('Restricted access');
if (!class_exists( 'VmConfig' ))
	require(JPATH_ADMINISTRATOR . DIRECTORY_SEPARATOR . 'components' . DIRECTORY_SEPARATOR . 'com_virtuemart'.DIRECTORY_SEPARATOR.'helpers'.DIRECTORY_SEPARATOR.'config.php');
if (!class_exists( 'VmText' ))
	require(JPATH_ADMINISTRATOR . DIRECTORY_SEPARATOR . 'components' . DIRECTORY_SEPARATOR . 'com_virtuemart'.DIRECTORY_SEPARATOR.'helpers'.DIRECTORY_SEPARATOR.'vmtext.php');
// Load the controller framework
if(!class_exists('VirtueMartCart')) require(JPATH_VM_SITE.DIRECTORY_SEPARATOR.'helpers'.DIRECTORY_SEPARATOR.'cart.php');

class modVmAjaxCartObHelper{

	public static function viewJSAjax() {
		VmConfig::loadConfig();
		$lang = JFactory::getLanguage();
		$extension = 'com_virtuemart';
		$lang->load($extension);
		$extension = 'mod_vm_ajax_cart_ob';
		$lang->load($extension);
		if (!class_exists('VirtueMartCart'))
			require(JPATH_VM_SITE . DIRECTORY_SEPARATOR . 'helpers' . DIRECTORY_SEPARATOR . 'cart.php');
		$cart = VirtueMartCart::getCart(false);
		$cart->prepareCartData();

		$productModel = VmModel::getModel('product');
		$productModel->addImages($cart->products);
		$data = $cart->prepareAjaxData();
		$list_products = $cart->products;
		foreach ($data->products as $key_prduct=>$product){
			$pr_images = $productModel->getProduct($list_products[$key_prduct]->virtuemart_product_id,TRUE,TRUE,TRUE,1);
			$productModel->addImages($pr_images);
			if ($pr_images->images[0]->file_url != '' ) {
				$data->products[$key_prduct]['image_ajax_cart'] = '<img src="' . JURI::root() . $pr_images->images[0]->file_url . '" alt="">';
			}
		}
		$data->cart_show .= '<a href="' . JRoute::_(JURI::root() . "index.php?option=com_virtuemart&view=checkout") . '" class="button checkout wc-forward">' . JText::_('OB_VIRTUEMART_SHOP_CHECKOUT') . '</a>';
		echo json_encode($data);
		Jexit();
	}
	/*
	 * 
	 * 
	 * @author
	 * 
	 * @access public
	 */
	public function removeCart() {
		
		$data= 'abc';
		$mainframe = JFactory::getApplication();
		if (!class_exists( 'VmModel' )) require(JPATH_ADMINISTRATOR.DIRECTORY_SEPARATOR.'components'.DIRECTORY_SEPARATOR.'com_virtuemart'.DIRECTORY_SEPARATOR.'helpers'.DIRECTORY_SEPARATOR.'vmmodel.php');
		if (!class_exists( 'VmConfig' )) require(JPATH_ADMINISTRATOR . DIRECTORY_SEPARATOR . 'components' . DIRECTORY_SEPARATOR . 'com_virtuemart'.DIRECTORY_SEPARATOR.'helpers'.DIRECTORY_SEPARATOR.'config.php');
		if(!class_exists('VirtueMartCart')) require(JPATH_VM_SITE.DIRECTORY_SEPARATOR.'helpers'.DIRECTORY_SEPARATOR.'cart.php');
		$cart = VirtueMartCart::getCart(false);
		$rootUri = JURI::root();
			
		$rootUri = explode('/',JURI::root(true));
		array_splice($rootUri,-2);
		$base_uri =   JURI::getInstance()->getScheme().'://'
				. JURI::getInstance()->getHost()
				. implode('/',$rootUri).'/';
		if (!empty($cart->products)) {
			foreach ( $cart->products as $key => $val ) {
				unset($cart->products[$key]);
				$cart->setCartIntoSession();
				//$mainframe->enqueueMessage('All is done.');
			}
			//$mainframe->redirect(JRoute::_($base_uri.'index.php?option=com_virtuemart&view=cart'));
			return $data;
		}
		
	}

}