<?php
/**
 *---------------------------------------------------------------------------------------
 * @package      VP Conversion Tracking - Joomla! System Plugin
 * @subpackage   For VirtueMart 3+
 *---------------------------------------------------------------------------------------
 * @copyright    Copyright (C) 2012-2021 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;

class VPCTHelperOrderitem extends VPCTHelperMustache
{
	public function _product_variant()
	{
		JLoader::register('VirtueMartModelCustomfields', JPATH_ADMINISTRATOR . '/components/com_virtuemart/models/customfields.php');
		
		$product_variant    = '';
		$product_attributes = VirtueMartModelCustomfields::CustomsFieldOrderDisplay($this, 'FE');
		$product_attributes = !empty($product_attributes) ? str_replace(array('<br />', '<br/>'), array('|[]|', '|[]|'), $product_attributes) : $product_attributes;
		
		if (!empty($product_attributes))
		{
			if (strpos($product_attributes, '|[]|'))
			{
				$product_attributes = explode('|[]|', $product_attributes);
			}
			else
			{
				$product_attributes = array($product_attributes);
			}
			
			foreach($product_attributes as $key => &$variant)
			{
				$variant = htmlspecialchars(strip_tags($variant), ENT_COMPAT, 'UTF-8');
				$variant = trim($variant, ' ');
				
				if (str_replace(' ', '', $variant) == '')
				{
					unset($product_attributes[$key]);
				}
				else
				{
					$product_attributes[$key] = $variant;
				}
			}

			$product_variant = implode(', ', $product_attributes);
		}
		
		return $product_variant;
	}
	
	public function _thumbnail_image()
	{
		$model = VmModel::getModel('product');
		$product = $model->getProduct($this->virtuemart_product_id, true, true, true, 1);
		
		$model->addImages($product);
		
		$image = $this->_no_image_found();
		
		if (!empty($product->images[0]) && !empty($product->images[0]->virtuemart_media_id))
		{
			$image = JUri::root() . $product->images[0]->file_url;
			
			if ($product->images[0] instanceof VmImage)
			{
				$thumb_image = $product->images[0]->getFileUrlThumb();
				
				if (!empty($thumb_image))
				{
					$image = JUri::root() . $thumb_image;
				}
			}
		}
		
		return $image;
	}
	
	public function _list_thumbnail_images($args = array())
	{
		$model = VmModel::getModel('product');
		$product = $model->getProduct($this->virtuemart_product_id, true, true, true, 1);
		
		$model->addImages($product);
		
		if (!empty($args) && is_string($args))
		{
			$args = strpos($args, '|') ? explode('|', $args) : explode(',', $args);
			$args = array_map('trim', $args);
			
			foreach($args as $key => &$value)
			{
				$args[$key] = constant($args[$key]);
			}
		}
		
		$images = array();
		
		if (!empty($product->images[0]) && !empty($product->images[0]->virtuemart_media_id))
		{
			foreach($product->images as $imageObject)
			{
				$image = JUri::root() . $imageObject->file_url;
				
				if ($imageObject instanceof VmImage)
				{
					$thumb_image = $imageObject->getFileUrlThumb();
					
					if (!empty($thumb_image))
					{
						$image = JUri::root() . $thumb_image;
					}
				}
				
				$images[] = $image;
			}
		}
		else
		{
			$images[] = $this->_no_image_found();
		}
		
		array_unshift($args, $images);
		
		return call_user_func_array('json_encode', $args);
	}
	
	public function _image()
	{
		$model = VmModel::getModel('product');
		$product = $model->getProduct($this->virtuemart_product_id, true, true, true, 1);
		
		$model->addImages($product);
		
		$image = $this->_no_image_found();
		
		if (!empty($product->images[0]) && !empty($product->images[0]->virtuemart_media_id))
		{
			$image = JUri::root() . $product->images[0]->file_url;
		}
		
		return $image;
	}
	
	public function _list_images($args = array())
	{
		$model = VmModel::getModel('product');
		$product = $model->getProduct($this->virtuemart_product_id, true, true, true, 1);
		
		$model->addImages($product);
		
		if (!empty($args) && is_string($args))
		{
			$args = strpos($args, '|') ? explode('|', $args) : explode(',', $args);
			$args = array_map('trim', $args);
			
			foreach($args as $key => &$value)
			{
				$args[$key] = constant($args[$key]);
			}
		}
		
		$images = array();
		
		if (!empty($product->images[0]) && !empty($product->images[0]->virtuemart_media_id))
		{
			foreach($product->images as $imageObject)
			{
				$images[] = JUri::root() . $imageObject->file_url;
			}
		}
		else
		{
			$images[] = $this->_no_image_found();
		}
		
		array_unshift($args, $images);
		
		return call_user_func_array('json_encode', $args);
	}
	
	public function _no_image_found()
	{
		$template = JFactory::getApplication()->getTemplate();
		
		$file_1 = 'templates/' . $template . '/assets/images/vmgeneral/' . VmConfig::get('no_image_found');
		$file_2 = 'components/com_virtuemart/assets/images/vmgeneral/' . VmConfig::get('no_image_found');
		
		if (is_file(JPATH_SITE . '/' . $file_1))
		{
			return JUri::root() . $file_1;
		}
		elseif (is_file(JPATH_SITE . '/' . $file_2))
		{
			return JUri::root() . $file_2;
		}

		return '';
	}
}