<?php
/**
 *---------------------------------------------------------------------------------------
 * @package      VP Conversion Tracking - Joomla! System Plugin
 * @subpackage   For VirtueMart 3+
 *---------------------------------------------------------------------------------------
 * @copyright    Copyright (C) 2012-2016 VirtuePlanet Services LLP. All rights reserved.
 * @license      GNU General Public License version 2 or later; see LICENSE.txt
 * @authors      Abhishek Das
 * @email        info@virtueplanet.com
 * @link         https://www.virtueplanet.com
 *---------------------------------------------------------------------------------------
*/
defined('_JEXEC') or die;

/**
* Thankyou view class
*/
class VirtueMartViewThankyou extends VmView
{
	protected $order;
	protected $user;
	protected $helper;
	protected $params;
	protected $trackingCodes;
	protected $continue_link;
	protected $continue_link_html;
	protected $cart_link;
	protected $related_products;
	protected $suggested_products;
	protected $products;
	protected $currency;
	
	
	public function display($tpl = null)
	{
		$this->helper = plgSystemVPConversionTrackingHelper::getInstance();
		$this->order  = $this->helper->getOrder();
		$this->params = $this->helper->get('params');
		$this->user   = JFactory::getUser();
		
		if(!empty($this->order))
		{
			$tracker = VPCTHelperTracker::getInstance($this->order);
			$this->trackingCodes = $tracker->get('pageStringToReplace');
		}
		
		if($this->params->get('show_related_products', 1))
		{
			$this->related_products = $this->helper->getRelatedProducts();
		}
		else
		{
			$this->related_products = array();
		}
		
		if($this->params->get('show_suggested_products', 1))
		{
			$this->suggested_products = $this->helper->getSuggestedProducts();
		}
		else
		{
			$this->suggested_products = array();
		}
		
		if($this->order)
		{
			$this->currency = $this->order->currency;
		}
		
		$this->_prepareTemplatePath();
		$this->prepareContinueLink();
		$this->_prepareDocument();
		
		parent::display($tpl);
	}
	
	function prepareContinueLink($product = false)
	{
		$app              = JFactory::getApplication();
		$category_id      = shopFunctionsF::getLastVisitedCategoryId();
		$Itemid           = shopFunctionsF::getLastVisitedItemId();
		$last_visited_url = $app->getUserState('proopc.lastvisited.url', '');
		$continue_link    = !empty($last_visited_url) ? $last_visited_url : JUri::root(true);
		
		if($category_id)
		{
			$url = 'index.php?option=com_virtuemart&view=category&virtuemart_category_id=' . $category_id;
			
			if(class_exists('VmLanguage') && isset(VmLanguage::$jLangCount) && VmLanguage::$jLangCount > 1 && !empty(VmConfig::$vmlangSef))
			{
				$url .= '&lang=' . VmConfig::$vmlangSef;
			}
			
			if($Itemid)
			{
				$url .= '&Itemid=' . (int) $Itemid;
			}
			
			$continue_link = JRoute::_($url);
		}

		$this->continue_link      = $continue_link;
		$this->continue_link_html = '<a class="continue_link" href="' . $continue_link . '">' . JText::_ ('COM_VIRTUEMART_CONTINUE_SHOPPING') . '</a>';
		$this->cart_link          = JRoute::_('index.php?option=com_virtuemart&view=cart');
	}
	
	protected function _prepareTemplatePath()
	{
		$templates = (array) $this->_path['template'];
		$path = JPath::clean(VPCT_PATH . '/views/thankyou/tmpl');
		
		if(empty($templates))
		{
			$templates = array($path);
		}
		elseif(count($templates) >= 2)
		{
			array_splice($templates, 1, 0, $path);
		}
		else
		{
			$templates[] = $path;
		}
		
		$this->_path['template'] = $templates;
	}
	
	protected function _prepareDocument()
	{
		$app = JFactory::getApplication();
		$doc = JFactory::getDocument();
		
		$doc->setTitle(JText::_('COM_VIRTUEMART_CART_THANKYOU'));
		$doc->setMetaData('robots','NOINDEX, NOFOLLOW, NOARCHIVE, NOSNIPPET');
		
		$app->getPathway()->addItem(JText::_('COM_VIRTUEMART_CART_THANKYOU'));
	}
}

