<?php

class renderFunctionsClass extends renderClass_abstract
{
	private $_navData = array();
	private $_subtpl = null;
	private $_node = null;
	private $_websiteIds = array();
	private $_prefix = '';

	public function createNavigationRights($navData, $tpl, $node)
	{
		$this->_navData = $navData;
		$this->_subtpl = $tpl;
		$this->_node = $node;
		$this->_prefix = renderFunctionsClass::getNavigationPrefix();

		$node = renderFunctionsClass::getNavigationNode(0);
		if ($node !== false)
		{
			$this->_node->appendChild($node);
		}

		return $this->_node;
	}

	private function getNavigationPrefix()
	{
		foreach ($this->_navData[0] as $tmp_data)
		{
			foreach ($tmp_data as $key => $value)
			{
				if (strpos($key, '_') !== false)
				{
					return substr($key, 0, strpos($key, '_') + 1);
				}
				else
				{
					return '';
				}
			}
		}
	}

	private function getNavigationNode($navStart)
	{
		if (!isset ($this->_navData[$navStart]) || count($this->_navData [$navStart]) == 0)
		{
			return false;
		}
		$newNode = $this->_subtpl->createElement('ul');
		foreach ($this->_navData [$navStart] AS $key => $value)
		{
			$newLi = $this->_subtpl->createElement('li');

			$input = $this->_subtpl->createElement('input');
			$input->setAttributeNode(new DOMAttr ('type', 'checkbox'));
			$input->setAttributeNode(new DOMAttr ('name', str_replace('_outer', '', $this->_node->getAttribute('id') . '[ ]')));
			$input->setAttributeNode(new DOMAttr ('value', $this->_prefix . $value[$this->_prefix . 'navId']));
			$input->setAttributeNode(new DOMAttr ('id', $this->_prefix . $value[$this->_prefix . 'navId']));
			if ($this->_prefix === 'portal_' && array_key_exists($value[$this->_prefix . 'navId'], $this->_website))
			{
				$input->setAttributeNode(new DOMAttr ('data-referer', $this->_website[$value[$this->_prefix . 'navId']]['prefix'] . 'Rights'));
			}
			$label = $this->_subtpl->createElement('label');
			$label->setAttributeNode(new DOMAttr ('for', $this->_prefix . $value[$this->_prefix . 'navId']));
			$label->appendChild(new DOMText ($value[$this->_prefix . 'navName']));

			$newLi->appendChild($input);
			$newLi->appendChild($label);

			$sub_node = renderFunctionsClass:: getNavigationNode($value [$this->_prefix . 'navId']);
			if ($sub_node !== false)
			{
				$newLi->setAttributeNode(new DOMAttr ('class', 'hasChild'));
				$newLi->appendChild($sub_node);
			}
			$newNode->appendChild($newLi);

		}

		unset ($this->_navData [$navStart]);

		return $newNode;

	}
}