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

defined('_JEXEC') or die ('Restricted access');

require_once dirname(__FILE__) . '/../kernel/class.AriKernel.php';

AriKernel::import('Xml.XmlHelper');
AriKernel::import('Web.JSON.JSON');

class JElementArticlesorting extends JElement 
{
	var $_name = 'Articlesorting';

	function fetchElement($name, $value, &$node, $control_name) 
	{
		$ctrlId = str_replace(array('[', ']'), array('_', ''), $control_name);
		$fields =& AriXmlHelper::getSingleNode($node, 'fields');
		$dirs =& AriXmlHelper::getSingleNode($node, 'directions');

		$fields =& AriXmlHelper::getNode($fields, 'option');
		$dirs =& AriXmlHelper::getNode($dirs, 'option');

		$count = intval(AriXmlHelper::getAttribute($node, 'count'), 10);
		$selValues = $this->getValues(
			$value, 
			$fields, 
			$dirs);
			
		$fields = $this->getNodesAssocValues($fields);
		$dirs = $this->getNodesAssocValues($dirs);

		array_unshift($fields, array('text' => 'None', 'value' => ''));
		$valCount = count($selValues);
		$id = uniqid('', false);
		$element = '<div id="' . $id . '" class="article_sorting">';
		for ($i = 0; $i < $count; $i++) 
		{
			$selValue = $i < $valCount ? $selValues[$i] : array('field' => null, 'dir' => null);

			$element .= sprintf('<div class="sort_item">%s<br />%s</div>',
				JHTML::_(
					'select.genericlist', 
					$fields, 
					'', 
					' class="sort_field"', 
					'value', 
					'text', 
					$selValue['field']),
				JHTML::_(
					'select.genericlist', 
					$dirs, 
					'', 
					' class="sort_dir"', 
					'value', 
					'text', 
					$selValue['dir']));
			
			if ($i < $count - 1)
				$element .= '<hr />';
		}
		
		$element .= sprintf('</div><input type="hidden" name="%1$s[%2$s]" id="%1$s%2$s" value="%3$s" />',
			$control_name,
			$name,
			$value);
			
		$this->_includeAssets();
		
		$document =& JFactory::getDocument();
		$document->addScriptDeclaration(
			sprintf('window.addEvent("domready", function(){ new ARIArticleSorting(%1$s); });',
				json_encode(
					array(
						'containerId' => $id,
						'hiddenId' => $control_name . $name
					)
				)
			)
		);

		return $element;
	}
	
	function _includeAssets() 
	{
		static $loaded;
		
		if ($loaded)
			return ;

		$filePath = str_replace(DS == '\\' ? '/' : '\\', DS, dirname(__FILE__));
		if (strlen(JPATH_ROOT) > 1)
			$filePath = str_replace(JPATH_ROOT, '', $filePath);

		$uri = JURI::root(true) . str_replace(DS, '/', $filePath) . '/';

		$document =& JFactory::getDocument();
		$document->addScript($uri . 'articlesorting.js');
		$document->addStyleSheet($uri . 'articlesorting.css', 'text/css', null, array());

		$loaded = true;
	}

	function getValues($value, $fields, $dirs) 
	{
		$values = array();
		$fields = $this->getNodesValues($fields);
		$dirs = $this->getNodesValues($dirs);
		$value = explode(';', $value);
		foreach ($value as $val) 
		{
			$attrs = explode(',', $val);
			$field = $dir = null;

			if (!empty($attrs[0]) && in_array($attrs[0], $fields))
				$field = $attrs[0]; 
				
			if (!empty($attrs[1]) && in_array($attrs[1], $dirs))
				$dir = $attrs[1]; 
			
			if (!empty($field) && !empty($dir))
				$values[] = array('field' => $field, 'dir' => $dir);
		}
		
		return $values;
	}
	
	function getNodesAssocValues($nodes)
	{
		$values = array();
		if (empty($nodes))
			return $values;

		foreach ($nodes as $node)
		{
			$value = new stdClass();
			$value->value = AriXmlHelper::getAttribute($node, 'value');
			$value->text = JText::_(AriXmlHelper::getData($node));
			$values[] = $value;
		}

		return $values;
	}
	
	function getNodesValues($nodes)
	{
		$values = array();
		if (empty($nodes))
			return $values;

		foreach ($nodes as $node)
		{
			$values[] = AriXmlHelper::getAttribute($node, 'value');
		}

		return $values;
	}
}