<?php
/*
 * DON'T REMOVE THE FOLLOWING LICENSE
 *           INFORMATION!
 * ----------------------------------
 * Copyright by
 * Global Cube Business Solutions
 * Author: Dennis Ritz
 * info@global-cube.de
 * 2007-2008
 * ----------------------------------
 */

/*
 * DATA Class
 * This class is not callable for clients
 */
define('CONFIG_SESSION',		'MISConfig');
require_once(ROOT_DIR."init.inc");

class MISConfig extends App {
	

	static private $generatedSectionIDs = array();
	static private $generatedReportIDs = array();
	static private $generatedImageIDs = array();
	static private $generatedScorecardIDs = array();
	
	public function __construct() {
		parent::__construct(__file__);
	}
	
	public function getDefaultConfigFile() {
		$settingsDOM = new DOMDocument('1.0', 'iso-8859-1');
		$settingsDOM->load(ROOT_DIR."apps/".__CLASS__."/".__CLASS__.".settings.xml");
		$settingsXpath = new DOMXpath($settingsDOM);
		
		$defaultConfigFile = $settingsXpath->query("/settings/defaultConfigFile")->item(0)->nodeValue;

		if(!file_exists(ROOT_DIR."apps/".__CLASS__."/data/".$defaultConfigFile)) {
			$defaultConfigFile = $settingsXpath->query("/settings/rescueConfigFile")->item(0)->nodeValue;
			$this->setDefaultConfigFile($defaultConfigFile);
		}
		return $defaultConfigFile;
	}
	
	public function setDefaultConfigFile($p_configFile) {
		$settingsDOM = new DOMDocument('1.0', 'iso-8859-1');
		$settingsDOM->load(ROOT_DIR."apps/".__CLASS__."/".__CLASS__.".settings.xml");
		$settingsXpath = new DOMXpath($settingsDOM);
		
		$settingsNode = $settingsXpath->query("/settings")->item(0);
		$this->setNodeValue("defaultConfigFile",$p_configFile,$settingsDOM,$settingsNode);
		$settingsDOM->save(ROOT_DIR."apps/".__CLASS__."/".__CLASS__.".settings.xml");
	}
	
	public function setConfigFile($p_configFile) {
		$_SESSION[CONFIG_SESSION]['configFile'] = utf8_encode($p_configFile);
	}
	
	public function getConfigFile() {
		if(!empty($_SESSION[CONFIG_SESSION]) && !empty($_SESSION[CONFIG_SESSION]['configFile'])
		&& file_exists(ROOT_DIR."apps/".__CLASS__."/data/".$_SESSION[CONFIG_SESSION]['configFile']))
			return ROOT_DIR."apps/".__CLASS__."/data/".$_SESSION[CONFIG_SESSION]['configFile'];
		else {
			$this->setConfigFile($this->getDefaultConfigFile());
			return ROOT_DIR."apps/".__CLASS__."/data/".$this->getDefaultConfigFile();
		}
	}
	
	public function getAllConfigFiles(){
		$dataDir = ROOT_DIR."apps/MISConfig/data/";
		$dataDirArr = array();
		$handle = opendir($dataDir);
		while ($file = readdir ($handle)) {
		    if($file != "." && $file != "..") {
		        if(is_file($dataDir.$file)) {
		        	$dataDirArr[] = $file;
		        }
		    }
		}
		return $dataDirArr;
	}

	public function getConfigFilesPath() {
		return $dataDir = ROOT_DIR."apps/".__CLASS__."/data/";
	}
	
	public function getTempFilesPath() {
		return $dataDir = ROOT_DIR."temp/";
	}
	
	public function setNodeValue($p_nodeName,$p_nodeValue,$p_DOM,$p_parentNode,$p_item=0) {
		$p_Xpath = new DOMXpath($p_DOM);	
		$temp = $p_DOM->createElement($p_nodeName);
		$temp->appendChild($p_DOM->createTextNode(utf8_encode($p_nodeValue)));
		$nodeList = $p_Xpath->query("./".$p_nodeName,$p_parentNode);
		if($nodeList->length >0 && $nodeList->length > $p_item)
			$p_parentNode->replaceChild($temp,$p_parentNode->getElementsByTagName($p_nodeName)->item($p_item));
		else
			$p_parentNode->appendChild($temp);
	}
	
	public function getAllLangs(){
		return array("de" => "deutsch","en" => "english","fr" => "französisch","it" => "italiano");
	}
	
	public function getAllViews() {
		return array("Client" => "Client","Info-User" => "Info-User");
	}
	
	public function getAllPPR(){
		$projectDOM = new DOMDocument('1.0', 'iso-8859-1');
		$projectDOM->load($this->getConfigFile());
		$projectXpath = new DOMXpath($projectDOM);	
		
		$PPRPath = $projectXpath->query("/Configuration/General/ReportPath")->item(0)->nodeValue;
		$PPRArr = array();
		$handle = opendir($PPRPath);
		while ($file = readdir ($handle)) {
		    if($file != "." && $file != "..") {
		        if(is_file($PPRPath."/".$file)) {
		        	//$filename = substr($file,0,strripos($file,"."));
		        	if("ppr" == getFileExtension($file)) {
			        	$filename = basename($file,".ppr");
			        	$filename = basename($filename,"_Graf");
			        	if(!in_array($filename,$PPRArr)) {
			        	     array_push($PPRArr,$filename);
			        	}
		        	}
		        }
		    }
		}
		if(!$this->isRole("admin")) return array();
		return $PPRArr;
	}
	
	public function getAllScorcardFormats() {
		return array($this->_getI18N("Ganzzahlig"),$this->_getI18N("ZahlMitTrennz"),$this->_getI18N("ZahlOhneTrennz"),$this->_getI18N("Waehrung"),$this->_getI18N("Prozent"));
	}
	
	public function getAllPPRprimary() {
		$projectDOM = new DOMDocument('1.0', 'iso-8859-1');
		$projectDOM->load($this->getConfigFile());
		$projectXpath = new DOMXpath($projectDOM);	
		
		$PPRPath = $projectXpath->query("/Configuration/General/ReportPath")->item(0)->nodeValue;
		$PPRArr = array();
		$handle = opendir($PPRPath);
		while ($file = readdir ($handle)) {
		    if($file != "." && $file != "..") {
		        if(is_file($PPRPath."/".$file)) {
		        	$filename = substr($file,0,strripos($file,"."));
		        	array_push($PPRArr,$filename);
		        }
		    }
		}
		return $PPRArr;
	}
	
	public function getAllImageTypes() {
		return array(
		$this->_getI18N("Kreuztabelle"),
		$this->_getI18N("Kreisdiagramm"),
		$this->_getI18N("ThreeD_Kreisdiagramm"),
		$this->_getI18N("Balkendiagramm"),
		$this->_getI18N("Balkengruppendiagramm"),
		$this->_getI18N("Stapelbalkendiagramm"),
		$this->_getI18N("Liniendiagramm"),
		$this->_getI18N("Mehrliniendiagramm"),
		$this->_getI18N("Korrelationsdiagramm"), 
		$this->_getI18N("Punktdiagramm"));
	}
	
	public function getDataFile() {
		$projectDOM = new DOMDocument('1.0', 'iso-8859-1');
		$projectDOM->load($MISConfig->getConfigFile());
		$projectXpath = new DOMXpath($projectDOM);
		
		$dataFile = $projectXpath->query("/Configuration/General/PublishImagePath")->item(0)->nodeValue;
		return $dataFile.$this->getUsername()."/data.xml";
	}
	

}
?>