<?php
/**
* @package css2switch
* @author Dominik Gorczyca - mediahof
* 
* --------------------------------------------------------------------------------
* All rights reserved.  css2switch Component for Joomla! 1.5
*
**/
defined( '_JEXEC' ) or die( 'Restricted access' );

require_once( JApplicationHelper::getPath( 'class' ) );		//Datei css2switch.class.php lesen
$task = JRequest::getVar( 'task' );
$option = JRequest::getVar( 'option' );

$c2s =& new css2switch;

switch ($task)
{
	//Konfiguration anzeigen
	default:
	case 'config':
	$farbe = 0;
	$menuItems		= $c2s->tabelleAuslesen('menu'); //Alle Menueinträge auslesen
	$tmpl			= $c2s->tabelleAuslesen('templates_menu', 'client_id = 0 AND menuid = 0'); //Aktives Template ermitteln
	$css_konfig 	= $c2s->tabelleAuslesen('css2switch_items'); //Zuweisungen von Menupunkten auslesen
	$c2s->stdCSSdat = $c2s->tabelleAuslesen('css2switch_menutype'); //Menutypen auslesen
	$c2s->ladeMenutypen(); //Ablegen in $c2s->menuTypArray
	
	if (count($c2s->stdCSSdat) < 1) //Falls
	{
		foreach ($c2s->menuArray as $tempType)
		{
			$c2s->inhaltSpeichern('css2switch_menutype', "menutype = '".$tempType."'");
		}
		$c2s->stdCSSdat = $c2s->tabelleAuslesen('css2switch_menutype'); //Menutypen auslesen
	}

	$direk = JPATH_SITE.DS.'templates'.DS.$tmpl->template.DS.'css';
	$c2s->leseTempVerzeichnis($direk, 'CSS'); //Ablegen in $c2s->CSS
	$c2s->leseTempVerzeichnis($direk, 'stdCSSFile', $prefix = ''); //Ablegen in $c2s->stdCSSFile

	ob_start();

	// Menupunkte die sich im Papierkorb befinden ausschliessen
	foreach ($menuItems as $menuItem)
	{
		if ($menuItem->published != -2)
		{
			$css_ids[] = $menuItem;
		}
	}

	for ($mItems = 0; $mItems < count($css_ids); $mItems++)
	{
		$menuTyp = $css_ids[$mItems]->menutype;
		if ($css_ids[$mItems]->parent != '0')
		{
			$listMenuTemp = $c2s->tabelleAuslesen('menu', 'id ='.$css_ids[$mItems]->parent);
			$listMenu1 = $listMenuTemp->name.' - '.$css_ids[$mItems]->name;
		}
		else
		{
			$listMenu1 = $css_ids[$mItems]->name;
		}
		$listMenu2 = $css_ids[$mItems]->id;

		for ($ids = 0; $ids < count($css_konfig); $ids++)
		{
			if ($listMenu2 == $css_konfig[$ids]->items) 
			{
				$cssDatei = $css_konfig[$ids]->css_datei;
			}
		}
		if (!empty($css_ids[$mItems]->parent))
		{
			$arrayMenu[] = array( 'css' => (isset($cssDatei) ? $cssDatei : ''), 'typ' => $menuTyp, 'id' => (int)$listMenu2, 'name' => $listMenu1, 'parentname' => $listMenuTemp->name, 'parentid' => $css_ids[$mItems]->parent, 'published' => $css_ids[$mItems]->published );
		}
		else
		{
			$arrayMenu[] = array( 'css' => (isset($cssDatei) ? $cssDatei : ''), 'typ' => $menuTyp, 'id' => (int)$listMenu2, 'name' => $listMenu1, 'published' => $css_ids[$mItems]->published );
		}
	}

	for ($snt = 0; $snt < count($arrayMenu); $snt++)
	{
		foreach ($c2s->menuArray as $typek)
		{
			if ($arrayMenu[$snt]['typ'] == $typek)
			{
				${'menuArraysort'.$arrayMenu[$snt]['typ']}[] = $arrayMenu[$snt];
			}
		}
	}

	for ($ii = 0; $ii < count($c2s->menuArray); $ii++)
	{
		${'menuArraysort'.$c2s->menuArray[$ii]} = $c2s->arraySort(${'menuArraysort'.$c2s->menuArray[$ii]}, 'name');
		$c2s->sortArray = array_merge($c2s->sortArray, ${'menuArraysort'.$c2s->menuArray[$ii]});
	}
	$document   =& JFactory::getDocument();
	$document->addStyleSheet(JURI::base() .'components/com_css2switch/css/c2sadmin.css','text/css',"screen");
	$lang = & JFactory::getLanguage();
	$lang->load(JRequest::getVar( 'component' ));
	//echo '<pre>';
	//print_r($lang);
	require_once( JApplicationHelper::getPath( 'admin_html' ) ); //Datei admin.css2switch.html.php lesen
	$inhalt = ob_get_contents();
	$inhalt = str_replace("\t", ' ', $inhalt);
	ob_end_clean();
	echo $inhalt;
	$inhalt = null;
	break;

	//Konfiguration speichern
	case 'save':
	global $mainframe;
	$db =& JFactory::getDBO();
	$c2s->tabelleLeeren('css2switch_items');
	$c2s->tabelleLeeren('css2switch_menutype');
	foreach ($_POST as $id => $css)
	{
		$idd = str_replace('css', '', $id);
		if ($_POST['sel'.$idd] != '')
		{
			$parentid = $_POST['sel'.$idd];
		}
		else
		{
			$parentid = '';
		}
		if ($id != 'task' && $id != 'option' && substr($id, 0, 3) != 'sel' && substr($id, 0, 5) != 'tmpl_') 
		{
			$c2s->inhaltSpeichern('css2switch_items', ($parentid != '' ? "parentid = ".$parentid.', ' : "" )."css_datei = '".$css."', items = '".str_replace('css', '', $id)."'");

		}
		if(substr($id, 0, 5) == 'tmpl_')
		{
			$c2s->inhaltSpeichern('css2switch_menutype', "default_templ = '".$css."', menutype = '".str_replace('tmpl_', '', $id)."'");
		}
	}
	$mainframe->redirect('index.php?option=' . $option . '&task=config', JText::_( 'C2S_SAVED' )."!!!");
	break;
}

?>
