<?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;

require_once JPATH_ADMINISTRATOR . '/components/com_arismartcontent/kernel/class.AriKernel.php';
require_once JPATH_LIBRARIES . '/arismartcontent/mediagallery/libs/helper.php';

AriKernel::import('Data.DataFilter');
AriKernel::import('Application.ARISmartContent.Providers.ProvidersHelper');

class AriSmartContentLibraryArismartcontentMediagalleryAjaxDispatcher
{
	function ajax_getData()
	{
		$jInput = JFactory::getApplication()->input;
		$ticket = $jInput->request->get('ticket');
		$params = AriSmartContentWidgetMediaGalleryHelper::getParams($ticket);
		$pageNum = $jInput->request->getInt('pageNum');
		$pageSize = $jInput->request->getInt('pageSize');
		$sourceId = isset($params['_default']['source']) ? $params['_default']['source'] : null;
		
		$filter = new AriDataFilter(
			array(
				'startOffset' => $pageNum * $pageSize,
				'limit' => $pageSize
			)
		);
		
		$data = null;
		$processData = true;
		if (!empty($params['gallery']['visibleItemCount']))
		{
			$visibleItemCount = intval($params['gallery']['visibleItemCount'], 10);
			if ($visibleItemCount > 0)
			{
				
				$offset = $pageNum * $pageSize;
				if ($offset > $visibleItemCount)
				{
					$processData = false;
				}
				else if ($pageSize == 0 || ($offset + $pageSize > $visibleItemCount))
				{
					$filter->setConfigValue('limit', $visibleItemCount - $offset);
				}
			}
		}

		if ($processData)
		{
			$sourceData = AriSmartContentProvidersHelper::resolveSource($sourceId);
			$data = AriSmartContentProvidersHelper::getData($sourceData, $filter);
		}

		$content = AriSmartContentWidgetMediaGalleryHelper::getContent($data, $params);

		return array(
			'itemCount' => count($data),
			'content' => $content
		);
	}
}