<?php
/*
 * ARI Framework Lite
 *
 * @package		ARI Framework Lite
 * @version		1.0.0
 * @author		ARI Soft
 * @copyright	Copyright (c) 2009 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('Utils.Utils');

class AriJoomlaArticleSearchStruct
{
	var $_sectionIdList = array();
	var $_excludeFrontend = false;
	var $_excludeSection = false;
	var $_categoryIdList = array();
	var $_excludeCategory = false;
	var $_creatorIdList = array();
	var $_excludeCreator = false;
	var $_excludeArticleIdList = array();
	var $_includeArticleIdList = array();
	var $_isRandom = false;
	var $_includeSubCategory = false;
	var $_itemCount = 0;
	var $_sort = array(array('field' => 'created', 'dir' => 'ASC'));

	function _getIntList($list)
	{
		$intList = array();
		
		if (!is_array($list)) $list = array($list);
		
		foreach ($list as $item)
		{
			$intItem = @intval($item, 10);
			if ($intItem > 0) $intList[] = $intItem;
			else if ($intItem == 0 && ($item === 0 || $item === '0')) $intList[] = 0;
		}
		
		$intList = array_unique($intList);
		if (count($intList) == 0) $intList = null;
		
		return $intList;
	}
	
	function setExcludeFrontend($excludeFrontend)
	{
		if ($excludeFrontend != 'only')		
			$this->_excludeFrontend = $excludeFrontend;
		else
			$this->_excludeFrontend = (bool)$excludeFrontend;
	}
	
	function getExcludeFrontend()
	{
		return $this->_excludeFrontend;
	}
	
	function setCategoryIdList($idList)
	{
		$this->_categoryIdList = $this->_getIntList($idList);
	}
	
	function getCategoryIdList()
	{
		return $this->_categoryIdList;
	}
	
	function setSectionIdList($idList)
	{
		$this->_sectionIdList = $this->_getIntList($idList);
	}
	
	function getSectionIdList()
	{
		return $this->_sectionIdList;
	}
	
	function setCreatorIdList($idList)
	{
		$this->_creatorIdList = $this->_getIntList($idList);
	}
	
	function getCreatorIdList()
	{
		return $this->_creatorIdList;
	}
	
	function setIncludeArticleIdList($idList)
	{
		$this->_includeArticleIdList = $this->_getIntList($idList);
	}
	
	function getIncludeArticleIdList()
	{
		return $this->_includeArticleIdList;
	}
	
	function setExcludeArticleIdList($idList)
	{
		$this->_excludeArticleIdList = $this->_getIntList($idList);
	}
	
	function getExcludeArticleIdList()
	{
		return $this->_excludeArticleIdList;
	}
	
	function setExcludeCategory($excludeCategory)
	{
		$this->_excludeCategory = $excludeCategory;
	}
	
	function getExcludeCategory()
	{
		return $this->_excludeCategory;
	}

	function setExcludeSection($excludeSection)
	{
		$this->_excludeSection = $excludeSection;
	}
	
	function getExcludeSection()
	{
		return $this->_excludeSection;
	}
	
	function setExcludeCreator($excludeCreator)
	{
		$this->_excludeCreator = $excludeCreator;
	}
	
	function getExcludeCreator()
	{
		return $this->_excludeCreator;
	}
	
	function setIsRandom($isRandom)
	{
		$this->_isRandom = $isRandom;
	}
	
	function getIsRandom()
	{
		return $this->_isRandom; 
	}
	
	function setIncludeSubCategory($includeSubCategory)
	{
		$this->_includeSubCategory = $includeSubCategory;
	}
	
	function getIncludeSubCategory()
	{
		return $this->_includeSubCategory; 
	}
	
	function setItemCount($itemCount)
	{
		$itemCount = @intval($itemCount, 10);
		$this->_itemCount = $itemCount > -1 ? $itemCount : 0;
	}
	
	function getItemCount()
	{
		return $this->_itemCount;
	}
	
	function setSort($sort)
	{
		$fixSort = array();
		$availSortFields = array('title', 'ordering', 'hits', 'introtext', 'fulltext', 'created', 'created_by', 'modified', 'modified_by', 'id');
		if (is_array($sort))
		{
			foreach ($sort as $sortItem)
			{
				if (empty($sortItem['field']) || !in_array($sortItem['field'], $availSortFields))
					continue ;
					
				$dir = (!empty($sortItem['dir']) && strtoupper($sortItem['dir']) == 'DESC') ? 'DESC' : 'ASC';
				
				$fixSort[] = array('field' => $sortItem['field'], 'dir' => $dir);
			}
		}
		
		$this->_sort = $fixSort;
	}
	
	function getSort()
	{
		return $this->_sort;
	}
	
	function setSortDir($sortDir)
	{
		if (count($this->_sort) == 0) $this->_sort[] = array();
	
		$this->_sort[0]['dir'] = ((strtoupper($sortDir) == 'ASC') ? 'ASC' : 'DESC');
	}
	
	function getSortDir()
	{
		return count($this->_sort) > 0 ? $this->_sort[0]['dir'] : 'ASC';
	}
	
	function setSortField($sortField)
	{
		$availSortFields = array('title', 'ordering', 'hits', 'introtext', 'fulltext', 'created', 'created_by', 'modified', 'modified_by', 'id');
		if (@!in_array($sortField, $availSortFields)) $sortField = 'created';
		
		if (count($this->_sort) == 0) $this->_sort[] = array();
		
		$this->_sort[0]['field'] = $sortField;
	}

	function getSortField()
	{
		return count($this->_sort) > 0 ? $this->_sort[0]['field'] : null;
	}

	function bindFromArray($params)
	{
		$isRandomSorting = (bool)AriUtils::getParam($params, 'randomSorting');
		$includeSubCategory = (bool)AriUtils::getParam($params, 'includeSubCategory');

		$this->setItemCount(AriUtils::getParam($params, 'itemsCount'));
		$this->setExcludeFrontend(AriUtils::getParam($params, 'excludeFrontend'));
		$this->setExcludeSection((bool)AriUtils::getParam($params, 'invertSection'));
		$this->setSectionIdList($this->_getListParam(AriUtils::getParam($params, 'section')));
		$this->setExcludeCategory((bool)AriUtils::getParam($params, 'invertCategory'));
		$this->setCategoryIdList($this->_getListParam(AriUtils::getParam($params, 'category')));
		$this->setIncludeArticleIdList($this->_getListParam(AriUtils::getParam($params, 'includedArticle')));
		$this->setExcludeArticleIdList($this->_getListParam(AriUtils::getParam($params, 'excludedArticle')));
		$this->setIsRandom($isRandomSorting);
		$this->setIncludeSubCategory($includeSubCategory);
		
		if (!$isRandomSorting)
		{
			$sort = array(); 
			$sorting = AriUtils::getParam($params, 'sorting');
			if ($sorting)
			{
				$sorting = explode(';', $sorting);
				foreach ($sorting as $sortItem)
				{
					$sortItemParts = explode(',', $sortItem);
					$sort[] = array('field' => $sortItemParts[0], 'dir' => $sortItemParts[1]);
				}
			}

			$this->setSort($sort);
		}
	}
	
	function _getListParam($param)
	{
		if (is_array($param))
			return $param;
			
		$value = array();
		if ($param)
			$value = preg_split('/[;,]/', $param);
			
		return $value;
	}
}
?>