<?php
/*
 *
 * @package		ARI Smart Content
 * @author		ARI Soft
 * @copyright	Copyright (c) 2011 www.ari-soft.com. All rights reserved
 * @license		GNU/GPL (http://www.gnu.org/copyleft/gpl.html)
 * 
 */

(defined('_JEXEC') && defined('ARI_FRAMEWORK_LOADED')) or die;

AriKernel::import('Application.ARISmartContent.Providers.ProviderBase');
AriKernel::import('Application.ARISmartContent.Utils.ApplicationHelper');
AriKernel::import('Utils.Utils');
AriKernel::import('SimpleTemplate.SimpleTemplate');
AriKernel::import('Web.JSON.JSON');
AriKernel::import('Parameters.ParametersHelper');
AriKernel::import('Image.Thumbnail');

class AriSmartContentProviderImagesImages extends AriSmartContentProviderBase
{
	function getData($source, $filter)
	{
		$params = $source->Params;
		$paramsTree = AriParametersHelper::flatParametersToArray($params);
		$genParams = AriUtils::getParam($paramsTree, 'gen', array());

		$genParams['checkSum'] = md5(serialize($genParams));
		$genParams['key'] = 'source_' . $source->Id;

		$helper = AriSmartContentApplicationHelper::getInstance();
		$cacheFolder = $helper->getCacheFolder();

		$thumbProvider = new AriThumbnail('arithumb', $cacheFolder);
		$data = $thumbProvider->getStoredData($genParams);
		
		$data = $this->normalizeData($data, $params);

		return $data;
	}

	function getTotalRecords($source, $filter)
	{
		
	}
	
	function normalizeData($data, $params)
	{
		$normalizedData = array();

		if (empty($data))
			return $normalizedData;

		foreach ($data as $dataItem)
		{
			$img = $dataItem['image'];
			
			$newDataItem = $dataItem;
			unset($newDataItem['image']);
			unset($newDataItem['thumb']);
			
			$newDataItem['Item_Url'] = $img['url'];
			$newDataItem['Item_Width'] = $img['w'];
			$newDataItem['Item_Height'] = $img['h'];
			
			$thumb = AriUtils::getParam($dataItem, 'thumb');
			if (!empty($thumb))
			{
				$newDataItem['Preview_Url'] = $thumb['url'];
				$newDataItem['Preview_Width'] = $thumb['w'];
				$newDataItem['Preview_Height'] = $thumb['h'];
			}

			$navThumb = AriUtils::getParam($dataItem, 'nav');
			if (!empty($navThumb))
			{
				$newDataItem['Nav_Url'] = $navThumb['url'];
				$newDataItem['Nav_Width'] = $navThumb['w'];
				$newDataItem['Nav_Height'] = $navThumb['h'];
			}
			
			$normalizedData[] = $newDataItem;
		}
			
		return $normalizedData;
	}
}