StateMgr.php 1.62 KB
<?php

  class StateMgr  
  {
  	protected $stateXmlName, $stateXml;
  	protected $rootElement;
  	
  	function __construct() 
  	{	 		 
		$this->stateXmlName = USERWSDIR."State.xml";
        $this->stateXml = new DomDocument("1.0");
        if (file_exists($this->stateXmlName))
        {
			$this->stateXml->load($this->stateXmlName); 
			$this->rootElement = $this->stateXml->documentElement;
		}
		else 
		{
			$this->rootElement = $this->stateXml->createElement('state');
			$this->stateXml->appendChild($this->rootElement);
			$this->stateXml->save($this->stateXmlName);
		}
    }
    
    function saveState($datas)
    {
    	if (!isset($this->rootElement))
    		return array('error' => true, 'message' => 'Root node not defined.');
    		
    	while ($this->rootElement->hasChildNodes())
   			$this->rootElement->removeChild($this->rootElement->childNodes->item(0));
    	
    	foreach ($datas as $key => $value)
    	{
    		$node = $this->stateXml->createElement("ui",$value);
    		$node->setAttribute("name",$key);
    		$this->rootElement->appendChild($node);
    	}
    	
    	$this->stateXml->save($this->stateXmlName);
    	    	
    	return array('error' => false);
    }
    
    function loadState()
    {
   		if (!isset($this->rootElement))
    		return array('error' => true, 'message' => 'Root node not defined.');

    	$state = array();
    	
    	$uis = $this->rootElement->getElementsByTagName('ui');
    	foreach($uis as $ui)
    	{
    		$name  = $ui->getAttribute("name");
    		$value = $ui->nodeValue;	
    		$state[$name] = $value;
    	}
    	
    	return array('error' => false, 'state' => $state);
    }
  	
  }

?>